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
+374
@@ -0,0 +1,374 @@
|
||||
import { describe, expect, test } from "vitest";
|
||||
import { luhnValid, sanitizePluginOutput, type SanitizationStrictness } from "./sanitize.js";
|
||||
|
||||
/** Build a base64url JWT with the given JSON header + payload. */
|
||||
function makeJwt(header: object, payload: object, signature = "abcd1234"): string {
|
||||
const b64 = (obj: object) =>
|
||||
Buffer.from(JSON.stringify(obj))
|
||||
.toString("base64")
|
||||
.replace(/=+$/, "")
|
||||
.replace(/\+/g, "-")
|
||||
.replace(/\//g, "_");
|
||||
return `${b64(header)}.${b64(payload)}.${signature}`;
|
||||
}
|
||||
|
||||
describe("luhnValid", () => {
|
||||
test("accepts a valid PAN", () => {
|
||||
expect(luhnValid("4111111111111111")).toBe(true);
|
||||
expect(luhnValid("5500000000000004")).toBe(true);
|
||||
});
|
||||
|
||||
test("rejects an invalid PAN", () => {
|
||||
expect(luhnValid("1234567890123456")).toBe(false);
|
||||
expect(luhnValid("0000000000000000")).toBe(false);
|
||||
});
|
||||
|
||||
test("rejects non-digit input", () => {
|
||||
expect(luhnValid("4111-1111")).toBe(false);
|
||||
expect(luhnValid("")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("sanitizePluginOutput — credit cards", () => {
|
||||
test("redacts a Luhn-valid PAN", () => {
|
||||
expect(sanitizePluginOutput("paid with 4111 1111 1111 1111 today")).toBe(
|
||||
"paid with [REDACTED:credit-card] today"
|
||||
);
|
||||
});
|
||||
|
||||
test("redacts a hyphenated PAN", () => {
|
||||
expect(sanitizePluginOutput("card: 4111-1111-1111-1111.")).toBe(
|
||||
"card: [REDACTED:credit-card]."
|
||||
);
|
||||
});
|
||||
|
||||
test("does NOT redact a Luhn-invalid 16-digit string (order id false positive)", () => {
|
||||
expect(sanitizePluginOutput("order 1234567890123456 placed")).toBe(
|
||||
"order 1234567890123456 placed"
|
||||
);
|
||||
});
|
||||
|
||||
test("does NOT redact a short numeric (12 digits) — out of PAN range", () => {
|
||||
expect(sanitizePluginOutput("zip 123456789012")).toBe("zip 123456789012");
|
||||
});
|
||||
});
|
||||
|
||||
describe("sanitizePluginOutput — JWT", () => {
|
||||
test("redacts a JWT whose payload + header parse as JSON object", () => {
|
||||
const jwt = makeJwt({ alg: "HS256", typ: "JWT" }, { sub: "u1", iat: 1 });
|
||||
expect(sanitizePluginOutput(`bearer ${jwt}`)).toBe("bearer [REDACTED:jwt]");
|
||||
});
|
||||
|
||||
test("does NOT redact three random dotted segments (not JSON)", () => {
|
||||
// No quote in any segment, payload doesn't parse to object.
|
||||
const fake = "foo.bar.baz";
|
||||
expect(sanitizePluginOutput(fake)).toBe(fake);
|
||||
});
|
||||
});
|
||||
|
||||
describe("sanitizePluginOutput — IBAN", () => {
|
||||
test("redacts a German IBAN (DE, 22 chars)", () => {
|
||||
expect(sanitizePluginOutput("IBAN DE89370400440532013000 ok")).toBe(
|
||||
"IBAN [REDACTED:iban] ok"
|
||||
);
|
||||
});
|
||||
|
||||
test("redacts an Austrian IBAN (AT, 20 chars)", () => {
|
||||
expect(sanitizePluginOutput("AT611904300234573201 sent")).toBe("[REDACTED:iban] sent");
|
||||
});
|
||||
|
||||
test("does NOT redact a DE-prefix with wrong length", () => {
|
||||
// 18 chars total — wrong for DE (expected 22).
|
||||
expect(sanitizePluginOutput("DE893704004405320")).toBe("DE893704004405320");
|
||||
});
|
||||
|
||||
test("does NOT redact an unknown country code", () => {
|
||||
expect(sanitizePluginOutput("ZZ12345678901234567890")).toBe("ZZ12345678901234567890");
|
||||
});
|
||||
});
|
||||
|
||||
describe("sanitizePluginOutput — email", () => {
|
||||
test("strict mode redacts emails", () => {
|
||||
expect(sanitizePluginOutput("contact alice@example.com", "strict")).toBe(
|
||||
"contact [REDACTED:email]"
|
||||
);
|
||||
});
|
||||
|
||||
test("moderate mode leaves emails intact", () => {
|
||||
expect(sanitizePluginOutput("contact alice@example.com", "moderate")).toBe(
|
||||
"contact alice@example.com"
|
||||
);
|
||||
});
|
||||
|
||||
test("off mode leaves emails intact", () => {
|
||||
expect(sanitizePluginOutput("contact alice@example.com", "off")).toBe(
|
||||
"contact alice@example.com"
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("sanitizePluginOutput — API key heuristic", () => {
|
||||
test("redacts Stripe-style secret key (sk_live_)", () => {
|
||||
expect(sanitizePluginOutput("Authorization: sk_live_abcdef0123456789ABCDEFxyz")).toBe(
|
||||
"Authorization: [REDACTED:api-key]"
|
||||
);
|
||||
});
|
||||
|
||||
test("redacts GitHub PAT (ghp_)", () => {
|
||||
expect(sanitizePluginOutput("token=ghp_abcdefghijklmnopqrstuvwxyz0123456789")).toBe(
|
||||
"token=[REDACTED:api-key]"
|
||||
);
|
||||
});
|
||||
|
||||
test("redacts a high-entropy 40-char hex+alpha token", () => {
|
||||
const secret = "A1b2C3d4E5f6G7h8I9j0K1l2M3n4O5p6Q7r8S9t0";
|
||||
const out = sanitizePluginOutput(`key=${secret} end`);
|
||||
expect(out).toBe("key=[REDACTED:api-key] end");
|
||||
});
|
||||
|
||||
test("does NOT redact a pure-lowercase 40-char id (looks like uuid-ish)", () => {
|
||||
const id = "abcdefghijklmnopqrstuvwxyzabcdefghijklmn";
|
||||
expect(sanitizePluginOutput(`id=${id}`)).toBe(`id=${id}`);
|
||||
});
|
||||
|
||||
test("does NOT redact a short alphanumeric token (< 32 chars)", () => {
|
||||
expect(sanitizePluginOutput("hello WORLD123")).toBe("hello WORLD123");
|
||||
});
|
||||
});
|
||||
|
||||
describe("sanitizePluginOutput — field-name awareness", () => {
|
||||
test("strict mode redacts `password` value regardless of pattern", () => {
|
||||
const input = { user: "bob", password: "hunter2" };
|
||||
expect(sanitizePluginOutput(input, "strict")).toEqual({
|
||||
user: "bob",
|
||||
password: "[REDACTED:field]",
|
||||
});
|
||||
});
|
||||
|
||||
test("moderate mode redacts `password` value when length > 4", () => {
|
||||
expect(sanitizePluginOutput({ password: "hunter2" }, "moderate")).toEqual({
|
||||
password: "[REDACTED:field]",
|
||||
});
|
||||
});
|
||||
|
||||
test("moderate mode passes short field-name value through", () => {
|
||||
expect(sanitizePluginOutput({ password: "abcd" }, "moderate")).toEqual({
|
||||
password: "abcd",
|
||||
});
|
||||
});
|
||||
|
||||
test("redacts `apiKey` (camelCase) value", () => {
|
||||
expect(sanitizePluginOutput({ apiKey: "supersecret" }, "moderate")).toEqual({
|
||||
apiKey: "[REDACTED:field]",
|
||||
});
|
||||
});
|
||||
|
||||
test("redacts `api_key` (snake_case) value", () => {
|
||||
expect(sanitizePluginOutput({ api_key: "supersecret" }, "moderate")).toEqual({
|
||||
api_key: "[REDACTED:field]",
|
||||
});
|
||||
});
|
||||
|
||||
test("redacts `credit_card`, `creditCard`, `cardnumber` aliases", () => {
|
||||
expect(
|
||||
sanitizePluginOutput(
|
||||
{ credit_card: "abcdef", creditCard: "abcdef", cardnumber: "abcdef" },
|
||||
"moderate"
|
||||
)
|
||||
).toEqual({
|
||||
credit_card: "[REDACTED:field]",
|
||||
creditCard: "[REDACTED:field]",
|
||||
cardnumber: "[REDACTED:field]",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("sanitizePluginOutput — strictness modes", () => {
|
||||
test("off is identity for strings", () => {
|
||||
const s = "alice@example.com / 4111111111111111";
|
||||
expect(sanitizePluginOutput(s, "off")).toBe(s);
|
||||
});
|
||||
|
||||
test("off is identity for nested objects", () => {
|
||||
const input = { password: "hunter2", nested: { token: "abc" } };
|
||||
expect(sanitizePluginOutput(input, "off")).toBe(input);
|
||||
});
|
||||
});
|
||||
|
||||
describe("sanitizePluginOutput — walk depth cap", () => {
|
||||
test("replaces values past depth 16 with [DEPTH_LIMIT]", () => {
|
||||
type Nest = { v: unknown };
|
||||
let value: Nest = { v: "deep-secret" };
|
||||
for (let i = 0; i < 20; i++) {
|
||||
value = { v: value };
|
||||
}
|
||||
const out = sanitizePluginOutput(value, "moderate") as Nest;
|
||||
// Walk a few levels and confirm we eventually hit the depth marker.
|
||||
let cursor: unknown = out;
|
||||
let found = false;
|
||||
for (let i = 0; i < 25; i++) {
|
||||
if (cursor === "[DEPTH_LIMIT]") {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
if (typeof cursor === "object" && cursor && "v" in cursor) {
|
||||
cursor = (cursor as Nest).v;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
expect(found).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("sanitizePluginOutput — string length cap", () => {
|
||||
test("truncates strings over 64 KB and appends [TRUNCATED]", () => {
|
||||
const big = "a".repeat(64 * 1024 + 100);
|
||||
const out = sanitizePluginOutput(big, "moderate") as string;
|
||||
expect(out.length).toBe(64 * 1024 + "[TRUNCATED]".length);
|
||||
expect(out.endsWith("[TRUNCATED]")).toBe(true);
|
||||
});
|
||||
|
||||
test("leaves short strings untouched", () => {
|
||||
expect(sanitizePluginOutput("short", "moderate")).toBe("short");
|
||||
});
|
||||
});
|
||||
|
||||
describe("sanitizePluginOutput — non-string pass-through", () => {
|
||||
test.each<[unknown]>([[42], [3.14], [true], [false], [null], [undefined]])(
|
||||
"passes %s through unchanged",
|
||||
(val) => {
|
||||
expect(sanitizePluginOutput(val, "moderate")).toBe(val);
|
||||
expect(sanitizePluginOutput(val, "strict")).toBe(val);
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
describe("sanitizePluginOutput — arrays and nested objects", () => {
|
||||
test("walks arrays element-wise", () => {
|
||||
const out = sanitizePluginOutput(["4111 1111 1111 1111", "ok", 42], "moderate");
|
||||
expect(out).toEqual(["[REDACTED:credit-card]", "ok", 42]);
|
||||
});
|
||||
|
||||
test("walks nested objects", () => {
|
||||
const out = sanitizePluginOutput(
|
||||
{ a: { b: { iban: "DE89370400440532013000" } } },
|
||||
"moderate"
|
||||
);
|
||||
expect(out).toEqual({ a: { b: { iban: "[REDACTED:field]" } } });
|
||||
});
|
||||
});
|
||||
|
||||
describe("sanitizePluginOutput — immutability", () => {
|
||||
test("does not mutate the original object", () => {
|
||||
const original = { password: "hunter2", nested: { card: "4111 1111 1111 1111" } };
|
||||
const snapshot = JSON.parse(JSON.stringify(original));
|
||||
sanitizePluginOutput(original, "moderate");
|
||||
expect(original).toEqual(snapshot);
|
||||
});
|
||||
|
||||
test("does not mutate the original array", () => {
|
||||
const original = ["4111 1111 1111 1111", { token: "supersecret" }];
|
||||
const snapshot = JSON.parse(JSON.stringify(original));
|
||||
sanitizePluginOutput(original, "moderate");
|
||||
expect(original).toEqual(snapshot);
|
||||
});
|
||||
});
|
||||
|
||||
describe("sanitizePluginOutput — strictness type guard", () => {
|
||||
test("accepts each strictness mode without throwing", () => {
|
||||
const modes: SanitizationStrictness[] = ["strict", "moderate", "off"];
|
||||
for (const m of modes) {
|
||||
expect(() => sanitizePluginOutput("hello", m)).not.toThrow();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// F-023 — circular reference safety.
|
||||
describe("sanitizePluginOutput — circular references (F-023)", () => {
|
||||
test("handles a self-referential object without throwing", () => {
|
||||
type Circ = { name: string; self?: Circ };
|
||||
const root: Circ = { name: "root" };
|
||||
root.self = root;
|
||||
expect(() => sanitizePluginOutput(root, "moderate")).not.toThrow();
|
||||
const out = sanitizePluginOutput(root, "moderate") as { name: string; self: unknown };
|
||||
expect(out.name).toBe("root");
|
||||
expect(out.self).toBe("[CIRCULAR]");
|
||||
});
|
||||
|
||||
test("handles two-step cycles between objects", () => {
|
||||
type Node = { tag: string; ref?: Node };
|
||||
const a: Node = { tag: "a" };
|
||||
const b: Node = { tag: "b" };
|
||||
a.ref = b;
|
||||
b.ref = a;
|
||||
expect(() => sanitizePluginOutput(a, "moderate")).not.toThrow();
|
||||
const out = sanitizePluginOutput(a, "moderate") as { tag: string; ref: { tag: string; ref: unknown } };
|
||||
expect(out.tag).toBe("a");
|
||||
expect(out.ref.tag).toBe("b");
|
||||
expect(out.ref.ref).toBe("[CIRCULAR]");
|
||||
});
|
||||
|
||||
test("handles circular arrays", () => {
|
||||
const arr: unknown[] = [1, 2];
|
||||
arr.push(arr);
|
||||
expect(() => sanitizePluginOutput(arr, "moderate")).not.toThrow();
|
||||
const out = sanitizePluginOutput(arr, "moderate") as unknown[];
|
||||
expect(out[0]).toBe(1);
|
||||
expect(out[2]).toBe("[CIRCULAR]");
|
||||
});
|
||||
});
|
||||
|
||||
// F-014 — total output-size budget across the walk.
|
||||
describe("sanitizePluginOutput — total size budget (F-014)", () => {
|
||||
const originalBudget = process.env.PLUGIN_SANITIZE_TOTAL_BUDGET_BYTES;
|
||||
|
||||
function setBudget(bytes: number) {
|
||||
process.env.PLUGIN_SANITIZE_TOTAL_BUDGET_BYTES = String(bytes);
|
||||
}
|
||||
|
||||
function restoreBudget() {
|
||||
if (originalBudget === undefined) {
|
||||
delete process.env.PLUGIN_SANITIZE_TOTAL_BUDGET_BYTES;
|
||||
} else {
|
||||
process.env.PLUGIN_SANITIZE_TOTAL_BUDGET_BYTES = originalBudget;
|
||||
}
|
||||
}
|
||||
|
||||
test("a 10 MB-equivalent nested object is bounded by the budget", () => {
|
||||
setBudget(1024); // tiny budget — 1 KB
|
||||
const big: Record<string, string> = {};
|
||||
for (let i = 0; i < 500; i++) {
|
||||
big[`k${i}`] = "abcdefghijklmnopqrstuvwxyz".repeat(20); // ~520 chars
|
||||
}
|
||||
const out = sanitizePluginOutput(big, "moderate") as Record<string, unknown>;
|
||||
restoreBudget();
|
||||
// Every key should still be in the output (we don't drop keys, only
|
||||
// values), but later values must be SIZE_LIMIT_MARKER once the
|
||||
// budget is exhausted.
|
||||
expect(Object.keys(out)).toHaveLength(500);
|
||||
const limited = Object.values(out).filter((v) => v === "[SIZE_LIMIT]");
|
||||
expect(limited.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
test("budget exhaustion replaces array tails with the size marker", () => {
|
||||
setBudget(64);
|
||||
const arr = Array.from({ length: 50 }, (_, i) => `value-${i}-payload-string`);
|
||||
const out = sanitizePluginOutput(arr, "moderate") as unknown[];
|
||||
restoreBudget();
|
||||
expect(out[0]).toBe("value-0-payload-string");
|
||||
expect(out[out.length - 1]).toBe("[SIZE_LIMIT]");
|
||||
});
|
||||
|
||||
test("a small payload comfortably fits within the default 1 MiB budget", () => {
|
||||
delete process.env.PLUGIN_SANITIZE_TOTAL_BUDGET_BYTES;
|
||||
const out = sanitizePluginOutput({ a: "hello", b: [1, 2, "world"] }, "moderate");
|
||||
expect(out).toEqual({ a: "hello", b: [1, 2, "world"] });
|
||||
});
|
||||
|
||||
test("an invalid budget value falls back to the default and does not crash", () => {
|
||||
process.env.PLUGIN_SANITIZE_TOTAL_BUDGET_BYTES = "not-a-number";
|
||||
expect(() => sanitizePluginOutput({ a: "ok" }, "moderate")).not.toThrow();
|
||||
restoreBudget();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user