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/sarif_export.py

View on GitHub
#!/usr/bin/env python3
"""sarif_export.py - SARIF 2.1.0 converter for repo-forensics reports.

Converts the aggregated report dict produced by aggregate_json.build_report()
into a SARIF 2.1.0 document (GitHub code-scanning compatible). Additive and
pure-stdlib: the text/json/summary paths never import this module.
aggregate_json imports it lazily inside the --sarif branch only, so the
rule_loader dependency (used to build the rule-descriptor index) stays out of
the text/json paths and honors the KTD-14 leaf rule (rule_loader is a leaf;
this module is the only aggregator-side importer of it, and rule_loader never
imports this module).

Mapping spec: see design doc §1.2-1.4.
"""

import hashlib
import json
import os
import re
import urllib.parse
from pathlib import Path

# Resolved from the plugin manifests at import time so the SARIF tool version
# can never drift from the release version again (it went stale at 2.15.0 and
# 2.16.0 while hardcoded). The manifests sit three levels above this file in
# both the source checkout and installed plugin layouts. The literal fallback
# keeps standalone use (skill copied without manifests) working; it is pinned
# to .claude-plugin/plugin.json by test_sarif_export.
def _resolve_tool_version():
    root = Path(__file__).resolve().parents[3]
    for rel in (".claude-plugin/plugin.json", ".kimi-plugin/plugin.json",
                ".codex-plugin/plugin.json"):
        manifest = root / rel
        try:
            if manifest.is_file():
                version = json.loads(manifest.read_text(encoding="utf-8"))["version"]
                if version:
                    return version
        except (OSError, ValueError, KeyError):
            continue
    # Never a real release version -- if this fires, no manifest was found at
    # all (a standalone skill copy), and hardcoding one here would just be
    # the next value to go stale.
    return "unknown"


TOOL_VERSION = _resolve_tool_version()

TOOL_NAME = "repo-forensics"
INFORMATION_URI = "https://github.com/alexgreensh/repo-forensics"
SARIF_SCHEMA_URI = "https://json.schemastore.org/sarif-2.1.0.json"
_SARIF_SPEC_VERSION = "2.1.0"
SARIF_VERSION = _SARIF_SPEC_VERSION

# severity -> SARIF level (forensics_core.py:20 vocabulary + info/unknown).
# critical/high -> error, medium -> warning, low/info/unknown -> note.
_SEVERITY_TO_LEVEL = {
    "critical": "error",
    "high": "error",
    "medium": "warning",
    "low": "note",
    "info": "note",
}

# Confidence -> GitHub precision (mirrors VERDICT_BLOCK_MIN/WARN_MIN in
# aggregate_json.py:28-30).
def _precision(confidence):
    try:
        c = float(confidence)
    except (TypeError, ValueError):
        c = 0.0
    if c >= 0.92:
        return "high"
    if c >= 0.60:
        return "medium"
    return "low"


def _category_slug(category):
    slug = re.sub(r"[^a-z0-9]+", "-", (category or "general").lower()).strip("-")
    return slug or "general"


def _normalize_uri(file_path):
    r"""Normalize a relative path into a SARIF artifactLocation.uri.

    SARIF uris are relative to the SRCROOT uriBaseId: never absolute, never
    scheme-prefixed, and never containing a `..` segment that could escape
    SRCROOT.

    Platform handling (path-traversal & literal-backslash hardening):
      - Windows (os.sep == '\\'): the native separator is converted to `/`
        (SARIF requires forward slashes). Other backslashes are NOT treated
        as separators; they are percent-encoded as literal data (%5C).
      - POSIX (os.sep == '/'): a backslash is a literal filename character,
        never a separator. It is percent-encoded as %5C. Only the real `/`
        is kept as a path separator. (Previously the code rewrote `\`->`/`
        unconditionally, which on POSIX turned a literal backslash in a
        filename into a phantom directory separator and let `..\..\x`
        become a `..` traversal that escaped SRCROOT.)

    All URI-unsafe characters are percent-encoded per RFC 3986
    (urllib.parse.quote with safe='/') so spaces become %20, literal
    backslashes become %5C, etc., while real `/` separators are preserved.

    Traversal guard: after normalization, leading `../` segments are
    stripped and any remaining `..` segment forces a fallback to the bare
    basename. A leading `/` or scheme is also stripped. A location that
    cannot be safely expressed relative to SRCROOT falls back to the bare
    basename, never a `..` path."""
    if not isinstance(file_path, str):
        file_path = ""
    if not file_path:
        return ""
    path = file_path
    # Strip a leading scheme (defensive — never emit absolute URIs).
    if "://" in path:
        path = path.split("://", 1)[-1]
    # Windows: convert the native separator to `/`. POSIX: leave backslashes
    # as literal data (percent-encoded below as %5C, never a separator).
    if os.sep == "\\":
        path = path.replace("\\", "/")
    # Percent-encode URI-unsafe characters per RFC 3986; keep real `/`.
    norm = urllib.parse.quote(path, safe="/")
    # Strip a leading slash (absolute path) — URIs are repo-relative.
    norm = norm.lstrip("/")
    # Traversal guard: strip leading `../` segments so a repo-relative URI
    # can never climb out of SRCROOT.
    while norm.startswith("../"):
        norm = norm[3:]
    # If any `..` segment remains anywhere (e.g. `a/../../b`), fall back to
    # the bare basename so the URI stays within SRCROOT.
    segments = norm.split("/")
    if any(seg == ".." for seg in segments):
        base = segments[-1]
        norm = base if base and base != ".." else ""
    return norm


