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_scan_bytecode.py

View on GitHub
"""Tests for scan_bytecode.py — U2 isolated .pyc disassembly scanner."""

import os
import py_compile
import struct
import subprocess
import sys
import pytest
import scan_bytecode as scanner
import _pyc_unmarshal as child

_NEEDS_NATIVE_SANDBOX = pytest.mark.skipif(
    sys.platform not in ("darwin", "linux"),
    reason="disassembly requires macOS Seatbelt or Linux seccomp",
)


def _compile_to_pyc(src_code, directory, name="mod"):
    src = directory / f"{name}.py"
    src.write_text(src_code)
    pyc = directory / f"{name}.pyc"
    py_compile.compile(str(src), cfile=str(pyc), doraise=True)
    return src, pyc


def _decode_fixture(pyc):
    # Trusted compiler output exercises the same parser path as production.
    return child._Reader(pyc.read_bytes()[16:]).read()


def _cats(findings):
    return {f.category for f in findings}


def _poison_pyc(directory, malicious_code, benign_source, name="mod"):
    """Simulate bytecode poisoning: compile MALICIOUS source to .pyc, then swap
    the .py for BENIGN source. The loaded .pyc steals/execs; the visible source
    is innocent — the Trail of Bits simple-formatter pattern."""
    src = directory / f"{name}.py"
    src.write_text(malicious_code)
    pyc = directory / f"{name}.pyc"
    py_compile.compile(str(src), cfile=str(pyc), doraise=True)
    src.write_text(benign_source)  # decoy source replaces the real one
    # A working attack patches the header so Python accepts the .pyc for the
    # decoy (stored mtime/size match it); otherwise the import system ignores
    # the stale file and recompiles. Model the attack that actually runs.
    st = os.stat(src)
    raw = bytearray(pyc.read_bytes())
    raw[8:16] = struct.pack("<II", int(st.st_mtime) & 0xFFFFFFFF, st.st_size & 0xFFFFFFFF)
    pyc.write_bytes(bytes(raw))
    return src, pyc


class TestHeaderLen:
    def test_modern_pyc_header_16(self):
        # 3.7+ magic: version int >= 3390, followed by \r\n.
        magic = bytes([0x50, 0x0D]) + b"\r\n"  # 0x0D50 = 3408 -> 16
        assert scanner.pyc_header_len(magic) == 16

    def test_legacy_header_12(self):
        n = 3200
        magic = bytes([n & 0xFF, (n >> 8) & 0xFF]) + b"\r\n"
        assert scanner.pyc_header_len(magic) == 12

    def test_unrecognized_magic_none(self):
        assert scanner.pyc_header_len(b"\x00\x00\x00\x00") is None
        assert scanner.pyc_header_len(b"PK\x03\x04") is None
        assert scanner.pyc_header_len(b"ab") is None


