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

View on GitHub
"""Structural false-positive fixes: each benign case must go quiet and each
malicious twin must keep its finding at full severity."""
import os
import py_compile
import struct
import sys
import time

import pytest

import aggregate_json as agg
import scan_bytecode
import scan_dependencies
import scan_lifecycle

DEADMAN = "Dead-Man's Switch: Home Directory Resolved and Recursively Deleted"
HEAD = 'import { mkdtempSync, rmSync } from "node:fs";\nimport os from "node:os";\n'
RM = 'rmSync(HOME, { recursive: true, force: true });\n'


# --- temp-folder deletes are not home wipes ------------------------------------

def _deadman(tmp_path, text):
    p = tmp_path / "t.test.mjs"
    p.write_text(text)
    return any(f.title == DEADMAN for f in scan_lifecycle.scan_second_coming_indicators(str(p), "t.test.mjs"))


@pytest.mark.parametrize("text", [
    HEAD + 'const HOME = mkdtempSync(join(tmpdir(), "x-"));\nprocess.env.HOME = HOME;\n'
           'const p = join(HOME, ".claude");\n' + RM,
    HEAD + 'const HOME = await fs.promises.mkdtemp("x");\nprocess.env.HOME = HOME;\n' + RM,
])
def test_fresh_temp_dir_delete_is_not_a_home_wipe(tmp_path, text):
    assert not _deadman(tmp_path, text)


@pytest.mark.parametrize("text", [
    HEAD + 'rmSync(os.homedir(), { recursive: true });\n',
    HEAD + 'let HOME = mkdtempSync("x");\nHOME = os.homedir();\n' + RM,
    HEAD + 'const HOME = mkdtempSync("x") || os.homedir();\n' + RM,
    HEAD + 'const HOME = mkdtempSync("x");\nfunction f() { const HOME = os.homedir(); ' + RM + '}\n',
    HEAD + 'const HOME = mkdtempSync("x");\nfunction wipe(HOME) { ' + RM + '}\nwipe(os.homedir());\n',
    HEAD + 'const HOME = mkdtempSync("x");\nconst wipe = (HOME) => ' + RM + 'wipe(os.homedir());\n',
    HEAD + 'const HOME = mkdtempSync("x");\nclass A { w(HOME) { ' + RM + '} }\nnew A().w(os.homedir());\n',
    HEAD + 'const HOME = mkdtempSync("x");\nprocess.env.HOME = os.homedir();\n({ HOME } = process.env);\n' + RM,
    HEAD + 'const HOME = mkdtempSync("x");\nfor (const HOME of [os.homedir()]) ' + RM,
    HEAD + 'const HOME = mkdtempSync("x");\ntry { throw os.homedir() } catch (HOME) { ' + RM + '}\n',
    HEAD + 'const HOME = mkdtempSync("x");\neval("HOME = os.homedir()");\n' + RM,
    HEAD + 'const HOME = mkdtempSync("x");\n' + RM + 'rmSync(process.env.HOME, { recursive: true });\n',
    HEAD + 'const home = os.homedir();\nrmSync(home, { recursive: true });\n',
])
def test_real_home_wipes_still_reported(tmp_path, text):
    assert _deadman(tmp_path, text)


# --- capability findings -------------------------------------------------------

def _cap(rule, path, sev="high"):
    return {"scanner": "skill_threats", "rule_id": rule, "severity": sev, "file": path,
            "title": "Accessing user home directories", "category": "scope-escalation",
            "line": 3, "snippet": "~/x"}


def test_capability_in_code_without_outbound_graded_low():
    findings = [_cap("ST-SP-002", "a.py"),
                {"scanner": "skill_threats", "rule_id": "ST-EX-008", "severity": "low",
                 "file": "a.py", "title": "Environment variable access",
                 "category": "credential-exfiltration"}]
    agg.apply_capability_caps(findings)
    f = findings[0]
    assert f["severity"] == "low" and f["original_severity"] == "high" and f["capability"]
    assert any(m.get("title") == "Capability findings graded to LOW" for m in findings)


