Skip to content

alexgreensh/repo-forensics / skills/repo-forensics

repo-forensics

Flagged

Security forensics for git repos, AI skills, and MCP servers. Audits dependencies, detects prompt injection, credential theft, runtime dynamism, manifest drift, known CVEs, CISA KEV (actively exploited) vulns, and 2026 attack patterns. Not for fixing vulnerabilities or pentesting.

FileKindSize
.gitignoreother193 B
SKILL.mdskill md43.4 KB
checksums.jsonconfig10.8 KB
checksums.json.sigother64 B
data/README.mdmarkdown1.3 KB
data/compromised_versions.jsonconfig188.3 KB
data/rule_ids.csvother35.3 KB
data/rulepacks/.gitkeepother221 B
data/rulepacks/dead_anchors.jsonconfig9.0 KB
data/rulepacks/mcp_security.jsonconfig31.2 KB
data/rulepacks/runtime_dynamism.jsonconfig46.7 KB
data/rulepacks/sast.jsonconfig107.6 KB
data/rulepacks/secrets.jsonconfig30.4 KB
data/rulepacks/shared.jsonconfig4.4 KB
data/rulepacks/skill_threats.jsonconfig95.4 KB
data/runtime_floors.jsonconfig2.3 KB
data/yara/cryptominers.yarother1.8 KB
data/yara/hacktools.yarother2.2 KB
data/yara/malware.yarother2.2 KB
data/yara/manifest.jsonconfig8.0 KB
data/yara/webshells.yarother2.5 KB
references/cursor-hook-contract.mdmarkdown5.6 KB
references/kernel_privesc_sandbox_pack.mdmarkdown5.0 KB
references/mcp-attack-patterns.mdmarkdown8.7 KB
references/research_sources.mdmarkdown8.0 KB
references/threat_patterns.mdmarkdown20.3 KB
scripts/_context_gate.pyscript21.7 KB
scripts/_ed25519.pyscript5.1 KB
scripts/_pyc_unmarshal.pyscript17.1 KB
scripts/_shared_patterns.pyscript4.8 KB
scripts/adjudication.pyscript16.4 KB
scripts/adjudication_bridge.pyscript1.0 KB
scripts/aggregate_json.pyscript69.2 KB
scripts/auto_scan.pyexecutablescript152.1 KB
scripts/corpus_sync.pyscript10.0 KB
scripts/cursor_pre_scan.pyscript22.5 KB
scripts/dast_sandbox.sbother1.6 KB
scripts/dead_anchors_extract.pyscript14.8 KB
scripts/dead_anchors_probe.pyscript27.1 KB
scripts/forensics_core.pyscript176.5 KB
scripts/gen_rule_ids.pyscript8.6 KB
scripts/hook_adapter.pyscript19.4 KB
scripts/ioc_manager.pyscript49.6 KB
scripts/parse_pnpm_lock.pyscript7.9 KB
scripts/pre_scan.pyscript143.5 KB
scripts/pypi_pins.pyscript8.1 KB
scripts/refresh_controller.pyexecutablescript48.4 KB
scripts/refresh_threat_dbs.pyexecutablescript19.4 KB
scripts/rule_loader.pyscript44.8 KB
scripts/rulepack_feed.pyscript26.4 KB
scripts/run_forensics.shexecutablescript20.3 KB
scripts/sarif_export.pyscript18.6 KB
scripts/scan_agent_skills.pyscript33.2 KB
scripts/scan_archive.pyscript29.1 KB
scripts/scan_ast.pyscript44.9 KB
scripts/scan_binary.pyscript10.9 KB
scripts/scan_bytecode.pyscript28.2 KB
scripts/scan_dast.pyscript28.8 KB
scripts/scan_dataflow.pyscript15.4 KB
scripts/scan_dead_anchors.pyscript15.9 KB
scripts/scan_decode.pyscript37.5 KB
scripts/scan_dependencies.pyscript124.5 KB
scripts/scan_devcontainer.pyscript8.5 KB
scripts/scan_entropy.pyscript15.6 KB
scripts/scan_entrypoint.pyscript34.3 KB
scripts/scan_git_config.pyscript96.3 KB
scripts/scan_git_forensics.pyscript24.1 KB
scripts/scan_history.pyscript14.5 KB
scripts/scan_infra.pyscript54.3 KB
scripts/scan_integrity.pyscript41.0 KB
scripts/scan_lifecycle.pyscript65.1 KB
scripts/scan_manifest_drift.pyscript47.0 KB
scripts/scan_mcp_security.pyscript42.3 KB
scripts/scan_oversize.pyscript11.7 KB
scripts/scan_post_incident.pyscript36.9 KB
scripts/scan_provenance.pyscript19.4 KB
scripts/scan_runtime_dynamism.pyscript23.5 KB
scripts/scan_runtime_floors.pyscript33.5 KB
scripts/scan_sast.pyscript59.1 KB
scripts/scan_secrets.pyscript11.9 KB
scripts/scan_skill_threats.pyscript102.4 KB
scripts/scan_splitstream.pyscript29.5 KB
scripts/scan_yara.pyscript19.7 KB
scripts/session_scan.pyscript54.1 KB
scripts/suppressions.pyscript15.4 KB
scripts/validate_manifests.pyscript13.7 KB
scripts/verify_install.pyscript26.2 KB
scripts/vuln_feed.pyscript31.1 KB
tests/conftest.pyscript13.4 KB
tests/corpus/DEAD_ANCHORS_CORPUS.mdmarkdown2.3 KB
tests/corpus/budgets.jsonconfig5.6 KB
tests/cursor_helpers.pyscript12.6 KB
tests/fixtures/legacy_main_blocks.jsonconfig10.3 KB
tests/fixtures/powershell_verdict_corpus.jsonconfig114.9 KB
tests/golden/parity_mcp_security.jsonconfig17.5 KB
tests/golden/parity_runtime_dynamism.jsonconfig31.7 KB
tests/golden/parity_sast.jsonconfig59.8 KB
tests/golden/parity_secrets.jsonconfig18.8 KB
tests/golden/parity_skill_threats.jsonconfig48.8 KB
tests/parity_corpus.pyscript6.3 KB
tests/powershell_hook.pyscript8.2 KB
tests/report_builders.pyscript5.0 KB
tests/schemas/sarif-schema-2.1.0.jsonconfig109.1 KB
tests/shell_compat.pyscript2.2 KB
tests/test_adjudication_bridge.pyscript832 B
tests/test_adjudication_containment.pyscript1.4 KB
tests/test_adjudication_output.pyscript20.0 KB
tests/test_adjudication_suppressions.pyscript18.7 KB
tests/test_advisory_adjudication.pyscript1.3 KB
tests/test_agent_relay_rules.pyscript7.8 KB
tests/test_aggregate_json.pyscript10.0 KB
tests/test_auto_scan_patterns.pyscript10.5 KB
tests/test_benign_corpus.pyscript16.4 KB
tests/test_benign_new_scanners.pyscript6.1 KB
tests/test_build_output_skip.pyscript10.6 KB
tests/test_capability_ledger.pyscript18.8 KB
tests/test_codex_install.pyscript10.0 KB
tests/test_codex_marketplace.pyscript3.3 KB
tests/test_comment_false_positives.pyscript2.2 KB
tests/test_composer_dependencies.pyscript4.0 KB
tests/test_compromised_versions.pyscript51.2 KB
tests/test_context_gate.pyscript32.4 KB
tests/test_coverage_status.pyscript7.6 KB
tests/test_css_compounding_shrink.pyscript20.8 KB
tests/test_cursor_adapter.pyscript7.8 KB
tests/test_cursor_benign.pyscript3.4 KB
tests/test_cursor_degrade.pyscript12.7 KB
tests/test_cursor_install.pyscript17.1 KB
tests/test_cursor_latency.pyscript7.2 KB
tests/test_cursor_parity.pyscript5.8 KB
tests/test_cursor_redteam_fn.pyscript4.9 KB
tests/test_cursor_tamper_modify.pyscript14.2 KB
tests/test_cursor_wiring.pyscript32.3 KB
tests/test_d1_yara_correlation.pyscript9.2 KB
tests/test_dead_anchors_extract.pyscript7.6 KB
tests/test_dead_anchors_probe.pyscript19.7 KB
tests/test_evidence_engine_parity.pyscript8.1 KB
tests/test_evidence_model.pyscript11.4 KB
tests/test_forensics_core.pyscript79.2 KB
tests/test_fp_hardening_r3.pyscript9.7 KB
tests/test_fp_hardening_r4.pyscript18.8 KB
tests/test_fp_regression_pairs.pyscript62.5 KB
tests/test_fp_structural.pyscript21.2 KB
tests/test_freshness.pyscript6.2 KB
tests/test_frontmatter_validity.pyscript7.0 KB
tests/test_gpg_rce_hotfix.pyscript4.8 KB
tests/test_hook_inert_data.pyscript6.1 KB
tests/test_hook_scripts.pyscript14.0 KB
tests/test_inspection_cpu_budget.pyscript11.6 KB
tests/test_ioc_manager.pyscript55.4 KB
tests/test_kernel_privesc_rules.pyscript27.8 KB
tests/test_legit_precision_regressions.pyscript6.8 KB
tests/test_lethal_trifecta.pyscript20.7 KB
tests/test_manifest_signature.pyscript1.5 KB
tests/test_manifest_validity.pyscript2.9 KB
tests/test_monotonic_trust_e2e.pyscript4.4 KB
tests/test_non_breaking_contract.pyscript22.7 KB
tests/test_openclaw_install.pyscript6.6 KB
tests/test_pack_parity.pyscript24.8 KB
tests/test_parse_pnpm_lock.pyscript10.7 KB
tests/test_plugin_checkout_provenance.pyscript11.1 KB
tests/test_powershell_hook_wire.pyscript10.6 KB
tests/test_powershell_verdict_corpus.pyscript6.2 KB
tests/test_pr44_hardening.pyscript6.5 KB
tests/test_pre_scan.pyscript19.6 KB
tests/test_pre_scan_composer.pyscript7.4 KB
tests/test_precondition_fields.pyscript4.6 KB
tests/test_provenance_offline.pyscript2.5 KB
tests/test_published_rulepack_feed_current.pyscript1.4 KB
tests/test_py38_compat.pyscript3.3 KB
tests/test_pypi_pinned_versions.pyscript19.3 KB
tests/test_pyproject_string_forms.pyscript2.2 KB
tests/test_redteam_fn_regression.pyscript32.5 KB
tests/test_reflective_fetch_execute.pyscript23.5 KB
tests/test_reflective_review_fixes.pyscript17.8 KB
tests/test_refresh_controller.pyscript19.9 KB
tests/test_refresh_rulepacks.pyscript12.4 KB
tests/test_review_gate_regressions.pyscript9.3 KB
tests/test_rule_loader.pyscript23.2 KB
tests/test_rulepack_feed.pyscript38.1 KB
tests/test_run_forensics_json.pyscript1012 B
tests/test_run_scanner_fail_loud.pyscript6.5 KB
tests/test_runtime_floors.pyscript19.8 KB
tests/test_sandbox_probing_rules.pyscript9.6 KB
tests/test_sarif_export.pyscript19.9 KB
tests/test_scan_agent_skills.pyscript31.3 KB
tests/test_scan_archive.pyscript11.6 KB
tests/test_scan_ast.pyscript12.8 KB
tests/test_scan_binary.pyscript15.3 KB
tests/test_scan_bytecode.pyscript21.5 KB
tests/test_scan_dast.pyscript32.1 KB
tests/test_scan_dataflow.pyscript7.1 KB
tests/test_scan_dead_anchors.pyscript11.5 KB
tests/test_scan_decode.pyscript28.0 KB
tests/test_scan_dependencies.pyscript37.8 KB
tests/test_scan_devcontainer.pyscript8.5 KB
tests/test_scan_entropy.pyscript11.2 KB
tests/test_scan_entrypoint.pyscript19.5 KB
tests/test_scan_git_config.pyscript58.5 KB
tests/test_scan_git_config_review_fixes.pyscript33.3 KB

