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

scripts/scan_git_forensics.py

View on GitHub
#!/usr/bin/env python3
"""
scan_git_forensics.py - Git History Forensics (v2: severity + GPG check)
Analyzes commit history for time anomalies, email inconsistencies,
and unsigned commits.

Created by Alex Greenshpun
"""

import sys
import os
import datetime
import json
import re
import stat

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import forensics_core as core

SCANNER_NAME = "git_forensics"


def get_git_log(repo_path):
    # Null byte delimiter prevents author-name spoofing with '|'.
    #
    # The pretty format deliberately does NOT include %G? (signature status):
    # %G? makes git verify each commit's signature, which executes the repo's
    # own gpg.program config value -- arbitrary code execution just from reading
    # a hostile repo's history (the scanner runs inside the untrusted tree). The
    # signature status of an untrusted checkout, verified against whatever keys
    # happen to be in the scanning machine's keyring, is not a meaningful signal
    # anyway (a real third-party signer's key is almost never present, so it
    # reports "cannot check", and an attacker simply leaves commits unsigned).
    # We drop it rather than trust config neutralization alone.
    #
    # The call still goes through the hardened runner, which overrides every
    # exec-capable config key (gpg.program, core.fsmonitor, core.hooksPath, ...)
    # so nothing the repo declares can run during `git log`.
    result = core.run_git_hardened(
        repo_path,
        "log", "--pretty=format:%H%x00%an%x00%ae%x00%aI%x00%cI", "-n", "1000",
    )
    if result is None or result.returncode != 0:
        return []
    return result.stdout.strip().split('\n')


def analyze_commits(commits, repo_path):
    findings = []
    authors = {}
    now = datetime.datetime.now(datetime.timezone.utc)

    for line in commits:
        try:
            parts = line.split('\x00')
            if len(parts) < 5:
                continue

            commit_hash = parts[0][:12]
            author_name = parts[1]
            author_email = parts[2]
            author_date_str = parts[3]
            committer_date_str = parts[4]

            if author_email not in authors:
                authors[author_email] = set()
            authors[author_email].add(author_name)

            a_date = datetime.datetime.fromisoformat(author_date_str)
            c_date = datetime.datetime.fromisoformat(committer_date_str)

            # Future dates
            if a_date > now + datetime.timedelta(days=1):
                findings.append(core.Finding(
                    scanner=SCANNER_NAME, severity="high",
                    title="Future Author Date",
                    description=f"Commit {commit_hash} has author date in the future",
                    file=f"commit:{commit_hash}", line=0,
                    snippet=f"Author date: {author_date_str}",
                    category="time-anomaly"
                ))

            # Time stomping (>30 day lag)
            delta = c_date - a_date
            if delta > datetime.timedelta(days=30):
                findings.append(core.Finding(
                    scanner=SCANNER_NAME, severity="medium",
                    title="Time Lag (>30 days)",
                    description="Large gap between author and committer dates",
                    file=f"commit:{commit_hash}", line=0,
                    snippet=f"Author: {author_date_str}, Commit: {committer_date_str}",
                    category="time-anomaly"
                ))

            # Impossible time
            if delta < datetime.timedelta(0):
                findings.append(core.Finding(
                    scanner=SCANNER_NAME, severity="high",
                    title="Impossible Time (Committer before Author)",
                    description="Committer date is before author date (time manipulation)",
                    file=f"commit:{commit_hash}", line=0,
                    snippet=f"Author: {author_date_str}, Commit: {committer_date_str}",
                    category="time-anomaly"
                ))

            # GPG signature status is intentionally not collected here: reading
            # it (%G?) makes git execute the repo's own gpg.program, an RCE
            # vector from an untrusted checkout, and the status is not a
            # trustworthy signal for a third-party repo anyway. See get_git_log.

        except (ValueError, IndexError):
            continue

    # Check for multiple names per email
    for email, names in authors.items():
        if len(names) > 2:
            findings.append(core.Finding(
                scanner=SCANNER_NAME, severity="medium",
                title="Multiple Identities per Email",
                description=f"Email '{email}' used by {len(names)} different author names",
                file="git-log", line=0,
                snippet=f"{email}: {', '.join(list(names)[:3])}",
                category="identity-anomaly"
            ))

    return findings