class TestBytecodePoisoning:
    """GAP 1 (Trail of Bits simple-formatter): benign .py source, malicious
    bytecode in the .pyc. Detection is raw-bytes vs source diff — no execution,
    no matching interpreter required."""

    _BENIGN = "def format_text(t):\n    return t.strip().title()\n"
    _MALICIOUS = (
        "import os\n"
        "for k, v in os.environ.items():\n"
        "    print(k, v)\n"
        "eval(\"print('x')\")\n"
        "def format_text(t):\n    return t.strip().title()\n"
    )

    def test_poisoned_pyc_flagged_high(self, tmp_path):
        _poison_pyc(tmp_path, self._MALICIOUS, self._BENIGN)
        findings = scanner.scan_repo(str(tmp_path))
        assert "bytecode-poisoning" in _cats(findings)
        poison = [f for f in findings if f.category == "bytecode-poisoning"]
        assert all(f.severity == "high" for f in poison)
        assert "environ" in poison[0].snippet

    def test_poison_detected_without_disassembly(self, tmp_path, monkeypatch):
        # Force ALL disassembly to fail (simulates a .pyc no installed interpreter
        # can read). The raw-bytes detector must still fire — proving detection
        # never depends on unmarshalling attacker code.
        _poison_pyc(tmp_path, self._MALICIOUS, self._BENIGN)
        monkeypatch.setattr(scanner, "_disassemble_best",
                            lambda *a, **k: (None, "could not unmarshal bytecode (corrupt or cross-version)"))
        findings = scanner.scan_repo(str(tmp_path))
        assert "bytecode-poisoning" in _cats(findings)

    def test_clean_compiled_module_no_poison(self, tmp_path):
        # Source legitimately uses os.environ -> the marker is in BOTH source and
        # bytecode -> the diff cancels -> no false positive.
        code = "import os\ndef cfg():\n    return os.environ.get('HOME')\n"
        _compile_to_pyc(code, tmp_path)
        findings = scanner.scan_repo(str(tmp_path))
        assert "bytecode-poisoning" not in _cats(findings)

    def test_opaque_pyc_with_source_flagged_medium(self, tmp_path, monkeypatch):
        # An unreadable .pyc beside readable source is suspicious but also matches
        # a benign cross-version-committed-.pyc shape -> MEDIUM review, not HIGH
        # (the raw-marker poison detector is the load-bearing HIGH signal).
        _compile_to_pyc("def f():\n    return 1\n", tmp_path)
        monkeypatch.setattr(scanner, "_disassemble_best",
                            lambda *a, **k: (None, "could not unmarshal bytecode (corrupt or cross-version)"))
        findings = scanner.scan_repo(str(tmp_path))
        assert "opaque-bytecode-with-source" in _cats(findings)
        assert any(f.severity == "medium" for f in findings
                   if f.category == "opaque-bytecode-with-source")

    @_NEEDS_NATIVE_SANDBOX
    def test_obfuscated_getattr_gadget_flagged(self, tmp_path):
        # getattr(os, chr(115)+"ystem")("id") hides the attribute name from the
        # raw-marker + co_name detectors; the gadget co-occurrence catches it.
        code = ("import os\n"
                "f = getattr(os, chr(115) + 'ystem')\n"
                "f('id')\n")
        src, _pyc = _compile_to_pyc(code, tmp_path)
        src.unlink()  # orphan, so poison-diff is skipped and gadget must carry it
        findings = scanner.scan_repo(str(tmp_path))
        assert "bytecode-hidden-logic" in _cats(findings)

    def test_plain_getattr_no_false_positive(self, tmp_path):
        # getattr without char-building + sensitive import must NOT fire.
        code = "import os\nx = getattr(os.path, 'join')\n"
        _compile_to_pyc(code, tmp_path)
        findings = scanner.scan_repo(str(tmp_path))
        assert "bytecode-hidden-logic" not in _cats(findings)

    def test_decoy_source_substring_does_not_cancel_poison(self, tmp_path):
        # A decoy source containing `exec_command` must NOT cancel the standalone
        # `exec` co_name in the poisoned .pyc (word-boundary source match).
        malicious = "exec('bad')\ndef run():\n    return 1\n"
        benign = "def exec_command(c):\n    return c\ndef run():\n    return 1\n"
        _poison_pyc(tmp_path, malicious, benign)
        findings = scanner.scan_repo(str(tmp_path))
        assert "bytecode-poisoning" in _cats(findings)