tests/test_dead_anchors_probe.py

View on GitHub
"""Tests for dead_anchors_probe.py — U2 network claimability layer.

EVERY network call is monkeypatched: vuln_feed's urllib.request.urlopen (the one
real HTTPS chokepoint, reached through vuln_feed._https_fetch) and the probe
module's socket.gethostbyname. NO test ever performs a real HTTP request or DNS
lookup. A module-level call counter backs the "zero real network" assertions,
the budget/ceiling caps, the circuit breaker, and the no-retry guarantee.
"""

import io
import socket
import urllib.error

import pytest

import dead_anchors_probe as probe
import vuln_feed


# --- network doubles ---------------------------------------------------------

class _FakeResp:
    def __init__(self, body):
        self._body = body if isinstance(body, bytes) else body.encode("utf-8")

    def __enter__(self):
        return self

    def __exit__(self, *a):
        return False

    def read(self, n):
        return self._body[:n]


class _Net:
    """Routes a request URL to a 2-tuple (code, body), a 3-tuple
    (code, body, final_url) — final_url models the URL a redirect chain actually
    landed on, used to test rdap.org registry-vs-bootstrap 404 discrimination —
    or an Exception. Counts every urlopen invocation."""

    def __init__(self, router):
        self.router = router
        self.calls = []

    def urlopen(self, req, timeout=None):
        url = getattr(req, "full_url", req)
        self.calls.append(url)
        outcome = self.router(url)
        if isinstance(outcome, BaseException):
            raise outcome
        if len(outcome) == 3:
            code, body, final_url = outcome
        else:
            code, body = outcome
            final_url = url
        if code == 200:
            return _FakeResp(body)
        fp = io.BytesIO(body.encode("utf-8") if isinstance(body, str) else body)
        raise urllib.error.HTTPError(final_url, code, "err", {}, fp)