@pytest.mark.parametrize("sink", [
    {"title": "Potential Data Exfiltration", "category": "exfiltration"},
    {"title": "Outbound network request", "category": "network"},
    {"title": "Lethal Trifecta (exec + network + credential read)", "category": "lethal-trifecta"},
    {"title": "Webhook URL", "category": "webhook"},
])
def test_capability_next_to_outbound_keeps_severity(sink):
    findings = [_cap("ST-SP-002", "a.py"), dict(sink, scanner="x", severity="medium", file="a.py")]
    agg.apply_capability_caps(findings)
    assert findings[0]["severity"] == "high"


def test_capability_in_agent_instructions_keeps_severity():
    findings = [_cap("ST-SP-005", "skills/x/SKILL.md")]
    agg.apply_capability_caps(findings)
    assert findings[0]["severity"] == "high"


def test_credential_rules_never_capped():
    findings = [_cap("ST-EX-004", "a.py", "critical"), _cap("ST-CR-002", "a.py")]
    agg.apply_capability_caps(findings)
    assert [f["severity"] for f in findings] == ["critical", "high"]


def test_postgres_word_is_not_an_outbound_sink():
    findings = [_cap("ST-SP-002", "a.py"),
                {"scanner": "secrets", "title": "PostgreSQL Connection URI", "category": "secret",
                 "severity": "high", "file": "a.py"}]
    agg.apply_capability_caps(findings)
    assert findings[0]["severity"] == "low"


# --- identical copies ------------------------------------------------------------

def _f(path, **kw):
    base = {"scanner": "sast", "rule_id": "R1", "title": "t", "severity": "high",
            "line": 2, "snippet": "x", "category": "c", "file": path}
    base.update(kw)
    return base


def test_identical_copies_collapse_with_every_path_listed(tmp_path):
    for d in ("a", "b/c", "z"):
        (tmp_path / d).mkdir(parents=True)
        (tmp_path / d / "m.py").write_text("x = 1\n")
    out = agg.collapse_identical_copies(
        [_f("z/m.py"), _f("a/m.py"), _f("b/c/m.py")], str(tmp_path))
    assert len(out) == 1
    assert out[0]["file"] == "a/m.py" and out[0]["also_in"] == ["b/c/m.py", "z/m.py"]
    assert out[0]["severity"] == "high"


def test_near_copies_do_not_collapse(tmp_path):
    (tmp_path / "a.py").write_text("x = 1\n")
    (tmp_path / "b.py").write_text("x = 1  # changed\n")
    out = agg.collapse_identical_copies([_f("a.py"), _f("b.py")], str(tmp_path))
    assert len(out) == 2


def test_different_findings_on_identical_files_do_not_collapse(tmp_path):
    (tmp_path / "a.py").write_text("x = 1\n")
    (tmp_path / "b.py").write_text("x = 1\n")
    out = agg.collapse_identical_copies([_f("a.py"), _f("b.py", line=3)], str(tmp_path))
    assert len(out) == 2


def test_symlinked_copy_never_collapses(tmp_path):
    (tmp_path / "a.py").write_text("x = 1\n")
    os.symlink("a.py", tmp_path / "b.py")
    out = agg.collapse_identical_copies([_f("a.py"), _f("b.py")], str(tmp_path))
    assert len(out) == 2


def test_meta_findings_without_file_untouched(tmp_path):
    meta = {"scanner": "meta", "title": "m", "severity": "medium", "file": ""}
    out = agg.collapse_identical_copies([dict(meta), dict(meta)], str(tmp_path))
    assert len(out) == 2


# --- dependency freshness budget ----------------------------------------------

@pytest.fixture
def freshness_state():
    saved = dict(scan_dependencies._FRESHNESS_STATE)
    scan_dependencies._FRESHNESS_STATE.update(
        enabled=True, offline=False, queried=set(), query_count=0, max_queries=100,
        started_at=None, time_skipped=0)
    yield scan_dependencies._FRESHNESS_STATE
    scan_dependencies._FRESHNESS_STATE.clear()
    scan_dependencies._FRESHNESS_STATE.update(saved)


