Refresh HiveGrid Cloud reference snapshot

This commit is contained in:
david committed 2026-08-03 15:31:14 +02:00
1 parent 5e99e14772
commit 7f88026f79
5955 files changed
+1177325 -188362

No files matched your search

@@ -12,6 +12,7 @@ export {
isHeaderAttribute,
redactSpanAttributes,
scrubDbStatement,
scrubSpanName,
scrubUrlQuery,
type DelegateSpanProcessor,
type PiiScrubableSpan,
@@ -5,10 +5,11 @@ import {
PIISpanProcessor,
redactSpanAttributes,
scrubDbStatement,
scrubSpanName,
scrubUrlQuery,
type DelegateSpanProcessor,
type PiiScrubableSpan,
} from "./pii-span-processor.js";
} from "./index.js";
describe("scrubUrlQuery", () => {
it("replaces the query-string with the [scrubbed] placeholder", () => {
@@ -22,6 +23,30 @@ describe("scrubUrlQuery", () => {
it("handles empty query-strings", () => {
expect(scrubUrlQuery("https://api.example.com/path?")).toBe("https://api.example.com/path?[scrubbed]");
});
it("removes URL fragments even when no query-string exists", () => {
expect(scrubUrlQuery("https://api.example.com/path#private")).toBe("https://api.example.com/path");
});
it("ignores query markers that occur inside a fragment", () => {
expect(scrubUrlQuery("https://api.example.com/path#private?token=secret")).toBe("https://api.example.com/path");
});
it("redacts public and internal customer-portal capability URLs", () => {
const token = "t".repeat(43);
expect(scrubUrlQuery(`https://portal.example.test/acme/login/magic/${token}?source=mail`)).toBe(
"https://portal.example.test/acme/login/magic/redacted?[scrubbed]",
);
expect(scrubUrlQuery(`/__portal/acme/reset-password/${token}`)).toBe("/__portal/acme/reset-password/redacted");
});
});
describe("scrubSpanName", () => {
it.each(["login/magic", "setup", "verify-email", "reset-password"])("redacts %s capability span names", (route) => {
const token = "t".repeat(43);
const path = route === "login/magic" ? `/acme/${route}/${token}` : `/__portal/acme/${route}/${token}`;
expect(scrubSpanName(`GET ${path}?source=mail`)).toBe(`GET ${path.replace(token, "redacted")}?[scrubbed]`);
});
});
describe("scrubDbStatement", () => {
@@ -47,18 +72,37 @@ describe("scrubDbStatement", () => {
});
describe("redactSpanAttributes", () => {
it("strips query-strings from URL-like attributes (http.url, http.target, url.full, url.path)", () => {
it("strips query-strings and fragments from URL-like attributes", () => {
const attributes: Record<string, unknown> = {
"http.url": "https://api.example.com/foo?token=secret",
"http.target": "/foo?token=secret",
"url.full": "https://api.example.com/foo?token=secret",
"url.path": "/foo?token=secret",
"url.query": "token=secret&email=alice@example.test",
"server.address": "api.example.com",
};
redactSpanAttributes(attributes);
expect(attributes["http.url"]).toBe("https://api.example.com/foo?[scrubbed]");
expect(attributes["http.target"]).toBe("/foo?[scrubbed]");
expect(attributes["url.full"]).toBe("https://api.example.com/foo?[scrubbed]");
expect(attributes["url.path"]).toBe("/foo?[scrubbed]");
expect(attributes["url.query"]).toBe("[scrubbed]");
expect(attributes["server.address"]).toBe("api.example.com");
});
it("redacts portal capabilities from URL and route attributes", () => {
const token = "t".repeat(43);
const attributes: Record<string, unknown> = {
"http.url": `https://portal.example.test/acme/setup/${token}`,
"http.route": `/__portal/acme/verify-email/${token}`,
"url.path": `/acme/reset-password/${token}`,
};
redactSpanAttributes(attributes);
expect(attributes).toEqual({
"http.url": "https://portal.example.test/acme/setup/redacted",
"http.route": "/__portal/acme/verify-email/redacted",
"url.path": "/acme/reset-password/redacted",
});
});
it("drops authorization, cookie, set-cookie and x-api-key request headers", () => {
@@ -88,12 +132,23 @@ describe("redactSpanAttributes", () => {
}
});
it("scrubs db.statement literals to `?`", () => {
it("does not export forwarding IPs or referer URLs", () => {
const attributes: Record<string, unknown> = {
"http.request.header.x-forwarded-for": "203.0.113.42",
"http.request.header.referer": "https://example.test/private?token=raw",
};
redactSpanAttributes(attributes);
expect(attributes).toEqual({});
});
it("scrubs legacy and current database statement literals to `?`", () => {
const attributes: Record<string, unknown> = {
"db.statement": "SELECT * FROM users WHERE email = 'leak@example.com'",
"db.query.text": "SELECT * FROM users WHERE phone = '+49 123'",
};
redactSpanAttributes(attributes);
expect(attributes["db.statement"]).toBe("SELECT * FROM users WHERE email = ?");
expect(attributes["db.query.text"]).toBe("SELECT * FROM users WHERE phone = ?");
});
it("drops messaging.message_payload but keeps the payload-size attribute", () => {
@@ -110,6 +165,12 @@ describe("redactSpanAttributes", () => {
expect(() => redactSpanAttributes(undefined)).not.toThrow();
});
it("preserves an empty url.query attribute without adding content", () => {
const attributes: Record<string, unknown> = { "url.query": "" };
redactSpanAttributes(attributes);
expect(attributes["url.query"]).toBe("");
});
it("ignores non-string values on URL-like and db.statement keys", () => {
const attributes: Record<string, unknown> = {
"http.url": 42,
@@ -182,7 +243,49 @@ describe("PIISpanProcessor", () => {
expect(span.attributes).not.toHaveProperty("messaging.message_payload");
});
it("still delegates onEnd when the scrubber throws", () => {
it("redacts a capability-bearing span name before export", () => {
const delegate = makeDelegateStub();
const processor = new PIISpanProcessor(delegate);
const token = "t".repeat(43);
const span: PiiScrubableSpan = {
name: `GET /__portal/acme/login/magic/${token}?source=mail`,
attributes: {},
};
processor.onEnd(span);
expect(delegate.onEnd).toHaveBeenCalledWith(span);
expect(span.name).toBe("GET /__portal/acme/login/magic/redacted?[scrubbed]");
});
it("exports an already-safe span name unchanged", () => {
const delegate = makeDelegateStub();
const processor = new PIISpanProcessor(delegate);
const span: PiiScrubableSpan = { name: "GET /health", attributes: {} };
processor.onEnd(span);
expect(delegate.onEnd).toHaveBeenCalledWith(span);
expect(span.name).toBe("GET /health");
});
it("drops a capability-bearing span when its name cannot be rewritten", () => {
const delegate = makeDelegateStub();
const processor = new PIISpanProcessor(delegate);
const token = "t".repeat(43);
const span = Object.defineProperty({ attributes: {} }, "name", {
configurable: false,
enumerable: true,
value: `GET /acme/reset-password/${token}`,
writable: false,
}) as PiiScrubableSpan;
processor.onEnd(span);
expect(delegate.onEnd).not.toHaveBeenCalled();
});
it("drops a span when the scrubber fails and attributes cannot be replaced", () => {
const delegate = makeDelegateStub();
const processor = new PIISpanProcessor(delegate);
// Inject a getter that throws when the scrubber iterates the attribute bag.
@@ -193,7 +296,33 @@ describe("PIISpanProcessor", () => {
} as PiiScrubableSpan;
expect(() => processor.onEnd(span)).not.toThrow();
expect(delegate.onEnd).toHaveBeenCalledTimes(1);
expect(delegate.onEnd).not.toHaveBeenCalled();
});
it("exports an empty attribute bag when a malformed bag can be replaced", () => {
const delegate = makeDelegateStub();
const processor = new PIISpanProcessor(delegate);
let attributes: Record<string, unknown> = new Proxy(
{ secret: "raw" },
{
ownKeys() {
throw new Error("malformed attributes");
},
}
);
const span: PiiScrubableSpan = {
get attributes() {
return attributes;
},
set attributes(value) {
attributes = value ?? {};
},
};
processor.onEnd(span);
expect(delegate.onEnd).toHaveBeenCalledWith(span);
expect(span.attributes).toEqual({});
});
it("forwards forceFlush and shutdown to the delegate", async () => {
@@ -11,12 +11,15 @@
*
* Scrubbing rules (see {@link redactSpanAttributes}):
* - `http.url`, `http.target` and the OTel v1.20+ aliases `url.full`,
* `url.path` get their query-string replaced with `?[scrubbed]`.
* `url.path` get their query-string replaced with `?[scrubbed]`; fragments
* are removed and `url.query` is redacted wholesale. Customer-portal
* capability segments are redacted in URL attributes and span names.
* - HTTP request/response headers are filtered through {@link HEADER_ALLOWLIST}.
* Anything outside the allowlist (notably `authorization`, `cookie`,
* `set-cookie`, `x-api-key`) is dropped.
* - `db.statement` literal-string parameters (`'...'` and `"..."`) are
* rewritten to `?` (defense-in-depth — Prisma already parametrises).
* - `db.statement` and `db.query.text` literal-string parameters (`'...'`
* and `"..."`) are rewritten to `?` (defense-in-depth — Prisma already
* parametrises).
* - `messaging.message_payload` is dropped entirely; the payload-size
* attribute (if present) is preserved.
*
@@ -34,6 +37,7 @@
* a runtime dependency on `@opentelemetry/sdk-trace-base`.
*/
export interface PiiScrubableSpan {
name?: string | undefined;
attributes?: Record<string, unknown> | undefined;
}
@@ -57,14 +61,17 @@ export const HEADER_ALLOWLIST: ReadonlySet<string> = new Set(
"accept-language",
"accept-encoding",
"x-request-id",
"x-forwarded-for",
"referer",
].map((name) => name.toLowerCase()),
);
const QUERY_STRING_PLACEHOLDER = "?[scrubbed]";
const URL_LIKE_ATTRIBUTES = ["http.url", "http.target", "url.full", "url.path", "url.query"] as const;
const URL_LIKE_ATTRIBUTES = ["http.url", "http.target", "http.route", "url.full", "url.path", "url.query"] as const;
const PORTAL_CAPABILITY_ROUTE_PATTERN =
/((?:\/__portal)?\/[a-z0-9][a-z0-9-]{1,62}\/(?:login\/magic|setup|verify-email|reset-password)\/)[A-Za-z0-9_-]+/giu;
const DB_STATEMENT_ATTRIBUTES = ["db.statement", "db.query.text"] as const;
const DROP_ATTRIBUTES = ["messaging.message_payload"] as const;
@@ -72,12 +79,21 @@ const HEADER_PREFIXES = ["http.request.header.", "http.response.header."] as con
/**
* Strips the query-string from a URL-like attribute, replacing it with
* `?[scrubbed]`. Returns the input unchanged when no `?` is present.
* `?[scrubbed]`, and redacts customer-portal capability path segments.
*/
export function scrubUrlQuery(value: string): string {
const queryIndex = value.indexOf("?");
if (queryIndex < 0) return value;
return `${value.slice(0, queryIndex)}${QUERY_STRING_PLACEHOLDER}`;
const fragmentIndex = value.indexOf("#");
if (queryIndex < 0 && fragmentIndex < 0) return redactPortalCapabilityTokens(value);
if (queryIndex >= 0 && (fragmentIndex < 0 || queryIndex < fragmentIndex)) {
return `${redactPortalCapabilityTokens(value.slice(0, queryIndex))}${QUERY_STRING_PLACEHOLDER}`;
}
return redactPortalCapabilityTokens(value.slice(0, fragmentIndex));
}
/** Redacts customer-portal one-time credentials from span/transaction names. */
export function scrubSpanName(value: string): string {
return scrubUrlQuery(value);
}
/**
@@ -148,13 +164,13 @@ export function redactSpanAttributes(attributes: Record<string, unknown> | undef
if ((URL_LIKE_ATTRIBUTES as readonly string[]).includes(key)) {
const value = attributes[key];
if (typeof value === "string") {
attributes[key] = scrubUrlQuery(value);
attributes[key] = key === "url.query" ? (value ? "[scrubbed]" : "") : scrubUrlQuery(value);
}
continue;
}
// Scrub SQL literals from db.statement.
if (key === "db.statement") {
// Scrub SQL literals from current and legacy database statement fields.
if ((DB_STATEMENT_ATTRIBUTES as readonly string[]).includes(key)) {
const value = attributes[key];
if (typeof value === "string") {
attributes[key] = scrubDbStatement(value);
@@ -185,12 +201,19 @@ export class PIISpanProcessor implements DelegateSpanProcessor {
}
onEnd(span: PiiScrubableSpan): void {
if (!redactSpanName(span)) {
return;
}
try {
redactSpanAttributes(span.attributes);
} catch {
// Scrubber failures must never block the exporter chain; the span is
// forwarded as-is on the assumption the exporter will reject malformed
// payloads at its own layer.
// Telemetry is optional; privacy is not. Replace the entire attribute bag
// on scrubber failure, or drop the span when even that is impossible.
try {
span.attributes = {};
} catch {
return;
}
}
this.delegate.onEnd(span);
}
@@ -203,3 +226,20 @@ export class PIISpanProcessor implements DelegateSpanProcessor {
return this.delegate.shutdown();
}
}
function redactPortalCapabilityTokens(value: string): string {
return value.replace(PORTAL_CAPABILITY_ROUTE_PATTERN, "$1redacted");
}
function redactSpanName(span: PiiScrubableSpan): boolean {
if (typeof span.name !== "string") return true;
const sanitized = scrubSpanName(span.name);
if (sanitized === span.name) return true;
try {
span.name = sanitized;
return span.name === sanitized;
} catch {
// A credential-bearing immutable span name cannot be exported safely.
return false;
}
}