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: preserve scans when reusing archived output (#77)

ianw-oaic7d78eebde28
diff --git a/sdk/typescript/_bundled_plugin/scripts/workbench_cli.py b/sdk/typescript/_bundled_plugin/scripts/workbench_cli.py
index acde8f97c..01c418b8c 100644
--- a/sdk/typescript/_bundled_plugin/scripts/workbench_cli.py
+++ b/sdk/typescript/_bundled_plugin/scripts/workbench_cli.py
@@ -158,6 +158,8 @@ def parse_args(description: str) -> argparse.Namespace:
register_cli_scan.add_argument("--repository", required=True)
register_cli_scan.add_argument("--recipe-json", required=True)
register_cli_scan.add_argument("--parent-scan-id")
+ register_cli_scan.add_argument("--archive-existing", action="store_true")
+ register_cli_scan.add_argument("--archived-scan-dir")
get_scan_recipe = subparsers.add_parser("get-scan-recipe")
get_scan_recipe.add_argument("--scan-id", required=True)
diff --git a/sdk/typescript/_bundled_plugin/scripts/workbench_db.py b/sdk/typescript/_bundled_plugin/scripts/workbench_db.py
index 683cfe574..e95aad9b3 100644
--- a/sdk/typescript/_bundled_plugin/scripts/workbench_db.py
+++ b/sdk/typescript/_bundled_plugin/scripts/workbench_db.py
@@ -80,6 +80,7 @@ from workbench_constants import (
)
from workbench_feedback import get_scan_feedback
from workbench_scan_start import (
+ archive_scan,
compact_timestamp,
insert_running_scan,
safe_segment,
@@ -1552,6 +1553,7 @@ def register_cli_scan(connection: sqlite3.Connection, args: argparse.Namespace)
connection.execute("BEGIN IMMEDIATE")
try:
+ archive_scan(connection, args, scan_dir, timestamp, require_canonical_scan_directory)
target_id = ensure_security_target(connection, str(repository))
if parent_scan_id is not None:
parent = require_scan(connection, parent_scan_id)
diff --git a/sdk/typescript/_bundled_plugin/scripts/workbench_scan_start.py b/sdk/typescript/_bundled_plugin/scripts/workbench_scan_start.py
index d93d967ae..fa02b0367 100644
--- a/sdk/typescript/_bundled_plugin/scripts/workbench_scan_start.py
+++ b/sdk/typescript/_bundled_plugin/scripts/workbench_scan_start.py
@@ -8,6 +8,7 @@ import os
import sqlite3
import sys
import tempfile
+from collections.abc import Callable
from datetime import datetime, timezone
from pathlib import Path
@@ -71,6 +72,68 @@ def scan_diff_identity(
)
+def archive_scan(
+ connection: sqlite3.Connection,
+ args: argparse.Namespace,
+ scan_dir: Path,
+ timestamp: str,
+ canonical_directory: Callable[[Path], Path],
+) -> None:
+ archived_scan_dir = (
+ canonical_directory(Path(args.archived_scan_dir).expanduser())
+ if args.archived_scan_dir is not None
+ else None
+ )
+ if archived_scan_dir is not None and (
+ not args.archive_existing
+ or archived_scan_dir.parent != scan_dir.parent
+ or not archived_scan_dir.name.startswith(f"{scan_dir.name}.previous-")
+ ):
+ raise SystemExit("The archived scan must be a previous sibling of the scan directory.")
+
+ previous_scan = connection.execute(
+ "SELECT id, status FROM scans WHERE scan_dir = ?", (str(scan_dir),)
+ ).fetchone()
+ if previous_scan is None:
+ return
+ if not args.archive_existing:
+ raise SystemExit(
+ "The scan artifact directory belongs to an existing scan. "
+ "Use --archive-existing to preserve that scan and start a new one."
+ )
+ if previous_scan["status"] == "running":
+ raise SystemExit("Cannot archive the output of a running scan.")
+ artifacts = connection.execute(
+ "SELECT kind, path FROM scan_artifacts WHERE scan_id = ?",
+ (previous_scan["id"],),
+ ).fetchall()
+ if archived_scan_dir is None:
+ if artifacts:
+ raise SystemExit(
+ "The archived scan directory is required to preserve existing scan artifacts."
+ )
+ archived_scan_dir = Path(
+ tempfile.mkdtemp(prefix=f"{scan_dir.name}.previous-", dir=scan_dir.parent)
+ ).resolve()
+ connection.execute(
+ "UPDATE scans SET scan_dir = ?, updated_at = ? WHERE id = ?",
+ (str(archived_scan_dir), timestamp, previous_scan["id"]),
+ )
+ for artifact in artifacts:
+ try:
+ relative_path = Path(artifact["path"]).relative_to(scan_dir)
+ except ValueError:
+ continue
+ connection.execute(
+ "UPDATE scan_artifacts SET path = ? WHERE scan_id = ? AND kind = ?",
+ (
+ str(archived_scan_dir / relative_path),
+ previous_scan["id"],
+ artifact["kind"],
+ ),
+ )
+
+
def insert_running_scan(
connection: sqlite3.Connection,
*,
diff --git a/sdk/typescript/src/api.ts b/sdk/typescript/src/api.ts
index 31925d11f..43fd54d44 100644
--- a/sdk/typescript/src/api.ts
+++ b/sdk/typescript/src/api.ts
@@ -299,6 +299,7 @@ export class CodexSecurity {
...(options.signal === undefined ? [] : [options.signal]),
]);
let scanDir = "";
+ let archivedScanDir: string | null = null;
let targetPathsFile: string | null = null;
let knowledgeBase: PreparedKnowledgeBase | null = null;
let costTracker: ScanCostTracker | null = null;
@@ -447,13 +448,15 @@ export class CodexSecurity {
scanOutputRoot,
(path) => requireOutputOutsideRepository(protectedRoot, path),
options.archiveExisting,
- (archiveDir) =>
+ (archiveDir) => {
+ archivedScanDir = archiveDir;
notifyObserver(
"onOutputArchived",
options.onOutputArchived,
options.onObserverError,
archiveDir,
- ),
+ );
+ },
);
requireOutputOutsideRepository(protectedRoot, scanDir);
requireModelSafeOutputDir(scanDir);
@@ -554,6 +557,10 @@ export class CodexSecurity {
scanDir,
"--recipe-json",
JSON.stringify(recipe),
+ ...(options.archiveExisting === true ? ["--archive-existing"] : []),
+ ...(archivedScanDir === null
+ ? []
+ : ["--archived-scan-dir", archivedScanDir]),
...(options.parentScanId === undefined
? []
: ["--parent-scan-id", options.parentScanId]),
diff --git a/sdk/typescript/tests-ts/api.test.ts b/sdk/typescript/tests-ts/api.test.ts
index c4d5f4da8..d3bd4b84d 100644
--- a/sdk/typescript/tests-ts/api.test.ts
+++ b/sdk/typescript/tests-ts/api.test.ts
@@ -923,6 +923,7 @@ describe("CodexSecurity orchestration", () => {
await mkdir(output, { mode: 0o700 });
await writeFile(join(output, "previous.txt"), "previous scan\n");
let archived: string | undefined;
+ let registration: readonly string[] | undefined;
const observerErrors: Array<[ScanObserverName, string]> = [];
const client = new TestClient(
{},
@@ -931,6 +932,22 @@ describe("CodexSecurity orchestration", () => {
prepareRuntime: async () => preparedRuntime(codexHome),
resolvePluginPython: async () => "/managed/python",
repositoryRevision: async () => null,
+ runWorkbench: async (_options: unknown, args: readonly string[]) => {
+ if (args[0] === "get-scan-feedback") {
+ return {
+ scanId: "scan_example_001",
+ targetId: "target_sha256_example",
+ falsePositives: [],
+ };
+ }
+ if (args[0] !== "register-cli-scan") return {};
+ registration = args;
+ return {
+ scanId: "scan_example_001",
+ targetId: "target_sha256_example",
+ scanDir: output,
+ };
+ },
createCodex: () => ({
startThread: () => ({
id: null,
@@ -959,6 +976,10 @@ describe("CodexSecurity orchestration", () => {
["onOutputArchived", "archive observer exploded"],
]);
expect(archived?.startsWith(`${output}.previous-`)).toBe(true);
+ expect(registration).toContain("--archive-existing");
+ expect(
+ registration?.[registration.indexOf("--archived-scan-dir") + 1],
+ ).toBe(archived);
expect(await readFile(join(archived!, "previous.txt"), "utf8")).toBe(
"previous scan\n",
);
diff --git a/sdk/typescript/tests-ts/runtime.test.ts b/sdk/typescript/tests-ts/runtime.test.ts
index 0c2f0e210..e6e167722 100644
--- a/sdk/typescript/tests-ts/runtime.test.ts
+++ b/sdk/typescript/tests-ts/runtime.test.ts
@@ -947,6 +947,102 @@ describe("runtime directories and plugin Python boundary", () => {
expect(result).toEqual({ ok: true });
});
+ test("preserves recorded artifact paths when archiving a completed scan", async () => {
+ const root = await temporaryDirectory();
+ const scanDir = join(root, "scan");
+ const archivedScanDir = `${scanDir}.previous-20260729T000000Z`;
+ await mkdir(scanDir, { mode: 0o700 });
+ await mkdir(archivedScanDir, { mode: 0o700 });
+
+ const python = Bun.which("python3") ?? Bun.which("python");
+ expect(python).not.toBeNull();
+ const result = spawnSync(
+ python!,
+ [
+ "-I",
+ "-B",
+ "-c",
+ [
+ "import argparse, json, sqlite3, sys",
+ "from pathlib import Path",
+ "sys.path.insert(0, sys.argv[1])",
+ "from workbench_scan_start import archive_scan",
+ "scan_dir = Path(sys.argv[2])",
+ "archived_scan_dir = Path(sys.argv[3])",
+ "connection = sqlite3.connect(':memory:')",
+ "connection.row_factory = sqlite3.Row",
+ "connection.execute('CREATE TABLE scans (id TEXT PRIMARY KEY, status TEXT NOT NULL, scan_dir TEXT NOT NULL, updated_at TEXT NOT NULL)')",
+ "connection.execute('CREATE TABLE scan_artifacts (scan_id TEXT NOT NULL, kind TEXT NOT NULL, path TEXT NOT NULL, PRIMARY KEY (scan_id, kind))')",
+ "connection.execute('INSERT INTO scans VALUES (?, ?, ?, ?)', ('previous-scan', 'complete', str(scan_dir), 'before'))",
+ "artifacts = {'coverage': 'coverage.json', 'findings': 'findings.json', 'manifest': 'scan-manifest.json', 'markdownReport': 'report.md'}",
+ "connection.executemany('INSERT INTO scan_artifacts VALUES (?, ?, ?)', [('previous-scan', kind, str(scan_dir / path)) for kind, path in artifacts.items()])",
+ "args = argparse.Namespace(archive_existing=True, archived_scan_dir=str(archived_scan_dir))",
+ "archive_scan(connection, args, scan_dir, 'after', lambda path: path.resolve(strict=True))",
+ "scan = connection.execute('SELECT scan_dir FROM scans WHERE id = ?', ('previous-scan',)).fetchone()",
+ "rows = connection.execute('SELECT kind, path FROM scan_artifacts WHERE scan_id = ? ORDER BY kind', ('previous-scan',))",
+ "print(json.dumps({'scanDir': scan['scan_dir'], 'artifacts': [dict(row) for row in rows]}))",
+ ].join("\n"),
+ join(PLUGIN_ROOT, "scripts"),
+ scanDir,
+ archivedScanDir,
+ ],
+ { encoding: "utf8" },
+ );
+
+ expect(result.status).toBe(0);
+ expect(result.stderr).toBe("");
+ expect(JSON.parse(result.stdout)).toEqual({
+ scanDir: archivedScanDir,
+ artifacts: [
+ { kind: "coverage", path: join(archivedScanDir, "coverage.json") },
+ { kind: "findings", path: join(archivedScanDir, "findings.json") },
+ { kind: "manifest", path: join(archivedScanDir, "scan-manifest.json") },
+ { kind: "markdownReport", path: join(archivedScanDir, "report.md") },
+ ],
+ });
+ });
+
+ test("does not strand completed scan artifacts without an archive path", async () => {
+ const root = await temporaryDirectory();
+ const scanDir = join(root, "scan");
+ await mkdir(scanDir, { mode: 0o700 });
+
+ const python = Bun.which("python3") ?? Bun.which("python");
+ expect(python).not.toBeNull();
+ const result = spawnSync(
+ python!,
+ [
+ "-I",
+ "-B",
+ "-c",
+ [
+ "import argparse, sqlite3, sys",
+ "from pathlib import Path",
+ "sys.path.insert(0, sys.argv[1])",
+ "from workbench_scan_start import archive_scan",
+ "scan_dir = Path(sys.argv[2])",
+ "connection = sqlite3.connect(':memory:')",
+ "connection.row_factory = sqlite3.Row",
+ "connection.execute('CREATE TABLE scans (id TEXT PRIMARY KEY, status TEXT NOT NULL, scan_dir TEXT NOT NULL, updated_at TEXT NOT NULL)')",
+ "connection.execute('CREATE TABLE scan_artifacts (scan_id TEXT NOT NULL, kind TEXT NOT NULL, path TEXT NOT NULL, PRIMARY KEY (scan_id, kind))')",
+ "connection.execute('INSERT INTO scans VALUES (?, ?, ?, ?)', ('previous-scan', 'complete', str(scan_dir), 'before'))",
+ "connection.execute('INSERT INTO scan_artifacts VALUES (?, ?, ?)', ('previous-scan', 'coverage', str(scan_dir / 'coverage.json')))",
+ "args = argparse.Namespace(archive_existing=True, archived_scan_dir=None)",
+ "archive_scan(connection, args, scan_dir, 'after', lambda path: path.resolve(strict=True))",
+ ].join("\n"),
+ join(PLUGIN_ROOT, "scripts"),
+ scanDir,
+ ],
+ { encoding: "utf8" },
+ );
+
+ expect(result.status).toBe(1);
+ expect(result.stderr).toContain(
+ "The archived scan directory is required to preserve existing scan artifacts.",
+ );
+ expect(await readdir(root)).toEqual(["scan"]);
+ });
+
test("reports an unwritable SQLite state directory without a Python traceback", async () => {
const root = await temporaryDirectory();
const pluginRoot = join(root, "plugin");