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
fix(scans): preserve archived workflows and saved scan replay (#1506)
mldangelo-oai45b3efb04d70
diff --git a/plugins/codex-security/scripts/workbench_scan_start.py b/plugins/codex-security/scripts/workbench_scan_start.pyindex 379216072..ab37bd4fd 100644--- a/plugins/codex-security/scripts/workbench_scan_start.py+++ b/plugins/codex-security/scripts/workbench_scan_start.py@@ -167,6 +167,19 @@ def archive_scan("UPDATE scans SET scan_dir = ?, updated_at = ? WHERE id = ?",(str(archived_scan_dir), timestamp, previous_scan["id"]),)+ connection.execute(+ """UPDATE finding_workflows SET scan_dir = ?,+ results_json = CASE WHEN json_extract(results_json, '$.scan.sarifPath') = ?+ THEN json_set(results_json, '$.scan.sarifPath', ?) ELSE results_json END+ WHERE scan_id = ? AND scan_dir = ?""",+ (+ str(archived_scan_dir),+ str(scan_dir / "exports" / "results.sarif"),+ str(archived_scan_dir / "exports" / "results.sarif"),+ previous_scan["id"],+ str(scan_dir),+ ),+ )for artifact in artifacts:try:relative_path = Path(artifact["path"]).relative_to(scan_dir)diff --git a/plugins/codex-security/tests/test_workbench_archive.py b/plugins/codex-security/tests/test_workbench_archive.pyindex 4953f1881..1d0c1d329 100644--- a/plugins/codex-security/tests/test_workbench_archive.py+++ b/plugins/codex-security/tests/test_workbench_archive.py@@ -70,6 +70,27 @@ def stored_paths(state, scan_id):)+def workflow(state, workflow_id="synthetic-workflow", **payload):+ return run_workbench(+ state,+ "finding-workflow",+ input_text=json.dumps({"id": workflow_id, "action": "get", **payload}),+ )["workflow"]+++def workflow_scan(previous_scan):+ state, _, output, scan_id = previous_scan+ workflow(state, action="bind", binding={"scanId": scan_id, "scanDir": str(output)})+ workflow(+ state,+ action="complete",+ stage="scan",+ result={"sarifPath": str(output / "exports" / "results.sarif"), "threadId": "synthetic"},+ )+ return previous_scan++def test_concurrent_registration_keeps_running_scan_output(previous_scan):state, target, output, scan_id = previous_scan@@ -85,9 +106,10 @@ def test_concurrent_registration_keeps_running_scan_output(previous_scan):assert stored_paths(state, scan_id) == (str(output), str(output / "report.md"))-def test_only_one_concurrent_registration_archives_previous_output(previous_scan):- state, target, output, scan_id = previous_scan+def test_only_one_concurrent_registration_archives_previous_output(workflow_scan):+ state, target, output, scan_id = workflow_scanmark_stopped(state, scan_id)+ saved_workflow = workflow(state)def attempt(_):try:@@ -103,6 +125,33 @@ def test_only_one_concurrent_registration_archives_previous_output(previous_scanassert (archive / "report.md").read_text() == "previous scan\n"assert list(output.iterdir()) == []assert stored_paths(state, scan_id) == (str(archive), str(archive / "report.md"))+ saved_workflow["scanDir"] = str(archive)+ saved_workflow["stages"]["scan"]["result"]["sarifPath"] = str(archive / "exports/results.sarif")+ assert workflow(state) == saved_workflow+ assert (+ workflow(state, action="bind", binding={"scanId": scan_id, "scanDir": str(archive)})+ == saved_workflow+ )++[email protected]("result", [None, {}, {"sarifPath": None}, {"sarifPath": "unrelated"}])+def test_archiving_preserves_other_workflow_bindings_and_result_metadata(previous_scan, result):+ state, target, output, scan_id = previous_scan+ mark_stopped(state, scan_id)+ bindings = {+ "matching": {"scanId": scan_id, "scanDir": str(output)},+ "copied-directory": {"scanId": scan_id, "scanDir": str(output.with_name("copy"))},+ "other-scan": {"scanId": str(uuid.uuid4()), "scanDir": str(output)},+ }+ before = {}+ for workflow_id, binding in bindings.items():+ workflow(state, workflow_id, action="bind", binding=binding)+ before[workflow_id] = workflow(+ state, workflow_id, action="complete", stage="scan", result=result+ )+ registered = register(state, target, output, "--archive-existing")+ before["matching"]["scanDir"] = registered["archivedScanDir"]+ assert {workflow_id: workflow(state, workflow_id) for workflow_id in bindings} == beforedef test_registration_rejection_restores_files_and_database(previous_scan):@@ -115,9 +164,10 @@ def test_registration_rejection_restores_files_and_database(previous_scan):assert stored_paths(state, scan_id) == (str(output), str(output / "report.md"))-def test_commit_failure_restores_archived_output(previous_scan, monkeypatch):- state, target, output, scan_id = previous_scan+def test_commit_failure_restores_archived_output(workflow_scan, monkeypatch):+ state, target, output, scan_id = workflow_scanmark_stopped(state, scan_id)+ saved_workflow = workflow(state)monkeypatch.setenv("CODEX_SECURITY_STATE_DIR", str(state))module = runpy.run_path(str(SCRIPT))connection = module["connect"]()@@ -147,6 +197,7 @@ def test_commit_failure_restores_archived_output(previous_scan, monkeypatch):assert (output / "report.md").read_text() == "previous scan\n"assert list(output.parent.glob("scan.previous-*")) == []assert stored_paths(state, scan_id) == (str(output), str(output / "report.md"))+ assert workflow(state) == saved_workflowdef test_legacy_caller_can_supply_already_archived_output(previous_scan):diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.tsindex 279a98c7f..4cc19497d 100644--- a/sdk/typescript/src/cli.ts+++ b/sdk/typescript/src/cli.ts@@ -2162,10 +2162,23 @@ export async function main(const result = await history(["get-scan", "--scan-id", scanId],async (value) => {+ const scan = value["scan"] as ScanLogSource;const logs = await readSavedScanLogs(- value["scan"] as ScanLogSource,- codexSecurityCredentialHome(dependencies.environment),+ scan,+ [+ codexSecurityCredentialHome(dependencies.environment),+ configuredCodexHome(dependencies.environment),+ ],+ {+ allowMissingRoot: Boolean(+ scan.threadIds?.length || scan.executionThreadIds?.length,+ ),+ },);+ if (logs.sessions.length === 0)+ throw new CodexSecurityError(+ `No saved session logs are available for scan ${scanId}.`,+ );// Incur owns filtering, envelopes and token controls. Keep those// requests on its formatter; plain JSON needs no aggregate string.if (@@ -6323,12 +6336,10 @@ async function prepareScanArgumentsFromRecipe("The saved scan recipe has an invalid Git head.",);}- const threshold = recipe["failOnSeverity"];- if (- threshold !== undefined &&- (typeof threshold !== "string" ||- !REPORTABLE_SEVERITIES.includes(threshold as FailureSeverity))- ) {+ const threshold = ScanSettingsSchema.shape.failureSeverity.safeParse(+ recipe["failOnSeverity"],+ );+ if (!threshold.success) {throw new CodexSecurityError("The saved scan recipe contains an invalid severity policy.",);@@ -6427,7 +6438,7 @@ async function prepareScanArgumentsFromRecipe(codexOverrides: Object.hasOwn(config, "approval_policy")? config: { ...config, approval_policy: "never" },- failureSeverity: threshold as FailureSeverity | undefined,+ failureSeverity: threshold.data,maxCostUsd,dryRun: false,parentScanId,diff --git a/sdk/typescript/src/runtime.ts b/sdk/typescript/src/runtime.tsindex 655266b35..5b7355e45 100644--- a/sdk/typescript/src/runtime.ts+++ b/sdk/typescript/src/runtime.ts@@ -2154,7 +2154,11 @@ async function prepareOutputDirectory(const archiveDir = await planOutputArchive(path);if (archiveDir !== null) {await rename(path, archiveDir);- onOutputArchived?.(archiveDir);+ try {+ void Promise.resolve(onOutputArchived?.(archiveDir)).catch(+ () => undefined,+ );+ } catch {}existing = null;}}diff --git a/sdk/typescript/tests-ts/cli-scan-logs.test.ts b/sdk/typescript/tests-ts/cli-scan-logs.test.tsindex 4c458ec4d..64227ca42 100644--- a/sdk/typescript/tests-ts/cli-scan-logs.test.ts+++ b/sdk/typescript/tests-ts/cli-scan-logs.test.ts@@ -16,10 +16,11 @@ import { capture, dependencies } from "./cli-fixtures.js";import { throwing } from "./support/errors.js";import { createCliTest } from "./support/cli-run.js";-async function fixture() {+async function fixture(desktop = false, archived = false, continuation = true) {const state = await realpath(await mkdtemp(join(tmpdir(), "saved-logs-")));- const home = join(state, "codex-home");- await mkdir(join(home, "sessions"), { recursive: true });+ const home = join(state, desktop ? "desktop-home" : "codex-home");+ const sessions = join(home, archived ? "archived_sessions" : "sessions");+ await mkdir(sessions, { recursive: true });const events = [{ type: "session_meta", payload: { id: "thread-1" } },{@@ -30,15 +31,22 @@ async function fixture() {},},];- await writeJsonLines(join(home, "sessions", "rollout.jsonl"), events);- const scan = { scanId: "scan-1", continuationThreadId: "thread-1" };- const logs = await readSavedScanLogs(scan, home);+ await writeJsonLines(join(sessions, "rollout.jsonl"), events);+ const scan = {+ scanId: "scan-1",+ ...(continuation ? { continuationThreadId: "thread-1" } : {}),+ threadIds: ["thread-1"],+ };+ const logs = await readSavedScanLogs(scan, home, { allowMissingRoot: true });const deps = dependencies({- environment: { CODEX_SECURITY_STATE_DIR: state },+ environment: {+ CODEX_SECURITY_STATE_DIR: state,+ CODEX_HOME: home,+ },onWorkbench: () => ({ scan }),});deps.createSecurity = throwing("Reading logs must not start Codex");- return { state, logs, deps };+ return { state, home, sessions, logs, deps };}async function referenceOutput(args: string[], logs: unknown) {@@ -80,6 +88,45 @@ describe("saved logs JSON output", () => {await rm(dataHome, { recursive: true, force: true });});+ test.each([+ [false, true],+ [true, true],+ [false, false],+ [true, false],+ ])(+ "reads Desktop logs archived=%p continuation=%p",+ async (archived, continuation) => {+ const f = await fixture(true, archived, continuation);+ try {+ await writeJsonLines(join(f.sessions, "unrelated.jsonl"), [+ {+ type: "session_meta",+ payload: {+ id: "unrelated",+ source: {+ subagent: { thread_spawn: { parent_thread_id: "thread-1" } },+ },+ },+ },+ ]);+ const { stdout, stderr, runCli } = createCliTest(main);+ expect(+ await runCli(["scans", "logs", "scan-1", "--json"], f.deps),+ ).toBe(0);+ expect(JSON.parse(stdout.text())).toEqual(f.logs);+ expect(stderr.text()).toBe("");+ await rm(f.sessions, { recursive: true });+ const absent = createCliTest(main);+ expect(await absent.runCli(["scans", "logs", "scan-1"], f.deps)).toBe(+ 2,+ );+ expect(absent.stderr.text()).toContain("No saved session logs");+ } finally {+ await rm(f.state, { recursive: true, force: true });+ }+ },+ );+test("preserves the stale installed-skills CTA after saved logs", async () => {const f = await fixture();try {diff --git a/sdk/typescript/tests-ts/cli-workbench.test.ts b/sdk/typescript/tests-ts/cli-workbench.test.tsindex 838340d41..cd350d1ad 100644--- a/sdk/typescript/tests-ts/cli-workbench.test.ts+++ b/sdk/typescript/tests-ts/cli-workbench.test.ts@@ -531,7 +531,10 @@ describe("CLI workbench", () => {const calls: Array<readonly string[]> = [];const stdout = captureCli(main, "stdout");const deps = dependencies({- environment: { CODEX_SECURITY_STATE_DIR: state },+ environment: {+ CODEX_SECURITY_STATE_DIR: state,+ CODEX_HOME: join(state, "codex-home"),+ },onWorkbench: (args): JsonObject => {calls.push(args);if (args[0] === "list-scans") {@@ -1499,6 +1502,29 @@ describe("CLI workbench", () => {expect(onTurn.mock.lastCall?.[1]?.parentScanId).toBe(scanId);});+ test.each([null, "", "unknown", 1])(+ "rejects an invalid saved severity policy: %j",+ async (failOnSeverity) => {+ const stderr = captureCli(main, "stderr");+ const onRun = mock();+ const saved = savedRecipe();+ expect(+ await stderr.run(+ ["scans", "rerun", saved.scanId],+ dependencies({+ onRun,+ onWorkbench: () => ({+ ...saved,+ recipe: { ...saved.recipe, failOnSeverity },+ }),+ }),+ ),+ ).toBe(2);+ expect(stderr.text()).toContain("invalid severity policy");+ expect(onRun).not.toHaveBeenCalled();+ },+ );+test("reruns canonical recipes with exact config, policy, plugin, and lineage", async () => {const onConfig = mock<(config: CodexSecurityConfig) => void>();const onTurn = mock<(repository: string, options: ScanOptions) => void>();diff --git a/sdk/typescript/tests-ts/mock-scan.test.ts b/sdk/typescript/tests-ts/mock-scan.test.tsindex 80624f5b0..fdf961566 100644--- a/sdk/typescript/tests-ts/mock-scan.test.ts+++ b/sdk/typescript/tests-ts/mock-scan.test.ts@@ -62,6 +62,7 @@ test("mock scans seal real artifacts and index shared and unique findings withoumock: true,auth: "api-key",maxCostUsd: 0.01,+ failureSeverity: "informational",onAuthentication: () => callbacks.push("authentication"),onScanStarted: () => callbacks.push("started"),});@@ -144,6 +145,7 @@ test("mock scans seal real artifacts and index shared and unique findings withou),).toBe(0);expect(onTurn.mock.results.at(-1)?.value).toBe(true);+ expect(onTurn.mock.lastCall?.[1].failureSeverity).toBe("informational");} finally {await client.close();}@@ -159,11 +161,13 @@ test("mock scans preserve output protection and archive existing completed resul}),).rejects.toThrow();const outputDir = join(root, "results");- const first = await client.run(repository, {+ const firstOptions = {mock: true,outputDir,target: ["example.ts"],- });+ workflowId: "synthetic-archived-workflow",+ };+ const first = await client.run(repository, firstOptions);const original = await readFile(first.manifestPath, "utf8");await expect(client.run(repository, { mock: true, outputDir }),@@ -183,6 +187,10 @@ test("mock scans preserve output protection and archive existing completed resulexpect(second.manifest.scan.id).not.toBe(first.manifest.scan.id);expect(first.coverage.mode).toBe("scoped_path");expect(first.coverage.includePaths).toEqual(["example.ts"]);+ const resumed = await client.run(repository, firstOptions);+ expect(resumed.manifest).toEqual(first.manifest);+ expect(resumed.scanDir).toBe(archive);+ expect(resumed.sarifPath).toBe(join(archive, "exports", "results.sarif"));} finally {await client.close();}diff --git a/sdk/typescript/tests-ts/runtime.test.ts b/sdk/typescript/tests-ts/runtime.test.tsindex 34fa6cfae..929b300a4 100644--- a/sdk/typescript/tests-ts/runtime.test.ts+++ b/sdk/typescript/tests-ts/runtime.test.ts@@ -5879,6 +5879,7 @@ describe("runtime directories and plugin Python boundary", () => {"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('CREATE TABLE finding_workflows (scan_id TEXT, scan_dir TEXT NOT NULL, results_json TEXT NOT NULL)')","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()])",@@ -6162,44 +6163,57 @@ describe("runtime directories and plugin Python boundary", () => {expect(await planOutputArchive(output)).not.toBeNull();});- test("archives prior output through the exported preparation helper", async () => {- const root = await temporaryDirectory();- const output = join(root, "scan");- await mkdir(output, { mode: 0o700 });- await writeFile(join(output, "previous.txt"), "previous scan\n");- const archived: string[] = [];+ test.each(["success", "sync", "async", "pending"])(+ "archives prior output with a %s observer",+ async (mode) => {+ const root = await temporaryDirectory();+ const output = join(root, "scan");+ await mkdir(output, { mode: 0o700 });+ await writeFile(join(output, "previous.txt"), "previous scan\n");+ const archived: string[] = [];+ const outputPresentDuringNotification: boolean[] = [];- expect(- await prepareOutputDir(- output,- "repo",- undefined,- undefined,- true,- (path) => {- archived.push(path);- },- ),- ).toBe(output);- expect(archived).toHaveLength(1);- expect(await readFile(join(archived[0]!, "previous.txt"), "utf8")).toBe(- "previous scan\n",- );- expect(await readdir(output)).toEqual([]);- expect(- await prepareOutputDir(- output,- "repo",- undefined,- undefined,- true,- (path) => {- archived.push(path);- },- ),- ).toBe(output);- expect(archived).toHaveLength(1);- });+ expect(+ await prepareOutputDir(+ output,+ "repo",+ undefined,+ undefined,+ true,+ (path) => {+ archived.push(path);+ outputPresentDuringNotification.push(existsSync(output));+ if (mode === "sync")+ throw new Error("Synthetic archival observer failed");+ if (mode === "async")+ return Promise.reject(+ new Error("Synthetic archival observer failed"),+ );+ if (mode === "pending") return new Promise<void>(() => {});+ },+ ),+ ).toBe(output);+ expect(archived).toHaveLength(1);+ expect(outputPresentDuringNotification).toEqual([false]);+ expect(await readFile(join(archived[0]!, "previous.txt"), "utf8")).toBe(+ "previous scan\n",+ );+ expect(await readdir(output)).toEqual([]);+ expect(+ await prepareOutputDir(+ output,+ "repo",+ undefined,+ undefined,+ true,+ (path) => {+ archived.push(path);+ },+ ),+ ).toBe(output);+ expect(archived).toHaveLength(1);+ },+ );test("validates explicit output directories and creates private temporary paths", async () => {const root = await temporaryDirectory();diff --git a/sdk/typescript/tests-ts/support/cli-logs-large.mts b/sdk/typescript/tests-ts/support/cli-logs-large.mtsindex 2ac765fbd..b5c128b9f 100644--- a/sdk/typescript/tests-ts/support/cli-logs-large.mts+++ b/sdk/typescript/tests-ts/support/cli-logs-large.mts@@ -59,7 +59,10 @@ const output = new Writable({},});const deps = dependencies({- environment: { CODEX_SECURITY_STATE_DIR: state },+ environment: {+ CODEX_SECURITY_STATE_DIR: state,+ CODEX_HOME: join(state, "codex-home"),+ },onWorkbench: () => ({scan: { scanId: "scan-1", continuationThreadId: "thread-1" },}),