fix(lint): recognize escaped sensitive literals instead of suppressing them

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.
This commit is contained in:
DeryFerd
2026-11-17 00:22:42 +07:00
parent de982cb7d3
commit 5464cb4cfd
2 changed files with 37 additions and 1 deletions
+26 -1
View File
@@ -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;
}
+11
View File
@@ -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);