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: retry Windows native wrapper publication (#2381)
Michael D'Angelo1f95245b1509
diff --git a/.github/workflows/node-ci.yml b/.github/workflows/node-ci.ymlindex 598e7fbb1..39d2fe3a6 100644--- a/.github/workflows/node-ci.yml+++ b/.github/workflows/node-ci.yml@@ -359,6 +359,9 @@ jobs:run: pnpm --dir plugins/codex-security/mcp-app install --frozen-lockfile- name: Build and test the standalone host runtimerun: node --experimental-strip-types --test plugins/codex-security/mcp-app/scripts/test_host_build.mts+ - name: Test concurrent native wrapper publication on Windows+ if: runner.os == 'Windows'+ run: node --experimental-strip-types --test plugins/codex-security/mcp-app/tests/test_build_native_wrappers.tsplugin-source:if: needs.classify-changes.outputs.plugin-tests-required == 'true'diff --git a/plugins/codex-security/mcp-app/scripts/build_native_wrappers.mjs b/plugins/codex-security/mcp-app/scripts/build_native_wrappers.mjsindex 747d85d1e..81c0b7e4e 100644--- a/plugins/codex-security/mcp-app/scripts/build_native_wrappers.mjs+++ b/plugins/codex-security/mcp-app/scripts/build_native_wrappers.mjs@@ -1,10 +1,33 @@import { randomUUID } from "node:crypto";-import { rename, rm, writeFile } from "node:fs/promises";+import fs from "node:fs/promises";import { resolve } from "node:path";+import { setTimeout as delay } from "node:timers/promises";import { build } from "esbuild";-export async function buildNativeWrappers() {- const root = resolve(import.meta.dirname, "../../native");+const windowsRenameDelays = [10, 25, 50, 100, 200, 400];++async function replaceWrapper(temporary, destination) {+ for (let attempt = 0; ; attempt++) {+ try {+ await fs.rename(temporary, destination);+ return;+ } catch (error) {+ // A concurrent reader can temporarily deny delete sharing on Windows.+ const wait = windowsRenameDelays[attempt];+ if (+ process.platform !== "win32" ||+ wait === undefined ||+ !["EPERM", "EACCES", "EBUSY"].includes(error.code)+ )+ throw error;+ await delay(wait);+ }+ }+}++export async function buildNativeWrappers(+ root = resolve(import.meta.dirname, "../../native"),+) {const result = await build({absWorkingDir: root,entryPoints: ["*.mts"],@@ -16,15 +39,19 @@ export async function buildNativeWrappers() {write: false,});// Concurrent runtime builds must never import a partially written wrapper.- await Promise.all(+ const publications = await Promise.allSettled(result.outputFiles.map(async (file) => {const temporary = `${file.path}.${randomUUID()}.tmp`;try {- await writeFile(temporary, file.contents);- await rename(temporary, file.path);+ await fs.writeFile(temporary, file.contents);+ await replaceWrapper(temporary, file.path);} finally {- await rm(temporary, { force: true });+ await fs.rm(temporary, { force: true });}}),);+ // A build process may exit as soon as its caller sees an error. Let every+ // in-flight wrapper finish publishing or cleaning up its own staged file.+ for (const publication of publications)+ if (publication.status === "rejected") throw publication.reason;}diff --git a/plugins/codex-security/mcp-app/tests/test_build_native_wrappers.ts b/plugins/codex-security/mcp-app/tests/test_build_native_wrappers.tsindex b5e272159..c62017695 100644--- a/plugins/codex-security/mcp-app/tests/test_build_native_wrappers.ts+++ b/plugins/codex-security/mcp-app/tests/test_build_native_wrappers.ts@@ -1,7 +1,11 @@import assert from "node:assert/strict";import { spawn } from "node:child_process";import { once } from "node:events";-import test from "node:test";+import fs from "node:fs/promises";+import { tmpdir } from "node:os";+import { join } from "node:path";+import test, { type TestContext } from "node:test";+import { setImmediate } from "node:timers/promises";const builder = new URL("../scripts/build_native_wrappers.mjs",@@ -23,7 +27,7 @@ test("concurrent runtime builds preserve complete native wrapper exports", asyncassert.equal(platform.nativeTarget, ${JSON.stringify(nativeTarget)});}`;- await Promise.all(+ const results = await Promise.all(Array.from({ length: 3 }, async () => {const child = spawn(process.execPath,@@ -38,7 +42,108 @@ test("concurrent runtime builds preserve complete native wrapper exports", asyncoutput += chunk;});const [code] = await once(child, "close");- assert.equal(code, 0, output);+ return { code, output };}),);+ for (const { code, output } of results) assert.equal(code, 0, output);+});++async function wrapperFixture(t: TestContext) {+ const root = await fs.mkdtemp(join(tmpdir(), "wrapper-publication-"));+ t.after(() => fs.rm(root, { recursive: true, force: true }));+ await fs.writeFile(join(root, "example.mts"), "export const version = 2;");+ await fs.writeFile(join(root, "example.mjs"), "previous");+ return root;+}++test(+ "publishes the same staged wrapper after a transient Windows conflict",+ {+ skip: process.platform !== "win32",+ },+ async (t) => {+ const root = await wrapperFixture(t);+ const destination = join(root, "example.mjs");+ const rename = fs.rename;+ const attempts: string[] = [];+ t.mock.method(fs, "rename", async (source: string, target: string) => {+ assert.equal(target, destination);+ attempts.push(source);+ if (attempts.length === 1) {+ assert.equal(await fs.readFile(target, "utf8"), "previous");+ throw Object.assign(new Error("wrapper is in use"), { code: "EBUSY" });+ }+ await rename(source, target);+ });+ await buildNativeWrappers(root);+ assert.equal(attempts.length, 2);+ assert.equal(attempts[0], attempts[1]);+ assert.match(await fs.readFile(destination, "utf8"), /version = 2/);+ assert.deepEqual((await fs.readdir(root)).sort(), [+ "example.mjs",+ "example.mts",+ ]);+ },+);++test("waits for a delayed sibling and cleans up when wrapper publication fails", async (t) => {+ const root = await wrapperFixture(t);+ await fs.writeFile(join(root, "sibling.mts"), "export const sibling = 1;");+ const rename = fs.rename;+ const remove = fs.rm;+ const failure = Object.assign(new Error("wrapper is in use"), {+ code: "EPERM",+ });+ const failedCleanup = Promise.withResolvers<void>();+ const siblingStaged = Promise.withResolvers<void>();+ const resumeSibling = Promise.withResolvers<void>();+ let attempts = 0;+ t.mock.method(fs, "rename", async (source: string, target: string) => {+ if (target === join(root, "example.mjs")) {+ attempts++;+ throw failure;+ }+ siblingStaged.resolve();+ await resumeSibling.promise;+ await rename(source, target);+ });+ t.mock.method(fs, "rm", async (...args: Parameters<typeof fs.rm>) => {+ await remove(...args);+ if (String(args[0]).startsWith(join(root, "example.mjs.")))+ failedCleanup.resolve();+ });+ let settled = false;+ const result = buildNativeWrappers(root).then(+ () => {+ throw new Error("Expected publication failure");+ },+ (error: unknown) => {+ settled = true;+ return error;+ },+ );+ try {+ await Promise.all([siblingStaged.promise, failedCleanup.promise]);+ // Give a prematurely rejected build a turn to notify its caller.+ await setImmediate();+ assert.equal(settled, false);+ } finally {+ resumeSibling.resolve();+ }+ assert.equal(await result, failure);+ assert.equal(attempts, process.platform === "win32" ? 7 : 1);+ assert.equal(+ await fs.readFile(join(root, "example.mjs"), "utf8"),+ "previous",+ );+ assert.match(+ await fs.readFile(join(root, "sibling.mjs"), "utf8"),+ /sibling = 1/,+ );+ assert.deepEqual((await fs.readdir(root)).sort(), [+ "example.mjs",+ "example.mts",+ "sibling.mjs",+ "sibling.mts",+ ]);});