@_NEEDS_NATIVE_SANDBOX
class TestBytecodeDetection:
    def test_exec_primitive_in_function_body_orphan_elevated(self, tmp_path):
        # os.system inside a function -> lives in a NESTED code object, which the
        # recursive co_consts walk must reach. .py deleted -> orphan.
        code = "def run():\n    import os\n    os.system('id')\n"
        src, _pyc = _compile_to_pyc(code, tmp_path)
        src.unlink()
        findings = scanner.scan_repo(str(tmp_path))
        assert "bytecode-hidden-logic" in _cats(findings)
        assert "orphan-bytecode" in _cats(findings)
        assert any(f.category == "orphan-bytecode" and f.severity == "high"
                   for f in findings), "orphan + primitive must elevate to high"

    def test_credential_path_in_constants(self, tmp_path):
        code = "def f():\n    p = '/home/u/.ssh/id_rsa'\n    return open(p).read()\n"
        _compile_to_pyc(code, tmp_path)
        findings = scanner.scan_repo(str(tmp_path))
        cats = _cats(findings)
        assert "bytecode-hidden-logic" in cats

    def test_url_in_constants(self, tmp_path):
        code = "def f():\n    return 'http://evil.example/payload'\n"
        _compile_to_pyc(code, tmp_path)
        findings = scanner.scan_repo(str(tmp_path))
        assert "bytecode-hidden-logic" in _cats(findings)

    def test_benign_pyc_with_sibling_no_finding(self, tmp_path):
        code = "def add(a, b):\n    return a + b\n\n\nclass C:\n    x = 1\n"
        _compile_to_pyc(code, tmp_path)  # leaves the .py in place (has sibling)
        findings = scanner.scan_repo(str(tmp_path))
        assert findings == []

    def test_large_string_constant_remains_analyzable(self, tmp_path):
        code = "data = " + repr("a" * 120000) + "\n"
        _compile_to_pyc(code, tmp_path)
        assert scanner.scan_repo(str(tmp_path)) == []

    def test_benign_orphan_outside_vendor_is_low_note(self, tmp_path):
        code = "def add(a, b):\n    return a + b\n"
        src, _pyc = _compile_to_pyc(code, tmp_path)
        src.unlink()
        findings = scanner.scan_repo(str(tmp_path))
        # Orphan but benign -> low note, never critical/high.
        assert "orphan-bytecode" in _cats(findings)
        assert all(f.severity == "low" for f in findings if f.category == "orphan-bytecode")
        assert not any(f.severity in ("critical", "high") for f in findings)

    def test_benign_orphan_in_vendor_suppressed(self, tmp_path):
        # Source-stripped wheel: loose .pyc under site-packages, no .py, benign.
        vendor = tmp_path / "site-packages" / "pkg"
        vendor.mkdir(parents=True)
        code = "def util():\n    return 42\n"
        src, _pyc = _compile_to_pyc(code, vendor)
        src.unlink()
        findings = scanner.scan_repo(str(tmp_path))
        # Benign orphan in a vendor root produces NO finding (R9 FP guard).
        assert findings == []


class TestGracefulDegradation:
    def test_corrupt_pyc_unanalyzable_no_crash(self, tmp_path):
        # Valid 3.7+ magic header but garbage marshal body -> child fails ->
        # parent records 'unanalyzable', scan survives. This is the safety test.
        pyc = tmp_path / "evil.pyc"
        magic = bytes([0x50, 0x0D]) + b"\r\n"  # 16-byte header
        pyc.write_bytes(magic + b"\x00" * 12 + b"\xff\xfe\xfd garbage marshal body")
        findings = scanner.scan_repo(str(tmp_path))
        assert "unanalyzable-bytecode" in _cats(findings)

    def test_unrecognized_magic_unanalyzable(self, tmp_path):
        pyc = tmp_path / "weird.pyc"
        pyc.write_bytes(b"\x00\x00\x00\x00" + b"x" * 64)
        findings = scanner.scan_repo(str(tmp_path))
        assert "unanalyzable-bytecode" in _cats(findings)

    def test_oversized_pyc_skipped(self, tmp_path, monkeypatch):
        monkeypatch.setattr(scanner, "MAX_PYC_BYTES", 100)
        pyc = tmp_path / "big.pyc"
        magic = bytes([0x50, 0x0D]) + b"\r\n"
        pyc.write_bytes(magic + b"\x00" * 200)
        findings = scanner.scan_repo(str(tmp_path))
        assert "unanalyzable-bytecode" in _cats(findings)