def _install(monkeypatch, router):
    net = _Net(router)
    monkeypatch.setattr(vuln_feed.urllib.request, "urlopen", net.urlopen)
    # Route the scanner's no-redirect / bounded-redirect openers back through the
    # single mocked chokepoint (see probe._DirectOpener). Production uses the
    # real openers; here every probe still hits the mock, never real network.
    monkeypatch.setattr(probe, "_no_redirect_opener", lambda: probe._DirectOpener())
    monkeypatch.setattr(probe, "_bounded_redirect_opener", lambda: probe._DirectOpener())
    # getaddrinfo defaults to "resolves to a public IP" unless a test overrides.
    monkeypatch.setattr(probe.socket, "getaddrinfo", _fake_getaddrinfo("1.2.3.4"))
    return net


def _fake_getaddrinfo(ip):
    fam = socket.AF_INET6 if ":" in ip else socket.AF_INET
    return lambda host, port, *a, **k: [
        (fam, socket.SOCK_STREAM, socket.IPPROTO_TCP, "", (ip, port))
    ]


def _nxdomain_getaddrinfo(host, port, *a, **k):
    raise socket.gaierror(socket.EAI_NONAME, "Name or service not known")


def _ctx(tmp_path, offline=False, github_token=None):
    return probe.ProbeContext(offline=offline, cache_dir=str(tmp_path),
                              github_token=github_token)


