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(artifacts): protect saved history when archiving scan output (#1271)

mldangelo-oai4eb88633250a
diff --git a/plugins/codex-security/scripts/workbench/storage.py b/plugins/codex-security/scripts/workbench/storage.py
index 257d4c74d..2584d646d 100644
--- a/plugins/codex-security/scripts/workbench/storage.py
+++ b/plugins/codex-security/scripts/workbench/storage.py
@@ -6,14 +6,16 @@ import os
from pathlib import Path
-def state_dir() -> Path:
- state_dir = os.environ.get("CODEX_SECURITY_STATE_DIR")
- if state_dir:
- return Path(state_dir).expanduser().resolve()
- codex_home = Path(
- os.environ["CODEX_HOME"] if os.environ.get("CODEX_HOME", "").strip() else "~/.codex"
- ).expanduser()
- return (codex_home / "state" / "plugins" / "codex-security").resolve()
+def state_dir(*, canonical: bool = True) -> Path:
+ configured = os.environ.get("CODEX_SECURITY_STATE_DIR")
+ if configured:
+ path = Path(configured).expanduser()
+ else:
+ codex_home = Path(
+ os.environ["CODEX_HOME"] if os.environ.get("CODEX_HOME", "").strip() else "~/.codex"
+ ).expanduser()
+ path = codex_home / "state" / "plugins" / "codex-security"
+ return path.resolve() if canonical else path.absolute()
def resolve_scan_root(scan_root: str | None) -> Path:
diff --git a/plugins/codex-security/scripts/workbench_scan_start.py b/plugins/codex-security/scripts/workbench_scan_start.py
index 06f5509f2..379216072 100644
--- a/plugins/codex-security/scripts/workbench_scan_start.py
+++ b/plugins/codex-security/scripts/workbench_scan_start.py
@@ -120,6 +120,8 @@ def archive_scan(
if previous_scan["status"] == "running":
raise SystemExit("Cannot archive the output of a running scan.")
has_contents = next(scan_dir.iterdir(), None) is not None
+ if has_contents and (not args.archive_existing or archived_scan_dir is not None):
+ raise SystemExit("The scan artifact directory must be empty before the scan starts.")
if previous_scan is None and not (args.archive_existing and has_contents):
yield archived_scan_dir
return
@@ -133,8 +135,9 @@ def archive_scan(
moved = False
try:
if archived_scan_dir is None:
+ configured_state = state_dir(canonical=False)
protected_directories = [
- state_dir(),
+ *(path.resolve() for path in (configured_state, *configured_state.parents)),
*(
Path(row[2]).resolve().parent
for row in connection.execute("PRAGMA database_list")
diff --git a/plugins/codex-security/tests/test_workbench_scan_archive.py b/plugins/codex-security/tests/test_workbench_scan_archive.py
index 8637af58a..f92e8b1d3 100644
--- a/plugins/codex-security/tests/test_workbench_scan_archive.py
+++ b/plugins/codex-security/tests/test_workbench_scan_archive.py
@@ -1,9 +1,13 @@
from __future__ import annotations
+import argparse
import json
import sqlite3
+import threading
+import uuid
from pathlib import Path
+import pytest
from workbench_test_support import fail_scan, run_workbench
@@ -152,3 +156,179 @@ def test_archive_does_not_replace_a_running_scan(tmp_path: Path) -> None:
assert connection.execute(
"SELECT scan_dir, status FROM scans WHERE id = ?", (str(previous["scanId"]),)
).fetchone() == (str(scan_dir), "running")
+
+
[email protected]("archived", [False, True])
+def test_nonempty_output_is_rejected_before_parsing_the_recipe(
+ tmp_path: Path, archived: bool
+) -> None:
+ repository, scan_dir = tmp_path / "repository", tmp_path / "scan"
+ repository.mkdir()
+ scan_dir.mkdir(mode=0o700)
+ (scan_dir / "checkpoint.txt").write_text("preserved output")
+ arguments = []
+ if archived:
+ previous_dir = tmp_path / "scan.previous-test"
+ previous_dir.mkdir(mode=0o700)
+ arguments = ["--archive-existing", "--archived-scan-dir", str(previous_dir)]
+
+ rejected = run_workbench(
+ tmp_path / "state",
+ "register-cli-scan",
+ "--repository",
+ str(repository),
+ "--scan-dir",
+ str(scan_dir),
+ "--recipe-json",
+ "{}",
+ *arguments,
+ check=False,
+ )
+
+ assert rejected["returncode"] != 0
+ assert "must be empty before the scan starts" in str(rejected["stderr"])
+ assert (scan_dir / "checkpoint.txt").read_text() == "preserved output"
+
+
+ "state_subdirectory",
+ [".", "state", "linked-state", "aliased-state", "aliased-state-child", "linked-parent"],
+)
+def test_archive_cannot_move_the_active_workbench_database(
+ tmp_path: Path, state_subdirectory: str
+) -> None:
+ repository = tmp_path / "repository"
+ scan_dir = tmp_path / "scan"
+ state_dir = scan_dir / state_subdirectory
+ previous_dir = tmp_path / "previous"
+ repository.mkdir()
+ scan_dir.mkdir(mode=0o700)
+ previous_dir.mkdir(mode=0o700)
+ if state_subdirectory in {
+ "linked-state",
+ "aliased-state",
+ "aliased-state-child",
+ "linked-parent",
+ }:
+ external_state = tmp_path / "external-state"
+ external_state.mkdir(mode=0o700)
+ state_dir.symlink_to(external_state, target_is_directory=True)
+ if state_subdirectory == "linked-parent":
+ child = scan_dir / "child"
+ child.mkdir(mode=0o700)
+ alias = tmp_path / "alias"
+ alias.symlink_to(child, target_is_directory=True)
+ state_dir = alias / ".." / state_subdirectory
+ elif state_subdirectory.startswith("aliased-state"):
+ alias = tmp_path / "alias"
+ alias.symlink_to(tmp_path, target_is_directory=True)
+ state_dir = alias / "scan" / state_subdirectory
+ if state_subdirectory == "aliased-state-child":
+ state_dir /= "child"
+ previous = register_scan(state_dir, repository, previous_dir)
+ (scan_dir / "previous.txt").write_text("keep existing output")
+
+ rejected = run_workbench(
+ state_dir,
+ "register-cli-scan",
+ "--repository",
+ str(repository),
+ "--scan-dir",
+ str(scan_dir),
+ "--recipe-json",
+ json.dumps(
+ {
+ "config": {},
+ "mode": "standard",
+ "repository": str(repository),
+ "target": {"kind": "repository", "paths": []},
+ }
+ ),
+ "--archive-existing",
+ check=False,
+ )
+
+ assert rejected["returncode"] != 0
+ assert "workbench state or active database" in str(rejected["stderr"])
+ assert (scan_dir / "previous.txt").read_text() == "keep existing output"
+ assert not list(tmp_path.glob("scan.previous-*"))
+ saved = run_workbench(state_dir, "get-scan", "--scan-id", str(previous["scanId"]))
+ assert saved["scan"]["scanId"] == previous["scanId"]
+ with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:
+ assert connection.execute("SELECT COUNT(*) FROM scans").fetchone()[0] == 1
+
+
+def test_registration_rechecks_empty_output_after_competing_archive_rolls_back(
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+ workbench_api,
+ workbench_schema,
+) -> None:
+ repository, scan_dir = tmp_path / "repository", tmp_path / "scan"
+ repository.mkdir()
+ scan_dir.mkdir(mode=0o700)
+ checkpoint = scan_dir / "checkpoint.txt"
+ checkpoint.write_text("preserved bytes")
+ database = tmp_path / "workbench.sqlite3"
+ arguments = argparse.Namespace(
+ repository=str(repository),
+ scan_dir=str(scan_dir),
+ archive_existing=True,
+ archived_scan_dir=None,
+ registration_json_stdin=False,
+ recipe_json_stdin=False,
+ parent_scan_id=str(uuid.uuid4()),
+ recipe_json=json.dumps(
+ {
+ "config": {},
+ "mode": "standard",
+ "repository": str(repository),
+ "target": {"kind": "repository", "paths": []},
+ }
+ ),
+ )
+ register = workbench_api["register_cli_scan"]
+ waiting = threading.Event()
+ outcomes = []
+
+ class WaitingConnection(sqlite3.Connection):
+ def execute(self, statement, *args, **kwargs):
+ if statement == "BEGIN IMMEDIATE":
+ waiting.set()
+ return super().execute(statement, *args, **kwargs)
+
+ def compete():
+ with sqlite3.connect(database, factory=WaitingConnection) as connection:
+ connection.row_factory = sqlite3.Row
+ second = argparse.Namespace(**vars(arguments))
+ second.archive_existing, second.parent_scan_id = False, None
+ try:
+ outcomes.append(register(connection, second))
+ except BaseException as error:
+ outcomes.append(error)
+
+ mkdir = Path.mkdir
+ competitor = threading.Thread(target=compete)
+
+ def replacement(path, *args, **kwargs):
+ result = mkdir(path, *args, **kwargs)
+ if path == scan_dir:
+ competitor.start()
+ assert waiting.wait(5)
+ return result
+
+ with sqlite3.connect(database) as first:
+ workbench_schema.backup(first)
+ first.row_factory = sqlite3.Row
+ with monkeypatch.context() as patch:
+ patch.setattr(Path, "mkdir", replacement)
+ with pytest.raises(SystemExit, match="not found"):
+ register(first, arguments)
+ competitor.join(10)
+ assert not competitor.is_alive()
+ assert len(outcomes) == 1
+ assert isinstance(outcomes[0], SystemExit)
+ assert "must be empty" in str(outcomes[0])
+ assert first.execute("SELECT COUNT(*) FROM scans").fetchone()[0] == 0
+ assert checkpoint.read_text() == "preserved bytes"
+ assert not list(tmp_path.glob("scan.previous-*"))
diff --git a/sdk/typescript/src/runtime.ts b/sdk/typescript/src/runtime.ts
index 20d3607da..ef5124927 100644
--- a/sdk/typescript/src/runtime.ts
+++ b/sdk/typescript/src/runtime.ts
@@ -1727,6 +1727,17 @@ export async function runWorkbench(
const scanDir = arguments_[arguments_.indexOf("--scan-dir") + 1]!;
const archiveDir = await planOutputArchive(scanDir);
if (archiveDir !== null) {
+ const stateDirectory = codexSecurityStateDirectory(
+ options.environment,
+ );
+ for (let path = stateDirectory; ; path = dirname(path)) {
+ if (isWithin(scanDir, await canonicalConfigPath(path))) {
+ throw new Error(
+ "The scan artifact directory cannot contain the active workbench database.",
+ );
+ }
+ if (dirname(path) === path) break;
+ }
const savedScans = await savedScanIdentities(scanDir);
signal?.throwIfAborted();
await rename(scanDir, archiveDir);
diff --git a/sdk/typescript/tests-ts/import-scan.test.ts b/sdk/typescript/tests-ts/import-scan.test.ts
index ff12a15aa..0c5d0079a 100644
--- a/sdk/typescript/tests-ts/import-scan.test.ts
+++ b/sdk/typescript/tests-ts/import-scan.test.ts
@@ -26,6 +26,7 @@ import { runCommand } from "./support/shell.js";
import { runTestInSubprocess } from "./support/test-subprocess.js";
import { createApiTestFixtures } from "./support/temporary-directories.js";
import { rejecting } from "./support/errors.js";
+import { TestClient } from "./support/api-client.js";
const { temporaryDirectories: roots, cleanup } = createApiTestFixtures();
afterEach(cleanup);
@@ -553,6 +554,94 @@ test.each(["bundled", "legacy"])(
},
);
+test.each(["root", "child", "linked", "inverse", "aliased", "aliased-child"])(
+ "legacy archive preserves the active workbench at a %s state path",
+ async (layout) => {
+ const context = await fixture();
+ const outputDir = join(context.root, "results");
+ await mkdir(outputDir, { mode: 0o700 });
+ let stateDirectory =
+ layout === "root"
+ ? outputDir
+ : layout === "inverse"
+ ? context.stateDirectory
+ : join(outputDir, "state");
+ if (layout === "linked" || layout.startsWith("aliased")) {
+ await mkdir(context.stateDirectory, { mode: 0o700 });
+ await symlink(
+ context.stateDirectory,
+ stateDirectory,
+ process.platform === "win32" ? "junction" : "dir",
+ );
+ }
+ if (layout.startsWith("aliased")) {
+ const alias = join(context.root, "alias");
+ await symlink(
+ context.root,
+ alias,
+ process.platform === "win32" ? "junction" : "dir",
+ );
+ stateDirectory = join(alias, "results", "state");
+ if (layout === "aliased-child")
+ stateDirectory = join(stateDirectory, "child");
+ }
+ if (layout === "inverse") {
+ const inside = join(outputDir, "state");
+ await mkdir(inside, { mode: 0o700 });
+ await symlink(
+ inside,
+ stateDirectory,
+ process.platform === "win32" ? "junction" : "dir",
+ );
+ }
+ context.dependencies.environment.CODEX_SECURITY_STATE_DIR = stateDirectory;
+ const first = completed(
+ await importScan(
+ {
+ ...context.options,
+ outputDir: join(context.root, "previous"),
+ },
+ context.dependencies,
+ ),
+ );
+ const repository = join(context.root, "repository");
+ await mkdir(repository);
+ await writeFile(
+ join(repository, "example.ts"),
+ "export const value = 1;\n",
+ );
+ const client = new TestClient(
+ {
+ ...context.options.config,
+ pluginPath: await legacyArchivePlugin(context),
+ },
+ { ...context.dependencies, runWorkbench },
+ );
+ try {
+ await expect(
+ client.run(repository, {
+ mock: true,
+ outputDir,
+ archiveExisting: true,
+ }),
+ ).rejects.toThrow("active workbench database");
+ const saved = await runWorkbench(context.workbenchOptions, [
+ "get-scan",
+ "--scan-id",
+ first.manifest.scan.id,
+ ]);
+ expect((saved["scan"] as { scanId: string }).scanId).toBe(
+ first.manifest.scan.id,
+ );
+ expect(await readFile(first.manifestPath, "utf8")).toContain(
+ first.manifest.scan.id,
+ );
+ } finally {
+ await client.close();
+ }
+ },
+);
+
test.each(["active scan", "missing parent"])(
"legacy registration restores output after rejecting %s",
async (reason) => {