Files
Claude-BugHunter/tests/test_secret_scan_skip_dirs.py
6a530fb314 fix: five tooling/engine correctness bugs — classify, extract_json, secret_scan, osint roots, leak-guard (#221)
* fix(cbh): classify_url matches path/query, not hostname (#219)

Pattern triggers ran against the full URL, so a keyword in the hostname
(verify.example.com, cart.shop.example) matched a leading-slash path
pattern via the //. Match against parsed.path+query instead; the
previously-unused parsed is now the source of truth.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* fix(engine): extract_json keeps the last top-level JSON value (#217)

The unfenced fallback used one greedy {.*} DOTALL span (first brace to
last), so any earlier brace in the reply — a curl payload in the prose —
made the whole span unparseable and the verdict was lost, re-running the
item as no-verdict. Scan top-level values with raw_decode and keep the
last. Fenced replies are unaffected.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* fix(osint): secret_scan prunes skip dirs by name, always scans the root (#216)

The skip check tested each noisy-dir word as a substring of the whole
walked path, root included. So scanning ./dist, .github/ (contains .git),
or nightly-builds/ matched and skipped everything, printing nothing and
exiting 0 — a false clean in a leak guard. Prune os.walk's dirs by exact
name below the root instead; the requested root is always scanned.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* fix(osint): derive enumeration roots from authorized scope (#213)

osint() and _expand_scope() took the enumeration apex by keeping a seed
host's last two labels. Under a multi-label public suffix that apex is
the suffix itself (example.co.uk -> co.uk), so subfinder/assetfinder/crt.sh
queried the whole suffix and the in-scope filter discarded the results;
--expand never enumerated the target at all. IP seeds became '3.4'. New
shared osint.enumeration_roots() takes roots from in-scope domain rules and
maps each seed to its shortest covering root (full hostname if uncovered),
with no public-suffix list.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* fix(leak-guard): reject malformed denylist hashes (#212)

A committed .sha256 line that is not a 64-hex digest was loaded verbatim
as a 'hash' that no real sha256 can match — a silent false-negative in
the leak guard. load_denylist now validates each entry and fails closed
(exit 2) on a malformed one, naming the line to fix without printing its
content.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* docs(changelog): credit five tooling/engine bugfix reporters (#219,#217,#216,#213,#212)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* fix(review): harden extract_json, classify_url, and the skill-linter denylist

Adversarial review of the five bugfixes surfaced four to fold:
- extract_json: a truncated top-level array let an inner element be
  returned as the verdict; stop at the first unparseable bracket instead
  of descending. Also widen the except to (ValueError, RecursionError) so
  deep nesting in the C scanner can't escape into the verdict path.
- classify_url: schemeless input (api.php.net/x) put the host in
  parsed.path, so .php still fired. Prefix // when there's no scheme.
- lint_skills.load_denylist: same malformed-hash fail-open as #212 in the
  skill linter — validate and fail closed there too.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-23 15:46:42 +05:30

61 lines
2.4 KiB
Python

"""secret_scan must prune noisy directories by exact name BELOW the root, and
always scan the root it was handed. The old substring check (`part in root`)
matched the whole walked path including the root, so `secret_scan ./dist`,
`.github/` (contains `.git`), or `nightly-builds/` printed nothing and exited 0 —
a false "clean" in a leak guard. Reported in #216."""
import importlib.util
import os
import unittest
from pathlib import Path
_REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
_SS = os.path.join(_REPO, "skills", "offensive-osint", "scripts", "secret_scan.py")
_spec = importlib.util.spec_from_file_location("secret_scan", _SS)
secret_scan = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(secret_scan)
_SECRET = "AKIAIOSFODNN7EXAMPLE" # AWS access-key-id shape, triggers a high-sev pattern
def _hits(path):
return [f for f in secret_scan.scan_path(str(path))]
class SecretScanSkipDirTests(unittest.TestCase):
def setUp(self):
import tempfile
self._tmp = tempfile.TemporaryDirectory()
self.root = Path(self._tmp.name)
self.addCleanup(self._tmp.cleanup)
def _seed(self, rel):
p = self.root / rel
p.parent.mkdir(parents=True, exist_ok=True)
p.write_text(f"key = {_SECRET}\n", encoding="utf-8")
def test_root_named_like_a_skip_dir_is_still_scanned(self):
# user explicitly points the scanner at ./dist — it must scan it
self._seed("dist/app/config.env")
self.assertTrue(_hits(self.root / "dist"), "root 'dist' was silently skipped")
def test_dotgithub_root_is_scanned(self):
# '.github' contains the substring '.git' but is not a skip dir
self._seed(".github/workflows/leak.yml")
self.assertTrue(_hits(self.root / ".github"), "'.github' silently skipped via '.git' substring")
def test_skip_dirs_below_root_still_pruned(self):
self._seed("node_modules/pkg/x.js")
self._seed(".git/config")
self._seed("venv/lib/y.py")
self._seed("src/real.py") # the only one that should be reported
hits = _hits(self.root)
sources = " ".join(h["source"] for h in hits)
self.assertIn("real.py", sources)
self.assertNotIn("node_modules", sources)
self.assertNotIn(os.sep + ".git" + os.sep, sources)
self.assertNotIn("venv", sources)
if __name__ == "__main__":
unittest.main()