From 5464cb4cfd0f1e0c3b474199bc5f9d019e2cf718 Mon Sep 17 00:00:00 2001 From: DeryFerd Date: Tue, 17 Nov 2026 00:22:42 +0700 Subject: [PATCH] fix(lint): recognize escaped sensitive literals instead of suppressing them MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit extractStringLiterals unescapes common sequences before classification, but looksAlreadyEncrypted searched the unescaped literal in the raw source — a secret written with one escape sequence could never match, so the finding was silently dropped and the bankrupt gate stayed open. Search the escaped spelling as well; add a bypass regression test. --- src/engine/plaintext-secret-lint.ts | 27 ++++++++++++++++++++++++++- tests/plaintext-secret-lint.test.ts | 11 +++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/engine/plaintext-secret-lint.ts b/src/engine/plaintext-secret-lint.ts index 5fde417..d0099a7 100644 --- a/src/engine/plaintext-secret-lint.ts +++ b/src/engine/plaintext-secret-lint.ts @@ -156,6 +156,22 @@ export function extractStringLiterals(source: string): string[] { return out; } +/** + * Re-escape a literal the way `extractStringLiterals` unescapes it, so a + * literal written with `\\n`-style escapes in the raw source can still be + * recognized as present. Order matters: backslashes first. + */ +function escapeForSourceSearch(literal: string): string { + return literal + .replace(/\\/g, "\\\\") + .replace(/\n/g, "\\n") + .replace(/\r/g, "\\r") + .replace(/\t/g, "\\t") + .replace(/'/g, "\\'") + .replace(/"/g, '\\"') + .replace(/`/g, "\\`"); +} + /** * True when a literal already lives inside the XOR encrypt-strings table * (opaque payload / index calls) — do not re-flag. @@ -168,7 +184,16 @@ export function looksAlreadyEncrypted(source: string, literal: string): boolean } } // Combinatorial AST leaves chunks but not the full literal as one piece ≥8. - if (literal.length >= 8 && !source.includes(literal)) return true; + // The literal was unescaped on extraction, so also accept its escaped + // spelling — otherwise a secret written with one escape sequence would be + // treated as "absent from source" and silently skipped. + if ( + literal.length >= 8 && + !source.includes(literal) && + !source.includes(escapeForSourceSearch(literal)) + ) { + return true; + } return false; } diff --git a/tests/plaintext-secret-lint.test.ts b/tests/plaintext-secret-lint.test.ts index 7a0cb3d..6c73ee8 100644 --- a/tests/plaintext-secret-lint.test.ts +++ b/tests/plaintext-secret-lint.test.ts @@ -37,6 +37,17 @@ describe("plaintext-secret lint (Direction-2)", () => { expect(report.findings.filter((f) => f.severity === "error")).toEqual([]); }); + it("flags a sensitive literal written with an escaped byte (bypass guard)", () => { + // The raw source carries a backslash-n escape, so the unescaped literal + // can never appear in the raw source. The "literal absent from source" + // suppression must not hide a real secret just because it was written + // with an escape sequence. + const src = `const api = "sk_live_A1\\nB2C3D4E5F6G7H8";`; + const report = lintPlaintextSecretsSource(src); + expect(report.findings.length).toBeGreaterThan(0); + expect(report.shouldRefuseBankrupt).toBe(true); + }); + it("treats http(s) URLs as warning, not bankrupt refuse", () => { const src = `const ep = "https://enclave.internal/v1/slot/demo";`; const report = lintPlaintextSecretsSource(src);