Skip to content

OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security

fix(plugin): keep saved-results source within import limit (#1891)

Michael D'Angelo2c8e6a02a7d7
diff --git a/plugins/codex-security/plugin-files.json b/plugins/codex-security/plugin-files.json
index e9fe70b06..b34ac93cf 100644
--- a/plugins/codex-security/plugin-files.json
+++ b/plugins/codex-security/plugin-files.json
@@ -81,6 +81,7 @@
"scripts/workbench/handoff.py",
"scripts/workbench/runtime_toml.py",
"scripts/workbench/saved_coverage.py",
+ "scripts/workbench/saved_outputs.py",
"scripts/workbench/storage.py",
"scripts/workbench_cli.py",
"scripts/workbench_constants.py",
diff --git a/plugins/codex-security/scripts/workbench/saved_outputs.py b/plugins/codex-security/scripts/workbench/saved_outputs.py
new file mode 100644
index 000000000..bb9910a9a
--- /dev/null
+++ b/plugins/codex-security/scripts/workbench/saved_outputs.py
@@ -0,0 +1,72 @@
+"""Snapshot and restore scan outputs around publication transactions."""
+
+from __future__ import annotations
+
+import os
+from pathlib import Path
+
+from finalize_scan_contract import (
+ ContractError,
+ _remove_scan_local_file_if_exists,
+ open_scan_local_file_descriptor,
+ write_scan_local_bytes,
+)
+
+_PUBLISHED_OUTPUTS = (
+ "findings.json",
+ "coverage.json",
+ "scan-manifest.json",
+ "report.md",
+ "threatmodel.md",
+ "report.html",
+ "exports/results.sarif",
+)
+
+
+def _snapshot_published_outputs(
+ scan_dir: Path, relative_paths: tuple[str, ...] = _PUBLISHED_OUTPUTS
+) -> dict[str, tuple[bytes, os.stat_result] | None]:
+ snapshots: dict[str, tuple[bytes, os.stat_result] | None] = {}
+ for relative in relative_paths:
+ descriptor = -1
+ try:
+ descriptor = open_scan_local_file_descriptor(
+ scan_dir, relative, "Published scan output"
+ )
+ with os.fdopen(descriptor, "rb") as handle:
+ descriptor = -1
+ metadata = os.fstat(handle.fileno())
+ snapshots[relative] = (handle.read(), metadata)
+ except ContractError:
+ path = scan_dir / relative
+ if path.exists() or path.is_symlink():
+ if relative == "threatmodel.md":
+ # This optional projection will not replace an unsafe destination.
+ continue
+ raise
+ snapshots[relative] = None
+ finally:
+ if descriptor >= 0:
+ os.close(descriptor)
+ return snapshots
+
+
+def _restore_published_outputs(
+ scan_dir: Path,
+ snapshots: dict[str, tuple[bytes, os.stat_result] | None],
+ created_paths: set[str] | None = None,
+) -> None:
+ for relative, snapshot in snapshots.items():
+ if snapshot is None:
+ path = scan_dir / relative
+ if path.exists() or path.is_symlink():
+ _remove_scan_local_file_if_exists(scan_dir, relative)
+ else:
+ contents, metadata = snapshot
+ write_scan_local_bytes(scan_dir, relative, contents)
+ os.utime(scan_dir / relative, ns=(metadata.st_atime_ns, metadata.st_mtime_ns))
+ for relative in sorted(created_paths or ()):
+ # A journaled write can fail before creating its parent directory.
+ path = scan_dir / relative
+ if path.exists() or path.is_symlink():
+ _remove_scan_local_file_if_exists(scan_dir, relative)
diff --git a/plugins/codex-security/scripts/workbench_saved_results.py b/plugins/codex-security/scripts/workbench_saved_results.py
index 052b8d1db..ffe954672 100644
--- a/plugins/codex-security/scripts/workbench_saved_results.py
+++ b/plugins/codex-security/scripts/workbench_saved_results.py
@@ -51,19 +51,11 @@ from workbench.saved_coverage import (
_project_worker_coverage_row,
_saved_coverage_id,
)
+from workbench.saved_outputs import _restore_published_outputs, _snapshot_published_outputs
from workbench_constants import PHASES
from workbench_target import committed_diff_snapshot_digest
from workbench_validation import path_within_scope
-_PUBLISHED_OUTPUTS = (
- "findings.json",
- "coverage.json",
- "scan-manifest.json",
- "report.md",
- "threatmodel.md",
- "report.html",
- "exports/results.sarif",
-)
_PUBLICATION_FOLLOW_UP_WARNING = (
"Saved scan evidence remains on disk; result publication needs follow-up:"
)
@@ -2472,55 +2464,6 @@ def coverage_for_comparison(db: Any, scan: Any) -> dict[str, Any]:
return prepared.coverage
-def _snapshot_published_outputs(
- scan_dir: Path, relative_paths: tuple[str, ...] = _PUBLISHED_OUTPUTS
-) -> dict[str, tuple[bytes, os.stat_result] | None]:
- snapshots: dict[str, tuple[bytes, os.stat_result] | None] = {}
- for relative in relative_paths:
- descriptor = -1
- try:
- descriptor = open_scan_local_file_descriptor(
- scan_dir, relative, "Published scan output"
- )
- with os.fdopen(descriptor, "rb") as handle:
- descriptor = -1
- metadata = os.fstat(handle.fileno())
- snapshots[relative] = (handle.read(), metadata)
- except ContractError:
- path = scan_dir / relative
- if path.exists() or path.is_symlink():
- if relative == "threatmodel.md":
- # This optional projection will not replace an unsafe destination.
- continue
- raise
- snapshots[relative] = None
- finally:
- if descriptor >= 0:
- os.close(descriptor)
- return snapshots
-
-
-def _restore_published_outputs(
- scan_dir: Path,
- snapshots: dict[str, tuple[bytes, os.stat_result] | None],
- created_paths: set[str] | None = None,
-) -> None:
- for relative, snapshot in snapshots.items():
- if snapshot is None:
- path = scan_dir / relative
- if path.exists() or path.is_symlink():
- _remove_scan_local_file_if_exists(scan_dir, relative)
- else:
- contents, metadata = snapshot
- write_scan_local_bytes(scan_dir, relative, contents)
- os.utime(scan_dir / relative, ns=(metadata.st_atime_ns, metadata.st_mtime_ns))
- for relative in sorted(created_paths or ()):
- # A journaled write can fail before creating its parent directory.
- path = scan_dir / relative
- if path.exists() or path.is_symlink():
- _remove_scan_local_file_if_exists(scan_dir, relative)
-
-
def preserve_deep_deadline_draft(
db: Any, connection: Any, scan: Any, *, allow_empty: bool = False
) -> tuple[Path, dict[str, tuple[bytes, os.stat_result] | None], set[str]] | None: