Files
Claude-BugHunter/scripts/test_scan_identifiers.py
d37233e0c3 security: repo-wide client-identifier leak guard (fail-closed) + pre-commit hook (#65)
The existing hashed-denylist scan lives in lint_skills.py and only covers
skills/**. The identifier leak that motivated this guard landed in README.md
and tests/ — files that linter never sees, and skill-lint.yml only triggers on
skills/** paths anyway. It also fails OPEN: a missing denylist prints a NOTE
and exits 0.

This closes both holes:

- scripts/scan_identifiers.py — standalone scanner reusing the same denylist
  file and 1-/2-word shingle-hash logic, but over EVERY tracked text file
  (git ls-files, minus binaries). Fails CLOSED: missing/empty denylist = exit 2.
- .github/workflows/leak-guard.yml — runs it (and its self-test) on every push
  and PR with NO path filter. contents: read.
- .githooks/pre-commit — same scan locally before push (opt-in via
  `git config core.hooksPath .githooks`).
- scripts/test_scan_identifiers.py — stdlib self-check: catch, clean,
  case-insensitive, and fail-closed behaviors.
- scripts/.identifier-denylist.sha256 — added the single-word hash to
  complement the existing two-word entry (both now covered).
- CONTRIBUTING.md — documents the guard, the hook, and how to ban a new
  identifier without committing the plaintext.

Verified: whole-repo scan 0 matches / exit 0; self-test passes; missing denylist
exits 2; a planted identifier is blocked by both the scanner and the hook;
lint_skills.py still 0 errors. (The guard even caught a client name I'd left in
this scanner's own docstring — removed.)

Scope note: identifier-only by design. Repo-wide secret scanning is deferred —
it would false-positive on the synthetic AKIA test keys in docs/verification/,
which need allowlist work first. Secrets stay covered by lint_skills.py (skills)
and secret_scan.py.

Co-authored-by: Sachin Sharma <elementalsoul@Sachins-MacBook-Pro.local>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-24 20:27:39 +05:30

74 lines
2.6 KiB
Python
Executable File

#!/usr/bin/env python3
"""Self-check for scan_identifiers.py. Stdlib only, no network, no git.
Run: python3 scripts/test_scan_identifiers.py (exit 0 = pass)
Loads the scanner as a module, points its denylist path at a temp dir, and
asserts the three behaviors that matter:
1. a planted identifier is caught -> main() returns 1
2. a clean file is not caught -> main() returns 0
3. an empty/missing denylist fails CLOSED -> main() returns 2
"""
import hashlib
import importlib.util
import os
import sys
import tempfile
HERE = os.path.dirname(os.path.abspath(__file__))
def load_module():
spec = importlib.util.spec_from_file_location(
"scan_identifiers", os.path.join(HERE, "scan_identifiers.py"))
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
return mod
def main():
mod = load_module()
with tempfile.TemporaryDirectory() as tmp:
scripts = os.path.join(tmp, "scripts")
os.makedirs(scripts)
mod.SCRIPTS_DIR = scripts # redirect denylist lookup
mod.REPO = tmp
deny = os.path.join(scripts, ".identifier-denylist.sha256")
# Denylist with the hash of a fake identifier "acmecorp".
banned = hashlib.sha256("acmecorp".encode()).hexdigest()
with open(deny, "w") as fh:
fh.write("# test denylist\n" + banned + "\n")
leak = os.path.join(tmp, "leak.md")
with open(leak, "w") as fh:
fh.write("The target was AcmeCorp Ltd, an internal engagement.\n")
clean = os.path.join(tmp, "clean.md")
with open(clean, "w") as fh:
fh.write("Generic methodology, no client names here.\n")
# 1. planted identifier caught
assert mod.main([leak]) == 1, "expected hit (exit 1) on planted identifier"
# 2. clean file passes
assert mod.main([clean]) == 0, "expected clean (exit 0) on benign file"
# 3. case-insensitive: "ACMECORP" also caught
loud = os.path.join(tmp, "loud.md")
with open(loud, "w") as fh:
fh.write("ACMECORP everywhere\n")
assert mod.main([loud]) == 1, "expected case-insensitive hit"
# 4. fail-closed on empty denylist
with open(deny, "w") as fh:
fh.write("# comments only, no hashes\n")
assert mod.main([clean]) == 2, "expected FAIL-CLOSED (exit 2) on empty denylist"
# 5. fail-closed on missing denylist
os.remove(deny)
assert mod.main([clean]) == 2, "expected FAIL-CLOSED (exit 2) on missing denylist"
print("PASS: scan_identifiers self-check (catch, clean, case-insensitive, fail-closed)")
return 0
if __name__ == "__main__":
sys.exit(main())