def _build_rule_index():
    """Build {rule_id: CompiledRule} across the seven shipped packs plus the
    YARA manifest's rule_meta. Lazy rule_loader import (leaf rule). Returns
    (pack_index, yara_index) where pack_index maps rule.id -> (rule, pack_name,
    pack_version) and yara_index maps YR-* id -> rule_meta dict."""
    pack_index = {}
    try:
        import rule_loader
    except ImportError:
        return pack_index, {}
    for name in ("secrets", "sast", "skill_threats", "mcp_security",
                 "runtime_dynamism", "dead_anchors", "shared"):
        try:
            pack = rule_loader.load_pack(name)
        except Exception:
            pack = None
        if pack is None:
            continue
        for rule in pack.all_rules:
            if rule.id:
                pack_index[rule.id] = (rule, pack.name, pack.pack_version)

    yara_index = {}
    try:
        manifest_path = os.path.normpath(os.path.join(
            os.path.dirname(os.path.realpath(__file__)), "..", "data", "yara",
            "manifest.json"))
        with open(manifest_path, "r", encoding="utf-8") as handle:
            manifest = __import__("json").load(handle)
        rule_meta = manifest.get("rule_meta", {}) if isinstance(manifest, dict) else {}
        if isinstance(rule_meta, dict):
            for rule_name, meta in rule_meta.items():
                if isinstance(meta, dict) and meta.get("id"):
                    yara_index[meta["id"]] = meta
    except (OSError, ValueError, KeyError):
        pass
    return pack_index, yara_index


def _synthesize_rule_id(scanner, category):
    """Synthesized descriptor id for findings with no pack rule_id.
    rf/<scanner>/<category-slug>; correlation -> rf/correlation/<slug>;
    meta -> rf/meta/<slug>. Category strings are code constants, so these ids
    are stable across runs even when titles are reworded."""
    return "rf/{}/{}".format(scanner or "general", _category_slug(category))


def _rule_descriptor(rule_id, finding, pack_index, yara_index):
    """Build one reportingDescriptor entry for `rule_id` using the first-seen
    `finding` for synthesized descriptors."""
    pack_entry = pack_index.get(rule_id)
    yara_meta = yara_index.get(rule_id)

    if pack_entry is not None:
        rule, pack_name, pack_version = pack_entry
        props = {
            "category": rule.category,
            "confidence": rule.confidence,
            "pack": pack_name,
            "packVersion": pack_version,
            "precision": _precision(rule.confidence),
            "tags": [rule.category] if rule.category else [],
        }
        threat = {}
        if rule.attacker or rule.boundary or rule.asset:
            threat = {
                "attacker": rule.attacker or "",
                "boundary": rule.boundary or "",
                "asset": rule.asset or "",
            }
        if threat:
            props["threatModel"] = threat
        desc = {
            "id": rule_id,
            "name": rule_id,
            "shortDescription": {"text": rule.title or rule_id},
            "fullDescription": {"text": rule.explanation or rule.title or rule_id},
            "defaultConfiguration": {"level": _SEVERITY_TO_LEVEL.get(rule.severity, "note")},
            "properties": props,
        }
        return desc

    if yara_meta is not None:
        props = {
            "category": yara_meta.get("category", ""),
            "confidence": yara_meta.get("confidence", 0.0),
            "pack": "yara",
            "packVersion": 1,
            "precision": _precision(yara_meta.get("confidence", 0.0)),
            "tags": [yara_meta.get("category", "")] if yara_meta.get("category") else [],
        }
        threat = {}
        for key in ("attacker", "boundary", "asset"):
            if yara_meta.get(key):
                threat[key] = yara_meta[key]
        if threat:
            props["threatModel"] = threat
        desc = {
            "id": rule_id,
            "name": rule_id,
            "shortDescription": {"text": yara_meta.get("title", rule_id)},
            "fullDescription": {"text": yara_meta.get("explanation", yara_meta.get("title", rule_id))},
            "defaultConfiguration": {"level": _SEVERITY_TO_LEVEL.get(yara_meta.get("severity", "low"), "note")},
            "properties": props,
        }
        return desc

    # Synthesized descriptor for non-pack / empty rule_id findings.
    scanner = finding.get("scanner", "")
    category = finding.get("category", "")
    syn_id = rule_id if rule_id else _synthesize_rule_id(scanner, category)
    title = finding.get("title", "") or syn_id
    description = finding.get("description", "") or title
    confidence = finding.get("confidence", 0.0)
    props = {
        "category": category,
        "confidence": confidence,
        "precision": _precision(confidence),
        "synthesized": True,
        "tags": [category] if category else [],
    }
    threat = {}
    for key in ("attacker", "boundary", "asset"):
        if finding.get(key):
            threat[key] = finding[key]
    if threat:
        props["threatModel"] = threat
    desc = {
        "id": syn_id,
        "name": syn_id,
        "shortDescription": {"text": title},
        "fullDescription": {"text": description},
        "defaultConfiguration": {"level": _SEVERITY_TO_LEVEL.get(finding.get("severity", "low"), "note")},
        "properties": props,
    }
    return desc


