Files
Claude-BugHunter/engine/test_scope_rule_normalization.py
a7e73914e6 fix(scope): harden the scope gate against three fail-open paths (#182, #198, #181) (#205)
* fix(scope): normalize every rule source to host; add Scope.warnings

Out-of-scope rules written with a scheme/port/path/trailing punctuation or a
wildcard matched nothing and silently failed open. One normalization point in
Scope.__init__ now reduces every rule source (scope.md, scope.json, CLI flags,
osint/recon helpers) to its host. Port/path widening is recorded in a new
Scope.warnings channel. Reported by @fairoz-539 (#182).

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

* fix(scope): recognize IPv6 CIDR rules via ipaddress

The CIDR detector used p.replace('.','').replace('/','').isdigit(), which colons
make non-digit, so an IPv6 CIDR deny/allow rule (2001:db8::/32) never matched.
Detect any CIDR via ipaddress.ip_network; a family mismatch returns False, never
raises. Reported by @fairoz-539 (#182).

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

* fix(scope): soft-fail an invalid out-of-scope regex (warn, treat as host)

A bad re: deny regex was caught and silently matched nothing — a fail-open on the
one gate that must fail closed. Now it warns loudly and re-admits the remainder as
a literal host deny rule, so the run continues with scope still enforcing. Valid
regexes are unaffected. Reported by @fairoz-539 (#182).

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

* fix(scope): open a section only on a start/end scope heading; hint recon hosts

A recon-seeded note heading ('... VERIFY in-scope before testing') matched the
substring 'in-scope' and opened the in-scope section, silently promoting passive
crt.sh/subfinder hosts to scope rules (#198). _section_of now opens a section only
when the heading's label starts or ends with a scope phrase (trailing parenthetical
and colon stripped first). Hosts found under a scope-mentioning note heading are
surfaced as a Scope.warnings hint instead. Scaffolder heading reworded (no inner
quotes; hunt.ps1 stays ASCII). Reported by @udarshmarthala (#198).

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

* fix(scope): surface scope warnings on stderr and in the engine log

Scope.warnings (bad deny regex, widened rule, recon-seeded hosts) now print to
stderr from scope.py and go to the engine log at start — nothing is silent, and
stdout stays clean for --json-style consumers.

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

* fix(engine): follow recon redirects one hop at a time, inside scope

Recon fetches followed redirects to any host: spa_recon adopted the landed
(possibly off-scope) origin and crawled its robots/sitemap; _probe ran curl -L, so
platform/openapi recon chased Location anywhere. A new _walk() follows at most
MAX_REDIRECTS hops and only while the scope predicate accepts the next URL; all
backends (requests, a no-redirect stdlib _OPENER, curl without -L) route through it.
spa_recon re-checks the landed host, and platform/openapi recon skip out-of-scope
seeds. In-scope redirects (http->https, / -> /home) are unaffected. Reported by
@fairoz-539 (#181).

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

* fix(engine): auth sweep grades a 3xx as enforcement, never chases Location

A declared-secured op answering 302 -> /login was graded by the followed login
page's 200 as 'declared-secured, NOT enforced' — a false positive. The sweep now
uses the no-redirect opener, so a 3xx is graded as itself (enforcement) and no
request chases Location. Repoints test_openapi_readonly to the module opener.
Adds the scope-hardening CHANGELOG entry. Reported by @fairoz-539 (#181).

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

* fix(scope): review follow-ups — bare IPv6 deny, prune redirect, test sockets

Fresh-review fixes:
- CRITICAL: a bare/zoned unbracketed IPv6 single-host rule normalized to its first
  hextet (urlparse split on ':'), so an IPv6 out-of-scope rule failed open. Detect a
  literal IP in _normalize_rule and return its canonical form.
- IMPORTANT: prune_inert_params followed no redirects (allow=None), so a live
  parameterized endpoint behind an in-scope redirect (http->https, canonical slash)
  was mis-judged inert and dropped. Thread scope through and follow in-scope hops.
- MINOR: loopback test servers now server_close() in tearDown (no leaked sockets
  under -W error).

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

* docs(superpowers): scope-hardening design + implementation plan

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

* test(osint): enumeration_roots covers host after scope normalizes port/path

Rebase interaction with #221: the scope gate (#205) now widens
example.com/path and example.com:8080 to the host example.com, so
enumeration_roots correctly yields ['example.com'] instead of dropping
them. Update the #221 regression case to the combined correct behavior.

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

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-23 20:52:33 +05:30

107 lines
4.9 KiB
Python

#!/usr/bin/env python3
"""Tests for scope rule normalization, IPv6 CIDR, soft-fail regex, warning surfacing.
Covers issues #182 (out-of-scope rules fail open) and the softer-defaults design.
"""
import os
import subprocess
import sys
import unittest
sys.path.insert(0, os.path.dirname(__file__))
from scope import Scope # noqa: E402
class RuleNormalizationTests(unittest.TestCase):
# ---- #182: deny rules in normal program-page syntax must actually deny ----
def test_port_rule_denies(self):
s = Scope(in_scope=["example.com"], out_of_scope=["admin.example.com:8443"])
self.assertFalse(s.in_scope_host("https://admin.example.com/"))
def test_scheme_rule_denies(self):
s = Scope(in_scope=["example.com"], out_of_scope=["https://admin.example.com"])
self.assertFalse(s.in_scope_host("https://admin.example.com/"))
def test_path_rule_denies_whole_host(self):
s = Scope(in_scope=["example.com"], out_of_scope=["admin.example.com/panel"])
self.assertFalse(s.in_scope_host("https://admin.example.com/"))
def test_trailing_dot_rule_denies(self):
s = Scope(in_scope=["example.com"], out_of_scope=["admin.example.com."])
self.assertFalse(s.in_scope_host("https://admin.example.com/"))
def test_trailing_comma_rule_denies(self):
s = Scope(in_scope=["example.com"], out_of_scope=["admin.example.com,"])
self.assertFalse(s.in_scope_host("https://admin.example.com/"))
def test_wildcard_scheme_rule_denies(self):
s = Scope(in_scope=["example.com"], out_of_scope=["https://*.corp.example.com"])
self.assertFalse(s.in_scope_host("https://x.corp.example.com/"))
# ---- in-scope normalization (Review Focus: applies to in-scope too) ----
def test_in_scope_url_rule_still_matches_host(self):
s = Scope(in_scope=["https://app.example.com/login"])
self.assertTrue(s.in_scope_host("http://app.example.com/x"))
def test_port_or_path_rule_records_widen_warning(self):
s = Scope(in_scope=["example.com"], out_of_scope=["admin.example.com/panel"])
self.assertTrue(any("widened to host" in w for w in s.warnings))
# ---- Task 2: IPv6-aware CIDR ----
def test_ipv6_cidr_matches_ipv6_host(self):
s = Scope(in_scope=["2001:db8::/32"])
self.assertTrue(s.in_scope_host("https://[2001:db8::1]/"))
def test_ipv4_cidr_rule_vs_ipv6_host_no_match_no_raise(self):
s = Scope(in_scope=["10.0.0.0/8"])
self.assertFalse(s.in_scope_host("https://[2001:db8::1]/"))
def test_ipv6_cidr_rule_vs_ipv4_host_no_match(self):
s = Scope(in_scope=["2001:db8::/32"])
self.assertFalse(s.in_scope_host("https://10.1.2.3/"))
def test_bare_ipv6_host_rule_denies(self):
# #1 (review): a bare, unbracketed IPv6 single-host deny rule must not fail open.
s = Scope(in_scope=["2001:db8::/32"], out_of_scope=["2001:db8::1"])
self.assertFalse(s.in_scope_host("https://[2001:db8::1]/"))
def test_zoned_ipv6_host_rule_denies(self):
s = Scope(in_scope=["2001:db8::/32"], out_of_scope=["2001:db8::1%eth0"])
self.assertFalse(s.in_scope_host("https://[2001:db8::1]/"))
def test_bracketed_ipv6_host_rule_denies(self):
s = Scope(in_scope=["2001:db8::/32"], out_of_scope=["[2001:db8::1]"])
self.assertFalse(s.in_scope_host("https://[2001:db8::1]/"))
# ---- Task 3: soft-fail an invalid out-of-scope regex ----
def test_bad_out_of_scope_regex_warns_and_continues(self):
s = Scope(in_scope=["example.com"], out_of_scope=["re:(unclosed"])
self.assertTrue(any("invalid" in w and "re:(unclosed" in w for w in s.warnings))
self.assertTrue(s.in_scope_host("https://example.com/")) # run continues
def test_valid_out_of_scope_regex_still_denies(self):
s = Scope(in_scope=["example.com"], out_of_scope=[r"re:^admin\.example\.com$"])
self.assertFalse(s.in_scope_host("https://admin.example.com/"))
def test_bad_regex_rule_is_replaced_not_kept_verbatim(self):
s = Scope(in_scope=["example.com"], out_of_scope=["re:(unclosed"])
self.assertFalse(any(r.startswith("re:") for r in s.out_of_scope))
class WarningSurfacingTests(unittest.TestCase):
# ---- Task 5: warnings go to stderr, stdout stays clean, exit unchanged ----
def test_cli_prints_warning_to_stderr(self):
scope_py = os.path.join(os.path.dirname(__file__), "scope.py")
r = subprocess.run(
[sys.executable, scope_py, "https://example.com/",
"--in-scope", "example.com", "--out-of-scope", "re:(bad"],
capture_output=True, text=True)
self.assertEqual(r.returncode, 0) # host in scope, exit 0
self.assertIn("invalid", r.stderr) # warning on stderr
self.assertNotIn("invalid", r.stdout) # stdout stays clean
self.assertIn("IN-SCOPE", r.stdout)
if __name__ == "__main__":
unittest.main()