OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security
TypeScript ai-securityapplication-securityclicode-scanningcodexcodex-securitycybersecuritydevsecopsnodejsnpmopenaisdksecuritytypescriptvulnerability-scanning
fix(plugin): calculate diff digests at completion (#1040)
mldangelo-oai8c78e6553215
diff --git a/plugins/codex-security/references/scan-contract.md b/plugins/codex-security/references/scan-contract.mdindex 5b449af8c..c5c144991 100644--- a/plugins/codex-security/references/scan-contract.md+++ b/plugins/codex-security/references/scan-contract.md@@ -65,7 +65,7 @@ A dirty checkout has `allowedKinds: ["git_worktree"]`: copy `requiredSnapshotDig`targetId` identifies the stable repository or workspace. Prefer a digest of a sanitized canonical absolute remote URL when one exists. Otherwise use a digest of a stable local workspace identity. Never persist remote URL credentials, query parameters, fragments, or tokens.-For dirty worktrees and working-tree diffs, calculate `snapshotDigest` from a deterministic representation of the reviewed content, including staged changes and reviewed untracked files where applicable. For committed or revision-range diffs, derive it from the exact authoritative diff kind and immutable base/head revisions. For directory snapshots, hash a sorted relative-path and file-hash inventory of the reviewed scope. Encode the result as `codex-security-snapshot/v1:sha256:<64 lowercase hex characters>`.+For dirty worktrees and working-tree diffs, calculate `snapshotDigest` from a deterministic representation of the reviewed content, including staged changes and reviewed untracked files where applicable. For committed or revision-range diffs, derive `snapshotDigest` from the authoritative diff kind and immutable base/head revisions. The workbench supplies it during finalization, so workbench-backed unsealed drafts may omit it. Previously sealed manifests retain their recorded digest. For directory snapshots, hash a sorted relative-path and file-hash inventory of the reviewed scope. Encode the result as `codex-security-snapshot/v1:sha256:<64 lowercase hex characters>`.## Finding Identitydiff --git a/plugins/codex-security/scripts/workbench_db.py b/plugins/codex-security/scripts/workbench_db.pyindex 282cd0c76..994df97fc 100644--- a/plugins/codex-security/scripts/workbench_db.py+++ b/plugins/codex-security/scripts/workbench_db.py@@ -116,6 +116,7 @@ from workbench_target import (clean_worktree_content_digest,copy_directory_excluding,copy_git_worktree_files,+ diff_snapshot_digest,directory_content_digest,directory_snapshot_regular_file_count,git_bytes,@@ -514,8 +515,8 @@ def workbench_completion_binding(if scan["mode"] == "diff":target["baseRevision"] = scan["diff_base_revision"]target["headRevision"] = scan["diff_head_revision"]- if scan["diff_target_kind"] == "working_tree" and scan["diff_content_digest"]:- target["snapshotDigest"] = scan["diff_content_digest"]+ if snapshot_digest := diff_snapshot_digest(scan, manifest):+ target["snapshotDigest"] = snapshot_digestelse:if scan["target_revision"] != "unversioned":target["revision"] = scan["target_revision"]diff --git a/plugins/codex-security/scripts/workbench_saved_results.py b/plugins/codex-security/scripts/workbench_saved_results.pyindex f5572f0c9..bad999a32 100644--- a/plugins/codex-security/scripts/workbench_saved_results.py+++ b/plugins/codex-security/scripts/workbench_saved_results.py@@ -37,6 +37,7 @@ from finalize_scan_contract import (write_scan_local_bytes,)from workbench_constants import PHASES+from workbench_target import committed_diff_snapshot_digestfrom workbench_validation import path_within_scope_PUBLISHED_OUTPUTS = (@@ -651,15 +652,9 @@ def merge_saved_results(target = {"kind": target_kind, **binding["target"]}if target["kind"] == "git_diff" and "snapshotDigest" not in target:diff_kind = {"commit": "commit", "branch_diff": "range"}[binding["coverageMode"]]- digest = hashlib.sha256(- b"codex-security-diff/v1\0"- + diff_kind.encode()- + b"\0"- + target["baseRevision"].encode()- + b"\0"- + target["headRevision"].encode()- ).hexdigest()- target["snapshotDigest"] = f"codex-security-snapshot/v1:sha256:{digest}"+ target["snapshotDigest"] = committed_diff_snapshot_digest(+ diff_kind, target["baseRevision"], target["headRevision"]+ )manifest = (copy.deepcopy(parent_manifest)if parent_manifest@@ -1190,6 +1185,7 @@ def preserve_scan_results_locked(existing_path = db.artifact_path(scan_dir, db.ARTIFACTS["manifest"], required=False)existing_scan = db.read_json_object(existing_path).get("scan", {}) if existing_path else {}+ existing = Noneif scan["seal_manifest_digest"] is not None or (isinstance(existing_scan, dict)and (@@ -1221,7 +1217,10 @@ def preserve_scan_results_locked(return Trueif recovery_source_digests is None:raise ContractError("Stopped scan sources changed after terminal publication.")- binding = {**db.workbench_completion_binding(scan, scan["completed_at"]), "status": outcome}+ binding = {+ **db.workbench_completion_binding(scan, scan["completed_at"], existing),+ "status": outcome,+ }documents = merge_saved_results(scan_dir,scan_id,diff --git a/plugins/codex-security/scripts/workbench_target.py b/plugins/codex-security/scripts/workbench_target.pyindex b380a275f..824c2a058 100644--- a/plugins/codex-security/scripts/workbench_target.py+++ b/plugins/codex-security/scripts/workbench_target.py@@ -20,6 +20,30 @@ from filesystem_identity import stored_filesystem_identity_matchesfrom workbench_constants import GIT_REPOSITORY_ENVIRONMENT+def committed_diff_snapshot_digest(kind: str, base_revision: str, head_revision: str) -> str:+ digest = hashlib.sha256(+ f"codex-security-diff/v1\0{kind}\0{base_revision}\0{head_revision}".encode()+ ).hexdigest()+ return f"codex-security-snapshot/v1:sha256:{digest}"+++def diff_snapshot_digest(scan: sqlite3.Row, manifest: dict[str, Any] | None) -> str | None:+ if scan["diff_target_kind"] == "working_tree":+ return scan["diff_content_digest"]+ if scan["diff_target_kind"] in {"commit", "range"}:+ manifest_scan = manifest.get("scan") if manifest is not None else None+ # Preserve the recorded digest when recovery rebuilds a sealed manifest.+ if isinstance(manifest_scan, dict) and (+ manifest_scan.get("sealedAt") is not None or manifest_scan.get("artifacts")+ ):+ target = manifest_scan.get("target")+ return target.get("snapshotDigest") if isinstance(target, dict) else None+ return committed_diff_snapshot_digest(+ scan["diff_target_kind"], scan["diff_base_revision"], scan["diff_head_revision"]+ )+ return None++def git_output(target: Path,*args: str,diff --git a/plugins/codex-security/tests/test_workbench_completion_binding.py b/plugins/codex-security/tests/test_workbench_completion_binding.pyindex dbcdafd9f..2dbb0e644 100644--- a/plugins/codex-security/tests/test_workbench_completion_binding.py+++ b/plugins/codex-security/tests/test_workbench_completion_binding.py@@ -1,6 +1,7 @@from __future__ import annotationsimport copy+import hashlibimport jsonimport sqlite3import subprocess@@ -9,6 +10,7 @@ import uuidfrom pathlib import Pathfrom typing import Any+import pytestfrom workbench_test_support import (create_saved_workspace,initialize_git_repository,@@ -17,6 +19,7 @@ from workbench_test_support import (source_plugin_version,stable_target_id,start_delivered_scan,+ write_checkpoint,write_completed_contract,)@@ -69,7 +72,13 @@ def _start_deep_scan_with_draft_findings(tmp_path: Path) -> tuple[Path, str, Patreturn state_dir, scan_id, scan_dir-def register_cli_scan(state_dir: Path, target: Path, scan_dir: Path) -> dict[str, Any]:+def register_cli_scan(+ state_dir: Path,+ target: Path,+ scan_dir: Path,+ *,+ scan_target: dict[str, Any] | None = None,+) -> dict[str, Any]:scan_dir.mkdir(mode=0o700)return run_workbench(state_dir,@@ -84,12 +93,193 @@ def register_cli_scan(state_dir: Path, target: Path, scan_dir: Path) -> dict[str"config": {},"mode": "standard","repository": str(target),- "target": {"kind": "repository", "paths": []},+ "target": scan_target or {"kind": "repository", "paths": []},}),)+def _start_diff_scan_with_draft(tmp_path: Path, kind: str) -> tuple[Path, Path, dict[str, Any]]:+ state_dir = tmp_path / "state"+ target = tmp_path / "target"+ base = initialize_git_repository(target)+ (target / "README.md").write_text("changed fixture\n")+ subprocess.run(["git", "commit", "-qam", "Update fixture"], cwd=target, check=True)+ head = subprocess.run(+ ["git", "rev-parse", "HEAD"], cwd=target, check=True, capture_output=True, text=True+ ).stdout.strip()+ if kind == "commit":+ workspace_id = str(uuid.uuid4())+ for command in ("create-workspace", "save-workspace"):+ run_workbench(+ state_dir,+ command,+ "--workspace-id",+ workspace_id,+ "--target-path",+ str(target),+ "--scope",+ ".",+ "--mode",+ "diff",+ "--diff-target-kind",+ kind,+ "--diff-head-revision",+ head,+ )+ registered = start_delivered_scan(+ state_dir,+ "--workspace-id",+ workspace_id,+ "--scan-root",+ str(tmp_path / "scans"),+ )["results"]+ else:+ if kind == "working_tree":+ (target / "README.md").write_text("uncommitted fixture\n")+ base = head+ registered = register_cli_scan(+ state_dir,+ target,+ tmp_path / "scan",+ scan_target={+ "kind": "refs" if kind == "range" else "working_tree",+ "paths": [],+ "base": base,+ "head": head,+ },+ )+ diff_target = registered["contract"]["diffTarget"]+ write_completed_contract(+ Path(registered["scanDir"]),+ registered["scanId"],+ target,+ relative_path="README.md",+ target_kind="git_diff",+ diff_base_revision=diff_target["baseRevision"],+ diff_head_revision=diff_target["headRevision"],+ snapshot_digest=f"codex-security-snapshot/v1:sha256:{'a' * 64}",+ coverage_mode="branch_diff" if kind == "range" else kind,+ inventory_strategy="diff",+ )+ return state_dir, target, registered+++def _seal_draft(scan_dir: Path, target: Path) -> None:+ subprocess.run(+ [+ sys.executable,+ str(Path(__file__).resolve().parent.parent / "scripts" / "finalize_scan_contract.py"),+ "--scan-dir",+ str(scan_dir),+ "--source-root",+ str(target),+ ],+ capture_output=True,+ check=True,+ text=True,+ )+++def _sealed_artifacts(scan_dir: Path) -> dict[str, bytes]:+ return {+ name: (scan_dir / name).read_bytes()+ for name in ("scan-manifest.json", "findings.json", "coverage.json", "report.md")+ }++[email protected]("kind", ["commit", "range", "working_tree"])[email protected]("draft_digest", [None, "incorrect-draft-digest"])+def test_completion_binds_diff_snapshot_digest(+ tmp_path: Path, kind: str, draft_digest: str | None+) -> None:+ state_dir, _, registered = _start_diff_scan_with_draft(tmp_path, kind)+ scan_id, scan_dir = registered["scanId"], Path(registered["scanDir"])+ diff_target = registered["contract"]["diffTarget"]+ manifest_path = scan_dir / "scan-manifest.json"+ manifest = json.loads(manifest_path.read_text())+ if draft_digest is None:+ manifest["scan"]["target"].pop("snapshotDigest")+ else:+ manifest["scan"]["target"]["snapshotDigest"] = draft_digest+ manifest_path.write_text(json.dumps(manifest))++ prepared = run_workbench(state_dir, "prepare-scan-completion", "--scan-id", scan_id)++ assert prepared["scan"]["progress"]["status"] == "running"+ if kind == "working_tree":+ expected = diff_target["contentDigest"]+ else:+ digest = hashlib.sha256(+ f"codex-security-diff/v1\0{kind}\0{diff_target['baseRevision']}\0"+ f"{diff_target['headRevision']}".encode()+ ).hexdigest()+ expected = f"codex-security-snapshot/v1:sha256:{digest}"+ sealed_manifest = json.loads(manifest_path.read_text())+ assert sealed_manifest["scan"]["target"]["snapshotDigest"] == expected+ assert sealed_manifest["scan"]["sealedAt"]+ sealed_artifacts = _sealed_artifacts(scan_dir)+ for _ in range(2):+ completed = run_workbench(state_dir, "complete-scan", "--scan-id", scan_id)+ assert completed["scan"]["progress"]["status"] == "complete"+ assert _sealed_artifacts(scan_dir) == sealed_artifacts++[email protected]("kind", ["commit", "range"])+def test_completion_preserves_legacy_sealed_diff_snapshot_digest(tmp_path: Path, kind: str) -> None:+ state_dir, target, registered = _start_diff_scan_with_draft(tmp_path, kind)+ scan_id, scan_dir = registered["scanId"], Path(registered["scanDir"])+ _seal_draft(scan_dir, target)+ sealed_artifacts = _sealed_artifacts(scan_dir)+ for command in ("prepare-scan-completion", "complete-scan", "complete-scan"):+ run_workbench(state_dir, command, "--scan-id", scan_id)+ assert _sealed_artifacts(scan_dir) == sealed_artifacts++[email protected]("kind", ["commit", "range"])+def test_stopped_recovery_preserves_legacy_diff_snapshot(tmp_path: Path, kind: str) -> None:+ state_dir, target, registered = _start_diff_scan_with_draft(tmp_path, kind)+ scan_id, scan_dir = registered["scanId"], Path(registered["scanDir"])+ legacy_digest = f"codex-security-snapshot/v1:sha256:{'a' * 64}"+ manifest_path = scan_dir / "scan-manifest.json"+ manifest = json.loads(manifest_path.read_text())+ manifest["scan"]["status"] = "failed"+ manifest_path.write_text(json.dumps(manifest))+ _seal_draft(scan_dir, target)+ original_findings = json.loads((scan_dir / "findings.json").read_text())["findings"]+ with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:+ connection.execute(+ "UPDATE scans SET status = 'failed', completed_at = ?, failure_message = ? WHERE id = ?",+ (manifest["scan"]["completedAt"], "Scan stopped.", scan_id),+ )++ recovered = run_workbench(state_dir, "recover-scan-results", "--scan-id", scan_id)++ assert recovered["scan"]["progress"]["status"] == "failed"+ manifest = json.loads(manifest_path.read_text())+ assert manifest["scan"]["target"]["snapshotDigest"] == legacy_digest+ assert manifest["scan"]["preservedSources"]+ assert json.loads((scan_dir / "findings.json").read_text())["findings"] == original_findings+ sealed_artifacts = _sealed_artifacts(scan_dir)+ run_workbench(state_dir, "recover-scan-results", "--scan-id", scan_id)+ assert _sealed_artifacts(scan_dir) == sealed_artifacts++ late_review = {"id": "late-review", "reason": "Review remains pending.", "paths": ["README.md"]}+ write_checkpoint(+ scan_dir / "checkpoints",+ {"scanId": scan_id, "findings": [], "coverage": {"deferred": [late_review]}},+ )+ run_workbench(state_dir, "recover-scan-results", "--scan-id", scan_id)++ manifest = json.loads(manifest_path.read_text())+ assert manifest["scan"]["target"]["snapshotDigest"] == legacy_digest+ assert json.loads((scan_dir / "findings.json").read_text())["findings"] == original_findings+ assert late_review in json.loads((scan_dir / "coverage.json").read_text())["deferred"]+ sealed_artifacts = _sealed_artifacts(scan_dir)+ run_workbench(state_dir, "recover-scan-results", "--scan-id", scan_id)+ assert _sealed_artifacts(scan_dir) == sealed_artifacts++def test_cli_registration_returns_authoritative_target_contract(tmp_path: Path) -> None:state_dir = tmp_path / "state"@@ -230,19 +420,7 @@ def test_cli_completion_accepts_sealed_clean_git_revision_without_snapshot_digesmanifest = json.loads(manifest_path.read_text())manifest["scan"]["target"].pop("snapshotDigest")manifest_path.write_text(json.dumps(manifest))- subprocess.run(- [- sys.executable,- str(Path(__file__).resolve().parent.parent / "scripts" / "finalize_scan_contract.py"),- "--scan-dir",- str(scan_dir),- "--source-root",- str(target),- ],- capture_output=True,- check=True,- text=True,- )+ _seal_draft(scan_dir, target)completed = run_workbench(state_dir, "complete-scan", "--scan-id", scan_id)diff --git a/plugins/codex-security/tests/test_workbench_db.py b/plugins/codex-security/tests/test_workbench_db.pyindex 9cb0301e5..5e8c8a9c1 100644--- a/plugins/codex-security/tests/test_workbench_db.py+++ b/plugins/codex-security/tests/test_workbench_db.py@@ -3428,7 +3428,6 @@ def test_workbench_populates_completed_manifest_with_exact_diff_target(tmp_path:)draft_manifest = json.loads((scan_dir / "scan-manifest.json").read_text())draft_target = draft_manifest["scan"]["target"]- authored_snapshot_digest = draft_target["snapshotDigest"]draft_target["revision"] = "stale-revision"(scan_dir / "scan-manifest.json").write_text(json.dumps(draft_manifest))completed = run_workbench(state_dir, "complete-scan", "--scan-id", scan_id)@@ -3437,7 +3436,14 @@ def test_workbench_populates_completed_manifest_with_exact_diff_target(tmp_path:assert "revision" not in manifest["scan"]["target"]assert manifest["scan"]["target"]["baseRevision"] == diff_target["baseRevision"]assert manifest["scan"]["target"]["headRevision"] == diff_target["headRevision"]- assert manifest["scan"]["target"]["snapshotDigest"] == authored_snapshot_digest+ expected_digest = hashlib.sha256(+ f"codex-security-diff/v1\0commit\0{diff_target['baseRevision']}\0"+ f"{diff_target['headRevision']}".encode()+ ).hexdigest()+ assert (+ manifest["scan"]["target"]["snapshotDigest"]+ == f"codex-security-snapshot/v1:sha256:{expected_digest}"+ )assert manifest["scan"]["scope"] == {"includePaths": ["."], "excludePaths": []}