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(import): preserve outcomes when scratch cleanup fails (#1824)
Michael D'Angelo31938494c3d6
diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.tsindex b58cd638d..455673525 100644--- a/sdk/typescript/src/cli.ts+++ b/sdk/typescript/src/cli.ts@@ -1973,7 +1973,9 @@ export async function main(return incurError({ code, message: outcome.error, exitCode });};const runImport = (options: ImportScanOptions) =>- runScanImport(options, errorOutput, dependencies);+ withTerminalErrorsHandled(errorOutput, () =>+ runScanImport(options, errorOutput, dependencies),+ );const history = async (args: readonly string[],select: (value: JsonObject) => JsonObject | Promise<JsonObject> = (value) =>@@ -6490,7 +6492,14 @@ async function runScanImport(try {const result = await (dependencies.importScan ?? importScan)({ ...options, signal: controller.signal },- { environment: dependencies.environment },+ {+ environment: dependencies.environment,+ onWarning: (warning) => {+ errorOutput.write(+ `codex-security: warning: ${diagnosticValue(warning)}\n`,+ );+ },+ },);if ("dryRun" in result) return { exitCode: 0, data: { ...result } };try {diff --git a/sdk/typescript/src/import-scan.ts b/sdk/typescript/src/import-scan.tsindex 700679fc1..8a6dad634 100644--- a/sdk/typescript/src/import-scan.ts+++ b/sdk/typescript/src/import-scan.ts@@ -66,6 +66,7 @@ export interface ImportScanDependencies {environment?: ProcessEnvironment;runWorkbench?: typeof runWorkbench;resolvePluginPython?: typeof resolvePluginPython;+ onWarning?: (warning: string) => void;}const IMPORT_DESCRIPTION =@@ -364,6 +365,14 @@ export async function importScan(}throw error;} finally {- await cleanupSdkDirectory(workspace);+ await cleanupSdkDirectory(workspace).catch((error: unknown) => {+ void Promise.resolve()+ .then(() =>+ dependencies.onWarning?.(+ `Could not clean up after the Codex Security import: ${errorMessage(error)}`,+ ),+ )+ .catch(() => {});+ });}}diff --git a/sdk/typescript/tests-ts/cli-scan-import.test.ts b/sdk/typescript/tests-ts/cli-scan-import.test.tsindex 1e3896f30..79ca6d94b 100644--- a/sdk/typescript/tests-ts/cli-scan-import.test.ts+++ b/sdk/typescript/tests-ts/cli-scan-import.test.ts@@ -1,4 +1,6 @@import { resolve } from "node:path";+import { Writable } from "node:stream";+import { setImmediate } from "node:timers/promises";import { describe, expect, test, mock } from "bun:test";import { main } from "../src/cli.js";import {@@ -32,6 +34,119 @@ function importDependencies() {}describe("scan import", () => {+ test.each(["completed", "failed", "interrupted", "preview"] as const)(+ "preserves %s import outcomes when cleanup warnings fail asynchronously",+ async (outcome) => {+ const signals = new FakeSignals();+ const deps = dependencies({ signals });+ deps.importScan = async (options, settings) => {+ settings?.onWarning?.("Synthetic scratch cleanup failure");+ if (outcome === "failed") throw new Error("Synthetic import failure");+ if (outcome === "interrupted") {+ signals.emit("SIGINT");+ options.signal?.throwIfAborted();+ }+ return options.dryRun+ ? {+ dryRun: true,+ inputPath: options.sourcePath,+ format: options.format,+ findingCount: 2,+ }+ : fakeResult(["high", "high"]);+ };+ const stdout = capture();+ const stderr = new Writable({+ write(_chunk, _encoding, callback) {+ queueMicrotask(() =>+ callback(new Error("Synthetic asynchronous stderr failure")),+ );+ },+ });+ expect(+ await main(+ [+ "scan",+ "import",+ "--json",+ "findings.json",+ "--format",+ "json",+ ...(outcome === "preview" ? ["--dry-run"] : []),+ ],+ stdout.stream,+ stderr,+ deps,+ ),+ ).toBe(outcome === "failed" ? 2 : outcome === "interrupted" ? 130 : 0);+ await setImmediate();+ expect(stderr.listenerCount("error")).toBe(0);+ expect(signals.listeners.get("SIGINT")?.size).toBe(0);+ expect(signals.listeners.get("SIGTERM")?.size).toBe(0);+ if (outcome === "completed") {+ expect(JSON.parse(stdout.text())).toMatchObject({+ manifest: { scan: { id: "scan" } },+ });+ } else if (outcome === "preview") {+ expect(JSON.parse(stdout.text())).toMatchObject({+ dryRun: true,+ findingCount: 2,+ });+ }+ },+ );++ test("keeps required import output failures fatal", async () => {+ const { deps } = importDependencies();+ const stdout = new Writable({+ write(_chunk, _encoding, callback) {+ queueMicrotask(() =>+ callback(new Error("Synthetic import output failure")),+ );+ },+ });+ const stderr = capture();+ expect(+ await main(+ ["scan", "import", "--json", "findings.json", "--format", "json"],+ stdout,+ stderr.stream,+ deps,+ ),+ ).toBe(2);+ expect(stderr.text()).toContain("Synthetic import output failure");+ });++ test.each(["completed", "failed"] as const)(+ "reports cleanup warnings without changing a %s import outcome",+ async (outcome) => {+ const { deps } = importDependencies();+ deps.importScan = async (_options, settings) => {+ settings?.onWarning?.("Synthetic scratch cleanup failure");+ if (outcome === "failed") throw new Error("Synthetic import failure");+ return fakeResult(["high", "high"]);+ };+ const { stdout, stderr, runCli } = createCliTest(main);+ expect(+ await runCli(+ ["scan", "import", "--json", "findings.json", "--format", "json"],+ deps,+ ),+ ).toBe(outcome === "failed" ? 2 : 0);+ expect(stderr.text()).toContain(+ "codex-security: warning: Synthetic scratch cleanup failure",+ );+ if (outcome === "failed") {+ expect(stderr.text()).toContain("Synthetic import failure");+ } else {+ expect(JSON.parse(stdout.text())).toMatchObject({+ manifest: { scan: { id: "scan" } },+ });+ expect(stderr.text()).toContain("Imported 2 findings as scan scan.");+ }+ },+ );+test.each(["csv", "json"] as const)("imports --%s while --format json controls output",async (format) => {diff --git a/sdk/typescript/tests-ts/import-scan.test.ts b/sdk/typescript/tests-ts/import-scan.test.tsindex a4e753f8b..4aee932da 100644--- a/sdk/typescript/tests-ts/import-scan.test.ts+++ b/sdk/typescript/tests-ts/import-scan.test.ts@@ -12,7 +12,7 @@ import {} from "node:fs/promises";import * as filesystem from "node:fs/promises";import { tmpdir } from "node:os";-import { join } from "node:path";+import { basename, join } from "node:path";import { afterEach, expect, spyOn, test, mock } from "bun:test";import { loadContract } from "../src/contract.js";import { ScanInterruptedError } from "../src/errors.js";@@ -167,6 +167,93 @@ async function storedScans(context: Awaited<ReturnType<typeof fixture>>) {}>;}+test.each(["completed", "failed", "interrupted", "preview"] as const)(+ "scratch cleanup failure preserves the %s import outcome",+ async (outcome) => {+ if (+ runTestInSubprocess(+ import.meta.path,+ `scratch cleanup failure preserves the ${outcome} import outcome`,+ )+ )+ return;+ const context = await fixture();+ const controller = new AbortController();+ const primaryError = new Error("Synthetic import failure");+ const warnings: string[] = [];+ const originalRm = filesystem.rm;+ const removing = spyOn(filesystem, "rm").mockImplementation(+ async (path, options) => {+ if (basename(String(path)).startsWith("codex-security-import-")) {+ roots.track(String(path));+ throw new Error("Synthetic scratch cleanup failure");+ }+ return await originalRm(path, options);+ },+ );+ try {+ const operation = importScan(+ {+ ...context.options,+ dryRun: outcome === "preview",+ signal: controller.signal,+ },+ {+ ...context.dependencies,+ onWarning: async (message) => {+ warnings.push(message);+ throw new Error("Synthetic warning observer failure");+ },+ runWorkbench: async (options, args, input) => {+ if (args[0] === "prepare-scan-completion" && outcome === "failed")+ throw primaryError;+ const result = await runWorkbench(options, args, input);+ if (args[0] === "register-cli-scan" && outcome === "interrupted")+ controller.abort(primaryError);+ return result;+ },+ },+ );+ if (outcome === "failed")+ await expect(operation).rejects.toBe(primaryError);+ else if (outcome === "interrupted") {+ await expect(operation).rejects.toMatchObject({+ name: "ScanInterruptedError",+ cause: primaryError,+ });+ } else if (outcome === "preview") {+ expect(await operation).toMatchObject({+ dryRun: true,+ findingCount: 2,+ });+ } else {+ const result = completed(await operation);+ expect(result.findings.findings).toHaveLength(2);+ expect(+ (await loadContract(result.scanDir, { pluginRoot: PLUGIN_ROOT }))+ .manifest.scan.id,+ ).toBe(result.manifest.scan.id);+ }+ expect(warnings).toHaveLength(1);+ expect(warnings[0]).toContain("Synthetic scratch cleanup failure");+ if (outcome === "preview") {+ await expect(stat(context.stateDirectory)).rejects.toMatchObject({+ code: "ENOENT",+ });+ } else {+ const scans = await storedScans(context);+ expect(scans).toHaveLength(1);+ expect(scans[0]!.status).toBe(+ outcome === "completed" ? "complete" : "failed",+ );+ if (outcome === "completed") expect(scans[0]!.occurrence_count).toBe(2);+ }+ } finally {+ removing.mockRestore();+ }+ },+);+test("a rejected import registration keeps existing output in place", async () => {const context = await fixture();const outputDir = join(context.root, "scan");