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(sdk): honor cancellation around embedding callbacks (#1789)

Michael D'Angelo4333dc99a995
diff --git a/sdk/typescript/docs/findings-service.md b/sdk/typescript/docs/findings-service.md
index f431a07b1..790ca3f8e 100644
--- a/sdk/typescript/docs/findings-service.md
+++ b/sdk/typescript/docs/findings-service.md
@@ -25,6 +25,8 @@ deployments; local deduplication uses the database selected by
For direct embedding/storage integrations, `OpenAiFindingEmbedder` accepts a
static key or `() => string | Promise<string>` as its first argument. It calls
the callback before each HTTP batch; the caller handles token acquisition.
+Cancellation prevents a new callback or HTTP request. Direct embedder callers
+still await a key callback that has already started.
Pass `fetch` as the second argument and the full endpoint URL as the third.
By default, the saved-scan deduplication APIs use environment credentials. A
custom SDK `embedding` binding can supply an adapter with its own credentials,
@@ -185,7 +187,8 @@ console.log(result.duplicateGroups);
For a different embedding provider or model, both saved-scan SDK methods accept
an `embedding` binding. Reuse the `FindingEmbedder` interface: return one
`{ model, vector }` per input finding, in input order. The adapter owns its
-credentials, tokenization, chunking, and provider request format.
+credentials, tokenization, chunking, and provider request format. Cancellation
+stops the SDK's wait for embeddings; the adapter owns cleanup of its ongoing work.
```typescript
import { deduplicateScan, type FindingEmbedder } from "@openai/codex-security";
diff --git a/sdk/typescript/src/deduplication/local.ts b/sdk/typescript/src/deduplication/local.ts
index 48f0274ed..ce7a44368 100644
--- a/sdk/typescript/src/deduplication/local.ts
+++ b/sdk/typescript/src/deduplication/local.ts
@@ -1,5 +1,5 @@
import { environmentEntry } from "../auth.js";
-import { gitProtectionRoots } from "../targets.js";
+import { abortable, gitProtectionRoots } from "../targets.js";
import type { Finding } from "../models.js";
import type {
FindingNeighborhood,
@@ -101,7 +101,10 @@ export class LocalDeduplication {
offset,
offset + EMBEDDING_BATCH_SIZE,
);
- const embeddings = await this.embedding.embedder.embed(batch);
+ const embeddings = await abortable(() => {
+ this.signal?.throwIfAborted();
+ return this.embedding.embedder.embed(batch);
+ }, this.signal);
this.signal?.throwIfAborted();
if (embeddings.length !== batch.length)
throw new CodexSecurityError(
diff --git a/sdk/typescript/src/server/embeddings.ts b/sdk/typescript/src/server/embeddings.ts
index b06ff843b..515507ca5 100644
--- a/sdk/typescript/src/server/embeddings.ts
+++ b/sdk/typescript/src/server/embeddings.ts
@@ -85,8 +85,10 @@ export class OpenAiFindingEmbedder implements FindingEmbedder {
): Promise<void> {
let response: Response;
try {
+ this.signal?.throwIfAborted();
const apiKey =
typeof this.apiKey === "function" ? await this.apiKey() : this.apiKey;
+ this.signal?.throwIfAborted();
if (!apiKey) throw new Error("Missing embedding credentials");
response = await this.request(this.url, {
method: "POST",
diff --git a/sdk/typescript/tests-ts/finding-embeddings.test.ts b/sdk/typescript/tests-ts/finding-embeddings.test.ts
index b9bebc196..15d8f1028 100644
--- a/sdk/typescript/tests-ts/finding-embeddings.test.ts
+++ b/sdk/typescript/tests-ts/finding-embeddings.test.ts
@@ -1,6 +1,7 @@
import { readJson } from "./support/json.js";
import { rejecting } from "./support/errors.js";
import { join } from "node:path";
+import { setImmediate as nextTurn } from "node:timers/promises";
import { expect, test, mock } from "bun:test";
import { Tiktoken } from "js-tiktoken/lite";
import cl100kBase from "js-tiktoken/ranks/cl100k_base";
@@ -53,6 +54,108 @@ test.each(["headers", "body"])(
},
);
+test.each(["before", "callback", "pending"] as const)(
+ "does not admit embedding work after cancellation at %s key acquisition",
+ async (stage) => {
+ const controller = new AbortController();
+ const canceled = new Error("Synthetic acquisition cancellation");
+ const entered = Promise.withResolvers<void>();
+ const key = Promise.withResolvers<string>();
+ const acquire = mock(() => {
+ if (stage === "callback") controller.abort(canceled);
+ if (stage === "pending") {
+ entered.resolve();
+ return key.promise;
+ }
+ return "synthetic-key";
+ });
+ const request = mock(async (_url: string, init: RequestInit) => {
+ init.signal?.throwIfAborted();
+ return Response.json({
+ model: EMBEDDING_MODEL,
+ data: [{ index: 0, embedding: vector() }],
+ });
+ });
+ const embedder = new OpenAiFindingEmbedder(
+ acquire,
+ request,
+ undefined,
+ controller.signal,
+ );
+ if (stage === "before") controller.abort(canceled);
+ const result = embedder.embed([example]);
+ const settled = result.then(
+ () => "resolved",
+ (error: unknown) => error,
+ );
+ try {
+ if (stage === "pending") {
+ await entered.promise;
+ controller.abort(canceled);
+ const pending = Symbol("pending");
+ expect(
+ await Promise.race([settled, nextTurn().then(() => pending)]),
+ ).toBe(pending);
+ key.resolve("synthetic-key");
+ }
+ await expect(result).rejects.toBe(canceled);
+ expect(acquire).toHaveBeenCalledTimes(stage === "before" ? 0 : 1);
+ expect(request).not.toHaveBeenCalled();
+ } finally {
+ key.resolve("synthetic-key");
+ await settled;
+ }
+ },
+);
+
+test("preserves empty input and missing static key precedence when canceled", async () => {
+ const controller = new AbortController();
+ controller.abort(new Error("Synthetic cancellation"));
+ const acquire = mock(() => "synthetic-key");
+ const request = mock(rejecting("Must not call"));
+ expect(
+ await new OpenAiFindingEmbedder(
+ acquire,
+ request,
+ undefined,
+ controller.signal,
+ ).embed([]),
+ ).toEqual([]);
+ expect(acquire).not.toHaveBeenCalled();
+ for (const key of [undefined, ""]) {
+ await expect(
+ new OpenAiFindingEmbedder(
+ key,
+ request,
+ undefined,
+ controller.signal,
+ ).embed([example]),
+ ).rejects.toMatchObject({ code: "embedding_unavailable" });
+ }
+ expect(request).not.toHaveBeenCalled();
+});
+
+test.each(["empty", "throw"])(
+ "preserves cancellation when key acquisition returns %s",
+ async (failure) => {
+ const controller = new AbortController();
+ const canceled = new Error("Synthetic acquisition cancellation");
+ const request = mock(rejecting("Must not call"));
+ const embedder = new OpenAiFindingEmbedder(
+ () => {
+ controller.abort(canceled);
+ if (failure === "throw") throw new Error("Synthetic callback failure");
+ return "";
+ },
+ request,
+ undefined,
+ controller.signal,
+ );
+ await expect(embedder.embed([example])).rejects.toBe(canceled);
+ expect(request).not.toHaveBeenCalled();
+ },
+);
+
test("uses the configured embedding model and preserves response indexes", async () => {
const findings = [
example,
diff --git a/sdk/typescript/tests-ts/local-dedupe.test.ts b/sdk/typescript/tests-ts/local-dedupe.test.ts
index 4322b687a..1d22a9c8e 100644
--- a/sdk/typescript/tests-ts/local-dedupe.test.ts
+++ b/sdk/typescript/tests-ts/local-dedupe.test.ts
@@ -1,6 +1,10 @@
import { setFindingIdentity, sha256 } from "./support/finding-identity.js";
import { existsSync } from "node:fs";
-import { setTimeout as delay } from "node:timers/promises";
+import { getEventListeners } from "node:events";
+import {
+ setTimeout as delay,
+ setImmediate as nextTurn,
+} from "node:timers/promises";
import { readFile, writeFile, mkdir, chmod } from "node:fs/promises";
import { join } from "node:path";
import { afterEach, expect, mock, spyOn, test } from "bun:test";
@@ -16,6 +20,7 @@ import { FindingWorkflow } from "../src/finding-workflow.js";
import {
EMBEDDING_DIMENSIONS,
EMBEDDING_MODEL,
+ OpenAiFindingEmbedder,
type FindingEmbedder,
} from "../src/server/embeddings.js";
import { SqliteFindingsStore } from "../src/server/sqlite-store.js";
@@ -793,11 +798,16 @@ test("embedding writes reject stale content and reuse certified vectors", async
).rejects.toThrow("Missing embedding credentials");
});
-test.each(["failure", "cancellation"] as const)(
+test.each([
+ "failure",
+ "cancellation",
+ "pending resolution",
+ "pending rejection",
+] as const)(
"preparation checkpoints batches and reuses them after a later %s",
async (failure) => {
const f = await fixture();
- const findings = Array.from({ length: 65 }, (_, index) => ({
+ const findings = Array.from({ length: 129 }, (_, index) => ({
...f.document.findings[0]!,
findingId: `csf_batch_${index}`,
fingerprints: {
@@ -809,13 +819,19 @@ test.each(["failure", "cancellation"] as const)(
const controller = new AbortController();
const reason = new Error(`Later batch ${failure}`);
const batches: Finding[][] = [];
+ const entered = Promise.withResolvers<void>();
+ const release = Promise.withResolvers<void>();
const interrupted = f.local(
{
async embed(batch) {
batches.push([...batch]);
if (batches.length === 2) {
if (failure === "failure") throw reason;
- controller.abort(reason);
+ if (failure === "cancellation") controller.abort(reason);
+ else {
+ entered.resolve();
+ await release.promise;
+ }
}
return await f.embed(batch);
},
@@ -823,9 +839,25 @@ test.each(["failure", "cancellation"] as const)(
false,
controller.signal,
);
- await expect(interrupted.prepare(findings, f.targetId)).rejects.toBe(
- reason,
+ const preparation = interrupted.prepare(findings, f.targetId);
+ const settled = preparation.then(
+ () => undefined,
+ (error: unknown) => error,
);
+ try {
+ if (failure.startsWith("pending")) {
+ await Promise.race([entered.promise, preparation]);
+ controller.abort(reason);
+ expect(await Promise.race([settled, nextTurn()])).toBe(reason);
+ }
+ await expect(preparation).rejects.toBe(reason);
+ } finally {
+ if (failure === "pending rejection")
+ release.reject(new Error("Synthetic late embedding failure"));
+ else release.resolve();
+ await settled;
+ await nextTurn();
+ }
expect(batches).toHaveLength(2);
expect(batches[0]!.length).toBeGreaterThan(1);
const completed = new Set(batches[0]!.map((finding) => finding.findingId));
@@ -852,6 +884,97 @@ test.each(["failure", "cancellation"] as const)(
},
);
+test("cancellation at embedding admission does not start the adapter", async () => {
+ const f = await fixture();
+ const finding = f.document.findings[0]!;
+ const controller = new AbortController();
+ const reason = new Error("Synthetic admission cancellation");
+ let prepared = false;
+ const addEventListener = controller.signal.addEventListener.bind(
+ controller.signal,
+ );
+ const listener = spyOn(
+ controller.signal,
+ "addEventListener",
+ ).mockImplementation((...args: Parameters<typeof addEventListener>) => {
+ addEventListener(...args);
+ if (prepared && args[0] === "abort") controller.abort(reason);
+ });
+ try {
+ const local = new LocalDeduplication(
+ f.environment,
+ { repositoryId: f.targetId },
+ f.repository,
+ controller.signal,
+ async () => {
+ prepared = true;
+ return JSON.parse(
+ JSON.stringify({
+ cacheKeys: { [finding.findingId]: "synthetic-cache-key" },
+ findingsToEmbed: [finding],
+ }),
+ );
+ },
+ f.embedding,
+ );
+ await expect(local.prepare([finding], f.targetId)).rejects.toBe(reason);
+ await nextTurn();
+ expect(f.embed).not.toHaveBeenCalled();
+ } finally {
+ listener.mockRestore();
+ }
+});
+
+test.each(["resolve", "reject"])(
+ "local preparation stops waiting for a pending key that will %s",
+ async (completion) => {
+ const f = await fixture();
+ const findings = [f.document.findings[0]!];
+ const controller = new AbortController();
+ const reason = new Error("Synthetic preparation cancellation");
+ const entered = Promise.withResolvers<void>();
+ const key = Promise.withResolvers<string>();
+ const request = mock(async () =>
+ Response.json({
+ model: EMBEDDING_MODEL,
+ data: [{ index: 0, embedding: vector }],
+ }),
+ );
+ const embedder = new OpenAiFindingEmbedder(
+ () => {
+ entered.resolve();
+ return key.promise;
+ },
+ request,
+ undefined,
+ controller.signal,
+ );
+ const preparation = f
+ .local(embedder, false, controller.signal)
+ .prepare(findings, f.targetId);
+ const settled = preparation.then(
+ () => undefined,
+ (error: unknown) => error,
+ );
+ try {
+ await Promise.race([entered.promise, preparation]);
+ controller.abort(reason);
+ expect(await Promise.race([settled, nextTurn()])).toBe(reason);
+ expect(request).not.toHaveBeenCalled();
+ expect(getEventListeners(controller.signal, "abort")).toEqual([]);
+ } finally {
+ if (completion === "reject")
+ key.reject(new Error("Synthetic late credential failure"));
+ else key.resolve("synthetic-key");
+ await settled;
+ await nextTurn();
+ }
+ expect(request).not.toHaveBeenCalled();
+ await f.local().prepare(findings, f.targetId);
+ expect(f.embed).toHaveBeenCalledWith(findings);
+ },
+);
+
test("local workflow replays a lost group acknowledgement without publication or more embeddings", async () => {
const f = await fixture();
const anchor = f.document.findings[0]!;