def test_freshness_stops_at_time_budget(freshness_state, monkeypatch):
    calls = []

    def slow_fetch(name, version, offline=False):
        calls.append(name)
        return None
    import vuln_feed
    monkeypatch.setattr(vuln_feed, "fetch_npm_freshness", slow_fetch)
    freshness_state["time_budget_s"] = 0.0
    freshness_state["started_at"] = time.monotonic() - 1
    findings = scan_dependencies._check_freshness(
        "npm", {f"obscure-pkg-{i}": "1.0.0" for i in range(5)}, "package.json")
    assert calls == []
    cap = [f for f in findings if f.title == "Freshness check time limit reached"]
    assert len(cap) == 1 and "5 packages" in cap[0].description


def test_cap_notices_merge_into_one(freshness_state):
    core = scan_dependencies.core
    mk = lambda path, n: core.Finding(
        scanner="dependencies", severity="medium", title="Freshness check query limit reached",
        description=f"{n} packages skipped due to 100 query limit.", file=path, line=0,
        snippet="", category="freshness-query-cap")
    other = core.Finding(scanner="dependencies", severity="high", title="x", description="",
                         file="p", line=0, snippet="", category="ioc")
    out = scan_dependencies._merge_freshness_cap_findings([mk("a/package.json", 3), other,
                                                           mk("b/package-lock.json", 4)])
    caps = [f for f in out if f.category == "freshness-query-cap"]
    assert len(caps) == 1 and caps[0].description.startswith("7 packages")
    assert "a/package.json" in caps[0].description and "b/package-lock.json" in caps[0].description
    assert other in out


# --- bytecode: stale caches and primitives already in source ----------------------

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


def test_stale_cache_with_extra_markers_is_low(tmp_path):
    src, pyc = _compile(tmp_path, "import json\nx = json.loads('1')\n")
    src.write_text("x = 1  # loads removed in a later edit\n".replace("loads", "parse"))
    os.utime(src, (time.time() + 100, time.time() + 100))
    findings = scan_bytecode.scan_pyc(str(pyc), "mod.pyc")
    assert not [f for f in findings if f.severity in ("high", "critical")]
    assert any(f.category == "bytecode-stale" for f in findings)


def test_header_valid_poison_with_new_marker_is_high(tmp_path):
    # `run` is one of the names newly diffed against source.
    src, pyc = _compile(tmp_path, "import subprocess\nsubprocess.run(['id'])\n")
    src.write_text("x = 1\n")
    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))
    findings = scan_bytecode.scan_pyc(str(pyc), "mod.pyc")
    poison = [f for f in findings if f.category == "bytecode-poisoning"]
    assert poison and poison[0].severity == "high"


def test_hash_based_pyc_never_treated_as_stale(tmp_path):
    src, pyc = _compile(tmp_path, "x = 1\n")
    raw = bytearray(pyc.read_bytes())
    raw[4:8] = struct.pack("<I", 1)  # unchecked-hash pyc: always loaded
    pyc.write_bytes(bytes(raw))
    src.write_text("x = 2  # different\n")
    assert scan_bytecode._pyc_is_stale(pyc.read_bytes(), str(src)) is False


@pytest.mark.skipif(not hasattr(scan_bytecode, "_disassemble_best"), reason="no disassembler")
def test_primitives_already_in_source_are_low(tmp_path):
    _compile(tmp_path, "import subprocess\nsubprocess.run(['id'])\n")
    findings = scan_bytecode.scan_pyc(str(tmp_path / "mod.pyc"), "mod.pyc")
    assert not [f for f in findings if f.severity in ("high", "critical")]
    assert not [f for f in findings if f.title.startswith("Hidden logic in bytecode")]
    if sys.platform in ("darwin", "linux"):
        # Proves the reader parsed this interpreter's bytecode (3.15 included)
        # rather than falling back to "opaque bytecode".
        assert any(f.title.startswith("Bytecode primitive (also in source)") for f in findings)


