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(workbench): serialize registrations with archival (#1587)

Michael D'Angeloa03fc48e78c4
diff --git a/plugins/codex-security/scripts/workbench_db.py b/plugins/codex-security/scripts/workbench_db.py
index 2e79a7757..34016d168 100644
--- a/plugins/codex-security/scripts/workbench_db.py
+++ b/plugins/codex-security/scripts/workbench_db.py
@@ -1645,14 +1645,7 @@ def complete_scan_locked(
return context
-def register_cli_scan(
- connection: sqlite3.Connection,
- args: argparse.Namespace,
- *,
- before_archive: Callable[[], None] | None = None,
-) -> dict[str, Any]:
- repository = require_target(args.repository)
- require_scannable_target(repository)
+def require_cli_scan_directory(repository: Path, args: argparse.Namespace) -> Path:
scan_dir = require_canonical_scan_directory(Path(args.scan_dir).expanduser())
if scan_dir == repository or repository in scan_dir.parents or scan_dir in repository.parents:
raise SystemExit(
@@ -1662,7 +1655,21 @@ def register_cli_scan(
scan_dir.iterdir(), None
) is not None:
raise SystemExit("The scan artifact directory must be empty before the scan starts.")
+ return scan_dir
+
+def register_cli_scan(
+ connection: sqlite3.Connection,
+ args: argparse.Namespace,
+ *,
+ before_archive: Callable[[], None] | None = None,
+) -> dict[str, Any]:
+ repository = require_target(args.repository)
+ require_scannable_target(repository)
+ scan_dir = None
+ if not args.archive_existing or args.archived_scan_dir is not None:
+ # Preserve early output rejection when this call will not move the directory.
+ scan_dir = require_cli_scan_directory(repository, args)
user_context = None
workflow_id = None
if args.registration_json_stdin:
@@ -1713,6 +1720,10 @@ def register_cli_scan(
connection.execute("BEGIN IMMEDIATE")
try:
+ if scan_dir is None:
+ # Host archiving temporarily removes the output directory. Wait for
+ # the existing writer before inspecting its replacement.
+ scan_dir = require_cli_scan_directory(repository, args)
with archive_scan(
connection,
args,
diff --git a/plugins/codex-security/tests/test_workbench_archive.py b/plugins/codex-security/tests/test_workbench_archive.py
index 1d0c1d329..60984a903 100644
--- a/plugins/codex-security/tests/test_workbench_archive.py
+++ b/plugins/codex-security/tests/test_workbench_archive.py
@@ -9,6 +9,7 @@ import subprocess
import uuid
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
+from threading import Event
import pytest
from workbench_test_support import SCRIPT, run_workbench
@@ -164,6 +165,64 @@ def test_registration_rejection_restores_files_and_database(previous_scan):
assert stored_paths(state, scan_id) == (str(output), str(output / "report.md"))
+def test_registration_waits_for_in_progress_archive_rename(workflow_scan, monkeypatch):
+ state, target, output, scan_id = workflow_scan
+ mark_stopped(state, scan_id)
+ monkeypatch.setenv("CODEX_SECURITY_STATE_DIR", str(state))
+ module = runpy.run_path(str(SCRIPT))
+ connection = module["connect"]()
+ ready, renamed, attempted = Event(), Event(), Event()
+ args = argparse.Namespace(
+ repository=str(target),
+ scan_dir=str(output),
+ archive_existing=True,
+ archived_scan_dir=None,
+ recipe_json=recipe(target),
+ recipe_json_stdin=False,
+ registration_json_stdin=False,
+ parent_scan_id=None,
+ )
+
+ def competing_registration():
+ other = module["connect"]()
+ other.set_trace_callback(lambda sql: attempted.set() if sql == "BEGIN IMMEDIATE" else None)
+ try:
+ ready.set()
+ assert renamed.wait(5)
+ with pytest.raises(SystemExit) as error:
+ module["register_cli_scan"](other, args)
+ return str(error.value)
+ finally:
+ attempted.set()
+ other.close()
+
+ rename = Path.rename
+
+ def pause_after_rename(path, destination):
+ result = rename(path, destination)
+ if path == output:
+ # The second caller reaches registration while output is absent.
+ # It must wait for the writer before inspecting the replacement.
+ renamed.set()
+ assert attempted.wait(5)
+ return result
+
+ monkeypatch.setattr(Path, "rename", pause_after_rename)
+ try:
+ with ThreadPoolExecutor(max_workers=1) as pool:
+ competing = pool.submit(competing_registration)
+ assert ready.wait(5)
+ registered = module["register_cli_scan"](connection, args)
+ assert competing.result(timeout=5) == "Cannot archive the output of a running scan."
+ archive = Path(registered["archivedScanDir"])
+ assert (archive / "report.md").read_text() == "previous scan\n"
+ assert list(output.iterdir()) == []
+ assert stored_paths(state, scan_id) == (str(archive), str(archive / "report.md"))
+ finally:
+ renamed.set()
+ connection.close()
+
+
def test_commit_failure_restores_archived_output(workflow_scan, monkeypatch):
state, target, output, scan_id = workflow_scan
mark_stopped(state, scan_id)