def scan_replace_refs(repo_path):
    """Detect git replace objects (refs/replace/*).

    Git replace objects silently rewrite what a commit hash resolves to,
    allowing an attacker to make a repo appear to have a clean history while
    serving a different object graph. This is a history-rewriting attack that
    bypasses normal git integrity checks unless --no-replace-objects is used.

    Detection: list any refs/replace/* refs via git for-each-ref. If any
    exist, report a critical finding.
    """
    findings = []
    result = core.run_git_hardened(repo_path, "for-each-ref", "refs/replace/")
    if result is None or result.returncode != 0:
        return findings
    output = result.stdout.strip()

    if output:
        ref_lines = [ln for ln in output.splitlines() if ln.strip()]
        ref_names = []
        for line in ref_lines:
            parts = line.split()
            if len(parts) >= 3:
                ref_names.append(parts[2])

        findings.append(core.Finding(
            scanner=SCANNER_NAME, severity="critical",
            title="Git Replace Objects Detected",
            description=(
                f"Repository contains {len(ref_lines)} git replace object(s) under "
                f"refs/replace/. Replace objects silently rewrite commit/tree/blob "
                f"resolution, enabling history forgery that bypasses normal git log "
                f"output. Use 'git log --no-replace-objects' to see unmodified history."
            ),
            file=".git/refs/replace/",
            line=0,
            snippet=(', '.join(ref_names[:5]) + (' ...' if len(ref_names) > 5 else ''))[:120],
            category="git-history-tampering"
        ))

    return findings


def scan_grafts(repo_path):
    """Detect presence of .git/info/grafts file.

    Grafts are a deprecated git mechanism that rewrites the apparent parentage
    of commits, allowing an attacker to detach part of the history or introduce
    fake merge ancestry. While superseded by replace objects, grafts still work
    in all git versions and are rarely present in legitimate repositories.

    Detection: check if .git/info/grafts exists and is non-empty.
    """
    findings = []
    dot_git = os.path.join(repo_path, '.git')
    if os.path.isfile(dot_git):
        try:
            with open(dot_git, 'r', encoding='utf-8', errors='ignore') as f:
                content = f.read().strip()
            if content.startswith('gitdir:'):
                dot_git = content[7:].strip()
                if not os.path.isabs(dot_git):
                    dot_git = os.path.join(repo_path, dot_git)
        except OSError:
            pass
    grafts_path = os.path.join(dot_git, 'info', 'grafts')

    if not os.path.isfile(grafts_path):
        return findings

    try:
        with open(grafts_path, 'r', encoding='utf-8', errors='ignore') as f:
            content = f.read().strip()
    except OSError:
        return findings

    if not content:
        return findings

    lines = [ln for ln in content.splitlines() if ln.strip() and not ln.startswith('#')]
    if not lines:
        return findings

    findings.append(core.Finding(
        scanner=SCANNER_NAME, severity="high",
        title="Git Grafts File Detected",
        description=(
            f".git/info/grafts exists with {len(lines)} graft(s). Grafts rewrite "
            f"commit parentage, enabling history falsification and detached ancestry "
            f"attacks. Grafts are deprecated (superseded by replace objects) and "
            f"are rarely present in legitimate repositories."
        ),
        file=".git/info/grafts",
        line=0,
        snippet=lines[0][:120],
        category="git-history-tampering"
    ))

    return findings



_SHA40_RE = re.compile(r"^[0-9a-fA-F]{40}$")
_AMBIGUOUS_REF_RE = re.compile(r"^(?:[0-9a-fA-F]{40}|FETCH_HEAD)$")
_PLUGIN_MARKERS = (
    ".claude-plugin", ".codex-plugin", ".agents/plugin.json",
    ".gemini/plugin.json", "openclaw.plugin.json",
)
_PIN_KEYS = frozenset({
    "sha", "commit", "commitsha", "commit_sha", "revision", "rev", "gitsha",
})
_PIN_METADATA_NAMES = frozenset({
    "plugin.json", "marketplace.json", "plugins.json", "extensions.json",
    "lock.json", "plugin-lock.json", "marketplace.lock.json",
})
_MAX_PROVENANCE_FILE_BYTES = 1024 * 1024
_MAX_METADATA_RECORDS = 50000
_MAX_PLUGIN_WALK_ENTRIES = 20000


def _read_regular_text(path, repo_path):
    """Read a bounded regular file whose resolved path stays in the repo."""
    if os.path.islink(path):
        return None
    try:
        if os.path.commonpath((os.path.realpath(path), os.path.realpath(repo_path))) != os.path.realpath(repo_path):
            return None
    except ValueError:
        return None
    try:
        fd = os.open(path, os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0))
    except OSError:
        return None
    try:
        with os.fdopen(fd, "rb") as source:
            info = os.fstat(source.fileno())
            if not stat.S_ISREG(info.st_mode) or info.st_size > _MAX_PROVENANCE_FILE_BYTES:
                return None
            data = source.read(_MAX_PROVENANCE_FILE_BYTES + 1)
    except OSError:
        return None
    if len(data) > _MAX_PROVENANCE_FILE_BYTES:
        return None
    return data.decode("utf-8", errors="ignore")


