mirror of
https://github.com/norandom/OpenUltraSAST.git
synced 2026-12-03 18:28:19 +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.
163 lines
6.3 KiB
Python
163 lines
6.3 KiB
Python
"""Scan-pipeline integrity & serialization (Phase 4 task 9).
|
|
|
|
9.1 — quick and standard scans run through the sync driver with the deterministic
|
|
hunter, verifier and fusion; a configured hunter model never changes the
|
|
deterministic findings.
|
|
9.2 — every persisted scan-state slot serializes and restores without loss (guards
|
|
against a non-serializable slot silently restoring as empty).
|
|
"""
|
|
|
|
import json
|
|
from dataclasses import asdict
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from openultrasast.benchmark import load_findings
|
|
from openultrasast.cli import main
|
|
from openultrasast.findings import StaticFinding, write_findings
|
|
from openultrasast.preprocess import FileTarget
|
|
from openultrasast.rank import RankingScore
|
|
from openultrasast.scoring import build_score_artifact
|
|
from openultrasast.verification import EvidenceLevel, VerificationResult, VerificationStatus
|
|
|
|
_VULN = "@app.route('/admin')\ndef admin():\n return eval(request.data)\n"
|
|
|
|
|
|
# ---- 9.1 deterministic parity + degradation --------------------------------------
|
|
|
|
|
|
def _scan(tmp_path: Path, monkeypatch: pytest.MonkeyPatch, *, mode: str, config_body: str | None) -> tuple[list, dict]:
|
|
repo = tmp_path / "repo"
|
|
repo.mkdir(parents=True)
|
|
(repo / "app.py").write_text(_VULN)
|
|
monkeypatch.setenv("OPENULTRASAST_RUNS_DIR", str(repo / ".runs"))
|
|
monkeypatch.delenv("OPENROUTER_API_KEY", raising=False)
|
|
monkeypatch.delenv("OPENULTRASAST_HUNTER_CLIENT", raising=False)
|
|
argv = ["scan", str(repo), "--mode", mode]
|
|
if config_body is not None:
|
|
cfg = tmp_path / "openultrasast.toml"
|
|
cfg.write_text(config_body)
|
|
argv += ["--config", str(cfg)]
|
|
assert main(argv) == 0
|
|
run_dir = sorted((repo / ".runs").iterdir())[-1]
|
|
findings = json.loads((run_dir / "findings.json").read_text())["findings"]
|
|
manifest = json.loads((run_dir / "manifest.json").read_text())
|
|
return findings, manifest
|
|
|
|
|
|
def test_standard_scan_with_a_hunter_model_keeps_deterministic_findings(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
# A configured hunter model (no client reachable here) must leave the deterministic findings byte-identical.
|
|
requested, requested_manifest = _scan(tmp_path / "a", monkeypatch, mode="standard", config_body='[models]\nhunter = "openai/gpt-4o"\n')
|
|
baseline, baseline_manifest = _scan(tmp_path / "b", monkeypatch, mode="standard", config_body=None)
|
|
|
|
assert requested == baseline
|
|
# The hunter model runs the tool hunter (its client is absent here, so it contributes nothing) and removes the
|
|
# `map` skip; `model` records that no CPG engine was available (contributor-scan Req 1.3).
|
|
assert requested_manifest["degradations"] == [{"reason": "cpg_unavailable", "stage": "model"}]
|
|
assert {(e["stage"], e["reason"]) for e in baseline_manifest["degradations"]} == {
|
|
("model", "cpg_unavailable"),
|
|
("map", "hunter_model_unavailable"),
|
|
}
|
|
|
|
|
|
def test_quick_scan_records_no_degradation(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
# Quick mode never touches a model: findings are produced and no degradation is recorded.
|
|
findings, manifest = _scan(tmp_path, monkeypatch, mode="quick", config_body=None)
|
|
|
|
assert findings
|
|
assert "degradations" not in manifest
|
|
|
|
|
|
# ---- 9.2 per-slot serialization round-trip ----------------------------------
|
|
|
|
|
|
def _finding() -> StaticFinding:
|
|
return StaticFinding(
|
|
finding_id="python-unsafe-eval:app.py:3",
|
|
path="app.py",
|
|
title="Dynamic Python execution",
|
|
severity="critical",
|
|
confidence="medium",
|
|
evidence_level="static_corroboration",
|
|
rationale="eval on request data",
|
|
line=3,
|
|
function_name="admin",
|
|
reachability_status="reachable",
|
|
reachability_evidence=[{"kind": "route", "access_level": "public", "line": 1, "end_line": 3}],
|
|
reachability_conditions=["public route"],
|
|
tags=["syscall_entry", "injection"],
|
|
ranking_priority=3.0,
|
|
)
|
|
|
|
|
|
def _ranking() -> RankingScore:
|
|
return RankingScore(
|
|
path="app.py",
|
|
surface=4,
|
|
influence=3,
|
|
reachability=4,
|
|
priority=3.5,
|
|
rationale="entry",
|
|
model_id=None,
|
|
static_boosts=["input_parser"],
|
|
)
|
|
|
|
|
|
def _file_target() -> FileTarget:
|
|
return FileTarget(
|
|
path="app.py",
|
|
absolute_path="/repo/app.py",
|
|
language="python",
|
|
loc=42,
|
|
tags=["network_entry"],
|
|
has_fuzz_entry_point=False,
|
|
static_hints=[{"rule": "x", "line": 1}],
|
|
reachability_hints=[{"kind": "route", "line": 1}],
|
|
)
|
|
|
|
|
|
def _verification() -> VerificationResult:
|
|
return VerificationResult(
|
|
finding_id="python-unsafe-eval:app.py:3",
|
|
status=VerificationStatus.ACCEPTED,
|
|
evidence_level=EvidenceLevel.STATIC_CORROBORATION,
|
|
verified=True,
|
|
pro_case="reachable tainted sink",
|
|
counter_case="needs dynamic confirmation",
|
|
tie_breaker="confirm attacker control",
|
|
required_next_step="dynamic repro",
|
|
context_sources=["app.py", "routes"],
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"obj", [_finding(), _ranking(), _file_target(), _verification()], ids=["finding", "ranking", "file_target", "verification"]
|
|
)
|
|
def test_scan_slot_json_round_trips_without_loss(obj: object) -> None:
|
|
restored = type(obj)(**json.loads(json.dumps(asdict(obj))))
|
|
assert restored == obj
|
|
|
|
|
|
def test_score_artifact_round_trips() -> None:
|
|
from openultrasast.policy import load_policy
|
|
|
|
finding = _finding()
|
|
artifact = build_score_artifact([finding], {finding.finding_id: "CWE-95"}, load_policy())
|
|
payload = json.loads(json.dumps(artifact.to_dict()))
|
|
assert payload == artifact.to_dict() # serialization is total and stable
|
|
assert payload["project_score"] == artifact.project_score
|
|
|
|
|
|
def test_findings_reload_preserves_nested_slots(tmp_path: Path) -> None:
|
|
# The real resume path (benchmark reloads findings.json); nested collection
|
|
# slots must survive rather than restore as empty.
|
|
path = tmp_path / "findings.json"
|
|
write_findings([_finding()], path)
|
|
(restored,) = load_findings(path)
|
|
|
|
assert restored == _finding()
|
|
assert restored.reachability_evidence == [{"kind": "route", "access_level": "public", "line": 1, "end_line": 3}]
|
|
assert restored.reachability_conditions == ["public route"]
|
|
assert restored.tags == ["syscall_entry", "injection"]
|