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(cli): avoid claiming output was kept after scan failures (#359)
hari11c27a4c5186
diff --git a/sdk/typescript/src/api.ts b/sdk/typescript/src/api.tsindex 17cc39fe9..c4015be5a 100644--- a/sdk/typescript/src/api.ts+++ b/sdk/typescript/src/api.ts@@ -4980,7 +4980,7 @@ export async function runScanEvents(let { usage } = turn;if (options.signal.aborted) {throw new ScanInterruptedError(- `Codex Security scan was interrupted; partial output remains at ${options.scanDir}.`,+ `Codex Security scan was interrupted; output directory: ${options.scanDir}.`,options.scanDir,);}@@ -5071,7 +5071,7 @@ export async function runScanEvents();if (options.signal.aborted) {throw new ScanInterruptedError(- `Codex Security scan was interrupted; partial output remains at ${options.scanDir}.`,+ `Codex Security scan was interrupted; output directory: ${options.scanDir}.`,options.scanDir,);}@@ -5082,7 +5082,7 @@ export async function runScanEvents(}if (options.signal.aborted && !(error instanceof ScanInterruptedError)) {throw new ScanInterruptedError(- `Codex Security scan was interrupted; partial output remains at ${options.scanDir}.`,+ `Codex Security scan was interrupted; output directory: ${options.scanDir}.`,options.scanDir,{ cause: error },);@@ -6296,7 +6296,7 @@ function throwIfAborted(signal?: AbortSignal, scanDir = ""): void {if (!signal?.aborted) return;if (signal.reason instanceof ScanCostLimitExceededError) throw signal.reason;const message = scanDir- ? `Codex Security scan was interrupted; partial output remains at ${scanDir}.`+ ? `Codex Security scan was interrupted; output directory: ${scanDir}.`: "Codex Security scan was interrupted during preparation.";throw new ScanInterruptedError(message, scanDir, { cause: signal.reason });}diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.tsindex f9aa0d0a7..9740f5dbf 100644--- a/sdk/typescript/src/cli.ts+++ b/sdk/typescript/src/cli.ts@@ -10050,7 +10050,7 @@ async function executeScan();if (validation["reportPath"] !== undefined) {errorOutput.write(- `Partial validation report: ${diagnosticValue(validation["reportPath"])}\n`,+ `Validation report opened at ${diagnosticValue(validation["reportPath"])}\n`,);}}@@ -10109,11 +10109,11 @@ async function executeScan(if (requestedSignal !== null) {diagnostic("scan.interrupted", {signal: requestedSignal,- partial_output: scanDir !== null,});if (result !== null && arguments_.validate) {const exitCode = requestedSignal === "SIGINT" ? 130 : 143;- const message = `Validation interrupted. Completed scan output was kept at ${errorMessage(result.scanDir)}.`;+ const scanId = quoteCliPath(diagnosticValue(result.manifest.scan.id));+ const message = `Validation interrupted. Original output directory: ${errorMessage(result.scanDir)}. Run codex-security scans show ${scanId} to inspect the completed scan.`;errorOutput.write(`codex-security: ${message}\n`);return {exitCode,@@ -10124,6 +10124,7 @@ async function executeScan(},};}+ if (failure instanceof ScanInterruptedError) scanDir ??= failure.scanDir;const exitCode = interruptedExit(requestedSignal, scanDir, errorOutput);printFailedScanReference(registeredScan, phase, arguments_, errorOutput);return {@@ -10153,7 +10154,6 @@ async function executeScan(: isLocalScanFailure(failure)? "local": classifyConnectionFailure(failure),- partial_output: scanDir !== null,max_cost_usd: costLimitFailure?.maxCostUsd,estimated_usd: costLimitFailure?.cost.estimatedUsd,cost_estimate:@@ -10163,9 +10163,7 @@ async function executeScan(});errorOutput.write(`${message}\n`);if (!(failure instanceof ScanInterruptedError) && scanDir !== null) {- errorOutput.write(- `Partial output was kept at ${errorMessage(scanDir)}.\n`,- );+ errorOutput.write(`Output directory: ${errorMessage(scanDir)}\n`);}printFailedScanReference(registeredScan, phase, arguments_, errorOutput);return { exitCode: 2, error: message, data: failureData };@@ -10714,7 +10712,7 @@ function printFailedScanReference(if (phase !== null)output.write(`Last observed phase: ${diagnosticValue(phase)}\n`);output.write(- `Retained results: ${diagnosticValue(scan["scanDir"])}\n` ++ `Output directory: ${diagnosticValue(scan["scanDir"])}\n` +"Inspect the saved scan status before choosing resume or rerun.\n",);const prompts = [@@ -11288,11 +11286,11 @@ function interruptedExit(errorOutput.write(`codex-security: Scan ${ctrlC ? "canceled by Ctrl-C" : "terminated by SIGTERM"}.\n`,);- errorOutput.write(- scanDir === null- ? "codex-security: No partial output was kept.\n"- : `codex-security: Partial output was kept at ${errorMessage(scanDir)}.\n`,- );+ if (scanDir) {+ errorOutput.write(+ `codex-security: Output directory: ${errorMessage(scanDir)}\n`,+ );+ }return ctrlC ? 130 : 143;}diff --git a/sdk/typescript/src/errors.ts b/sdk/typescript/src/errors.tsindex 30cc5ad5e..ca0e86230 100644--- a/sdk/typescript/src/errors.ts+++ b/sdk/typescript/src/errors.ts@@ -97,7 +97,7 @@ export class ScanCostLimitExceededError extends ScanInterruptedError {scanDir: string,) {super(- `Scan stopped: short-context budget baseline ${formatUsd(cost.estimatedUsd)} exceeded the ${formatUsd(maxCostUsd)} limit; estimated cost ${formatScanCost(cost)}; partial output remains at ${scanDir}.`,+ `Scan stopped: short-context budget baseline ${formatUsd(cost.estimatedUsd)} exceeded the ${formatUsd(maxCostUsd)} limit; estimated cost ${formatScanCost(cost)}; scan output directory: ${scanDir}.`,scanDir,);this.maxCostUsd = maxCostUsd;diff --git a/sdk/typescript/tests-ts/api-validation.test.ts b/sdk/typescript/tests-ts/api-validation.test.tsindex e3af96221..b5b514e16 100644--- a/sdk/typescript/tests-ts/api-validation.test.ts+++ b/sdk/typescript/tests-ts/api-validation.test.ts@@ -485,9 +485,13 @@ describe("CodexSecurity finding validation", () => {if (cancel === "signal") controller.abort();else await client.close();const error = await pending;- if (cancel === "signal")+ if (cancel === "signal") {expect(error).toBeInstanceOf(ScanInterruptedError);- else+ expect(error).toMatchObject({+ message: `Codex Security scan was interrupted; output directory: ${options.outputDir}.`,+ scanDir: options.outputDir,+ });+ } elseexpect((error as Error).message).toContain("CodexSecurity is closed");},);diff --git a/sdk/typescript/tests-ts/api.test.ts b/sdk/typescript/tests-ts/api.test.tsindex c6b1df056..0cc8c4d8a 100644--- a/sdk/typescript/tests-ts/api.test.ts+++ b/sdk/typescript/tests-ts/api.test.ts@@ -5455,7 +5455,11 @@ if __name__ == "__main__":cost: { estimatedUsd: 0.0036 },});else if (scenario === "canceled")- await expect(scan).rejects.toBeInstanceOf(ScanInterruptedError);+ await expect(scan).rejects.toMatchObject({+ name: ScanInterruptedError.name,+ message: `Codex Security scan was interrupted; output directory: ${scanDir}.`,+ scanDir,+ });elseawait expect(scan).rejects.toMatchObject({name: ScanCostLimitExceededError.name,@@ -5566,7 +5570,7 @@ if __name__ == "__main__":"fail-scan","--scan-id","scan_example_001",- `--message=Scan stopped: short-context budget baseline $0.00488 exceeded the $0.004 limit; estimated cost $0.00488–$0.01156 (standard, context unknown, cache writes unknown); partial output remains at ${scanDir}.`,+ `--message=Scan stopped: short-context budget baseline $0.00488 exceeded the $0.004 limit; estimated cost $0.00488–$0.01156 (standard, context unknown, cache writes unknown); scan output directory: ${scanDir}.`,"--cost-json",JSON.stringify(cost),]);@@ -5683,7 +5687,7 @@ if __name__ == "__main__":expect(recovered.threadId).toBe("scan-thread");expect(recovered.cost?.estimatedUsd).toBe(0.00488);expect(warnings).toEqual([- `Scan stopped: short-context budget baseline $0.00488 exceeded the $0.004 limit; estimated cost $0.00488–$0.01156 (standard, context unknown, cache writes unknown); partial output remains at ${scanDir}.`,+ `Scan stopped: short-context budget baseline $0.00488 exceeded the $0.004 limit; estimated cost $0.00488–$0.01156 (standard, context unknown, cache writes unknown); scan output directory: ${scanDir}.`,]);expect(commands.some((args) => args[0] === "fail-scan")).toBe(false);expect(diff --git a/sdk/typescript/tests-ts/cli-scan-navigation.test.ts b/sdk/typescript/tests-ts/cli-scan-navigation.test.tsindex 4ce28c20b..750ea88af 100644--- a/sdk/typescript/tests-ts/cli-scan-navigation.test.ts+++ b/sdk/typescript/tests-ts/cli-scan-navigation.test.ts@@ -151,6 +151,7 @@ describe("scan navigation", () => {const root = await temporaryDirectory("failed-scan-navigation-");try {const scanDir = join(root, "results");+ await mkdir(scanDir);const repository = join(root, "repository");await mkdir(repository);const promptFile = join(root, "instructions.md");@@ -184,6 +185,7 @@ describe("scan navigation", () => {["scan","--json",+ "--verbose",...(customPrompts? ["--scan-prompt-file",@@ -203,7 +205,10 @@ describe("scan navigation", () => {expect(cli.stderr.text()).toContain("Last observed phase: reviewing files",);- expect(cli.stderr.text()).toContain(`Retained results: ${scanDir}`);+ expect(cli.stderr.text()).toContain(`Output directory: ${scanDir}`);+ expect(cli.stderr.text()).not.toContain("partial_output=");+ expect(cli.stderr.text()).not.toContain("Partial output was kept");+ expect(cli.stderr.text()).not.toContain("Retained results:");expect(cli.stderr.text()).toContain(`scans show ${latestId}`);expect(cli.stderr.text()).toContain(`scans logs ${latestId}`);expect(cli.stderr.text()).toContain("Inspect the saved scan status");diff --git a/sdk/typescript/tests-ts/cli-signals.test.ts b/sdk/typescript/tests-ts/cli-signals.test.tsindex 1f9d3171d..43dad7aee 100644--- a/sdk/typescript/tests-ts/cli-signals.test.ts+++ b/sdk/typescript/tests-ts/cli-signals.test.ts@@ -1,5 +1,6 @@import { describe, expect, test, mock } from "bun:test";import { main } from "../src/cli.js";+import { ScanInterruptedError } from "../src/errors.js";import {FakeSignals,capture,@@ -11,7 +12,7 @@ import { throwing } from "./support/errors.js";import { createCliTest, runCapturedCli } from "./support/cli-run.js";describe("CLI signals", () => {- test("maps Ctrl-C and SIGTERM to conventional exits and preserves partial output", async () => {+ test("maps Ctrl-C and SIGTERM to conventional exits and preserves the output location", async () => {for (const [signal, expectedExit, phrase] of [["SIGINT", 130, "Scan canceled by Ctrl-C."],["SIGTERM", 143, "Scan terminated by SIGTERM."],@@ -31,7 +32,7 @@ describe("CLI signals", () => {expect(exit).toBe(expectedExit);expect(stdout.text()).toBe("");expect(stderr.text()).toContain(phrase);- expect(stderr.text()).toContain("Partial output was kept at /tmp/scan.");+ expect(stderr.text()).toContain("Output directory: /tmp/scan");expect(onInterrupt).toHaveBeenCalled();expect(signals.listeners.get(signal)?.size).toBe(0);}@@ -52,7 +53,34 @@ describe("CLI signals", () => {expect(await runCli(["scan", "."], deps)).toBe(130);expect(stdout.text()).toBe("");expect(stderr.text()).toContain("Scan canceled by Ctrl-C.");- expect(stderr.text()).toContain("No partial output was kept.");+ expect(stderr.text()).not.toMatch(/output directory|partial output/i);+ });++ test("uses the SDK output directory when archive registration is interrupted", async () => {+ const { stdout, stderr, runCli } = createCliTest(main);+ const scanDir = "/tmp/pending-registration";+ const signals = new FakeSignals();+ const deps = dependencies({ signals });+ deps.createSecurity = () =>+ fakeSecurity(async () => {+ signals.emit("SIGINT");+ throw new ScanInterruptedError("Scan interrupted", scanDir);+ });+ expect(+ await runCli(+ [+ "scan",+ ".",+ "--archive-existing",+ "--output-dir",+ "pending-registration",+ ],+ deps,+ ),+ ).toBe(130);+ expect(stdout.text()).toBe("");+ expect(stderr.text()).toContain(`Output directory: ${scanDir}`);+ expect(stderr.text()).not.toContain("scans show");});test("preserves signals received during client cleanup", async () => {diff --git a/sdk/typescript/tests-ts/cli.test.ts b/sdk/typescript/tests-ts/cli.test.tsindex 765454551..fa534b2c2 100644--- a/sdk/typescript/tests-ts/cli.test.ts+++ b/sdk/typescript/tests-ts/cli.test.ts@@ -620,12 +620,15 @@ describe("CLI", () => {).toBe(exitCode);expect(validations).toBe(2);expect(commands).toBe(0);+ expect(stderr.text()).toContain(+ `Validation report opened at ${join(scanDir, "validation.md")}`,+ );const output = JSON.parse(stdout.text());if (fullOutput) {expect(output.ok).toBe(false);const message = validationFailed? failureMessage- : `Validation interrupted. Completed scan output was kept at ${scanDir}.`;+ : `Validation interrupted. Original output directory: ${scanDir}. Run codex-security scans show scan to inspect the completed scan.`;expect(output.error).toEqual({ code: "SCAN_FAILED", message });expect(stderr.text()).toContain(message);}@@ -4144,7 +4147,7 @@ describe("CLI", () => {expect(failureDiagnostic).toBeDefined();expect(failureDiagnostic).toContain('classification="unknown"');- expect(failureDiagnostic).toContain("partial_output=false");+ expect(failureDiagnostic).not.toContain("partial_output=");expect(failureDiagnostic).not.toContain("tenant-private");expect(failureDiagnostic).not.toContain("req-internal");expect(stderr.text()).toContain("Provider failed for");@@ -5239,13 +5242,13 @@ describe("CLI", () => {message: expect.stringContaining("estimated cost"),});expect(stderr.text()).toContain(- "Scan stopped: short-context budget baseline $0.00488 exceeded the $0.004 limit; estimated cost $0.00488–$0.01156 (standard, context unknown, cache writes unknown); partial output remains at /tmp/scan.",+ "Scan stopped: short-context budget baseline $0.00488 exceeded the $0.004 limit; estimated cost $0.00488–$0.01156 (standard, context unknown, cache writes unknown); scan output directory: /tmp/scan.",);expect(stderr.text()).toMatch(/scan\.configuration[^\n]*max_cost_usd=0\.004/u,);expect(stderr.text()).toContain(- 'scan.failed classification="cost_limit_exceeded" partial_output=true max_cost_usd=0.004 estimated_usd=0.00488',+ 'scan.failed classification="cost_limit_exceeded" max_cost_usd=0.004 estimated_usd=0.00488',);});@@ -5706,7 +5709,7 @@ describe("CLI", () => {}});- test("preserves partial-output guidance for a late protected-root failure", async () => {+ test("identifies the output directory for a late protected-root failure", async () => {const { stdout, stderr, runCli } = createCliTest(main);const partial = "/tmp/codex-security-partial";@@ -5724,7 +5727,7 @@ describe("CLI", () => {expect(stderr.text()).toContain("Isolated Codex runtime directory must be outside the scanned directory and any enclosing Git worktree.",);- expect(stderr.text()).toContain(`Partial output was kept at ${partial}.`);+ expect(stderr.text()).toContain(`Output directory: ${partial}`);expect(stderr.text()).not.toContain("codex-security:");});@@ -5794,7 +5797,7 @@ describe("CLI", () => {expect(await runCli(["scan", "."], deps)).toBe(expectedExit);expect(stdout.text()).toBe("");- expect(stderr.text()).toContain(`Partial output was kept at ${path}.`);+ expect(stderr.text()).toContain(`Output directory: ${path}`);}}, 30_000);@@ -5820,7 +5823,7 @@ describe("CLI", () => {expect(stdout.text()).toBe("");}expect(stderr.text()).toContain("SYNTHETIC_AUTH_HOME_CLEANUP_FAILED");- expect(stderr.text()).toContain("Partial output was kept at /tmp/scan.");+ expect(stderr.text()).toContain("Output directory: /tmp/scan");}});diff --git a/sdk/typescript/tests-ts/errors.test.ts b/sdk/typescript/tests-ts/errors.test.tsindex b59e2ffd2..ec99e9b8c 100644--- a/sdk/typescript/tests-ts/errors.test.ts+++ b/sdk/typescript/tests-ts/errors.test.ts@@ -56,7 +56,7 @@ describe("error messages", () => {expect(limit.message).toContain("$1.00");expect(limit.message).toContain("short-context budget baseline $2.00");expect(limit.message).toContain("legacy estimate, context unknown");- expect(limit.message).toContain("/scan");+ expect(limit.message).toContain("scan output directory: /scan");});test.each([