def _safe_git(repo_path, *args):
    """Run a read-only git command through the release hardened choke point."""
    result = core.run_git_hardened(repo_path, *args, check=True)
    return result.stdout.strip() if result is not None else None


def _is_agent_plugin_repo(repo_path):
    return any(os.path.exists(os.path.join(repo_path, marker))
               for marker in _PLUGIN_MARKERS)


def _plugin_identity(repo_path):
    """Return the installed plugin identity from its own manifest, if unique."""
    candidates = []
    for rel in (".claude-plugin/plugin.json", ".codex-plugin/plugin.json",
                ".agents/plugin.json", ".gemini/plugin.json", "openclaw.plugin.json"):
        path = os.path.join(repo_path, rel)
        try:
            text = _read_regular_text(path, repo_path)
            if text is None:
                continue
            data = json.loads(text)
        except (RecursionError, json.JSONDecodeError):
            continue
        if isinstance(data, dict):
            for key in ("name", "id", "pluginId", "plugin_id"):
                value = data.get(key)
                if isinstance(value, str) and value.strip():
                    candidates.append(value.strip().lower())
                    break
    unique = set(candidates)
    return next(iter(unique)) if len(unique) == 1 else None


def _record_identities(value):
    if not isinstance(value, dict):
        return set()
    out = set()
    for key in ("name", "id", "pluginId", "plugin_id", "package"):
        item = value.get(key)
        if isinstance(item, str) and item.strip():
            out.add(item.strip().lower())
    return out


def _record_pins(value):
    if not isinstance(value, dict):
        return set()
    pins = set()
    for key, item in value.items():
        if str(key).lower() in _PIN_KEYS and isinstance(item, str) and _SHA40_RE.fullmatch(item):
            pins.add(item.lower())
    return pins


def _iter_records(value):
    pending = [value]
    examined = 0
    while pending:
        examined += 1
        if examined > _MAX_METADATA_RECORDS:
            raise ValueError("plugin metadata record limit exceeded")
        item = pending.pop()
        if isinstance(item, dict):
            yield item
            pending.extend(reversed(list(item.values())))
        elif isinstance(item, list):
            pending.extend(reversed(item))


def _recover_recorded_pins(repo_path):
    """Resolve pins only from metadata records belonging to this plugin.

    A lockfile may contain many plugins. Pins from unrelated records must never
    make this checkout look valid. Missing or ambiguous identity is a coverage
    gap, not acceptance.
    """
    identity = _plugin_identity(repo_path)
    metadata_seen = False
    matching_records = []
    ambiguous_identity = False
    # The installed plugin's own manifest is authoritative for its own pin.
    own_manifest_seen = False
    for rel in (".claude-plugin/plugin.json", ".codex-plugin/plugin.json",
                ".agents/plugin.json", ".gemini/plugin.json", "openclaw.plugin.json"):
        path = os.path.join(repo_path, rel)
        if not os.path.lexists(path):
            continue
        own_manifest_seen = True
        try:
            text = _read_regular_text(path, repo_path)
            if text is None:
                ambiguous_identity = True
                continue
            own = json.loads(text)
        except (RecursionError, json.JSONDecodeError):
            ambiguous_identity = True
            continue
        if identity is not None and _record_pins(own):
            matching_records.append(own)
    if identity is None and own_manifest_seen:
        ambiguous_identity = True
    walked = 0
    for root, dirs, files in os.walk(repo_path):
        rel_root = os.path.relpath(root, repo_path)
        if rel_root.count(os.sep) > 3:
            dirs[:] = []
            continue
        dirs[:] = [d for d in dirs if d not in (".git", "node_modules", "vendor", ".venv")]
        walked += 1 + len(files)
        if walked > _MAX_PLUGIN_WALK_ENTRIES:
            ambiguous_identity = True
            break
        for name in files:
            if name.lower() not in _PIN_METADATA_NAMES or name.lower() == "plugin.json":
                continue
            path = os.path.join(root, name)
            metadata_seen = True
            try:
                text = _read_regular_text(path, repo_path)
                if text is None:
                    ambiguous_identity = True
                    continue
                data = json.loads(text)
                records = list(_iter_records(data))
            except (RecursionError, ValueError, json.JSONDecodeError):
                ambiguous_identity = True
                continue
            if identity is None:
                if any(_record_pins(record) for record in records):
                    ambiguous_identity = True
                continue
            for record in records:
                ids = _record_identities(record)
                if identity in ids:
                    matching_records.append(record)
    pins = set()
    for record in matching_records:
        pins.update(_record_pins(record))
    # Multiple matching records that disagree are ambiguous. Never accept any.
    if len(pins) > 1:
        return set(), metadata_seen, True
    if identity is None and metadata_seen:
        ambiguous_identity = True
    if identity is not None and metadata_seen and not matching_records:
        ambiguous_identity = True
    return pins, metadata_seen, ambiguous_identity

