The 19 fixed credential shapes in pre-edit-secrets.mjs were regex literals; they now come from signatures/secret-egress.json in the vendored commons via a new scanners/lib/secret-egress.mjs. Policy-injected custom patterns (entries 20+) are unchanged and still appended by the hook. Measured before the swap, not assumed: all 19 positions compared for order, name, regex source and flags, plus recompilation identity, against the literal table sliced out of the module text. Zero divergences. Commons had reported the same result; that was their measurement, so this one was run anyway. STATE's expectation that the golden gate would go red on both table records and file sha256 was wrong: pre-edit-secrets.mjs is in neither PINNED_FILES nor WALKED_MODULES, so the table had no golden coverage at all and the swap moved nothing. Rather than leave the vendored data with only behavioural coverage, secret-egress.mjs joins WALKED_MODULES — walked, not pinned, since it inlines no regex of its own. Golden diff was 19 ADDED, 0 CHANGED, 0 REMOVED, each source byte-identical to the pre-swap literal; re-blessed. suite-counts.json untouched. Tests: coverage is derived from the loaded table, so an entry commons adds cannot arrive without an end-to-end probe. All 19 now block through the real hook and are asserted by label, which also pins the ordering contract (a Bearer-wrapped JWT must report as the header). Mutating the vendored JSON fires in both directions plus reorder: under-match (AKIA quantifier) reddens 3 hook tests + golden; over-match (Anthropic key truncated to its prefix) reddens the false-positive probe + golden; moving the JWT entry ahead of the Bearer entry reddens the ordering test. Suite 2231 tests / 2223 pass / 6 skipped. The two parallel-run failures (pre-compact size-cap, benchmark) pass alone — the known timing flakes. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MGMv5ZTUhVzZtCCwRrNZG5
84 lines
3.3 KiB
JavaScript
84 lines
3.3 KiB
JavaScript
#!/usr/bin/env node
|
|
// Hook: pre-edit-secrets.mjs (consolidated)
|
|
// Event: PreToolUse (Edit|Write)
|
|
// Purpose: Detect secrets/credentials in file content before writing.
|
|
// Consolidates patterns from global, kiur, llm-security, and ms-ai-architect.
|
|
//
|
|
// Protocol:
|
|
// - Read JSON from stdin: { tool_name, tool_input }
|
|
// - tool_input.file_path — destination path
|
|
// - tool_input.content — full content (Write)
|
|
// - tool_input.new_string — replacement text (Edit)
|
|
// - Block: stderr + exit 2
|
|
// - Allow: exit 0
|
|
|
|
import { readFileSync } from 'node:fs';
|
|
import { normalize } from 'node:path';
|
|
import { getPolicyValue } from '../../scanners/lib/policy-loader.mjs';
|
|
import { SECRET_PATTERNS as COMMONS_SECRET_PATTERNS } from '../../scanners/lib/secret-egress.mjs';
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Secret detection patterns
|
|
// ---------------------------------------------------------------------------
|
|
//
|
|
// The 19 fixed entries (union of global, kiur, llm-security, ms-ai-architect)
|
|
// were regex literals here until the v8 Phase 5 swap; they now come from
|
|
// `signatures/secret-egress.json` in the vendored commons, via
|
|
// scanners/lib/secret-egress.mjs — measured position-by-position before the
|
|
// swap (order, name, source, flags), zero divergences. Order is load-bearing:
|
|
// first match wins, and the entry a finding is reported AS depends on it.
|
|
// See that module's header for what happens when commons is unresolvable.
|
|
//
|
|
// Policy-defined patterns are appended after, unchanged: they are the scanned
|
|
// project's own policy, not commons data, and must never displace the fixed
|
|
// table's labels.
|
|
const SECRET_PATTERNS = [
|
|
...COMMONS_SECRET_PATTERNS,
|
|
...getPolicyValue('secrets', 'additional_patterns', []).map((p, i) => ({
|
|
name: `Custom pattern ${i + 1}`,
|
|
pattern: new RegExp(p),
|
|
})),
|
|
];
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Exclusions: files that may contain example patterns for documentation
|
|
// ---------------------------------------------------------------------------
|
|
function isExcluded(filePath) {
|
|
if (!filePath) return false;
|
|
const n = normalize(filePath);
|
|
if (/[\\/]knowledge[\\/].+\.md$/i.test(n)) return true;
|
|
if (/[\\/]references[\\/].+\.md$/i.test(n)) return true;
|
|
if (/\.(test|spec|mock)\.[jt]sx?$/.test(n)) return true;
|
|
if (/\.(example|template|sample)(\.|$)/.test(n)) return true;
|
|
return false;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Main
|
|
// ---------------------------------------------------------------------------
|
|
let input;
|
|
try {
|
|
const raw = readFileSync(0, 'utf-8');
|
|
input = JSON.parse(raw);
|
|
} catch { process.exit(0); }
|
|
|
|
const toolInput = input?.tool_input ?? {};
|
|
const filePath = toolInput.file_path ?? '';
|
|
|
|
if (isExcluded(filePath)) process.exit(0);
|
|
|
|
const contentToCheck = [toolInput.content ?? '', toolInput.new_string ?? ''].join('\n');
|
|
if (!contentToCheck.trim()) process.exit(0);
|
|
|
|
for (const { name, pattern } of SECRET_PATTERNS) {
|
|
if (pattern.test(contentToCheck)) {
|
|
process.stderr.write(
|
|
`BLOCKED: Potential secret detected — ${name}\n` +
|
|
` File: ${filePath || '(unknown)'}\n` +
|
|
` Remove the credential before writing. Use <YOUR_KEY_HERE> or .env.\n`
|
|
);
|
|
process.exit(2);
|
|
}
|
|
}
|
|
|
|
process.exit(0);
|