_FPS = {
    "vercel.app": [{"s": "DEPLOYMENT_NOT_FOUND", "confidence": 0.8, "strict": True}],
    "pages.dev": [{"s": "project not found", "confidence": 0.7}],  # non-strict
}


# --- positive CC verdicts ----------------------------------------------------

def test_github_user_404_is_cc(tmp_path, monkeypatch):
    _install(monkeypatch, lambda u: (404, ""))
    verdict, _ = probe.probe_github_user("ghostowner", _ctx(tmp_path))
    assert verdict == "CC"


def test_npm_and_pypi_404_are_cc(tmp_path, monkeypatch):
    _install(monkeypatch, lambda u: (404, ""))
    assert probe.probe_npm("phantom-pkg", _ctx(tmp_path))[0] == "CC"
    assert probe.probe_pypi("phantom-pkg", _ctx(tmp_path))[0] == "CC"


def test_rdap_404_is_cc(tmp_path, monkeypatch):
    # A registry RDAP server (reached after the rdap.org bootstrap redirect)
    # returning 404 means the domain genuinely does not exist => CC.
    _install(monkeypatch, lambda u: (
        404, "", "https://rdap.verisign.com/com/v1/domain/expired-example.com"))
    assert probe.probe_domain_rdap("expired-example.com", _ctx(tmp_path))[0] == "CC"


def test_rdap_bootstrap_gap_404_is_nc(tmp_path, monkeypatch):
    # A bare 404 straight from rdap.org (no redirect happened -> final host is
    # still rdap.org) means it could not bootstrap a server for this TLD; that is
    # ambiguous and must NOT fire a false CRITICAL on a possibly-live domain.
    _install(monkeypatch, lambda u: (404, ""))  # final_url defaults to rdap.org
    assert probe.probe_domain_rdap("some-cctld-domain.zz", _ctx(tmp_path))[0] == "NC"


def test_cloud_nxdomain_is_cc(tmp_path, monkeypatch):
    _install(monkeypatch, lambda u: (200, "ok"))
    monkeypatch.setattr(probe.socket, "getaddrinfo", _nxdomain_getaddrinfo)
    verdict, extra = probe.probe_cloud_subdomain("dead.vercel.app", "vercel.app",
                                                 _ctx(tmp_path), fingerprints=_FPS)
    assert verdict == "CC" and extra.get("reason") == "nxdomain"