def scan_plugin_checkout_provenance(repo_path):
    """Detect ambiguous Git refs and pin mismatches in agent plugin checkouts."""
    if not _is_agent_plugin_repo(repo_path):
        return []
    findings = []
    refs = _safe_git(repo_path, "for-each-ref", "--format=%(refname:short)", "refs/heads/")
    if refs is None:
        findings.append(core.Finding(
            scanner=SCANNER_NAME, severity="medium",
            title="Agent Plugin Git Provenance Unavailable",
            description="Plugin checkout has no readable Git refs; commit provenance cannot be verified.",
            file=".git", line=0, snippet="Git refs unavailable",
            category="coverage-gap", evidence_class="direct"))
        return findings
    ambiguous = sorted({r.strip() for r in refs.splitlines()
                        if _AMBIGUOUS_REF_RE.fullmatch(r.strip())})
    for ref in ambiguous:
        findings.append(core.Finding(
            scanner=SCANNER_NAME, severity="critical",
            title="Agent Plugin Ambiguous Git Ref",
            description=("Agent plugin repository contains a local branch whose name "
                         "can override commit-pin resolution during checkout."),
            file=f".git/refs/heads/{ref}", line=0, snippet=ref,
            category="plugin-provenance", evidence_class="direct"))

    head = _safe_git(repo_path, "rev-parse", "HEAD")
    if head is None:
        findings.append(core.Finding(
            scanner=SCANNER_NAME, severity="medium",
            title="Agent Plugin Git Provenance Unavailable",
            description="Plugin checkout has no readable HEAD; commit provenance cannot be verified.",
            file=".git/HEAD", line=0, snippet="Git HEAD unavailable",
            category="coverage-gap", evidence_class="direct"))
    symbolic = _safe_git(repo_path, "symbolic-ref", "--quiet", "--short", "HEAD")
    if symbolic and _AMBIGUOUS_REF_RE.fullmatch(symbolic):
        findings.append(core.Finding(
            scanner=SCANNER_NAME, severity="critical",
            title="Agent Plugin Checked Out on Ambiguous Ref",
            description=("Installed agent plugin HEAD is attached to an ambiguous branch "
                         "instead of a verified detached commit."),
            file=".git/HEAD", line=0, snippet=f"HEAD -> {symbolic}",
            category="plugin-provenance", evidence_class="direct"))

    pins, metadata_seen, ambiguous_identity = _recover_recorded_pins(repo_path)
    if pins and head and head.lower() not in pins:
        findings.append(core.Finding(
            scanner=SCANNER_NAME, severity="critical",
            title="Agent Plugin Checkout Does Not Match Recorded Pin",
            description=(f"Resolved HEAD {head[:12]} does not match any recorded plugin "
                         "commit pin. The installed tree may have been substituted."),
            file=".git/HEAD", line=0,
            snippet=f"HEAD={head}; pins={','.join(sorted(pins))[:80]}",
            category="plugin-provenance", evidence_class="direct"))
    elif pins and symbolic:
        findings.append(core.Finding(
            scanner=SCANNER_NAME, severity="critical",
            title="SHA-Pinned Agent Plugin Is Not Detached",
            description=("A plugin with a recorded commit pin is checked out on a symbolic "
                         "branch. Hash-pinned installs must use detached HEAD and verify it."),
            file=".git/HEAD", line=0, snippet=f"HEAD -> {symbolic}",
            category="plugin-provenance", evidence_class="direct"))
    elif (metadata_seen and not pins) or ambiguous_identity:
        findings.append(core.Finding(
            scanner=SCANNER_NAME, severity="medium",
            title="Agent Plugin Provenance Pin Unavailable",
            description=("Plugin metadata was found, but no explicit 40-hex commit pin could "
                         "be recovered; checkout provenance could not be verified."),
            file="plugin-metadata", line=0, snippet=("ambiguous plugin identity/pin" if ambiguous_identity else "no explicit commit pin"),
            category="coverage-gap", evidence_class="direct"))
    return findings