def test_import_names_decoded_on_this_interpreter(tmp_path):
    if sys.platform not in ("darwin", "linux"):
        pytest.skip("disassembly requires macOS Seatbelt or Linux seccomp")
    _, pyc = _compile(tmp_path, "import os, socket\nimport a.b as x\nfrom z import y\n")
    blob, err = scan_bytecode._disassemble_best(str(pyc), 16)
    assert err is None
    _, _, imported = scan_bytecode._parse_blob(blob)
    assert {"os", "socket", "a.b", "z"} <= imported


# --- shell variables are case-sensitive -------------------------------------------

@pytest.mark.parametrize("name,text,want", [
    ("tmp_lower.sh", 'home=$(mktemp -d)\nrm -rf "$home"\n', False),
    ("real_HOME.sh", 'rm -rf "$HOME"\n', True),
    ("real_braced.sh", "rm -rf ${HOME}\n", True),
    ("tilde.sh", "rm -rf ~\n", True),
    ("mixed_line.sh", 'rm -rf "$home"; rm -rf $HOME\n', True),
    ("ps_lower.ps1", "rm -r -f $home\n", True),
    ("del_profile.bat", "del /s /q %USERPROFILE%\n", True),
])
def test_shell_home_wipe_is_case_exact_outside_powershell(tmp_path, name, text, want):
    p = tmp_path / name
    p.write_text(text)
    got = any(f.title.startswith("Dead-Man's Switch")
              for f in scan_lifecycle.scan_second_coming_indicators(str(p), name))
    assert got is want


def test_sarif_keeps_one_result_per_identical_copy():
    import sarif_export
    finding = _f("a/m.py")
    finding["also_in"] = ["b/m.py", "c/m.py"]
    sarif = sarif_export.report_to_sarif({"findings": [finding], "suppressed": []})
    uris = sorted(r["locations"][0]["physicalLocation"]["artifactLocation"]["uri"]
                  for r in sarif["runs"][0]["results"])
    assert uris == ["a/m.py", "b/m.py", "c/m.py"]


# --- file paths are not base64 ---------------------------------------------------

@pytest.mark.parametrize("path", [
    "claude/plugins/marketplaces/outsourcerer/plugins/outsourcerer/skills/outsourcerer/scripts",
    "claude/plugins/marketplaces/outsourcerer/plugins/outsourcerer/skills/outsourcerer/SKILL.md",
])
def test_long_path_is_not_a_base64_block(tmp_path, path):
    import scan_entropy
    p = tmp_path / "log.jsonl"
    p.write_text('{"path": "%s"}\n' % path)
    assert not [f for f in scan_entropy.scan_file(str(p), "log.jsonl") if f.title == "Base64 Encoded Block"]


def test_random_base64_with_slashes_still_reported(tmp_path):
    import base64
    import random
    import scan_entropy
    random.seed(3)
    blob = base64.b64encode(bytes(random.getrandbits(8) for _ in range(120))).decode()
    assert "/" in blob
    p = tmp_path / "x.txt"
    p.write_text(blob + "\n")
    assert [f for f in scan_entropy.scan_file(str(p), "x.txt") if f.title == "Base64 Encoded Block"]


def test_path_check_rejects_random_base64():
    import base64
    import random
    import scan_entropy
    random.seed(11)
    assert not any(scan_entropy._looks_like_path(
        base64.b64encode(bytes(random.getrandbits(8) for _ in range(random.randint(40, 200)))).decode().rstrip("="))
        for _ in range(20000))


# --- one alarm, several rules ------------------------------------------------------

def test_same_line_family_merges_keeping_highest_severity():
    fs = [_f("s.sh", scanner="sast", rule_id="SA-SH-003", severity="critical"),
          _f("s.sh", scanner="infra", rule_id="", title="Shell: Pipe-to-Shell Install", severity="high"),
          _f("s.sh", scanner="skill_threats", rule_id="ST-PR-001", severity="critical")]
    out = agg.merge_same_line_matches(fs)
    assert len(out) == 1 and out[0]["severity"] == "critical"
    assert out[0]["rule_id"] == "SA-SH-003"
    assert sorted(m["rule_id"] or m["title"] for m in out[0]["also_matched"]) == [
        "ST-PR-001", "Shell: Pipe-to-Shell Install"]


