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): register operations before synchronous preparation (#1736)

Michael D'Angeloa3c7a28e4e47
diff --git a/sdk/typescript/src/api.ts b/sdk/typescript/src/api.ts
index 82356e79e..8646467fd 100644
--- a/sdk/typescript/src/api.ts
+++ b/sdk/typescript/src/api.ts
@@ -3193,14 +3193,15 @@ export class CodexSecurity {
"A Codex Security operation is already in progress.",
);
}
- const activeOperation = operation();
- this.#activeOperation = activeOperation;
+ const activeOperation = Promise.withResolvers<void>();
+ this.#activeOperation = activeOperation.promise;
try {
- return await activeOperation;
+ return await operation();
} finally {
- if (this.#activeOperation === activeOperation) {
+ if (this.#activeOperation === activeOperation.promise) {
this.#activeOperation = null;
}
+ activeOperation.resolve();
}
}
diff --git a/sdk/typescript/tests-ts/api-operation-registration.test.ts b/sdk/typescript/tests-ts/api-operation-registration.test.ts
new file mode 100644
index 000000000..99a3a4559
--- /dev/null
+++ b/sdk/typescript/tests-ts/api-operation-registration.test.ts
@@ -0,0 +1,121 @@
+import { expect, spyOn, test } from "bun:test";
+import { CodexSecurity } from "../src/api.js";
+import * as targets from "../src/targets.js";
+
+function client() {
+ const forbidden = () => {
+ throw new Error("Unexpected runtime or model boundary");
+ };
+ return new CodexSecurity(
+ {},
+ { environment: {}, createCodex: forbidden, prepareRuntime: forbidden },
+ { surface: "sdk" },
+ );
+}
+
+test.each(["validate", "patch"] as const)(
+ "%s reserves its operation before serializing a finding",
+ async (method) => {
+ const security = client();
+ const gate = Promise.withResolvers<void>();
+ const signals: AbortSignal[] = [];
+ const normalize = spyOn(targets, "normalizeRepository").mockImplementation(
+ async (_path, signal) => {
+ signals.push(signal!);
+ await gate.promise;
+ throw new Error("Synthetic repository boundary");
+ },
+ );
+ let serialized = false;
+ let nested: Promise<unknown> | undefined;
+ let pending: Promise<unknown> | undefined;
+ try {
+ pending = security[method]({
+ repositoryPath: "/synthetic-outer-never-read",
+ finding: {
+ toJSON() {
+ serialized = true;
+ nested = security[method === "validate" ? "patch" : "validate"]({
+ repositoryPath: "/synthetic-inner-never-read",
+ finding: "Synthetic finding",
+ }).catch((error: Error) => error.message);
+ return { title: "Synthetic outer finding" };
+ },
+ },
+ }).catch((error: Error) => error.message);
+ expect(serialized).toBe(true);
+ expect(signals).toHaveLength(1);
+ expect(await nested).toContain("operation is already in progress");
+ let closed = false;
+ const closing = security.close().then(() => {
+ closed = true;
+ });
+ await Promise.resolve();
+ expect(closed).toBe(false);
+ expect(signals[0]!.aborted).toBe(true);
+ gate.resolve();
+ expect(await pending).toContain("CodexSecurity is closed");
+ await closing;
+ } finally {
+ gate.resolve();
+ await Promise.allSettled([pending, nested]);
+ await security.close();
+ normalize.mockRestore();
+ }
+ },
+);
+
+test("a synchronous operation callback failure permits same-turn retry", async () => {
+ await using security = client();
+ const failure = new Error("Synthetic option getter");
+ const pending = security
+ .run("/synthetic-never-read", {
+ get workflowId(): string {
+ throw failure;
+ },
+ })
+ .catch((error: Error) => error);
+ const immediateRetry = security
+ .validate({
+ repositoryPath: "/synthetic-never-read",
+ finding: "",
+ })
+ .catch((error: Error) => error.message);
+ const concurrentRetry = security
+ .validate({
+ repositoryPath: "/synthetic-never-read",
+ finding: "",
+ })
+ .catch((error: Error) => error.message);
+ expect(await pending).toBe(failure);
+ expect(await immediateRetry).toContain(
+ "A finding must be nonempty text or a JSON object",
+ );
+ expect(await concurrentRetry).toContain("operation is already in progress");
+});
+
+test("an asynchronously rejected operation owns its slot until settlement", async () => {
+ await using security = client();
+ const failure = new Error("Synthetic finding serialization");
+ const pending = security
+ .validate({
+ repositoryPath: "/synthetic-never-read",
+ finding: {
+ toJSON() {
+ throw failure;
+ },
+ },
+ })
+ .catch((error: Error) => error);
+ const concurrent = security
+ .validate({
+ repositoryPath: "/synthetic-never-read",
+ finding: "",
+ })
+ .catch((error: Error) => error.message);
+ expect(await pending).toBe(failure);
+ expect(await concurrent).toContain("operation is already in progress");
+ await expect(
+ security.validate({ repositoryPath: "/synthetic-never-read", finding: "" }),
+ ).rejects.toThrow("A finding must be nonempty text or a JSON object");
+});