class TestBudget:
    def test_pyc_count_cap_emits_incomplete(self, tmp_path, monkeypatch):
        monkeypatch.setattr(scanner, "MAX_PYC", 1)
        for i in range(3):
            code = f"x = {i}\n"
            _compile_to_pyc(code, tmp_path, name=f"m{i}")
        findings = scanner.scan_repo(str(tmp_path))
        assert "archive-scan-incomplete" in _cats(findings)


class TestSiblingResolution:
    def test_pycache_sibling_mapping(self, tmp_path):
        pkg = tmp_path / "pkg"
        cache = pkg / "__pycache__"
        cache.mkdir(parents=True)
        (pkg / "mod.py").write_text("x = 1\n")
        pyc = cache / "mod.cpython-314.pyc"
        assert scanner._sibling_py(str(pyc)) == str(pkg / "mod.py")


class TestBytecodeSandbox:
    @pytest.mark.parametrize("limit", ["MAX_DEPTH", "MAX_CODE_OBJECTS"])
    def test_traversal_limit_never_returns_partial_success(self, tmp_path, monkeypatch, limit):
        _, pyc = _compile_to_pyc("def nested():\n    return 1\n", tmp_path)
        code = _decode_fixture(pyc)
        monkeypatch.setattr(child, limit, 0)
        with pytest.raises(ValueError, match="traversal limit"):
            child._walk_code(code, [], set(), 0)

    def test_bad_opcode_walk_never_returns_partial_success(self, tmp_path, monkeypatch):
        def bad_instructions(_):
            raise ValueError("malformed opcode")
        monkeypatch.setattr(child, "_instructions", bad_instructions)
        _, pyc = _compile_to_pyc("x = 1\n", tmp_path)
        with pytest.raises(ValueError, match="malformed opcode"):
            child._walk_code(_decode_fixture(pyc), [], set(), 0)

    def test_output_limit_never_returns_partial_success(self, tmp_path, monkeypatch):
        _, pyc = _compile_to_pyc("x = 'output exceeds cap'\n", tmp_path)
        monkeypatch.setattr(child.sys, "argv", ["child", str(pyc), "16"])
        monkeypatch.setattr(child, "_apply_limits", lambda: None)
        # This trusted fixture runs in the test process; never sandbox pytest.
        monkeypatch.setattr(child, "_apply_sandbox", lambda: True)
        monkeypatch.setattr(child, "MAX_OUTPUT_CHARS", 1)
        with pytest.raises(ValueError, match="output limit"):
            child.main()

    def test_output_collection_is_bounded_before_join(self, monkeypatch):
        monkeypatch.setattr(child, "MAX_OUTPUT_CHARS", 4)
        lines = child._BoundedLines()
        lines.append("abcd")
        with pytest.raises(child.AnalysisLimitExceeded):
            lines.append("excess")
        assert lines == ["abcd"]

    def test_analysis_limit_maps_to_incomplete_reason(self, monkeypatch):
        monkeypatch.setattr(scanner.subprocess, "run", lambda *args, **kwargs:
                            subprocess.CompletedProcess(args[0], child.ANALYSIS_LIMIT, b"", b""))
        blob, error, retry = scanner._disassemble("input.pyc", 16)
        assert blob is None
        assert retry is False
        assert "safety limit" in error and "incomplete" in error

    def test_cross_version_parse_failure_retries_other_interpreter(self, monkeypatch):
        calls = []
        def parse(_path, _header, interpreter=None):
            calls.append(interpreter)
            if interpreter is None:
                return None, "could not parse bytecode", True
            return "NAME os", None, False
        monkeypatch.setattr(scanner, "_disassemble", parse)
        monkeypatch.setattr(scanner, "_alt_interpreters", lambda: ("python-other",))
        assert scanner._disassemble_best("input.pyc", 16) == ("NAME os", None)
        assert calls == [None, "python-other"]

    def test_only_magic_mismatch_retries_other_interpreter(self, monkeypatch):
        for code, retry in ((child.MAGIC_MISMATCH, True), (4, False)):
            monkeypatch.setattr(scanner.subprocess, "run", lambda *args, **kwargs:
                                subprocess.CompletedProcess(args[0], code, b"", b""))
            blob, error, should_retry = scanner._disassemble("input.pyc", 16)
            assert blob is None and error
            assert should_retry is retry

    def test_sandbox_unavailable_is_visible_in_scan(self, tmp_path, monkeypatch):
        _compile_to_pyc("value = 42\n", tmp_path)
        monkeypatch.setattr(scanner, "_disassemble_best", lambda *args: (
            None, "OS sandbox unavailable; unsafe bytecode disassembly was skipped"))
        findings = scanner.scan_repo(str(tmp_path))
        assert "unanalyzable-bytecode" in _cats(findings)
        assert any("OS sandbox unavailable" in f.description for f in findings)

    def test_unsupported_platform_refuses_sandbox(self, monkeypatch):
        monkeypatch.setattr(child.sys, "platform", "unsupported")
        assert child._apply_sandbox() is False

    def test_no_sandbox_never_parses(self, tmp_path, monkeypatch):
        _, pyc = _compile_to_pyc("value = 1\n", tmp_path)
        monkeypatch.setattr(child.sys, "argv", ["child", str(pyc), "0"])
        monkeypatch.setattr(child, "_apply_limits", lambda: None)
        monkeypatch.setattr(child, "_apply_sandbox", lambda: False)
        monkeypatch.setattr(child._Reader, "read", lambda *_: pytest.fail("parsed without sandbox"))
        with pytest.raises(SystemExit) as exc:
            child.main()
        assert exc.value.code == child.SANDBOX_UNAVAILABLE

    def test_cyclic_marshal_container_is_bounded(self):
        # CPython accepts container back-references; keep scanning safely.
        raw = b"\xa9\x01\x72\x00\x00\x00\x00"
        value = child._Reader(raw).read()
        assert value[0] is value

    def test_flagged_reference_does_not_shift_reference_indexes(self):
        raw = (b"\xa9\x04" + b"\xfa\x01x" + b"\xf2\x01\x00\x00\x00"
               + b"\xfa\x01y" + b"r\x02\x00\x00\x00")
        assert list(child._Reader(raw).read()) == ["x", "x", "y", "y"]

    def test_flagged_singleton_does_not_shift_reference_indexes(self):
        # CPython accepts FLAG_REF on a singleton but does not register it.
        raw = (b"(\x03\x00\x00\x00" + b"\xce" + b"\xe1\x01\x00\x00\x00a"
               + b"r\x00\x00\x00\x00")
        assert list(child._Reader(raw).read()) == [None, "a", "a"]

    def test_ascii_tags_preserve_non_utf8_bytes(self):
        assert child._Reader(b"\xfa\x01\x80").read() == "\x80"

    def test_nested_constant_containers_are_walked(self, tmp_path):
        _, pyc = _compile_to_pyc("import os\n", tmp_path)
        nested = _decode_fixture(pyc)
        _, outer_pyc = _compile_to_pyc("value = 1\n", tmp_path, name="outer")
        outer = _decode_fixture(outer_pyc)
        consts = child._Sequence("(")
        consts.items.extend(["hidden-marker", nested])
        consts.items.append(consts)  # Cyclic references remain bounded.
        outer.co_consts.items.append(consts)
        out = child._BoundedLines()
        child._walk_code(outer, out, set(), 0)
        assert "CONST hidden-marker" in out
        assert "OP IMPORT_NAME os" in out

    @pytest.mark.skipif(sys.version_info < (3, 11), reason="no inline caches")
    def test_missing_inline_cache_table_fails_closed(self, tmp_path, monkeypatch):
        _, pyc = _compile_to_pyc("import os\n", tmp_path)
        monkeypatch.setattr(child.dis, "_inline_cache_entries", None)
        with pytest.raises(child.AnalysisLimitExceeded, match="inline cache table"):
            list(child._instructions(_decode_fixture(pyc)))

    def test_large_container_is_not_rejected_by_arbitrary_object_cap(self):
        raw = b"(" + (100001).to_bytes(4, "little") + b"N" * 100001
        assert len(child._Reader(raw).read()) == 100001

    @pytest.mark.skipif(sys.version_info < (3, 11), reason="no inline caches")
    def test_inline_cache_bytes_cannot_forge_import(self, tmp_path):
        _, pyc = _compile_to_pyc("x = object()\ny = x.real\n", tmp_path)
        code = _decode_fixture(pyc)
        opcodes = bytearray(code.co_code)
        cache_sizes = child.dis._inline_cache_entries
        for offset in range(0, len(opcodes), 2):
            name = child.dis.opname[opcodes[offset]]
            count = (cache_sizes[opcodes[offset]] if isinstance(cache_sizes, list)
                     else cache_sizes.get(name, 0))
            if count:
                opcodes[offset + 2:offset + 4] = bytes([child.dis.opmap["IMPORT_NAME"], 255])
                break
        else:
            pytest.skip("interpreter has no inline caches")
        code.co_code = bytes(opcodes)
        out = child._BoundedLines()
        child._walk_code(code, out, set(), 0)
        assert not any(line.startswith("OP IMPORT_NAME") for line in out)

    def test_unused_non_string_name_does_not_hide_other_imports(self, tmp_path):
        _, pyc = _compile_to_pyc("import os\n", tmp_path)
        code = _decode_fixture(pyc)
        code.co_names.items.append(42)
        out = child._BoundedLines()
        child._walk_code(code, out, set(), 0)
        assert "OP IMPORT_NAME os" in out

    def test_missing_linux_library_fails_closed(self, monkeypatch):
        monkeypatch.setattr(child.sys, "platform", "linux")
        monkeypatch.setattr(child.ctypes.util, "find_library", lambda _: None)
        assert child._apply_sandbox() is False

    def test_launcher_removes_environment_and_reports_sandbox_failure(self, monkeypatch):
        def run(command, **kwargs):
            assert command[1:3] == ["-I", "-S"]
            assert set(kwargs["env"]) == {"PATH", "LANG"}
            assert kwargs["close_fds"] is True
            assert kwargs["stdin"] == subprocess.DEVNULL
            return subprocess.CompletedProcess(command, child.SANDBOX_UNAVAILABLE, b"", b"")
        monkeypatch.setattr(scanner.subprocess, "run", run)
        blob, error, retry = scanner._disassemble("input.pyc", 16)
        assert blob is None
        assert retry is False
        assert "OS sandbox unavailable" in error

    @pytest.mark.skipif(sys.platform not in ("darwin", "linux"), reason="native sandbox unavailable")
    def test_native_sandbox_denies_host_capabilities(self, tmp_path):
        secret = tmp_path / "secret.txt"
        secret.write_text("PRIVATE_CANARY")
        target = tmp_path / "write-target"
        probe = '''
import errno, os, socket, sys
sys.path.insert(0, sys.argv[1])
import _pyc_unmarshal as child
network = socket.socket()
if not child._apply_sandbox():
    sys.exit(5)
for operation in (
    lambda: open(sys.argv[2]).read(),
    lambda: open(sys.argv[3], "w"),
    lambda: network.connect(("127.0.0.1", 9)),
    os.fork,
):
    try:
        operation()
    except OSError as exc:
        if exc.errno not in (errno.EPERM, errno.EACCES):
            raise
        continue
    sys.exit(7)
print("all denied")
'''
        proc = subprocess.run(
            [sys.executable, "-I", "-S", "-c", probe,
             os.path.dirname(scanner._UNMARSHAL), str(secret), str(target)],
            capture_output=True, text=True, timeout=10,
            stdin=subprocess.DEVNULL,
        )
        if proc.returncode == child.SANDBOX_UNAVAILABLE:
            pytest.skip("OS sandbox cannot activate on this host")
        assert proc.returncode == 0, proc.stderr
        assert proc.stdout.strip() == "all denied"
        assert not target.exists()
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: 194kUpdated Claude CodeCodexVerifiedInstall