def _result_rule_id(finding):
    """The ruleId for a result. Pack rule_id when non-empty; else synthesized
    rf/<scanner>/<category-slug> (correlation/meta get their own namespace)."""
    rule_id = finding.get("rule_id", "") or ""
    if rule_id:
        return rule_id
    scanner = finding.get("scanner", "")
    category = finding.get("category", "")
    return _synthesize_rule_id(scanner, category)


def _expand_identical_copies(findings):
    """The human report folds byte-identical copies into one finding with
    `also_in`, and repeats on other lines into `also_lines`. Code scanning
    tracks alerts per file and line, so SARIF gets one result per path and
    line again and existing alert fingerprints stay stable."""
    lines = []
    for finding in findings:
        repeats = finding.get("also_lines") or []
        head = {k: v for k, v in finding.items() if k != "also_lines"}
        lines.append(head)
        for repeat in repeats:
            row = {k: v for k, v in head.items() if k not in ("also_in", "also_matched")}
            row.update(repeat)
            lines.append(row)
    out = []
    for finding in lines:
        others = finding.get("also_in") or []
        base = {k: v for k, v in finding.items() if k not in ("also_in", "also_matched")}
        variants = [base] + [dict(base, **{k: m.get(k) for k in ("scanner", "rule_id", "title", "severity", "snippet")})
                             for m in finding.get("also_matched") or []]
        for variant in variants:
            out.append(variant)
            for path in others:
                copy = dict(variant)
                copy["file"] = path
                out.append(copy)
    return out


def _build_result(finding, occurrence_index):
    """Build one SARIF result object from a finding dict."""
    rule_id = _result_rule_id(finding)
    severity = finding.get("severity", "low") or "low"
    level = _SEVERITY_TO_LEVEL.get(severity, "note")

    title = finding.get("title", "") or ""
    description = finding.get("description", "") or ""
    message_text = "{} — {}".format(title, description) if description else title

    file_path = finding.get("file", "") or ""
    uri = _normalize_uri(file_path)

    physical = {"artifactLocation": {"uri": uri, "uriBaseId": "SRCROOT"}}
    line = finding.get("line", 0) or 0
    try:
        line = int(line)
    except (TypeError, ValueError):
        line = 0
    snippet = finding.get("snippet", "") or ""
    if line > 0:
        region = {"startLine": line}
        if snippet:
            region["snippet"] = {"text": snippet}
        physical["region"] = region
    elif snippet:
        # No region: stash snippet in properties so the evidence is not lost.
        pass

    result = {
        "ruleId": rule_id,
        "level": level,
        "message": {"text": message_text},
        "locations": [{"physicalLocation": physical}],
        "partialFingerprints": {
            "repoForensics/findingId": finding.get("finding_id", "") or "",
            "repoForensics/stableLocationV1": _stable_location(
                rule_id, finding, occurrence_index),
        },
        "properties": {
            "scanner": finding.get("scanner", "") or "",
            "category": finding.get("category", "") or "",
            "confidence": finding.get("confidence", 0.0),
        },
    }

    if line > 0 and snippet:
        # already in region.snippet
        pass
    elif snippet:
        result["properties"]["snippet"] = snippet

    ev_class = finding.get("evidence_class", "") or ""
    if ev_class:
        result["properties"]["evidenceClass"] = ev_class
    orig_sev = finding.get("original_severity", "") or ""
    if orig_sev:
        result["properties"]["originalSeverity"] = orig_sev
    freshness = finding.get("freshness_status", "") or ""
    if freshness:
        result["properties"]["freshnessStatus"] = freshness
    if finding.get("needs_adjudication"):
        result["properties"]["needsAdjudication"] = True

    return result


