fix(leaks): parse structured secret candidates
baf0004dc1a3dfb42fa2d55bfbc971f110374001
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
- Parent 1: 615caa1cc6af
bugabingas pi extensions
baf0004dc1a3dfb42fa2d55bfbc971f110374001
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
+97 / −32 text lines
+17 / −0 text lines
it("keeps source code and complete-name near misses visible", () => { const ordinary = ["ordinary", "metadata", "value", "123456"].join("-"); const negatives = [ ["agendaFlick", "visibleArea", "yPosition"].join("."), ["application/vnd", "openxmlformats-officedocument", "sheet"].join("."), `const STATUS_KEY = "${ordinary}";`, `const COOKIE = "${ordinary}";`, `const result = { session: runtimeSession };`, 'function sum(key: "additions" | "deletions") {}', ["https://example.test/?mon", `key=${ordinary}`].join(""), ["Cookie", `: keyboard=${ordinary}`].join(""), ["<session>", ordinary, "</session>"].join(""), ]; for (const value of negatives) expect(redacted(value), value).toBe(value); });+42 / −25 text lines
it("redacts assignment values across common one-line syntaxes", () => { it("redacts only provider-shaped assignment values", () => { const samples = [ `TOKEN=${secret}`, `token=${secret}`, `token: ${secret}`, `"token": "${secret}"`, `token = "${secret}"`, `<token>${secret}</token>`, `export TOKEN=${secret}`, ]; for (const sample of samples) { const result = engine.redactText(sample, "provider"); expect(result.text.includes(secret)).toBe(false); expect(result.text.toLowerCase(), sample).toContain("token".slice(0, 3)); it("requires decoded JWT objects", () => { const dotted = ["agendaFlick", "visibleArea", "yPosition"].join("."); expect(__test.findJwtSpans(dotted)).toEqual([]); expect(__test.findJwtSpans(jwt).map((span) => span.value)).toEqual([jwt]); }); it("uses complete query and cookie names", () => { const ordinary = ["ordinary", "metadata", "value", "123456"].join("-"); expect( __test.findSensitiveQuerySpans( ["?mon", `key=${ordinary}`].join(""), ), ).toEqual([]); expect( __test.findCookieSpans(["Cookie", `: keyboard=${ordinary}`].join("")), ).toEqual([]); expect( __test.findSensitiveQuerySpans(`?token=${secret}`).map((span) => span.value), ).toEqual([secret]); expect( __test .findCookieSpans(["Cookie", `: session=${secret}`].join("")) .map((span) => span.value), ).toEqual([secret]); }); it("stoplist does not block keys that overlap with SENSITIVE", () => { const longValue = "abcdef1234567890"; // 'key' is in dotenv.SENSITIVE; assignment detection must still match. expect( __test.findAssignmentSpans(`KEY=${longValue}`).length, ).toBeGreaterThan(0); // 'token' is in dotenv.SENSITIVE; assignment detection must still match. expect( __test.findAssignmentSpans(`TOKEN=${longValue}`).length, ).toBeGreaterThan(0);+38 / −7 text lines
const name = match[2] as string; if (isWeakValue(value) && !highConfidenceSecretShape(value)) continue; if (!highConfidenceSecretShape(value)) continue; return [...text.matchAll(JWT_RE)].map((match) => { const spans: SecretSpan[] = []; for (const match of text.matchAll(JWT_RE)) { const [headerPart, payloadPart, signaturePart] = match[0].split("."); if (!headerPart || !payloadPart || !strictBase64Url(signaturePart)) continue; const header = decodeJsonObject(headerPart); const payload = decodeJsonObject(payloadPart); if (!header || !payload || typeof header.alg !== "string" || !header.alg) continue; }); } return spans;} function strictBase64Url(value: string | undefined) { if (!value || !/^[A-Za-z0-9_-]+$/.test(value)) return false; return Buffer.from(value, "base64url").toString("base64url") === value;}function decodeJsonObject(value: string) { if (!strictBase64Url(value)) return undefined; try { const bytes = Buffer.from(value, "base64url"); const text = new TextDecoder("utf-8", { fatal: true }).decode(bytes); const parsed: unknown = JSON.parse(text); if (!isPlainRecord(parsed)) return undefined; return parsed; } catch { return undefined; }}function isPlainRecord(value: unknown): value is Record<string, unknown> { return value !== null && typeof value === "object" && !Array.isArray(value);