def test_cloud_ipv6_only_is_not_nxdomain(tmp_path, monkeypatch):
    # AAAA-only host resolves fine via getaddrinfo -> must NOT be a false CC
    # (security-sentinel F3: gethostbyname is IPv4-only and would misread it).
    _install(monkeypatch, lambda u: (200, "<html>live app</html>"))
    monkeypatch.setattr(probe.socket, "getaddrinfo",
                        _fake_getaddrinfo("2606:4700::1111"))
    verdict, _ = probe.probe_cloud_subdomain("live.vercel.app", "vercel.app",
                                             _ctx(tmp_path), fingerprints=_FPS)
    assert verdict == "LO"


def test_cloud_internal_ip_not_fetched(tmp_path, monkeypatch):
    # A subdomain that resolves into a private/metadata range is never fetched
    # (structural SSRF backstop, security-sentinel F1/F4) -> NC, no HTTP call.
    net = _install(monkeypatch, lambda u: (200, "DEPLOYMENT_NOT_FOUND"))
    monkeypatch.setattr(probe.socket, "getaddrinfo",
                        _fake_getaddrinfo("169.254.169.254"))
    verdict, _ = probe.probe_cloud_subdomain("evil.vercel.app", "vercel.app",
                                             _ctx(tmp_path), fingerprints=_FPS)
    assert verdict == "NC"
    assert net.calls == []  # no HTTP fetch to the internal address


def test_cloud_fingerprint_vercel_is_cc(tmp_path, monkeypatch):
    _install(monkeypatch, lambda u: (200, "<h1>DEPLOYMENT_NOT_FOUND</h1>"))
    verdict, extra = probe.probe_cloud_subdomain("gone.vercel.app", "vercel.app",
                                                 _ctx(tmp_path), fingerprints=_FPS)
    assert verdict == "CC" and extra.get("reason") == "fingerprint"


def test_cloud_non_strict_fingerprint_is_lo(tmp_path, monkeypatch):
    # pages.dev 'project not found' is a NON-strict entry now (generic phrase
    # that collides with live apps) -> must never fire CC on a resolving host.
    _install(monkeypatch, lambda u: (200, "project not found"))
    verdict, _ = probe.probe_cloud_subdomain("x.pages.dev", "pages.dev",
                                             _ctx(tmp_path), fingerprints=_FPS)
    assert verdict == "LO"


def test_cloud_resolving_404_no_fingerprint_is_lo(tmp_path, monkeypatch):
    # Resolving host + bare 404 + no strict fingerprint -> LO (silent), NEVER a
    # false CRITICAL (error-soundness F2). netlify has no fingerprint at all.
    _install(monkeypatch, lambda u: (404, "Page not found"))
    verdict, _ = probe.probe_cloud_subdomain("app.netlify.app", "netlify.app",
                                             _ctx(tmp_path), fingerprints=_FPS)
    assert verdict == "LO"


def test_cloud_fingerprint_on_404_body_is_cc(tmp_path, monkeypatch):
    # A strict provider token present in a 404 error body still fires CC — the
    # body must be captured on the non-200 path (error-soundness F2 fix).
    _install(monkeypatch, lambda u: (404, "<h1>DEPLOYMENT_NOT_FOUND</h1>"))
    verdict, extra = probe.probe_cloud_subdomain("gone.vercel.app", "vercel.app",
                                                 _ctx(tmp_path), fingerprints=_FPS)
    assert verdict == "CC" and extra.get("reason") == "fingerprint"


def test_cloud_generic_netlify_is_lo(tmp_path, monkeypatch):
    # netlify.app is deliberately NOT in the fingerprint map -> generic 404 copy
    # is never trusted -> LO (spec §4 "do not guess").
    _install(monkeypatch, lambda u: (200, "Page not found - back to home"))
    verdict, _ = probe.probe_cloud_subdomain("app.netlify.app", "netlify.app",
                                             _ctx(tmp_path), fingerprints=_FPS)
    assert verdict == "LO"


# --- negative / LO verdicts --------------------------------------------------

def test_all_types_200_are_lo(tmp_path, monkeypatch):
    _install(monkeypatch, lambda u: (200, '{"login":"x","public_repos":50}'))
    assert probe.probe_github_user("torvalds", _ctx(tmp_path))[0] == "LO"
    assert probe.probe_npm("lodash", _ctx(tmp_path))[0] == "LO"
    assert probe.probe_pypi("requests", _ctx(tmp_path))[0] == "LO"
    assert probe.probe_domain_rdap("python.org", _ctx(tmp_path))[0] == "LO"


