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(severity): propagate cancellation to saved-scan history (#1823)
Michael D'Angelo24c68f475f14
diff --git a/sdk/typescript/src/classify-scan-severity.ts b/sdk/typescript/src/classify-scan-severity.tsindex 51632ed11..f6d869149 100644--- a/sdk/typescript/src/classify-scan-severity.ts+++ b/sdk/typescript/src/classify-scan-severity.ts@@ -60,7 +60,8 @@ export async function classifyScanSeverityInternal(});const scan = await resolveCompletedScan(scanId, {currentDirectory: () => currentDirectory,- runWorkbench: dependencies.runWorkbench ?? bootstrap!,+ runWorkbench: (args, input) =>+ (dependencies.runWorkbench ?? bootstrap!)(args, input, options.signal),});if (options.expectedScanId !== undefined &&diff --git a/sdk/typescript/tests-ts/classify-scan-severity.test.ts b/sdk/typescript/tests-ts/classify-scan-severity.test.tsindex 927c1a283..5d1587d28 100644--- a/sdk/typescript/tests-ts/classify-scan-severity.test.ts+++ b/sdk/typescript/tests-ts/classify-scan-severity.test.ts@@ -842,6 +842,73 @@ test("publication reads SQLite even when the JSON export is modified or symlinkeexpect(await readFile(external, "utf8")).toBe(JSON.stringify(result));});+test.each(["saved scan", "latest listing", "latest scan"])(+ "cancels a pending %s history request and preserves its cleanup and error",+ async (boundary) => {+ const controller = new AbortController();+ const reason = new Error("Classification canceled");+ const abortError = Object.assign(new Error("History request aborted"), {+ name: "AbortError",+ cause: reason,+ });+ const entered = Promise.withResolvers<void>();+ const response = Promise.withResolvers<JsonObject>();+ const calls: string[] = [];+ const events: string[] = [];+ let receivedSignal: AbortSignal | undefined;+ const operation = classifyScanSeverityInternal(+ boundary === "saved scan" ? "scan-example" : "latest",+ { signal: controller.signal },+ {+ currentDirectory: () => process.cwd(),+ runWorkbench: async (args, _input, signal) => {+ calls.push(args[0]!);+ if (boundary === "latest scan" && args[0] === "list-scans")+ return { scans: [{ scanId: "scan-example" }] };+ receivedSignal = signal;+ const abort = () => {+ events.push("aborted");+ response.reject(abortError);+ };+ signal?.addEventListener("abort", abort, { once: true });+ events.push("entered");+ entered.resolve();+ try {+ return await response.promise;+ } finally {+ signal?.removeEventListener("abort", abort);+ events.push("cleaned up");+ }+ },+ },+ );+ const pending = operation.catch((error: unknown) => error);+ try {+ await Promise.race([+ entered.promise,+ pending.then((error) => {+ throw error;+ }),+ ]);+ controller.abort(reason);+ response.reject(new Error("History lookup did not observe cancellation"));+ expect(await pending).toBe(abortError);+ expect(receivedSignal).toBe(controller.signal);+ expect(events).toEqual(["entered", "aborted", "cleaned up"]);+ expect(calls).toEqual(+ boundary === "saved scan"+ ? ["get-scan"]+ : boundary === "latest listing"+ ? ["list-scans"]+ : ["list-scans", "get-scan"],+ );+ } finally {+ response.resolve({});+ await pending;+ }+ },+);+test.each(["latest", "scan_prefix"])("resolves %s using existing saved-scan history",async (selector) => {diff --git a/sdk/typescript/tests-ts/cli-classify-severity.test.ts b/sdk/typescript/tests-ts/cli-classify-severity.test.tsindex a0763fa57..262064afb 100644--- a/sdk/typescript/tests-ts/cli-classify-severity.test.ts+++ b/sdk/typescript/tests-ts/cli-classify-severity.test.ts@@ -117,10 +117,12 @@ test.each([deps.addSignalListener = (name, listener) => signals.add(name, listener);deps.removeSignalListener = (name, listener) =>signals.remove(name, listener);- deps.classifyScanSeverity = async (_scanId, options) => {+ let historySignal: AbortSignal | undefined;+ deps.runWorkbench = async (_args, _input, receivedSignal) => {+ historySignal = receivedSignal;signals.emit(signal);- options!.signal!.throwIfAborted();- return result;+ receivedSignal?.throwIfAborted();+ throw new Error("History lookup did not observe cancellation");};expect(await runCapturedCli(@@ -129,6 +131,9 @@ test.each([deps,),).toBe(expectedCode);+ expect(historySignal).toBeInstanceOf(AbortSignal);+ expect(historySignal!.aborted).toBe(true);+ expect(historySignal!.reason).toBe(signal);expect(signals.listeners.get("SIGINT")?.size).toBe(0);expect(signals.listeners.get("SIGTERM")?.size).toBe(0);},