mirror of
https://github.com/norandom/OpenUltraSAST.git
synced 2026-12-03 02:08:18 +00:00
Where a scan writes. A native scan now creates its run under the user cache, ~/.cache/openultrasast/runs/<scan-id>/ by default ($XDG_CACHE_HOME when it is set and absolute), prints run_dir=<path> once, and no longer writes anything under the analysed tree. OPENULTRASAST_RUNS_DIR still wins: an absolute value is used as given, a relative one resolves against the working directory. Pre-push storage is unchanged. The calibration ledger, by maintainer decision: .openultrasast/calibration/ stays the store, and a scan writes the false-positive ledger into the tree only when its content would change. On a read-only tree the scan records ledger_not_written, with a sentence in the report, and completes; before, a read-only target with one rejected finding crashed before report.md existed. benchmarks/repos/measure.py follows the printed run directory. It read the run from under the scanned copy, which would have recorded every repository as failed, and it also read a stale run left in a copy. Tests: tests/test_scan_output_location.py drives the real CLI and compares the whole tree before and after; tests/test_repo_measure.py pins the measurement script; an autouse fixture keeps every test's scan runs out of the developer's cache; fourteen test files follow the run to its new place. README, docs/examples.md and the scan skill name the ledger as the one write a scan makes. _run_scan is touched by one line and stays over the complexity cap. Task 3.1 stays open for part B, the readable summary.
168 lines
6.5 KiB
Python
168 lines
6.5 KiB
Python
"""Fusion adjudication: trigger policy, two-panel decider, dispositions (Phase 13)."""
|
|
|
|
import json
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from openultrasast.findings import StaticFinding
|
|
from openultrasast.fusion import FusionDisposition, fuse_finding, fuse_findings, fusion_triggers
|
|
from openultrasast.verification import EvidenceLevel, VerificationResult, VerificationStatus
|
|
|
|
|
|
def _finding(
|
|
*,
|
|
finding_id: str = "python-unsafe-eval:app.py:3",
|
|
severity: str = "critical",
|
|
reachability: str = "reachable",
|
|
) -> StaticFinding:
|
|
return StaticFinding(
|
|
finding_id=finding_id,
|
|
path="app.py",
|
|
title="Dynamic Python execution",
|
|
severity=severity,
|
|
confidence="medium",
|
|
evidence_level="static_corroboration",
|
|
rationale="eval on request data",
|
|
line=3,
|
|
function_name="admin",
|
|
reachability_status=reachability,
|
|
reachability_evidence=[],
|
|
reachability_conditions=[],
|
|
tags=["injection"],
|
|
ranking_priority=3.0,
|
|
)
|
|
|
|
|
|
def _verification(status: VerificationStatus, *, verified: bool) -> VerificationResult:
|
|
return VerificationResult(
|
|
finding_id="python-unsafe-eval:app.py:3",
|
|
status=status,
|
|
evidence_level=EvidenceLevel.STATIC_CORROBORATION,
|
|
verified=verified,
|
|
pro_case="reachable tainted sink",
|
|
counter_case="input may be constant",
|
|
tie_breaker="confirm attacker control",
|
|
required_next_step="dynamic repro",
|
|
context_sources=["app.py"],
|
|
)
|
|
|
|
|
|
# ---- trigger policy ---------------------------------------------------------
|
|
|
|
|
|
def test_triggers_on_high_severity_and_reachable() -> None:
|
|
triggers = fusion_triggers(_finding(severity="critical", reachability="reachable"))
|
|
assert "severity:critical" in triggers
|
|
assert "gates_risky_fix" in triggers
|
|
|
|
|
|
def test_verifier_disagreement_triggers() -> None:
|
|
triggers = fusion_triggers(
|
|
_finding(severity="low", reachability="unknown"),
|
|
_verification(VerificationStatus.NEEDS_EVIDENCE, verified=False),
|
|
)
|
|
assert triggers == ["verifier_disagreement"]
|
|
|
|
|
|
def test_evidence_conflict_triggers_when_rejected_but_reachable() -> None:
|
|
triggers = fusion_triggers(
|
|
_finding(severity="low", reachability="reachable"),
|
|
_verification(VerificationStatus.REJECTED, verified=False),
|
|
)
|
|
assert "evidence_conflict" in triggers
|
|
|
|
|
|
# ---- deterministic two-panel decider + dispositions -------------------------
|
|
|
|
|
|
def test_accepted_when_reachable_and_verified() -> None:
|
|
decision = fuse_finding(_finding(), _verification(VerificationStatus.ACCEPTED, verified=True), ["severity:critical"])
|
|
assert decision.disposition == FusionDisposition.ACCEPTED
|
|
assert decision.decision_source == "deterministic-reconciler"
|
|
assert {p.role for p in decision.panels} == {"panel_a", "panel_b"}
|
|
assert sum(decision.votes.values()) == 2 # both panels voted
|
|
|
|
|
|
def test_blocked_when_reachable_high_severity_but_unverified() -> None:
|
|
decision = fuse_finding(_finding(), _verification(VerificationStatus.NEEDS_EVIDENCE, verified=False), ["gates_risky_fix"])
|
|
assert decision.disposition == FusionDisposition.BLOCKED
|
|
|
|
|
|
def test_rejected_when_verifier_rejects_unreachable() -> None:
|
|
decision = fuse_finding(
|
|
_finding(reachability="unknown"), _verification(VerificationStatus.REJECTED, verified=False), ["severity:critical"]
|
|
)
|
|
assert decision.disposition == FusionDisposition.REJECTED
|
|
|
|
|
|
def test_deferred_when_evidence_conflicts() -> None:
|
|
decision = fuse_finding(_finding(severity="low"), _verification(VerificationStatus.REJECTED, verified=False), ["evidence_conflict"])
|
|
assert decision.disposition == FusionDisposition.DEFERRED
|
|
|
|
|
|
def test_mitigated_when_finding_is_known_mitigated() -> None:
|
|
finding = _finding()
|
|
decision = fuse_finding(
|
|
finding,
|
|
_verification(VerificationStatus.ACCEPTED, verified=True),
|
|
["severity:critical"],
|
|
mitigated_ids=frozenset({finding.finding_id}),
|
|
)
|
|
assert decision.disposition == FusionDisposition.MITIGATED
|
|
|
|
|
|
# ---- dispatch + disclosure + serialization ----------------------------------
|
|
|
|
|
|
def test_dispatch_only_fuses_triggered_findings() -> None:
|
|
findings = [
|
|
_finding(finding_id="hot:app.py:3", severity="critical"),
|
|
_finding(finding_id="cold:lib.py:9", severity="low", reachability="unknown"),
|
|
]
|
|
decisions = fuse_findings(findings, [])
|
|
assert [d.finding_id for d in decisions] == ["hot:app.py:3"]
|
|
|
|
|
|
def test_decision_serializes_with_full_disclosure() -> None:
|
|
decision = fuse_finding(_finding(), _verification(VerificationStatus.ACCEPTED, verified=True), ["severity:critical"])
|
|
payload = json.loads(json.dumps(decision.to_dict()))
|
|
assert payload["disposition"] == "accepted"
|
|
assert payload["decision_source"] == "deterministic-reconciler"
|
|
assert "votes" in payload and "model_ids" in payload and "degradations" in payload
|
|
assert all("leaning" in panel and "disposition" in panel for panel in payload["panels"])
|
|
|
|
|
|
# ---- CLI integration: standard-mode fusion stage ----------------------------
|
|
|
|
|
|
def test_standard_scan_emits_fusion_artifact_and_manifest_block(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
from openultrasast.cli import main
|
|
|
|
repo = tmp_path / "repo"
|
|
repo.mkdir()
|
|
(repo / "app.py").write_text("@app.route('/admin')\ndef admin():\n return eval(request.data)\n")
|
|
monkeypatch.setenv("OPENULTRASAST_RUNS_DIR", str(repo / ".runs"))
|
|
assert main(["scan", str(repo), "--mode", "standard"]) == 0
|
|
|
|
run_dir = sorted((repo / ".runs").iterdir())[-1]
|
|
fusion = json.loads((run_dir / "fusion.json").read_text())
|
|
assert fusion and fusion[0]["disposition"] in {"accepted", "blocked", "deferred", "rejected", "mitigated"}
|
|
manifest = json.loads((run_dir / "manifest.json").read_text())
|
|
assert manifest["fusion"][0]["finding_id"] == fusion[0]["finding_id"]
|
|
assert "fusion" in manifest["artifacts"]
|
|
|
|
|
|
def test_quick_scan_does_not_emit_fusion(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
from openultrasast.cli import main
|
|
|
|
repo = tmp_path / "repo"
|
|
repo.mkdir()
|
|
(repo / "app.py").write_text("@app.route('/admin')\ndef admin():\n return eval(request.data)\n")
|
|
monkeypatch.setenv("OPENULTRASAST_RUNS_DIR", str(repo / ".runs"))
|
|
assert main(["scan", str(repo), "--mode", "quick"]) == 0
|
|
|
|
run_dir = sorted((repo / ".runs").iterdir())[-1]
|
|
assert not (run_dir / "fusion.json").exists()
|
|
assert "fusion" not in json.loads((run_dir / "manifest.json").read_text())
|