@pytest.mark.parametrize("code", [403, 429, 500, 502, 418])
def test_error_codes_are_nc_no_raise(tmp_path, monkeypatch, code):
    _install(monkeypatch, lambda u: (code, ""))
    # None of these may raise; all degrade to NC.
    assert probe.probe_github_user("someone", _ctx(tmp_path))[0] == "NC"
    assert probe.probe_npm("pkg", _ctx(tmp_path))[0] == "NC"


@pytest.mark.parametrize("exc", [
    urllib.error.URLError("boom"),
    socket.timeout("slow"),
    TimeoutError("slow"),
    OSError("io"),
])
def test_network_exceptions_are_nc_no_raise(tmp_path, monkeypatch, exc):
    _install(monkeypatch, lambda u: exc)
    assert probe.probe_github_user("someone", _ctx(tmp_path))[0] == "NC"
    assert probe.probe_domain_rdap("example.com", _ctx(tmp_path))[0] == "NC"


def test_rdap_past_expiration_sets_flag(tmp_path, monkeypatch):
    body = '{"events":[{"eventAction":"expiration","eventDate":"2000-01-01T00:00:00Z"}]}'
    _install(monkeypatch, lambda u: (200, body))
    verdict, extra = probe.probe_domain_rdap("lapsing.com", _ctx(tmp_path))
    assert verdict == "LO" and extra and extra.get("imminent_expiry") is True


# --- offline: cheapest path, zero sockets ------------------------------------

def test_offline_all_probes_nc_zero_network(tmp_path, monkeypatch):
    net = _install(monkeypatch, lambda u: (404, ""))

    def _boom(*a, **k):
        raise AssertionError("DNS must not be resolved offline")

    monkeypatch.setattr(probe.socket, "getaddrinfo", _boom)
    ctx = _ctx(tmp_path, offline=True)
    assert probe.probe_github_user("x", ctx)[0] == "NC"
    assert probe.probe_github_repo("x", "y", ctx)[0] == "NC"
    assert probe.probe_npm("x", ctx)[0] == "NC"
    assert probe.probe_pypi("x", ctx)[0] == "NC"
    assert probe.probe_domain_rdap("x.com", ctx)[0] == "NC"
    assert probe.probe_cloud_subdomain("a.vercel.app", "vercel.app", ctx,
                                       fingerprints=_FPS)[0] == "NC"
    assert net.calls == []  # zero urlopen calls


# --- budget / ceiling / deadline (loop-safety) -------------------------------

def test_github_budget_exhausted_no_urlopen(tmp_path, monkeypatch):
    net = _install(monkeypatch, lambda u: (404, ""))
    ctx = _ctx(tmp_path)
    ctx.budget = probe._ProbeBudget(0, 50)  # GH cap exhausted
    verdict, _ = probe.probe_github_user("x", ctx)
    assert verdict == "NC"
    assert net.calls == []  # never issued a request


def test_total_ceiling_caps_probes(tmp_path, monkeypatch):
    net = _install(monkeypatch, lambda u: (200, "{}"))
    ctx = _ctx(tmp_path)
    ctx.budget = probe._ProbeBudget(100, 50)  # generous GH, total ceiling 50
    attempted = 0
    for i in range(60):
        probe.probe_npm(f"pkg-{i}", ctx)
        attempted += 1
    assert len(net.calls) <= 50
    assert attempted == 60  # all called, but ceiling capped real requests


def test_global_deadline_stops_probes(tmp_path, monkeypatch):
    net = _install(monkeypatch, lambda u: (200, "{}"))
    monkeypatch.setattr(probe, "NETWORK_DEADLINE_SEC", 0.0)
    ctx = _ctx(tmp_path)  # deadline already expired at construction
    assert probe.probe_github_user("x", ctx)[0] == "NC"
    assert probe.probe_npm("y", ctx)[0] == "NC"
    assert net.calls == []


# --- circuit breaker + no-retry ----------------------------------------------

def test_circuit_breaker_trips_on_429(tmp_path, monkeypatch):
    net = _install(monkeypatch, lambda u: (429, ""))
    ctx = _ctx(tmp_path)
    assert probe.probe_github_user("owner1", ctx)[0] == "NC"  # trips breaker
    # A DIFFERENT owner on the SAME host must not touch the network.
    calls_before = len(net.calls)
    assert probe.probe_github_user("owner2", ctx)[0] == "NC"
    assert len(net.calls) == calls_before  # zero further api.github.com calls


