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(plugin): recover incomplete staging and preserve bundles (#1255)

mldangelo-oaifc192fa4b86f
diff --git a/plugins/codex-security/mcp-app/scripts/build_mcp_app.mjs b/plugins/codex-security/mcp-app/scripts/build_mcp_app.mjs
index 4a9c52f05..aa1372c60 100644
--- a/plugins/codex-security/mcp-app/scripts/build_mcp_app.mjs
+++ b/plugins/codex-security/mcp-app/scripts/build_mcp_app.mjs
@@ -5,6 +5,7 @@ import { pathToFileURL } from "node:url";
import { brotliCompressSync, constants as zlibConstants } from "node:zlib";
import { execFileSync } from "node:child_process";
import { build } from "esbuild";
+import { buildNativeWrappers } from "./build_native_wrappers.mjs";
const root = resolve(import.meta.dirname, "..");
const maxChunkBytes = 140_000;
@@ -13,6 +14,7 @@ export async function buildMcpApp({ output, native = "universal" }) {
if (native !== "universal" && native !== "host") {
throw new Error("Native packaging must be universal or host.");
}
+ await buildNativeWrappers();
const mcpDir = resolve(output);
const nativeRoot = join(
root,
diff --git a/plugins/codex-security/mcp-app/scripts/build_native.mjs b/plugins/codex-security/mcp-app/scripts/build_native.mjs
index b0a573775..a46df4b1f 100644
--- a/plugins/codex-security/mcp-app/scripts/build_native.mjs
+++ b/plugins/codex-security/mcp-app/scripts/build_native.mjs
@@ -1,19 +1,11 @@
#!/usr/bin/env node
import { execFileSync } from "node:child_process";
import { join, resolve } from "node:path";
-import { build } from "esbuild";
+import { buildNativeWrappers } from "./build_native_wrappers.mjs";
const root = resolve(import.meta.dirname, "../../native");
-await build({
- absWorkingDir: root,
- entryPoints: ["*.mts"],
- outdir: root,
- outExtension: { ".js": ".mjs" },
- format: "esm",
- platform: "node",
- target: "node20",
-});
+await buildNativeWrappers();
execFileSync("cargo", ["fetch", "--locked"], {
cwd: root,
diff --git a/plugins/codex-security/mcp-app/scripts/build_native_wrappers.mjs b/plugins/codex-security/mcp-app/scripts/build_native_wrappers.mjs
new file mode 100644
index 000000000..c22cd0994
--- /dev/null
+++ b/plugins/codex-security/mcp-app/scripts/build_native_wrappers.mjs
@@ -0,0 +1,15 @@
+import { resolve } from "node:path";
+import { build } from "esbuild";
+
+export async function buildNativeWrappers() {
+ const root = resolve(import.meta.dirname, "../../native");
+ await build({
+ absWorkingDir: root,
+ entryPoints: ["*.mts"],
+ outdir: root,
+ outExtension: { ".js": ".mjs" },
+ format: "esm",
+ platform: "node",
+ target: "node20",
+ });
+}
diff --git a/sdk/typescript/scripts/build-plugin.mjs b/sdk/typescript/scripts/build-plugin.mjs
index 8227afa3f..1bc0a21d1 100644
--- a/sdk/typescript/scripts/build-plugin.mjs
+++ b/sdk/typescript/scripts/build-plugin.mjs
@@ -1,21 +1,21 @@
-import { execFile } from "node:child_process";
+import { execFileSync } from "node:child_process";
import {
chmod,
copyFile,
lstat,
mkdir,
+ mkdtemp,
readFile,
readdir,
+ rename,
rm,
} from "node:fs/promises";
import { dirname, join, posix, resolve } from "node:path";
-import { promisify } from "node:util";
import { isMain } from "./is-main.mjs";
import { pluginContractFiles } from "./plugin-contract.mjs";
const packageRoot = resolve(import.meta.dirname, "..");
const repositoryRoot = resolve(packageRoot, "../..");
-const execFileAsync = promisify(execFile);
function validatePath(path) {
const parts = path.split("/");
@@ -87,33 +87,58 @@ export async function buildBundledPlugin({
}),
);
- await rm(destination, { force: true, recursive: true });
- if (files.some((path) => path.startsWith("mcp/"))) {
- const mcpApp = join(source, "mcp-app");
- await execFileAsync(
- process.execPath,
- [
- join(mcpApp, "scripts/build_mcp_app.mjs"),
- "--output",
- join(destination, "mcp"),
- ],
- { cwd: mcpApp, maxBuffer: 10 * 1024 * 1024 },
- );
- }
- for (const { file, mode, path } of sourceFiles) {
- const output = join(destination, path);
- await mkdir(dirname(output), { recursive: true });
- await copyFile(file, output);
- await chmod(output, mode);
- }
+ await mkdir(dirname(destination), { recursive: true });
+ const staging = await mkdtemp(join(dirname(destination), ".plugin-build-"));
+ const staged = join(staging, "bundle");
+ const previous = join(staging, "previous");
+ let preservePrevious = false;
+ try {
+ if (files.some((path) => path.startsWith("mcp/"))) {
+ const mcpApp = join(source, "mcp-app");
+ execFileSync(
+ process.execPath,
+ [
+ join(mcpApp, "scripts/build_mcp_app.mjs"),
+ "--output",
+ join(staged, "mcp"),
+ ],
+ { cwd: mcpApp, stdio: "inherit" },
+ );
+ }
+ for (const { file, mode, path } of sourceFiles) {
+ const output = join(staged, path);
+ await mkdir(dirname(output), { recursive: true });
+ await copyFile(file, output);
+ await chmod(output, mode);
+ }
- const generated = await destinationFiles(destination);
- const expected = [...files].sort();
- if (
- generated.length !== expected.length ||
- generated.some((path, index) => path !== expected[index])
- ) {
- throw new Error("Bundled plugin generated files outside its contract.");
+ const generated = await destinationFiles(staged);
+ const expected = [...files].sort();
+ if (
+ generated.length !== expected.length ||
+ generated.some((path, index) => path !== expected[index])
+ ) {
+ throw new Error("Bundled plugin generated files outside its contract.");
+ }
+ try {
+ await rename(destination, previous);
+ preservePrevious = true;
+ } catch (error) {
+ if (error.code !== "ENOENT") throw error;
+ }
+ try {
+ await rename(staged, destination);
+ } catch (error) {
+ if (preservePrevious) {
+ await rename(previous, destination);
+ preservePrevious = false;
+ }
+ throw error;
+ }
+ preservePrevious = false;
+ } finally {
+ // Leave the previous bundle available if restoring it fails.
+ if (!preservePrevious) await rm(staging, { recursive: true, force: true });
}
return files;
diff --git a/sdk/typescript/src/runtime.ts b/sdk/typescript/src/runtime.ts
index f095d1827..f13cd593d 100644
--- a/sdk/typescript/src/runtime.ts
+++ b/sdk/typescript/src/runtime.ts
@@ -75,6 +75,22 @@ const execFile = promisify(execFileCallback);
export const MARKETPLACE_NAME = "codex-security-sdk";
export const PLUGIN_NAME = "codex-security";
+const MARKETPLACE_MANIFEST = `${JSON.stringify(
+ {
+ name: MARKETPLACE_NAME,
+ interface: { displayName: "Codex Security SDK" },
+ plugins: [
+ {
+ name: PLUGIN_NAME,
+ source: { source: "local", path: `./plugins/${PLUGIN_NAME}` },
+ policy: { installation: "AVAILABLE", authentication: "ON_INSTALL" },
+ category: "Security",
+ },
+ ],
+ },
+ null,
+ 2,
+)}\n`;
const MAX_ZIP_ENTRIES = 4_096;
const MAX_ZIP_CENTRAL_DIRECTORY = 16 * 1024 * 1024;
@@ -2353,19 +2369,15 @@ export async function createMarketplace(
const marketplace = join(codexHome, "sdk-marketplace");
const pluginDestination = join(marketplace, "plugins", PLUGIN_NAME);
await copyPluginTree(root, pluginDestination, signal);
+ await writeMarketplaceManifest(marketplace, signal);
+ return marketplace;
+}
+
+async function writeMarketplaceManifest(
+ marketplace: string,
+ signal?: AbortSignal,
+): Promise<void> {
throwIfSignalAborted(signal);
- const manifest = {
- name: MARKETPLACE_NAME,
- interface: { displayName: "Codex Security SDK" },
- plugins: [
- {
- name: PLUGIN_NAME,
- source: { source: "local", path: `./plugins/${PLUGIN_NAME}` },
- policy: { installation: "AVAILABLE", authentication: "ON_INSTALL" },
- category: "Security",
- },
- ],
- };
const manifestPath = join(
marketplace,
".agents",
@@ -2373,14 +2385,20 @@ export async function createMarketplace(
"marketplace.json",
);
await mkdir(dirname(manifestPath), { recursive: true, mode: 0o700 });
- await writeFile(manifestPath, `${JSON.stringify(manifest, null, 2)}\n`, {
- encoding: "utf8",
- flag: "wx",
- mode: 0o600,
- signal,
- });
- throwIfSignalAborted(signal);
- return marketplace;
+ const temporary = `${manifestPath}.${randomUUID()}.tmp`;
+ try {
+ await writeFile(temporary, MARKETPLACE_MANIFEST, {
+ encoding: "utf8",
+ flag: "wx",
+ mode: 0o600,
+ signal,
+ });
+ throwIfSignalAborted(signal);
+ await rename(temporary, manifestPath);
+ throwIfSignalAborted(signal);
+ } finally {
+ await rm(temporary, { force: true });
+ }
}
export function resolveCodexCommand(
@@ -2488,6 +2506,13 @@ export async function bootstrapPlugin(
await rm(marketplace, { recursive: true, force: true });
}
await createMarketplace(codexHome, root, options.signal);
+ } else if (
+ (await readFile(
+ join(marketplace, ".agents", "plugins", "marketplace.json"),
+ "utf8",
+ ).catch(nullIfMissingFileError)) !== MARKETPLACE_MANIFEST
+ ) {
+ await writeMarketplaceManifest(marketplace, options.signal);
}
const config = await readFile(join(codexHome, "config.toml"), "utf8").catch(
diff --git a/sdk/typescript/tests-ts/build-plugin.test.ts b/sdk/typescript/tests-ts/build-plugin.test.ts
index cf670f145..02eb832f7 100644
--- a/sdk/typescript/tests-ts/build-plugin.test.ts
+++ b/sdk/typescript/tests-ts/build-plugin.test.ts
@@ -1,14 +1,17 @@
import { execFile } from "node:child_process";
import {
chmod,
+ cp,
mkdir,
readFile,
readdir,
stat,
+ symlink,
writeFile,
} from "node:fs/promises";
-import { delimiter, join } from "node:path";
+import { basename, delimiter, join } from "node:path";
import { fileURLToPath } from "node:url";
+import { brotliDecompressSync } from "node:zlib";
import { promisify } from "node:util";
import { afterEach, describe, expect, test } from "bun:test";
import { buildBundledPlugin } from "../scripts/build-plugin.mjs";
@@ -59,63 +62,189 @@ async function snapshot(root: string) {
afterEach(temporaryDirectories.cleanup);
describe("bundled plugin build", () => {
- test("builds the MCP runtime without invoking an npm launcher", async () => {
- const root = await temporaryDirectory();
- const bin = join(root, "bin");
- const launcher = process.platform === "win32" ? "npm.cmd" : "npm";
- await writeFixture(
- bin,
- launcher,
- process.platform === "win32" ? "@exit /b 91\r\n" : "#!/bin/sh\nexit 91\n",
- );
- if (process.platform !== "win32") await chmod(join(bin, launcher), 0o755);
+ test.each(["missing", "stale"])(
+ "builds the MCP runtime from source with %s native wrappers and no npm launcher",
+ async (emitted) => {
+ const root = await temporaryDirectory();
+ const plugin = fileURLToPath(
+ new URL("../../../plugins/codex-security/", import.meta.url),
+ );
+ const source = join(root, "plugin");
+ await cp(join(plugin, "mcp-app"), join(source, "mcp-app"), {
+ recursive: true,
+ filter: (path) =>
+ !["node_modules", ".preview"].includes(basename(path)),
+ });
+ await cp(join(plugin, "schemas"), join(source, "schemas"), {
+ recursive: true,
+ });
+ await writeFixture(
+ source,
+ "scripts/reserved_artifact_paths.json",
+ await readFile(
+ join(plugin, "scripts", "reserved_artifact_paths.json"),
+ "utf8",
+ ),
+ );
+ await symlink(
+ join(plugin, "mcp-app", "node_modules"),
+ join(source, "mcp-app", "node_modules"),
+ "junction",
+ );
+ for (const name of await readdir(join(plugin, "native"))) {
+ if (name.endsWith(".mts")) {
+ await writeFixture(
+ source,
+ `native/${name}`,
+ await readFile(join(plugin, "native", name), "utf8"),
+ );
+ }
+ }
+ await symlink(
+ join(plugin, "native", "prebuilt"),
+ join(source, "native", "prebuilt"),
+ "junction",
+ );
+ await writeFixture(
+ source,
+ "plugin-files.json",
+ await readFile(join(plugin, "plugin-files.json"), "utf8"),
+ );
+ const platformSource = join(source, "native", "platform.mts");
+ await writeFile(
+ platformSource,
+ `Object.defineProperty(globalThis, "syntheticNativeWrapper", { value: "fresh-wrapper-source" });\n${await readFile(platformSource, "utf8")}`,
+ );
+ if (emitted === "stale") {
+ await writeFixture(
+ source,
+ "native/platform.mjs",
+ 'throw new Error("stale-wrapper-output");\n',
+ );
+ }
+ const bin = join(root, "bin");
+ const launcher = process.platform === "win32" ? "npm.cmd" : "npm";
+ await writeFixture(
+ bin,
+ launcher,
+ process.platform === "win32"
+ ? "@exit /b 91\r\n"
+ : "#!/bin/sh\nexit 91\n",
+ );
+ if (process.platform !== "win32") await chmod(join(bin, launcher), 0o755);
+
+ const destination = join(root, "mcp");
+ await execFileAsync(
+ "node",
+ [
+ join(source, "mcp-app", "scripts", "build_mcp_app.mjs"),
+ "--output",
+ destination,
+ ],
+ {
+ env: {
+ ...process.env,
+ PATH: [bin, process.env["PATH"]].filter(Boolean).join(delimiter),
+ },
+ },
+ );
- const destination = join(root, "mcp");
- await execFileAsync(
- "node",
- [
- fileURLToPath(
+ const contract = JSON.parse(
+ await readFile(
new URL(
- "../../../plugins/codex-security/mcp-app/scripts/build_mcp_app.mjs",
+ "../../../plugins/codex-security/plugin-files.json",
import.meta.url,
),
+ "utf8",
),
- "--output",
- destination,
- ],
- {
- env: {
- ...process.env,
- PATH: [bin, process.env["PATH"]].filter(Boolean).join(delimiter),
- },
- },
- );
-
- const contract = JSON.parse(
- await readFile(
- new URL(
- "../../../plugins/codex-security/plugin-files.json",
- import.meta.url,
+ ) as { shippedExact: string[] };
+ expect(await files(destination)).toEqual(
+ contract.shippedExact
+ .filter((path) => path.startsWith("mcp/"))
+ .map((path) => path.slice(4))
+ .sort(),
+ );
+ const chunks = (await readdir(destination))
+ .filter((name) => name.startsWith("helpers.mjs.br.part-"))
+ .sort();
+ const runtime = brotliDecompressSync(
+ Buffer.concat(
+ await Promise.all(
+ chunks.map((name) => readFile(join(destination, name))),
+ ),
),
- "utf8",
- ),
- ) as { shippedExact: string[] };
- expect(await files(destination)).toEqual(
- contract.shippedExact
- .filter((path) => path.startsWith("mcp/"))
- .map((path) => path.slice(4))
- .sort(),
- );
- const helper = await execFileAsync("node", [
- join(destination, "helpers.mjs"),
- "resolve-security-md",
- "--repo",
- root,
- "--list",
- ]);
- expect(helper.stdout).toBe("[]\n");
- expect(helper.stderr).toBe("");
- });
+ ).toString("utf8");
+ expect(runtime.includes("fresh-wrapper-source")).toBe(true);
+ expect(runtime.includes("stale-wrapper-output")).toBe(false);
+ const helper = await execFileAsync("node", [
+ join(destination, "helpers.mjs"),
+ "resolve-security-md",
+ "--repo",
+ root,
+ "--list",
+ ]);
+ expect(helper.stdout).toBe("[]\n");
+ expect(helper.stderr).toBe("");
+ },
+ );
+
+ test.each(["compiler", "file contract"])(
+ "preserves the previous bundle when the %s fails",
+ async (failure) => {
+ const root = await temporaryDirectory();
+ const packageRoot = join(root, "sdk", "typescript");
+ const source = join(root, "plugins", "codex-security");
+ const destination = join(packageRoot, "_bundled_plugin");
+ for (const script of ["build-plugin", "plugin-contract", "is-main"]) {
+ await writeFixture(
+ packageRoot,
+ `scripts/${script}.mjs`,
+ await readFile(
+ new URL(`../scripts/${script}.mjs`, import.meta.url),
+ "utf8",
+ ),
+ );
+ }
+ await writeFixture(source, ".codex-plugin/plugin.json", "{}\n");
+ await writeFixture(
+ source,
+ "plugin-files.json",
+ JSON.stringify({
+ externalOwnedExact: [".codex-plugin/plugin.json"],
+ shippedExact: ["mcp/server.mjs"],
+ }),
+ );
+ await writeFixture(
+ source,
+ "mcp-app/scripts/build_mcp_app.mjs",
+ failure === "compiler"
+ ? 'console.log("Synthetic compiler diagnostic"); process.exit(2);\n'
+ : `import { mkdir, writeFile } from "node:fs/promises";
+import { join } from "node:path";
+const output = process.argv[process.argv.indexOf("--output") + 1];
+await mkdir(output, { recursive: true });
+await writeFile(join(output, "server.mjs"), "generated runtime");
+await writeFile(join(output, "unexpected.txt"), "undeclared output");\n`,
+ );
+ await writeFixture(
+ destination,
+ "server.mjs",
+ "export const usable = true;\n",
+ );
+ const previous = await snapshot(destination);
+ const result = await execFileAsync("node", [
+ join(packageRoot, "scripts", "build-plugin.mjs"),
+ ]).catch((error) => error);
+ expect(result.code).toBe(1);
+ if (failure === "compiler")
+ expect(result.stdout).toContain("Synthetic compiler diagnostic");
+ else
+ expect(result.stderr).toContain(
+ "Bundled plugin generated files outside its contract.",
+ );
+ expect(await snapshot(destination)).toEqual(previous);
+ },
+ );
test("builds from a source snapshot without Git metadata", async () => {
const root = await temporaryDirectory();
diff --git a/sdk/typescript/tests-ts/runtime-plugin-reuse.test.ts b/sdk/typescript/tests-ts/runtime-plugin-reuse.test.ts
index 9b597a3c1..2ff3ae2de 100644
--- a/sdk/typescript/tests-ts/runtime-plugin-reuse.test.ts
+++ b/sdk/typescript/tests-ts/runtime-plugin-reuse.test.ts
@@ -1,12 +1,23 @@
import { spawn } from "node:child_process";
import { once } from "node:events";
-import { cp, mkdir, rm, writeFile } from "node:fs/promises";
-import { join } from "node:path";
-import { expect, test } from "bun:test";
+import * as fsPromises from "node:fs/promises";
+import { cp, mkdir, readFile, rm, writeFile } from "node:fs/promises";
+import { dirname, join } from "node:path";
+import { expect, spyOn, test } from "bun:test";
import { bootstrapPlugin } from "../src/runtime.js";
import { temporaryDirectory } from "./support/temporary-directories.js";
-test("repeated bootstrap preserves the plugin directory used by an active worker", async () => {
+test.each(["unchanged", "missing", "truncated"] as const)(
+ "%s manifest preserves active workers",
+ (metadata) => checkWorkerReuse(metadata),
+);
+
+test.each(["missing", "truncated"] as const)(
+ "concurrent %s manifest repairs preserve active workers",
+ (metadata) => checkWorkerReuse(metadata, true),
+);
+
+async function checkWorkerReuse(metadata: string, concurrent = false) {
const root = await temporaryDirectory("codex-security-plugin-worker-", true);
try {
const selected = join(root, "plugin");
@@ -41,6 +52,13 @@ test("repeated bootstrap preserves the plugin directory used by an active worker
};
const options = { codexCommand: { command: process.execPath }, runCodex };
const first = await bootstrapPlugin(home, selected, options);
+ const manifest = join(
+ marketplace,
+ ".agents",
+ "plugins",
+ "marketplace.json",
+ );
+ const expectedManifest = await readFile(manifest, "utf8");
const worker = spawn(
process.execPath,
[
@@ -65,12 +83,49 @@ test("repeated bootstrap preserves the plugin directory used by an active worker
try {
const [ready] = await Promise.race([once(worker, "message"), exited]);
expect(ready).toBe("ready");
- const second = await bootstrapPlugin(home, selected, options);
+ if (metadata === "missing") await rm(manifest);
+ else if (metadata === "truncated") await writeFile(manifest, "{");
+ const originalWrite = fsPromises.writeFile;
+ let writes = 0;
+ let releaseWrites = () => {};
+ const bothWriting = new Promise<void>((resolve) => {
+ releaseWrites = resolve;
+ });
+ const write = concurrent
+ ? spyOn(fsPromises, "writeFile").mockImplementation(async (...args) => {
+ // Both repairs reach the filesystem before either write completes.
+ if (dirname(String(args[0])) === dirname(manifest)) {
+ if (++writes === 2) releaseWrites();
+ await bothWriting;
+ }
+ return originalWrite(...args);
+ })
+ : undefined;
+ try {
+ const results = await Promise.allSettled(
+ Array.from({ length: concurrent ? 2 : 1 }, () =>
+ bootstrapPlugin(home, selected, options),
+ ),
+ );
+ for (const result of results) {
+ expect(result.status).toBe("fulfilled");
+ if (result.status === "fulfilled") {
+ expect(result.value.installedRoot).toBe(first.installedRoot);
+ }
+ }
+ } finally {
+ write?.mockRestore();
+ }
const response = once(worker, "message");
worker.send("read");
const [content] = await Promise.race([response, exited]);
expect(content).toBe(helper);
- expect(second.installedRoot).toBe(first.installedRoot);
+ expect(installs).toBe(1);
+ expect(await readFile(manifest, "utf8")).toBe(expectedManifest);
+ expect(await fsPromises.readdir(dirname(manifest))).toEqual([
+ "marketplace.json",
+ ]);
+ await bootstrapPlugin(home, selected, options);
expect(installs).toBe(1);
} finally {
if (worker.exitCode === null && worker.signalCode === null) worker.kill();
@@ -79,4 +134,4 @@ test("repeated bootstrap preserves the plugin directory used by an active worker
} finally {
await rm(root, { recursive: true, force: true });
}
-});
+}
diff --git a/sdk/typescript/tests-ts/runtime.test.ts b/sdk/typescript/tests-ts/runtime.test.ts
index 4bf2ba487..486d092ee 100644
--- a/sdk/typescript/tests-ts/runtime.test.ts
+++ b/sdk/typescript/tests-ts/runtime.test.ts
@@ -1639,6 +1639,14 @@ describe("plugin runtime preparation", () => {
const { home, staged, installed, record, calls, bootstrap } =
await fixture();
const helper = join(installed, "scripts", "helper.py");
+ const manifest = join(
+ home,
+ "sdk-marketplace",
+ ".agents",
+ "plugins",
+ "marketplace.json",
+ );
+ const expectedManifest = await readFile(manifest, "utf8");
switch (damage) {
case "missing file":
await rm(helper);
@@ -1672,6 +1680,7 @@ describe("plugin runtime preparation", () => {
expect(calls.filter((args) => args[1] === "add")).toHaveLength(2);
expect(await readFile(helper, "utf8")).toBe("print('ok')\n");
expect(existsSync(join(staged, "stale.py"))).toBe(false);
+ expect(await readFile(manifest, "utf8")).toBe(expectedManifest);
expect(JSON.parse(await readFile(record, "utf8"))).toEqual({
installedPath: installed,
version: "1.2.3",