Files
Claude-BugHunter/engine/test_scope_regex_cache.py
5843d2765d feat(cbh): cbh scope import fills scope.md from a program's published scope (#208)
Closes the README roadmap item "auto-generate structured scope.md from
program text". scope.md is the gate engine/scope.py enforces, and today it
is filled by hand from the program page.

`cbh scope import <ref> [-o scope.md]` reads:
- public HackerOne / Bugcrowd / Intigriti / YesWeHack scopes from
  bounty-targets-data (hourly MIT dump, cached 6h, no API key);
- `--json`: the HackerOne MCP get_my_program / get_program_scope output,
  for private programs;
- `--text`: pasted program-page lists, plus researcher-conduct rules.

It fails closed. Only unambiguous hosts, wildcards, IPs and CIDRs become
rules. Exact hosts are written as anchored `re:^host$` rules, because a
bare host rule also admits every subdomain. Path-limited assets,
placeholders, free text, public suffixes and mid-label wildcards go to
`## Needs manual review` and are not rules. Non-host assets go to
`## Other assets`.

Imported bullets sit between cbh-import markers, so re-running refreshes
them and keeps hand-written rules, Focus areas and recon-seeded hosts.

Checked against all 939 public programs in the dump (52,947 assets), with
both main's and #205's scope.py:
- every rule round-trips through _scope_from_md;
- every declared asset is admitted;
- no subdomain of an exact host is admitted;
- every deny rule rejects its host;
- re-importing is a no-op.

Rule conversion of host assets: HackerOne 97.5%, Intigriti 91%,
YesWeHack 90%, Bugcrowd 62% (68% excluding entries redacted in the dump).

Also:
- engine/scope.py compiles each `re:` rule once. `re` caches only about
  512 patterns, so a large imported program took ~150 ms per check at
  5,000 rules; it now takes ~3 ms.
- The scope.py and /scope docs said `api.example.com` matches "that exact
  host". It also matches the host's subdomains; the docs now say so.
- hunt.sh and hunt.ps1 point at the importer (hunt.ps1 stays ASCII).

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 23:19:35 +05:30

30 lines
1.2 KiB
Python

import unittest
import scope
from scope import Scope
class RegexRuleCacheTests(unittest.TestCase):
"""`re:` rules are compiled once, not on every check. re's own cache holds ~512
patterns, so an imported program with thousands of exact-host rules recompiled
every rule on every check."""
def test_each_rule_compiles_once_across_many_checks(self):
rules = [rf"re:^cache-h{i}\.example\.com$" for i in range(600)]
s = Scope(rules)
before = scope._regex.cache_info().misses
for i in range(3):
self.assertFalse(s.in_scope_host(f"https://other{i}.example.org/"))
self.assertTrue(s.in_scope_host("https://cache-h599.example.com/"))
self.assertEqual(scope._regex.cache_info().misses - before, len(rules))
def test_semantics_are_unchanged(self):
s = Scope([r"re:^App\.Example\.com$", "re:([bad"])
self.assertTrue(s.in_scope_host("https://app.example.COM/")) # still case-insensitive
self.assertFalse(s.in_scope_host("https://x.app.example.com/"))
self.assertFalse(s.in_scope_host("https://([bad/")) # invalid regex matches nothing
if __name__ == "__main__":
unittest.main()