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
refactor(workbench): simplify report formatting and scan completion (#1366)
mldangelo-oai168552ee3fc2
diff --git a/plugins/codex-security/scripts/finalize_scan_contract.py b/plugins/codex-security/scripts/finalize_scan_contract.pyindex 60a44238b..e8466e087 100644--- a/plugins/codex-security/scripts/finalize_scan_contract.py+++ b/plugins/codex-security/scripts/finalize_scan_contract.py@@ -18,7 +18,6 @@ import secretsimport statimport structimport sys-import timefrom datetime import datetime, timezonefrom pathlib import Path, PurePosixPathfrom typing import Any, TextIO@@ -91,7 +90,7 @@ class ContractError(ValueError):class RecoverableContractError(ContractError):- """Raised when report projection can safely be retried before publication."""+ """Raised when scan completion can safely be retried before publication."""class SealedArtifactError(ContractError):@@ -132,21 +131,10 @@ def _generate_report_projection(raise ContractError(f"could not load report projection helper: {script}")module = importlib.util.module_from_spec(spec)spec.loader.exec_module(module)- attempts = (- getattr(sys.modules.get("workbench_constants"), "SQLITE_RETRY_ATTEMPTS", 1)- if coverage.get("mode") == "deep_repository"- else 1- )- for attempt in range(attempts):- try:- return module.generate_report_markdown(manifest, findings, coverage)- except OSError as exc:- if attempt == attempts - 1:- raise RecoverableContractError(f"report projection failed: {exc}") from exc- time.sleep(0.05 * (2**attempt))- except ValueError as exc:- raise ContractError(f"report projection failed: {exc}") from exc- raise AssertionError("Report projection retry loop exhausted unexpectedly.")+ try:+ return module.generate_report_markdown(manifest, findings, coverage)+ except ValueError as exc:+ raise ContractError(f"report projection failed: {exc}") from excdef _threat_model_renderer() -> Any:@@ -2440,7 +2428,7 @@ def _github_line_hash_cache(return line_hash_cache-def _sarif_location(location: dict[str, Any], location_id: int | None = None) -> dict[str, Any]:+def _sarif_location(location: dict[str, Any]) -> dict[str, Any]:sarif_location: dict[str, Any] = {"physicalLocation": {"artifactLocation": {@@ -2452,8 +2440,6 @@ def _sarif_location(location: dict[str, Any], location_id: int | None = None) ->},}}- if location_id is not None:- sarif_location["id"] = location_idif location.get("role"):sarif_location["message"] = {"text": location["role"]}return sarif_location@@ -2482,7 +2468,7 @@ def _sarif_result(line_hash = _github_primary_location_line_hash(finding, source_root, line_hash_cache)if line_hash is not None:partial_fingerprints["primaryLocationLineHash"] = line_hash- result = {+ return {"ruleId": finding["ruleId"],"ruleIndex": rule_index,"level": SARIF_LEVELS[finding["severity"]["level"]],@@ -2491,7 +2477,6 @@ def _sarif_result("partialFingerprints": partial_fingerprints,"properties": properties,}- return resultdef build_sarif(diff --git a/plugins/codex-security/scripts/report_projection.py b/plugins/codex-security/scripts/report_projection.pyindex 25eff4c61..b068a28dd 100644--- a/plugins/codex-security/scripts/report_projection.py+++ b/plugins/codex-security/scripts/report_projection.py@@ -28,8 +28,6 @@ class ReportProjectionError(ValueError):def _text(value: Any, fallback: str) -> str:candidate = value if isinstance(value, str) and value.strip() else fallbacknormalized = " ".join(candidate.split())- if not normalized:- return ""if re.match(r"^(?:#{1,6}\s|[-*+]\s|>\s|```|\d+\.\s|\|)", normalized):normalized = f"Text: {normalized}"return "".join(@@ -43,12 +41,7 @@ def _strings(value: Any) -> list[str]:value = [value]if not isinstance(value, list):return []- normalized: list[str] = []- for item in value:- text = _text(item, "")- if text:- normalized.append(text)- return normalized+ return [text for item in value if (text := _text(item, ""))]def _cell(value: Any) -> str:@@ -284,8 +277,6 @@ def merged_root_cause(value: dict[str, Any]) -> tuple[str | None, Any]:details.append({"summary": detail})elif isinstance(detail, dict):details.append(detail)- elif detail is not None:- continueif not details:return keys[0], Nonediff --git a/plugins/codex-security/tests/test_workbench_completion_binding.py b/plugins/codex-security/tests/test_workbench_completion_binding.pyindex 8413c2d8a..66abae97c 100644--- a/plugins/codex-security/tests/test_workbench_completion_binding.py+++ b/plugins/codex-security/tests/test_workbench_completion_binding.py@@ -905,105 +905,6 @@ def test_deep_completion_rejects_invalid_target_even_with_recoverable_inventory(assert len(json.loads((scan_dir / "findings.json").read_text())["findings"]) == 1-def test_deep_completion_preserves_running_scan_after_transient_report_failure(- tmp_path: Path,-) -> None:- state_dir, scan_id, scan_dir = _start_deep_scan_with_draft_findings(tmp_path)- hook_dir = tmp_path / "report-failure-hook"- hook_dir.mkdir()- (hook_dir / "sitecustomize.py").write_text(- "import importlib.util\n"- "original_spec = importlib.util.spec_from_file_location\n"- "def injected_spec(name, *args, **kwargs):\n"- " spec = original_spec(name, *args, **kwargs)\n"- " if name == 'codex_security_report_projection':\n"- " original_load = spec.loader.exec_module\n"- " def injected_load(module):\n"- " original_load(module)\n"- " def unavailable(*args, **kwargs):\n"- " raise OSError('fixture report projection temporarily unavailable')\n"- " module.generate_report_markdown = unavailable\n"- " spec.loader.exec_module = injected_load\n"- " return spec\n"- "importlib.util.spec_from_file_location = injected_spec\n"- )- before = {- name: (scan_dir / name).read_bytes()- for name in ("scan-manifest.json", "findings.json", "coverage.json", "report.md")- }-- failed = run_workbench(- state_dir,- "complete-scan",- "--scan-id",- scan_id,- check=False,- environment={"PYTHONPATH": str(hook_dir)},- )-- assert failed["returncode"] != 0- assert "fixture report projection temporarily unavailable" in str(failed["stderr"])- preserved = run_workbench(state_dir, "get-scan", "--scan-id", scan_id)["scan"]- assert preserved["progress"]["status"] == "running"- with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:- assert connection.execute(- "SELECT status FROM deep_scan_runs WHERE scan_id = ?", (scan_id,)- ).fetchone() == ("succeeded",)- assert {- name: (scan_dir / name).read_bytes()- for name in ("scan-manifest.json", "findings.json", "coverage.json", "report.md")- } == before-- completed = run_workbench(state_dir, "complete-scan", "--scan-id", scan_id)-- assert completed["scan"]["progress"]["status"] == "complete"- assert completed["scan"]["findingCount"] == 1---def test_deep_completion_retries_transient_report_failure_within_one_invocation(- tmp_path: Path,-) -> None:- state_dir, scan_id, scan_dir = _start_deep_scan_with_draft_findings(tmp_path)- hook_dir = tmp_path / "report-failure-hook"- hook_dir.mkdir()- marker_path = hook_dir / "first-projection-attempt"- (hook_dir / "sitecustomize.py").write_text(- "import importlib.util\n"- "from pathlib import Path\n"- f"marker = Path({str(marker_path)!r})\n"- "original_spec = importlib.util.spec_from_file_location\n"- "def injected_spec(name, *args, **kwargs):\n"- " spec = original_spec(name, *args, **kwargs)\n"- " if name == 'codex_security_report_projection':\n"- " original_load = spec.loader.exec_module\n"- " def injected_load(module):\n"- " original_load(module)\n"- " original_report = module.generate_report_markdown\n"- " def unavailable_once(*args, **kwargs):\n"- " if not marker.exists():\n"- " marker.touch()\n"- " raise OSError('fixture report projection temporarily unavailable')\n"- " return original_report(*args, **kwargs)\n"- " module.generate_report_markdown = unavailable_once\n"- " spec.loader.exec_module = injected_load\n"- " return spec\n"- "importlib.util.spec_from_file_location = injected_spec\n"- )-- completed = run_workbench(- state_dir,- "complete-scan",- "--scan-id",- scan_id,- environment={"PYTHONPATH": str(hook_dir)},- )-- assert marker_path.is_file()- assert completed["scan"]["progress"]["status"] == "complete"- assert completed["scan"]["findingCount"] == 1- assert (scan_dir / "report.md").is_file()--@pytest.mark.parametrize("mode", ["standard", "deep"])@pytest.mark.parametrize("already_completed", [False, True])def test_completion_persists_optional_model_projection_warnings(