def test_circuit_breaker_is_per_host(tmp_path, monkeypatch):
    net = _install(monkeypatch,
                   lambda u: (429, "") if "github" in u else (404, ""))
    ctx = _ctx(tmp_path)
    assert probe.probe_github_user("owner1", ctx)[0] == "NC"  # GH breaker trips
    # npm host is unaffected -> still probes and returns CC on 404.
    assert probe.probe_npm("phantom", ctx)[0] == "CC"


def test_no_retry_single_attempt(tmp_path, monkeypatch):
    net = _install(monkeypatch, lambda u: (429, ""))
    ctx = _ctx(tmp_path)
    probe.probe_github_user("owner1", ctx)
    # Exactly one urlopen for that owner; the breaker prevents OTHERS, and there
    # is no retry of the original anchor.
    assert net.calls.count("https://api.github.com/users/owner1") == 1


# --- 24h atomic result cache -------------------------------------------------

def test_cache_hit_avoids_network(tmp_path, monkeypatch):
    net = _install(monkeypatch, lambda u: (404, ""))
    ctx = _ctx(tmp_path)
    # Pre-populate a fresh LO verdict for the exact key.
    ctx.cache_put("gh_user::torvalds", "LO", {"public_repos": 99})
    net.calls.clear()
    verdict, meta = probe.probe_github_user("torvalds", ctx)
    assert verdict == "LO"
    assert net.calls == []  # served from cache, zero network


def test_cache_miss_expired_reprobes(tmp_path, monkeypatch):
    net = _install(monkeypatch, lambda u: (200, "{}"))
    ctx = _ctx(tmp_path)
    # Write a stale (>24h) entry by hand.
    import time
    path = ctx._cache_path("npm::lodash")
    vuln_feed._atomic_write(path, {"_cached_at": time.time() - 90000,
                                   "verdict": "CC", "extra": {}})
    verdict, _ = probe.probe_npm("lodash", ctx)
    assert verdict == "LO"  # re-probed, not the stale CC
    assert len(net.calls) == 1


def test_malformed_cache_tolerated(tmp_path, monkeypatch):
    net = _install(monkeypatch, lambda u: (404, ""))
    ctx = _ctx(tmp_path)
    path = ctx._cache_path("npm::brokenpkg")
    import os
    os.makedirs(os.path.dirname(path), exist_ok=True)
    with open(path, "w") as f:
        f.write("{ this is not valid json")
    # Must fall through to a normal probe, no exception.
    verdict, _ = probe.probe_npm("brokenpkg", ctx)
    assert verdict == "CC"


def test_cache_write_uses_atomic_replace(tmp_path, monkeypatch):
    # Simulate two racing writes for the same key: both complete, final file is
    # valid JSON (never torn) — os.replace is the mechanism (via _atomic_write).
    import os
    import json as _json
    _install(monkeypatch, lambda u: (200, "{}"))
    ctx = _ctx(tmp_path)
    ctx.cache_put("npm::race", "LO", {"n": 1})
    ctx.cache_put("npm::race", "LO", {"n": 2})
    path = ctx._cache_path("npm::race")
    with open(path) as f:
        data = _json.load(f)  # parses cleanly => not torn
    assert data["verdict"] == "LO"


def test_nc_is_never_cached(tmp_path, monkeypatch):
    _install(monkeypatch, lambda u: (403, ""))
    ctx = _ctx(tmp_path)
    probe.probe_npm("throttled", ctx)
    import os
    path = ctx._cache_path("npm::throttled")
    assert not os.path.exists(path)  # transient NC not persisted


# --- assert the suite itself never hit real network --------------------------

def test_no_real_network_sentinel(tmp_path, monkeypatch):
    # If any probe reached real urlopen, that means the monkeypatch missed. Prove
    # the chokepoint is vuln_feed._https_fetch -> urllib.request.urlopen (routed
    # through the no-redirect opener, seamed via _DirectOpener in tests). A fresh
    # tmp cache dir guarantees a real network attempt (no cache short-circuit).
    hit = {"n": 0}
    monkeypatch.setattr(probe, "_no_redirect_opener", lambda: probe._DirectOpener())
    monkeypatch.setattr(vuln_feed.urllib.request, "urlopen",
                        lambda *a, **k: hit.__setitem__("n", hit["n"] + 1) or (_ for _ in ()).throw(urllib.error.URLError("blocked")))
    ctx = probe.ProbeContext(offline=False, cache_dir=str(tmp_path))
    probe.probe_npm("anything", ctx)
    assert hit["n"] == 1  # went through the mocked chokepoint, not the real net