def test_different_threats_on_one_line_stay_separate():
    fs = [_f("a.py", rule_id="SC-SEC-007", title="OpenAI API Key"),
          _f("a.py", rule_id="SA-SH-003", title="Pipe to Shell Interpreter")]
    assert len(agg.merge_same_line_matches(fs)) == 2


def test_sarif_expands_merged_rules():
    import sarif_export
    f = _f("s.sh", rule_id="SA-SH-003")
    f["also_matched"] = [{"scanner": "skill_threats", "rule_id": "ST-PR-001",
                          "title": "Pipe-to-shell download pattern", "severity": "critical", "snippet": "x"}]
    sarif = sarif_export.report_to_sarif({"findings": [f], "suppressed": []})
    rule_ids = [r["ruleId"] for r in sarif["runs"][0]["results"]]
    assert len(rule_ids) == 2 and len(set(rule_ids)) == 2
    assert any("SA-SH-003" in r for r in rule_ids) and any("ST-PR-001" in r for r in rule_ids)



# --- one rule, many lines in one file ----------------------------------------------

def test_repeats_in_one_file_fold_with_every_line_listed():
    fs = [_f("m.py", line=n, snippet="s%d" % n) for n in (40, 5, 22)]
    out = agg.group_same_file_repeats(fs)
    assert len(out) == 1 and out[0]["line"] == 5
    assert [r["line"] for r in out[0]["also_lines"]] == [22, 40]
    assert [r["snippet"] for r in out[0]["also_lines"]] == ["s22", "s40"]


def test_strongest_repeat_is_kept_so_the_verdict_never_weakens():
    fs = [_f("m.py", line=1, severity="medium", confidence=0.9),
          _f("m.py", line=9, severity="critical", confidence=0.5),
          _f("m.py", line=4, severity="critical", confidence=0.95)]
    before = agg.build_verdicts(fs, [])
    out = agg.group_same_file_repeats([dict(f) for f in fs])
    assert out[0]["line"] == 4 and out[0]["severity"] == "critical"
    assert {r["line"]: r["severity"] for r in out[0]["also_lines"]} == {1: "medium", 9: "critical"}
    after = agg.build_verdicts(out, [])
    rank = {"info": 0, "warn": 1, "block": 2}
    assert max(rank[k] for k in rank if after[k]) == max(rank[k] for k in rank if before[k])


def test_higher_confidence_on_a_weaker_line_is_not_lost():
    fs = [_f("m.py", line=1, severity="medium", confidence=0.95),
          _f("m.py", line=2, severity="critical", confidence=0.5)]
    before = agg.build_verdicts(fs, [])
    out = agg.group_same_file_repeats([dict(f) for f in fs])
    assert out[0]["severity"] == "critical"
    assert agg.build_verdicts(out, [])["block"] == (1 if before["block"] else 0)


@pytest.mark.parametrize("change", [
    {"file": "other.py"}, {"rule_id": "R2"}, {"title": "u"}, {"scanner": "secrets"},
    {"category": "d"}, {"line": 0},
])
def test_repeats_only_fold_within_same_file_rule_and_title(change):
    out = agg.group_same_file_repeats([_f("m.py", line=3), _f("m.py", **dict({"line": 7}, **change))])
    assert len(out) == 2


def test_correlation_and_meta_never_fold():
    fs = [_f("m.py", scanner="correlation", line=n) for n in (1, 2)]
    fs += [_f("m.py", scanner="meta", line=n) for n in (1, 2)]
    assert len(agg.group_same_file_repeats(fs)) == 4


