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(runtime): reuse unchanged plugins across concurrent scans (#1082)
Anirudh N Shandilyae9cac9f691be
diff --git a/sdk/typescript/src/runtime.ts b/sdk/typescript/src/runtime.tsindex f7850203a..e28075492 100644--- a/sdk/typescript/src/runtime.ts+++ b/sdk/typescript/src/runtime.ts@@ -2514,28 +2514,37 @@ export async function bootstrapPlugin(: await pluginMetadata(join(marketplace, "plugins", PLUGIN_NAME)).catch(() => null,);- if (staged?.version !== version) {+ const stagedRoot = join(marketplace, "plugins", PLUGIN_NAME);+ const stagedMatches =+ staged?.version === version &&+ (await pluginContentsMatch(root, stagedRoot, options.signal));++ if (!stagedMatches) {if (existing !== null) {await rm(marketplace, { recursive: true, force: true });}await createMarketplace(codexHome, root, options.signal);}+const config = await readFile(join(codexHome, "config.toml"), "utf8").catch((error: unknown) => {if (nodeErrorCode(error) === "ENOENT") return "";throw error;},);- const marketplaces = parse(config)["marketplaces"];+ const configuration = parse(config);+ const marketplaces = configuration["marketplaces"];const registration = isRecord(marketplaces)? marketplaces[MARKETPLACE_NAME]: undefined;- if (- !isRecord(registration) ||- registration["source_type"] !== "local" ||- typeof registration["source"] !== "string" ||- !(await sameFile(registration["source"], marketplace))- ) {++ const registered =+ isRecord(registration) &&+ registration["source_type"] === "local" &&+ typeof registration["source"] === "string" &&+ (await sameFile(registration["source"], marketplace));++ if (!registered) {await run(command,["plugin", "marketplace", "add", marketplace],@@ -2543,12 +2552,56 @@ export async function bootstrapPlugin(options.signal,);}++ const installRecord = join(marketplace, "installed-plugin.json");+ const previous: unknown = await readFile(installRecord, "utf8")+ .then((value) => JSON.parse(value) as unknown)+ .catch((error: unknown) => {+ if (nodeErrorCode(error) === "ENOENT" || error instanceof SyntaxError) {+ return null;+ }+ throw error;+ });++ const plugins = configuration["plugins"];+ const plugin = isRecord(plugins)+ ? plugins[`${PLUGIN_NAME}@${MARKETPLACE_NAME}`]+ : undefined;++ // Codex replaces the shared install even at the same version. Reuse it so+ // workers from an earlier scan can keep using their plugin files.+ if (+ stagedMatches &&+ registered &&+ isRecord(plugin) &&+ plugin["enabled"] === true &&+ isRecord(previous) &&+ typeof previous["installedPath"] === "string" &&+ previous["version"] === version &&+ (await pluginContentsMatch(+ root,+ previous["installedPath"],+ options.signal,+ true,+ ))+ ) {+ return {+ pluginRoot: root,+ marketplaceRoot: marketplace,+ installedRoot: previous["installedPath"],+ marketplaceName: MARKETPLACE_NAME,+ name,+ version,+ };+ }+const output = await run(command,["plugin", "add", "--json", `${PLUGIN_NAME}@${MARKETPLACE_NAME}`],environment,options.signal,);+let installed: unknown;try {installed = JSON.parse(output);@@ -2558,6 +2611,7 @@ export async function bootstrapPlugin({ cause: error },);}+if (!isRecord(installed) ||typeof installed["installedPath"] !== "string" ||@@ -2567,6 +2621,16 @@ export async function bootstrapPlugin("Codex plugin install did not return the selected plugin path and version.",);}++ await writeFile(+ installRecord,+ JSON.stringify({+ installedPath: installed["installedPath"],+ version,+ }),+ { mode: 0o600, signal: options.signal },+ );+return {pluginRoot: root,marketplaceRoot: marketplace,@@ -2577,6 +2641,71 @@ export async function bootstrapPlugin(};}+async function pluginContentsMatch(+ source: string,+ destination: string,+ signal?: AbortSignal,+ allowExtraFiles = false,+): Promise<boolean> {+ throwIfSignalAborted(signal);++ const sourceMetadata = await lstat(source);+ const destinationMetadata = await lstat(destination).catch(+ (error: unknown) => {+ if (["ENOENT", "ENOTDIR"].includes(nodeErrorCode(error) ?? "")) {+ return null;+ }+ throw error;+ },+ );++ if (destinationMetadata === null) return false;++ if (sourceMetadata.isFile() && destinationMetadata.isFile()) {+ if (+ sourceMetadata.size !== destinationMetadata.size ||+ (sourceMetadata.mode & 0o111) !== (destinationMetadata.mode & 0o111)+ ) {+ return false;+ }++ const [sourceBytes, destinationBytes] = await Promise.all([+ readFile(source, { signal }),+ readFile(destination, { signal }),+ ]);++ return sourceBytes.equals(destinationBytes);+ }++ if (!sourceMetadata.isDirectory() || !destinationMetadata.isDirectory()) {+ return false;+ }++ const entries = await readdir(source);++ if (+ !allowExtraFiles &&+ entries.length !== (await readdir(destination)).length+ ) {+ return false;+ }++ for (const entry of entries) {+ if (+ !(await pluginContentsMatch(+ join(source, entry),+ join(destination, entry),+ signal,+ allowExtraFiles,+ ))+ ) {+ return false;+ }+ }++ return true;+}+export async function pluginMetadata(root: string,): Promise<{ name: typeof PLUGIN_NAME; version: string }> {diff --git a/sdk/typescript/tests-ts/runtime-plugin-reuse.test.ts b/sdk/typescript/tests-ts/runtime-plugin-reuse.test.tsnew file mode 100644index 000000000..9d34f4bff--- /dev/null+++ b/sdk/typescript/tests-ts/runtime-plugin-reuse.test.ts@@ -0,0 +1,84 @@+import { spawn } from "node:child_process";+import { once } from "node:events";+import { cp, mkdir, mkdtemp, realpath, rm, writeFile } from "node:fs/promises";+import { tmpdir } from "node:os";+import { join } from "node:path";+import { expect, test } from "bun:test";+import { bootstrapPlugin } from "../src/runtime.js";++test("repeated bootstrap preserves the plugin directory used by an active worker", async () => {+ const root = await realpath(+ await mkdtemp(join(tmpdir(), "codex-security-plugin-worker-")),+ );+ try {+ const selected = join(root, "plugin");+ const home = join(root, "home");+ const marketplace = join(home, "sdk-marketplace");+ const installed = join(home, "plugins", "cache", "codex-security", "1.2.3");+ const helper = "worker helper remains available\n";+ await mkdir(join(selected, ".codex-plugin"), { recursive: true });+ await writeFile(+ join(selected, ".codex-plugin", "plugin.json"),+ JSON.stringify({ name: "codex-security", version: "1.2.3" }),+ );+ await mkdir(join(selected, "scripts"));+ await writeFile(join(selected, "scripts", "helper.txt"), helper);++ let installs = 0;+ const runCodex: NonNullable<+ NonNullable<Parameters<typeof bootstrapPlugin>[2]>["runCodex"]+ > = async (_command, args) => {+ const addingMarketplace = args[1] === "marketplace";+ await writeFile(+ join(home, "config.toml"),+ `[marketplaces.codex-security-sdk]\nsource_type = "local"\nsource = ${JSON.stringify(marketplace)}\n[plugins."codex-security@codex-security-sdk"]\nenabled = ${!addingMarketplace}\n`,+ );+ if (addingMarketplace) return "";+ installs += 1;+ await rm(installed, { recursive: true, force: true });+ await cp(join(marketplace, "plugins", "codex-security"), installed, {+ recursive: true,+ });+ return JSON.stringify({ installedPath: installed, version: "1.2.3" });+ };+ const options = { codexCommand: { command: process.execPath }, runCodex };+ const first = await bootstrapPlugin(home, selected, options);+ const worker = spawn(+ process.execPath,+ [+ "--eval",+ `process.once("message", () => {+ try {+ process.send(require("node:fs").readFileSync("scripts/helper.txt", "utf8"));+ } catch (error) {+ process.send({ error: String(error) });+ } finally {+ process.disconnect();+ }+ });+ process.send("ready");`,+ ],+ {+ cwd: first.installedRoot,+ stdio: ["ignore", "ignore", "ignore", "ipc"],+ },+ );+ const exited = once(worker, "exit");+ try {+ const [ready] = await Promise.race([once(worker, "message"), exited]);+ expect(ready).toBe("ready");+ const second = await bootstrapPlugin(home, selected, options);+ 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);+ } finally {+ if (worker.exitCode === null && worker.signalCode === null) worker.kill();+ await exited;+ }+ } 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.tsindex f47aef5ff..961fc1a49 100644--- a/sdk/typescript/tests-ts/runtime.test.ts+++ b/sdk/typescript/tests-ts/runtime.test.ts@@ -5,6 +5,7 @@ import { existsSync, renameSync, symlinkSync } from "node:fs";import {chmod,copyFile,+ cp,link,lstat,mkdir,@@ -1693,6 +1694,147 @@ describe("plugin runtime preparation", () => {]);});+ describe("installed plugin reuse", () => {+ async function fixture() {+ const root = await temporaryDirectory();+ const selected = await plugin(root);+ const home = join(root, "home");+ const marketplace = join(home, "sdk-marketplace");+ const staged = join(marketplace, "plugins", "codex-security");+ const installed = join(home, "installed", "1.2.3");+ const record = join(marketplace, "installed-plugin.json");+ const configuration = `[marketplaces.codex-security-sdk]\nsource_type = "local"\nsource = ${JSON.stringify(marketplace)}\n`;+ const calls: string[][] = [];+ await mkdir(home);+ const bootstrap = () =>+ bootstrapPlugin(home, selected, {+ codexCommand: { command: "/codex" },+ runCodex: async (_command, args) => {+ calls.push([...args]);+ if (args[1] === "marketplace") {+ await writeFile(join(home, "config.toml"), configuration);+ return "";+ }+ await rm(installed, { recursive: true, force: true });+ await cp(staged, installed, { recursive: true });+ await writeFile(+ join(home, "config.toml"),+ `${configuration}\n[plugins."codex-security@codex-security-sdk"]\nenabled = true\n`,+ );+ return JSON.stringify({+ installedPath: installed,+ version: "1.2.3",+ });+ },+ });+ await bootstrap();+ return { selected, home, staged, installed, record, calls, bootstrap };+ }++ test("preserves generated installed files during reuse", async () => {+ const { installed, calls, bootstrap } = await fixture();+ const generated = join(installed, "scripts", "__pycache__", "helper.pyc");+ await mkdir(dirname(generated));+ await writeFile(generated, "generated cache");++ expect((await bootstrap()).installedRoot).toBe(installed);+ expect(calls.filter((args) => args[1] === "add")).toHaveLength(1);+ expect(await readFile(generated, "utf8")).toBe("generated cache");+ });++ test.each(["changed", "added", "removed"] as const)(+ "refreshes %s same-version source files",+ async (change) => {+ const { selected, staged, installed, bootstrap } = await fixture();+ const name = change === "added" ? "added.py" : "helper.py";+ const source = join(selected, "scripts", name);+ if (change === "removed") {+ await rm(source);+ } else {+ // Same length as the original helper, so byte comparison must detect it.+ await writeFile(source, "print('no')\n");+ }++ await bootstrap();++ for (const root of [staged, installed]) {+ const path = join(root, "scripts", name);+ if (change === "removed") {+ expect(existsSync(path)).toBe(false);+ } else {+ expect(await readFile(path, "utf8")).toBe("print('no')\n");+ }+ }+ },+ );++ testPosix("refreshes changed source executable permissions", async () => {+ const { selected, staged, installed, bootstrap } = await fixture();+ await chmod(join(selected, "scripts", "helper.py"), 0o750);++ await bootstrap();++ for (const root of [staged, installed]) {+ expect(+ (await stat(join(root, "scripts", "helper.py"))).mode & 0o111,+ ).toBe(0o110);+ }+ });++ test.each([+ "missing file",+ "changed file",+ "missing record",+ "invalid record",+ "disabled plugin",+ "missing registration",+ "extra staged file",+ ])("repairs %s before reusing the installation", async (damage) => {+ const { home, staged, installed, record, calls, bootstrap } =+ await fixture();+ const helper = join(installed, "scripts", "helper.py");+ switch (damage) {+ case "missing file":+ await rm(helper);+ break;+ case "changed file":+ await writeFile(helper, "print('no')\n");+ break;+ case "missing record":+ await rm(record);+ break;+ case "invalid record":+ await writeFile(record, "{");+ break;+ case "disabled plugin": {+ const config = await readFile(join(home, "config.toml"), "utf8");+ await writeFile(+ join(home, "config.toml"),+ config.replace("enabled = true", "enabled = false"),+ );+ break;+ }+ case "missing registration":+ await writeFile(join(home, "config.toml"), "");+ break;+ case "extra staged file":+ await writeFile(join(staged, "stale.py"), "pass\n");+ break;+ }++ await bootstrap();+ 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(JSON.parse(await readFile(record, "utf8"))).toEqual({+ installedPath: installed,+ version: "1.2.3",+ });+ await bootstrap();+ expect(calls.filter((args) => args[1] === "add")).toHaveLength(2);+ });+ });+test("does not preserve a different marketplace when numeric identities collide", async () => {const root = await temporaryDirectory();const home = join(root, "home");