# --- redirect / SSRF hardening -----------------------------------------------

def test_no_redirect_handler_refuses_all_redirects():
    # The production no-redirect handler never follows a 3xx (returns None so
    # urllib raises the redirect as its own HTTPError instead of auto-fetching).
    h = probe._NoRedirectHandler()
    assert h.redirect_request(None, None, 302, "Found", {},
                              "http://169.254.169.254/latest/meta-data/") is None


def test_bounded_redirect_rejects_http_scheme():
    # RDAP's bounded opener refuses a downgrade to http:// (and only follows
    # https hops to non-internal hosts).
    h = probe._BoundedRedirectHandler()
    assert h.redirect_request(None, None, 302, "Found", {},
                              "http://rdap.verisign.com/x") is None


def test_3xx_not_followed_single_call_breaker_intact(tmp_path, monkeypatch):
    # A 3xx from an anchor host is treated as COULDN'T-CHECK, NOT auto-followed:
    # exactly one call, no extra request to the Location, breaker untouched.
    net = _install(monkeypatch, lambda u: (
        302, "", "http://169.254.169.254/latest/meta-data/"))
    ctx = _ctx(tmp_path)
    verdict, _ = probe.probe_github_user("someowner", ctx)
    assert verdict == "NC"
    assert len(net.calls) == 1  # no follow to the redirect target
    assert not ctx.breaker.is_tripped(probe._HOST_GITHUB)


# --- name normalization (no phantom on brand-cased / separator-variant) -------

def test_npm_brand_case_resolves_live_not_phantom(tmp_path, monkeypatch):
    # 'React' must probe the canonical 'react' (live) -> LO, never a phantom CC.
    def router(u):
        return (200, "{}") if u.endswith("/react") else (404, "")
    net = _install(monkeypatch, router)
    assert probe.probe_npm("React", _ctx(tmp_path))[0] == "LO"


def test_pypi_pep503_normalization_resolves_live(tmp_path, monkeypatch):
    # 'My_Package' must probe canonical 'my-package' -> LO, never a phantom CC.
    def router(u):
        return (200, "{}") if "/my-package/" in u else (404, "")
    _install(monkeypatch, router)
    assert probe.probe_pypi("My_Package", _ctx(tmp_path))[0] == "LO"


def test_github_token_does_not_inflate_budget(tmp_path, monkeypatch):
    # A GITHUB_TOKEN must NOT raise the call cap (it is never attached to a
    # request), so the budget stays at the unauthenticated ceiling.
    _install(monkeypatch, lambda u: (200, "{}"))
    ctx = _ctx(tmp_path, github_token="ghp_dummy")
    assert ctx.budget.gh_remaining == probe.GITHUB_CALL_CAP


def test_cache_symlinked_dir_not_written(tmp_path, monkeypatch):
    # If the cache dir is a symlink, no verdict is written through it
    # (cache-poisoning backstop, security-sentinel F6).
    import os
    real = tmp_path / "real"
    real.mkdir()
    link = tmp_path / "link"
    os.symlink(str(real), str(link))
    _install(monkeypatch, lambda u: (404, ""))
    ctx = probe.ProbeContext(offline=False, cache_dir=str(link))
    ctx.cache_put("npm::x", "CC", None)
    assert os.listdir(str(real)) == []  # nothing written through the symlink
rea-analysis-optimizationmorluto/reaOptimize REA providers and shared analysis workflows for agent context, latency, CPU, and memory while preserving correctness and Evidence. Use for analysis bottlenecks and cache or query design; CI/test-suite optimization belongs to rea-ci-tests-audit.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
rea-ci-tests-auditmorluto/reaAudit REA CI and test suites for correctness, fixture drift, false passes, and measured performance; use for suite or workflow investigations and efficiency changes, rather than ordinary feature testing or a single test failure.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
rea-tool-designmorluto/reaAudit, investigate, design, or change REA investigation tools, CLI/MCP contracts, provider capabilities, and Evidence semantics.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
ponytail-auditDietrichGebert/ponytailQuality audit of the whole repo: bugs, security, real load, missing tests, speed, and what to delete. Most important first.Stars: 196kUpdated Claude CodeCodexVerifiedInstall