def test_sarif_expands_folded_lines_with_their_own_snippets():
    import sarif_export
    out = agg.group_same_file_repeats([_f("m.py", line=n, snippet="s%d" % n) for n in (3, 8)])
    out[0]["also_lines"][0]["also_in"] = ["copy/m.py"]
    sarif = sarif_export.report_to_sarif({"findings": out, "suppressed": []})
    got = sorted((r["locations"][0]["physicalLocation"]["artifactLocation"]["uri"],
                  r["locations"][0]["physicalLocation"]["region"]["startLine"],
                  r["locations"][0]["physicalLocation"]["region"]["snippet"]["text"])
                 for r in sarif["runs"][0]["results"])
    assert got == [("copy/m.py", 8, "s8"), ("m.py", 3, "s3"), ("m.py", 8, "s8")]


# --- hex digests in files that hash but never decode hex ---------------------------

_DIGEST = "558d7fcb9138afa090ccf16b23355edbf8b98f63c26151646bf1721a6969840a"


@pytest.mark.parametrize("call", ['.digest("hex")', ".toString('hex')"])
def test_named_digest_exempt_when_file_only_encodes_hex(call):
    import scan_entropy
    text = 'const INDEX_SHA256 = "%s";\nconst h = (b) => createHash("sha256").update(b)%s;\n' % (_DIGEST, call)
    assert scan_entropy._HEX_DECODER.search(text) is None


@pytest.mark.parametrize("call", ['Buffer.from(x, "hex")', "bytes.fromhex(x)", "codecs.decode(x, 'hex')"])
def test_files_that_decode_hex_are_still_never_exempted(call):
    import scan_entropy
    text = 'const INDEX_SHA256 = "%s";\nconst p = %s;\n' % (_DIGEST, call)
    assert scan_entropy._HEX_DECODER.search(text) is not None


def _warn_repeat(line, snippet, **kw):
    base = _f("SKILL.md", rule_id="ST-PE-004", title="Shell RC file modification",
              severity="high", confidence=0.7, line=line, snippet=snippet,
              finding_id="id%d" % line, description="d%d" % line)
    base.update(kw)
    return base


def test_adjudicator_sees_every_folded_line_not_just_the_decoy():
    import adjudication
    fs = [_warn_repeat(3, "edit the config object"), _warn_repeat(40, "echo evil >> ~/.bashrc")]
    agg.mark_adjudication(fs)
    block = adjudication.build_adjudication_block(agg.group_same_file_repeats(fs))
    assert "evil" in block and "edit the config object" in block


def test_too_many_folded_lines_cannot_be_persisted(monkeypatch):
    import adjudication
    monkeypatch.setattr(adjudication, "_persist_line", lambda f: "rf persist x")
    fs = [_warn_repeat(n, "s%d" % n) for n in range(1, adjudication.REPEAT_SNIPPET_CAP + 3)]
    agg.mark_adjudication(fs)
    block = adjudication.build_adjudication_block(agg.group_same_file_repeats(fs))
    assert "persist: rf persist x" not in block and "cannot be persisted, " not in block and "so this finding cannot be persisted" in block
    few = [_warn_repeat(n, "s%d" % n) for n in (1, 2)]
    agg.mark_adjudication(few)
    assert "persist: rf persist x" in adjudication.build_adjudication_block(agg.group_same_file_repeats(few))


def test_raised_confidence_leaving_warn_drops_the_adjudication_mark():
    fs = [_warn_repeat(1, "a", severity="high", confidence=0.70),
          _warn_repeat(2, "b", severity="medium", confidence=0.95)]
    agg.mark_adjudication(fs)
    out = agg.group_same_file_repeats(fs)
    assert agg.verdict_tier(agg._finding_confidence(out[0])) == "block"
    assert "needs_adjudication" not in out[0]


def test_sarif_repeat_rows_keep_their_own_identity():
    import sarif_export
    out = agg.group_same_file_repeats([_warn_repeat(3, "a"), _warn_repeat(40, "b")])
    row = out[0]["also_lines"][0]
    assert row["finding_id"] == "id40" and row["description"] == "d40"
    rows = sarif_export._expand_identical_copies(out)
    assert {(r["line"], r["finding_id"], r["description"]) for r in rows} == {
        (3, "id3", "d3"), (40, "id40", "d40")}
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