def scan_plugin_installers(repo_path):
    """Detect installer checkout flows that trust ref resolution without verifying HEAD."""
    if not _is_agent_plugin_repo(repo_path):
        return []
    findings = []
    skipped = []
    walked = 0
    for root, dirs, files in os.walk(repo_path):
        dirs[:] = [d for d in dirs if d not in (".git", "node_modules", "vendor", ".venv")]
        walked += 1 + len(files)
        if walked > _MAX_PLUGIN_WALK_ENTRIES:
            skipped.append("plugin tree exceeds scan entry limit")
            break
        for name in files:
            if not name.endswith((".sh", ".bash", ".zsh", ".js", ".jsx", ".ts", ".tsx", ".py")):
                continue
            path = os.path.join(root, name)
            text = _read_regular_text(path, repo_path)
            rel = os.path.relpath(path, repo_path)
            if text is None:
                skipped.append(rel)
                continue
            pin_name = r"(?:pinned_?sha|commit_?sha|sha|revision)"
            # Shell and common subprocess/exec representations. We require git,
            # the relevant subcommand, and the pin/FETCH_HEAD in one bounded call.
            has_fetch_pin = bool(re.search(
                rf"git[^\n]{{0,100}}fetch[^\n]{{0,100}}{pin_name}", text, re.I))
            checkout_fetch = bool(re.search(
                r"git[^\n]{0,100}checkout[^\n]{0,60}FETCH_HEAD", text, re.I))
            checkout_pin = bool(re.search(
                rf"git[^\n]{{0,100}}checkout[^\n]{{0,80}}{pin_name}", text, re.I))
            has_rev_parse = bool(re.search(
                r"git[^\n]{0,100}rev-parse[^\n]{0,40}HEAD", text, re.I))
            has_compare = bool(re.search(
                rf"(?:!=|===?|equals?\s*\(|compare_digest\s*\()[^\n]{{0,100}}{pin_name}|{pin_name}[^\n]{{0,100}}(?:!=|===?|equals?\s*\(|compare_digest\s*\()",
                text, re.I))
            has_abort = bool(re.search(
                r"(?:exit\s*\(?[1-9]|process\.exit\s*\(\s*[1-9]|abort|raise|throw)",
                text, re.I))
            verifies = has_rev_parse and has_compare and has_abort
            if has_fetch_pin and checkout_fetch and not verifies:
                findings.append(core.Finding(
                    scanner=SCANNER_NAME, severity="critical",
                    title="Agent Plugin Installer Trusts Ambiguous FETCH_HEAD",
                    description=("Installer fetches a pinned revision then checks out FETCH_HEAD "
                                 "without verifying resolved HEAD matches the pin."),
                    file=rel, line=0, snippet="git fetch ...; git checkout FETCH_HEAD",
                    category="plugin-provenance", evidence_class="direct"))
            elif checkout_pin and not verifies:
                findings.append(core.Finding(
                    scanner=SCANNER_NAME, severity="high",
                    title="Agent Plugin Installer Does Not Verify Resolved Commit",
                    description=("Installer checks out a commit-pin variable but does not abort "
                                 "unless git rev-parse HEAD exactly matches the requested pin."),
                    file=rel, line=0, snippet="git checkout <pin> without HEAD verification",
                    category="plugin-provenance", evidence_class="direct"))
    if skipped:
        findings.append(core.Finding(
            scanner=SCANNER_NAME, severity="medium",
            title="Agent Plugin Installer Source Not Scanned",
            description=f"{len(skipped)} source paths could not be scanned because of input limits or unreadable files.",
            file=skipped[0], line=0, snippet="installer source skipped",
            category="coverage-gap", evidence_class="direct"))
    return findings


def main():
    args = core.parse_common_args(sys.argv, "Git History Forensics")
    repo_path = args.repo_path

    core.emit_status(args.format, f"[*] Analyzing Git History in {repo_path}...")

    commits = get_git_log(repo_path)
    findings = []
    if commits and commits != ['']:
        findings.extend(analyze_commits(commits, repo_path))
    else:
        core.emit_status(args.format, "[-] No git history found; scanning installer source only.")
    findings.extend(scan_replace_refs(repo_path))
    findings.extend(scan_grafts(repo_path))
    findings.extend(scan_plugin_checkout_provenance(repo_path))
    findings.extend(scan_plugin_installers(repo_path))

    core.emit_status(args.format, f"[+] Analyzed {len(commits)} recent commits.")
    core.output_findings(findings, args.format, SCANNER_NAME)


if __name__ == "__main__":
    main()
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