def _stable_location(rule_id, finding, occurrence_index):
    """sha1(ruleId ␟ scanner ␟ file ␟ category ␟ occurrenceIndex)[:16].
    Survives snippet rotation; the ordinal disambiguates multiple occurrences
    of the same rule in one file so GitHub does not collapse distinct
    findings."""
    parts = [
        rule_id,
        finding.get("scanner", "") or "",
        finding.get("file", "") or "",
        finding.get("category", "") or "",
        str(occurrence_index),
    ]
    fingerprint = "\x1f".join(parts)
    return hashlib.sha1(fingerprint.encode("utf-8", "replace")).hexdigest()[:16]


def _attach_suppression(result):
    result["suppressions"] = [{
        "kind": "external",
        "justification": "Suppressed via .forensicsignore rule directive",
    }]


def report_to_sarif(report):
    """Convert an aggregate_json.build_report() dict into a SARIF 2.1.0 dict."""
    pack_index, yara_index = _build_rule_index()

    active = _expand_identical_copies(report.get("findings", []) or [])
    suppressed = report.get("suppressed", []) or []

    # Occurrence index: 0-based ordinal of the finding among all findings
    # sharing (ruleId, file), ordered by (line, snippet). Computed across
    # active+suppressed together so indices are stable regardless of
    # suppression state.
    all_for_index = list(active) + list(suppressed)
    occ_counter = {}
    occ_map = {}
    # Group by (ruleId, file) then assign ordinal by (line, snippet) order.
    groups = {}
    for finding in all_for_index:
        rid = _result_rule_id(finding)
        key = (rid, finding.get("file", "") or "")
        groups.setdefault(key, []).append(finding)
    for key, group in groups.items():
        group.sort(key=lambda f: (
            int(f.get("line", 0) or 0),
            (f.get("snippet", "") or "")[:80],
        ))
        for idx, finding in enumerate(group):
            occ_map[id(finding)] = idx

    results = []
    descriptor_ids = set()
    descriptor_order = []

    for finding in active:
        occ = occ_map.get(id(finding), 0)
        result = _build_result(finding, occ)
        results.append(result)
        rid = result["ruleId"]
        if rid not in descriptor_ids:
            descriptor_ids.add(rid)
            descriptor_order.append((rid, finding))

    for finding in suppressed:
        occ = occ_map.get(id(finding), 0)
        result = _build_result(finding, occ)
        _attach_suppression(result)
        results.append(result)
        rid = result["ruleId"]
        if rid not in descriptor_ids:
            descriptor_ids.add(rid)
            descriptor_order.append((rid, finding))

    rules = []
    for rid, finding in descriptor_order:
        rules.append(_rule_descriptor(rid, finding, pack_index, yara_index))
    # Dedup by id (defensive) and sort by id for deterministic output.
    seen = set()
    deduped = []
    for desc in rules:
        if desc["id"] in seen:
            continue
        seen.add(desc["id"])
        deduped.append(desc)
    deduped.sort(key=lambda d: d["id"])

    target = report.get("target", "") or ""
    srcroot_uri = "file://{}/".format(target.rstrip("/")) if target else "file://./"

    run = {
        "tool": {
            "driver": {
                "name": TOOL_NAME,
                "version": TOOL_VERSION,
                "semanticVersion": TOOL_VERSION,
                "informationUri": INFORMATION_URI,
                "rules": deduped,
            }
        },
        "originalUriBaseIds": {
            "SRCROOT": {"uri": srcroot_uri}
        },
        "results": results,
        "properties": {
            "mode": report.get("mode", "") or "",
            "exitCode": report.get("exit_code", 99),
            "summary": report.get("summary", {}),
            "coreVerdictTier": (report.get("core_verdict") or {}).get("tier", ""),
        },
    }

    sarif = {
        "$schema": SARIF_SCHEMA_URI,
        "version": SARIF_VERSION,
        "runs": [run],
    }
    return sarif
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