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: Python cache files break plugin integrity checks (#1625)
ianw-oaid39c305e03ae
diff --git a/plugins/codex-security/mcp-app/scripts/test_host_build.mts b/plugins/codex-security/mcp-app/scripts/test_host_build.mtsindex 49793937e..8d75c34b4 100644--- a/plugins/codex-security/mcp-app/scripts/test_host_build.mts+++ b/plugins/codex-security/mcp-app/scripts/test_host_build.mts@@ -10,7 +10,7 @@ import {rm,writeFile,} from "node:fs/promises";-import { join, relative, resolve, sep } from "node:path";+import { dirname, join, relative, resolve, sep } from "node:path";import test from "node:test";const plugin = resolve(import.meta.dirname, "../..");@@ -113,6 +113,27 @@ test("builds and loads the host runtime from standalone plugin source", async (t},);assert.equal(launcherOutput, "[]\n");++ for (const file of contract.shippedExact.filter(+ (file: string) => !file.startsWith("mcp/"),+ )) {+ await mkdir(dirname(join(output, file)), { recursive: true });+ await copyFile(join(plugin, file), join(output, file));+ }+ runNode(+ [+ "--experimental-strip-types",+ "--test",+ "mcp-app/tests/test_plugin_bytecode.ts",+ ],+ {+ env: {+ ...process.env,+ NODE_TEST_CONTEXT: undefined,+ CODEX_SECURITY_TEST_PLUGIN_ROOT: output,+ },+ },+ );});test("rejects an unsupported native host before replacing existing output", async (t) => {diff --git a/plugins/codex-security/mcp-app/src/artifact-inventory.ts b/plugins/codex-security/mcp-app/src/artifact-inventory.tsindex 0ce3ac300..01d5d3c07 100644--- a/plugins/codex-security/mcp-app/src/artifact-inventory.ts+++ b/plugins/codex-security/mcp-app/src/artifact-inventory.ts@@ -55,6 +55,7 @@ export async function prepareCodexSecurityReviewItems("generate_in_scope_files.py",);const arguments_ = [+ "-B",helper,"--repo",context.repoRoot,diff --git a/plugins/codex-security/mcp-app/src/python_command.ts b/plugins/codex-security/mcp-app/src/python_command.tsindex 124417f41..6919de2c6 100644--- a/plugins/codex-security/mcp-app/src/python_command.ts+++ b/plugins/codex-security/mcp-app/src/python_command.ts@@ -115,7 +115,7 @@ export function runPythonWithInput(label: string,): Promise<string> {return new Promise((resolve, reject) => {- const child = spawn(python, args, {+ const child = spawn(python, ["-B", ...args], {stdio: ["pipe", "pipe", "pipe"],windowsHide: true,});diff --git a/plugins/codex-security/mcp-app/src/workbench-client.ts b/plugins/codex-security/mcp-app/src/workbench-client.tsindex 7da2a8073..70392d585 100644--- a/plugins/codex-security/mcp-app/src/workbench-client.ts+++ b/plugins/codex-security/mcp-app/src/workbench-client.ts@@ -88,7 +88,8 @@ async function executeWorkbench(const execution = execFileAsync(pythonCommand,[- ...(options.isolatedPython ? ["-I", "-X", "utf8", "-B"] : []),+ ...(options.isolatedPython ? ["-I", "-X", "utf8"] : []),+ "-B","-c",WORKBENCH_PYTHON,workbenchScriptPath(),diff --git a/plugins/codex-security/mcp-app/tests/test_artifact_inventory.ts b/plugins/codex-security/mcp-app/tests/test_artifact_inventory.tsindex cd746f175..c2c31f4fd 100644--- a/plugins/codex-security/mcp-app/tests/test_artifact_inventory.ts+++ b/plugins/codex-security/mcp-app/tests/test_artifact_inventory.ts@@ -3,6 +3,8 @@ import assert from "node:assert/strict";import { execFile as nodeExecFile } from "node:child_process";import {chmod,+ cp,+ readdir,mkdir,readFile,symlink,@@ -23,6 +25,7 @@ const inventory = await importSource(try {await testSchemasAreBoundAndExact();await testPrepareUsesTheExistingStandardGenerator();+ await testPrepareDoesNotWritePluginBytecode();if (!["win32", "darwin"].includes(process.platform))await testNonUtf8InventoryPaths();await testPrepareListsIgnoredTrackedFilesOnce();@@ -128,6 +131,28 @@ async function testPrepareUsesTheExistingStandardGenerator() {);}+async function testPrepareDoesNotWritePluginBytecode() {+ const fixture = await createFixture("plugin bytecode");+ const pluginRoot = path.join(fixture.root, "plugin");+ const scripts = path.join(pluginRoot, "scripts");+ await cp(path.resolve(import.meta.dirname, "../../scripts"), scripts, {+ recursive: true,+ filter: (file) => path.basename(file) !== "__pycache__",+ });+ await fixture.writeRepositoryFile("example.py", "value = 1\n");+ const result = await inventory.prepareCodexSecurityReviewItems({+ ...fixture.scan,+ pluginRoot,+ });+ assert.equal(result.reviewItemsTotal, 1);+ assert.equal(+ (await readdir(scripts, { recursive: true })).some((file) =>+ file.endsWith(".pyc"),+ ),+ false,+ );+}+async function testNonUtf8InventoryPaths() {const fixture = await createFixture("non-UTF-8 repository names");const rawPath = Buffer.concat([diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_stdio_lifecycle.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_stdio_lifecycle.tsindex eacdd8b75..78ef4c306 100644--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_stdio_lifecycle.ts+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_stdio_lifecycle.ts@@ -944,9 +944,9 @@ model_reasoning_summary = "none");assert.ok(launches.length > 0);for (const launch of launches) {- assert.equal(launch.args[0], "-c");+ assert.deepEqual(launch.args.slice(0, 2), ["-B", "-c"]);assert.equal(launch.args.at(-1), workbenchPath);- assert.equal(launch.args.length, 3);+ assert.equal(launch.args.length, 4);assert.equal(launch.cwd, pluginRoot);}}diff --git a/plugins/codex-security/mcp-app/tests/test_plugin_bytecode.ts b/plugins/codex-security/mcp-app/tests/test_plugin_bytecode.tsnew file mode 100644index 000000000..67bfeb06e--- /dev/null+++ b/plugins/codex-security/mcp-app/tests/test_plugin_bytecode.ts@@ -0,0 +1,84 @@+import assert from "node:assert/strict";+import { createHash } from "node:crypto";+import { cp, mkdir, readFile, readdir, rm, writeFile } from "node:fs/promises";+import { basename, join, relative, resolve } from "node:path";+import test from "node:test";+import { assertNoError } from "./assertions.ts";+import { startRpcServer } from "./support/rpc-server.ts";+import { temporaryDirectory } from "./support/temporary-directories.ts";++const source =+ process.env.CODEX_SECURITY_TEST_PLUGIN_ROOT ??+ resolve(import.meta.dirname, "../../../../sdk/typescript/_bundled_plugin");++test("repeated MCP launches leave the installed plugin unchanged", async (t) => {+ const runtime = await temporaryDirectory(+ "codex-security-plugin-bytecode-",+ true,+ );+ t.after(() => rm(runtime, { recursive: true, force: true }));+ const output = join(runtime, "plugin with spaces");+ await cp(source, output, {+ recursive: true,+ filter: (file) => basename(file) !== "__pycache__",+ });+ const windows = process.platform === "win32";+ const launcher = `launch_codex_security_mcp${windows ? ".cmd" : ""}`;+ const snapshot = async () => {+ const files = await readdir(output, {+ recursive: true,+ withFileTypes: true,+ });+ const hashes: Record<string, string> = {};+ for (const file of files.filter((file) => file.isFile())) {+ const name = join(file.parentPath, file.name);+ hashes[relative(output, name)] = createHash("sha256")+ .update(await readFile(name))+ .digest("hex");+ }+ return hashes;+ };+ const before = await snapshot();+ const target = join(runtime, "target");+ await mkdir(target);+ await writeFile(join(target, "example.py"), "value = 1\n");+ for (let launch = 0; launch < 2; launch++) {+ const server = startRpcServer({+ command: windows ? (process.env.ComSpec ?? "cmd.exe") : "sh",+ args: windows+ ? ["/d", "/s", "/c", `scripts\\${launcher} --stdio`]+ : [`scripts/${launcher}`, "--stdio"],+ cwd: output,+ env: {+ ...process.env,+ CODEX_MCP_NODE_PATH: process.execPath,+ CODEX_SECURITY_PLUGIN_ROOT: output,+ CODEX_SECURITY_STATE_DIR: join(runtime, "state"),+ CODEX_SECURITY_SCAN_ROOT: join(runtime, "scans"),+ PYTHONDONTWRITEBYTECODE: "",+ },+ });+ try {+ assertNoError(await server.initialize("plugin-bytecode-test"));+ assertNoError(+ await server.callTool(2, {+ name: "inspect_codex_security_target",+ arguments: { targetPath: target },+ }),+ );+ assertNoError(+ await server.callTool(3, {+ name: "list_codex_security_scans",+ arguments: {},+ }),+ );+ } finally {+ await server.stop();+ }+ assert.deepEqual(+ await snapshot(),+ before,+ "MCP helpers must leave plugin files unchanged",+ );+ }+});diff --git a/plugins/codex-security/mcp-app/tests/test_python_command.ts b/plugins/codex-security/mcp-app/tests/test_python_command.tsindex fe738f8cd..f5a86035c 100644--- a/plugins/codex-security/mcp-app/tests/test_python_command.ts+++ b/plugins/codex-security/mcp-app/tests/test_python_command.ts@@ -139,6 +139,8 @@ assert.equal(overrideProbeCount, 0);const executableFixtureRoot = await mkdtemp(path.join(tmpdir(), "codex-security-python-command-"),);+const python =+ process.env.PYTHON || (process.platform === "win32" ? "python" : "python3");const previousPython = process.env.PYTHON;const previousResolvedPython = process.env.CODEX_SECURITY_PYTHON_COMMAND;try {@@ -162,25 +164,30 @@ try {"/custom/python",);const input = "λ😀\n".repeat(250_000);- assert.equal(- await runPythonWithInput(- await resolvePythonCommand(),- ["-e", "process.stdin.pipe(process.stdout)"],- Buffer.from(input),- "Fixture helper",- ),- input,- );+ for (const flags of [[], ["-I"]]) {+ assert.equal(+ await runPythonWithInput(+ python,+ [+ ...flags,+ "-c",+ "import sys\nassert sys.dont_write_bytecode\nsys.stdout.buffer.write(sys.stdin.buffer.read())",+ ],+ Buffer.from(input),+ "Fixture helper",+ ),+ input,+ );+ }delete process.env.CODEX_SECURITY_PYTHON_COMMAND;assert.equal(await resolvePythonCommand(), fileCandidate);const diagnostic = "--provider-error EACCES: λ😀\npermission denied";await assert.rejects(runPythonWithInput(- process.execPath,+ python,[- "-e",- "process.stdin.resume(); process.stdin.on('end', () => { const text = Buffer.from(process.argv[1]); process.stderr.write(text.subarray(0, 26)); setTimeout(() => { process.stderr.write(text.subarray(26)); process.exitCode = 2; }, 1); });",- "--",+ "-c",+ "import sys\nsys.stdin.buffer.read()\nsys.stderr.buffer.write(sys.argv[1].encode('utf-8'))\nsys.exit(2)",diagnostic,],"",@@ -190,11 +197,10 @@ try {);await assert.rejects(runPythonWithInput(- process.execPath,+ python,[- "-e",- "process.stderr.write(process.argv[1]); process.exit(1);",- "--",+ "-c",+ "import sys\nsys.stderr.buffer.write(sys.argv[1].encode('utf-8'))\nsys.exit(1)",diagnostic,],input,@@ -203,12 +209,7 @@ try {(error: Error) => error.message === diagnostic,);await assert.rejects(- runPythonWithInput(- process.execPath,- ["-e", "process.exit(0)"],- input,- "Fixture helper",- ),+ runPythonWithInput(python, ["-c", "pass"], input, "Fixture helper"),(error: NodeJS.ErrnoException) => typeof error.code === "string",);await assert.rejects(diff --git a/plugins/codex-security/mcp-app/tests/test_threat_model_document.ts b/plugins/codex-security/mcp-app/tests/test_threat_model_document.tsindex 4d4c9584f..dfbab5ebb 100644--- a/plugins/codex-security/mcp-app/tests/test_threat_model_document.ts+++ b/plugins/codex-security/mcp-app/tests/test_threat_model_document.ts@@ -298,6 +298,7 @@ test("overlapping draft projections retain the latest committed model", async (tt.mock.method(childProcess, "spawn", (command: string, args: string[]) => {assert.equal(command, "fixture-python");assert.deepEqual(args, [+ "-B","-I","-X","utf8",diff --git a/plugins/codex-security/mcp-app/tests/test_workbench_process.ts b/plugins/codex-security/mcp-app/tests/test_workbench_process.tsindex 913668023..8dbdba2bc 100644--- a/plugins/codex-security/mcp-app/tests/test_workbench_process.ts+++ b/plugins/codex-security/mcp-app/tests/test_workbench_process.ts@@ -169,7 +169,7 @@ for (const variable of ["PYTHONPATH", "PYTHONUSERBASE"]) {const executeFixture = await fixtureWorkbench(root,- 'import json\ndef main():\n print(json.dumps({"scans": []}))\n',+ 'import json, sys\nassert sys.dont_write_bytecode\ndef main():\n print(json.dumps({"scans": []}))\n',);const result = await executeFixture(python,@@ -217,7 +217,7 @@ await test("workbench can import dependencies installed in the Python user site"assert.equal(imported.trim(), "user-site dependency");const executeFixture = await fixtureWorkbench(root,- 'import json\nfrom synthetic_dependency import VALUE\ndef main():\n print(json.dumps({"dependency": VALUE}))\n',+ 'import json, sys\nassert sys.dont_write_bytecode\nfrom synthetic_dependency import VALUE\ndef main():\n print(json.dumps({"dependency": VALUE}))\n',);assert.deepEqual(await executeFixture(python, ["list-scans"]), {dependency: "user-site dependency",