Reorganize repo structure and standards references
This commit is contained in:
1 parent
3cb93a81be
commit
be2054274d
5885 files changed
+2588
-195
No files matched your search
+207
@@ -0,0 +1,207 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import {
|
||||
HEADER_ALLOWLIST,
|
||||
PIISpanProcessor,
|
||||
redactSpanAttributes,
|
||||
scrubDbStatement,
|
||||
scrubUrlQuery,
|
||||
type DelegateSpanProcessor,
|
||||
type PiiScrubableSpan,
|
||||
} from "./pii-span-processor.js";
|
||||
|
||||
describe("scrubUrlQuery", () => {
|
||||
it("replaces the query-string with the [scrubbed] placeholder", () => {
|
||||
expect(scrubUrlQuery("https://api.example.com/path?token=secret&foo=bar")).toBe("https://api.example.com/path?[scrubbed]");
|
||||
});
|
||||
|
||||
it("returns the input unchanged when no query-string is present", () => {
|
||||
expect(scrubUrlQuery("https://api.example.com/path")).toBe("https://api.example.com/path");
|
||||
});
|
||||
|
||||
it("handles empty query-strings", () => {
|
||||
expect(scrubUrlQuery("https://api.example.com/path?")).toBe("https://api.example.com/path?[scrubbed]");
|
||||
});
|
||||
});
|
||||
|
||||
describe("scrubDbStatement", () => {
|
||||
it("rewrites single-quoted literals to `?`", () => {
|
||||
expect(scrubDbStatement("SELECT * FROM users WHERE email = 'alice@example.com'")).toBe("SELECT * FROM users WHERE email = ?");
|
||||
});
|
||||
|
||||
it("rewrites double-quoted literals to `?`", () => {
|
||||
expect(scrubDbStatement('UPDATE t SET name = "Alice"')).toBe("UPDATE t SET name = ?");
|
||||
});
|
||||
|
||||
it("handles multiple literals", () => {
|
||||
expect(scrubDbStatement("INSERT INTO t (a, b) VALUES ('x', 'y')")).toBe("INSERT INTO t (a, b) VALUES (?, ?)");
|
||||
});
|
||||
|
||||
it("preserves escaped quotes inside literals without infinite loops", () => {
|
||||
expect(scrubDbStatement("SELECT 'foo\\'bar' AS x")).toBe("SELECT ? AS x");
|
||||
});
|
||||
|
||||
it("returns the input unchanged when no string literal is present", () => {
|
||||
expect(scrubDbStatement("SELECT count(*) FROM users WHERE id = $1")).toBe("SELECT count(*) FROM users WHERE id = $1");
|
||||
});
|
||||
});
|
||||
|
||||
describe("redactSpanAttributes", () => {
|
||||
it("strips query-strings from URL-like attributes (http.url, http.target, url.full, url.path)", () => {
|
||||
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",
|
||||
};
|
||||
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]");
|
||||
});
|
||||
|
||||
it("drops authorization, cookie, set-cookie and x-api-key request headers", () => {
|
||||
const attributes: Record<string, unknown> = {
|
||||
"http.request.header.authorization": "Bearer redacted-me",
|
||||
"http.request.header.cookie": "session=abc",
|
||||
"http.request.header.x-api-key": "k-123",
|
||||
"http.response.header.set-cookie": "session=xyz",
|
||||
};
|
||||
redactSpanAttributes(attributes);
|
||||
expect(attributes).not.toHaveProperty("http.request.header.authorization");
|
||||
expect(attributes).not.toHaveProperty("http.request.header.cookie");
|
||||
expect(attributes).not.toHaveProperty("http.request.header.x-api-key");
|
||||
expect(attributes).not.toHaveProperty("http.response.header.set-cookie");
|
||||
});
|
||||
|
||||
it("keeps every header in the allowlist", () => {
|
||||
const attributes: Record<string, unknown> = {};
|
||||
for (const allowed of HEADER_ALLOWLIST) {
|
||||
attributes[`http.request.header.${allowed}`] = "value";
|
||||
attributes[`http.response.header.${allowed}`] = "value";
|
||||
}
|
||||
redactSpanAttributes(attributes);
|
||||
for (const allowed of HEADER_ALLOWLIST) {
|
||||
expect(attributes).toHaveProperty(`http.request.header.${allowed}`, "value");
|
||||
expect(attributes).toHaveProperty(`http.response.header.${allowed}`, "value");
|
||||
}
|
||||
});
|
||||
|
||||
it("scrubs db.statement literals to `?`", () => {
|
||||
const attributes: Record<string, unknown> = {
|
||||
"db.statement": "SELECT * FROM users WHERE email = 'leak@example.com'",
|
||||
};
|
||||
redactSpanAttributes(attributes);
|
||||
expect(attributes["db.statement"]).toBe("SELECT * FROM users WHERE email = ?");
|
||||
});
|
||||
|
||||
it("drops messaging.message_payload but keeps the payload-size attribute", () => {
|
||||
const attributes: Record<string, unknown> = {
|
||||
"messaging.message_payload": "raw-job-blob",
|
||||
"messaging.message_payload_size_bytes": 42,
|
||||
};
|
||||
redactSpanAttributes(attributes);
|
||||
expect(attributes).not.toHaveProperty("messaging.message_payload");
|
||||
expect(attributes["messaging.message_payload_size_bytes"]).toBe(42);
|
||||
});
|
||||
|
||||
it("is a no-op on undefined attributes", () => {
|
||||
expect(() => redactSpanAttributes(undefined)).not.toThrow();
|
||||
});
|
||||
|
||||
it("ignores non-string values on URL-like and db.statement keys", () => {
|
||||
const attributes: Record<string, unknown> = {
|
||||
"http.url": 42,
|
||||
"db.statement": null,
|
||||
};
|
||||
redactSpanAttributes(attributes);
|
||||
expect(attributes["http.url"]).toBe(42);
|
||||
expect(attributes["db.statement"]).toBeNull();
|
||||
});
|
||||
|
||||
it("lowercases the header name before matching the allowlist", () => {
|
||||
const attributes: Record<string, unknown> = {
|
||||
"http.request.header.Authorization": "Bearer redacted-me",
|
||||
"http.request.header.Content-Type": "application/json",
|
||||
};
|
||||
redactSpanAttributes(attributes);
|
||||
expect(attributes).not.toHaveProperty("http.request.header.Authorization");
|
||||
expect(attributes).toHaveProperty("http.request.header.Content-Type", "application/json");
|
||||
});
|
||||
});
|
||||
|
||||
describe("PIISpanProcessor", () => {
|
||||
function makeDelegateStub(): DelegateSpanProcessor & {
|
||||
onStart: ReturnType<typeof vi.fn>;
|
||||
onEnd: ReturnType<typeof vi.fn>;
|
||||
forceFlush: ReturnType<typeof vi.fn>;
|
||||
shutdown: ReturnType<typeof vi.fn>;
|
||||
} {
|
||||
return {
|
||||
onStart: vi.fn(),
|
||||
onEnd: vi.fn(),
|
||||
forceFlush: vi.fn(async () => undefined),
|
||||
shutdown: vi.fn(async () => undefined),
|
||||
};
|
||||
}
|
||||
|
||||
it("delegates onStart unchanged", () => {
|
||||
const delegate = makeDelegateStub();
|
||||
const processor = new PIISpanProcessor(delegate);
|
||||
const span: PiiScrubableSpan = { attributes: { "http.url": "https://example.com/?q=1" } };
|
||||
const context = {};
|
||||
processor.onStart(span, context);
|
||||
expect(delegate.onStart).toHaveBeenCalledWith(span, context);
|
||||
// onStart must NOT scrub: attributes that are added later in span lifetime
|
||||
// must still be scrubbable at onEnd.
|
||||
expect(span.attributes?.["http.url"]).toBe("https://example.com/?q=1");
|
||||
});
|
||||
|
||||
it("redacts span attributes during onEnd before delegating", () => {
|
||||
const delegate = makeDelegateStub();
|
||||
const processor = new PIISpanProcessor(delegate);
|
||||
const span: PiiScrubableSpan = {
|
||||
attributes: {
|
||||
"http.url": "https://example.com/path?token=secret",
|
||||
"http.request.header.authorization": "Bearer secret",
|
||||
"http.request.header.content-type": "application/json",
|
||||
"db.statement": "SELECT * FROM users WHERE email = 'leak@example.com'",
|
||||
"messaging.message_payload": "raw-job-blob",
|
||||
},
|
||||
};
|
||||
|
||||
processor.onEnd(span);
|
||||
|
||||
expect(delegate.onEnd).toHaveBeenCalledTimes(1);
|
||||
expect(delegate.onEnd).toHaveBeenCalledWith(span);
|
||||
expect(span.attributes?.["http.url"]).toBe("https://example.com/path?[scrubbed]");
|
||||
expect(span.attributes).not.toHaveProperty("http.request.header.authorization");
|
||||
expect(span.attributes?.["http.request.header.content-type"]).toBe("application/json");
|
||||
expect(span.attributes?.["db.statement"]).toBe("SELECT * FROM users WHERE email = ?");
|
||||
expect(span.attributes).not.toHaveProperty("messaging.message_payload");
|
||||
});
|
||||
|
||||
it("still delegates onEnd when the scrubber throws", () => {
|
||||
const delegate = makeDelegateStub();
|
||||
const processor = new PIISpanProcessor(delegate);
|
||||
// Inject a getter that throws when the scrubber iterates the attribute bag.
|
||||
const span: PiiScrubableSpan = {
|
||||
get attributes() {
|
||||
throw new Error("attributes unavailable");
|
||||
},
|
||||
} as PiiScrubableSpan;
|
||||
|
||||
expect(() => processor.onEnd(span)).not.toThrow();
|
||||
expect(delegate.onEnd).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("forwards forceFlush and shutdown to the delegate", async () => {
|
||||
const delegate = makeDelegateStub();
|
||||
const processor = new PIISpanProcessor(delegate);
|
||||
await processor.forceFlush();
|
||||
await processor.shutdown();
|
||||
expect(delegate.forceFlush).toHaveBeenCalledTimes(1);
|
||||
expect(delegate.shutdown).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user