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: report exit status when the scan helper fails (#2406)

ianw-oaiaa57e09e3dc6
diff --git a/plugins/codex-security/mcp-app/src/workbench-client.ts b/plugins/codex-security/mcp-app/src/workbench-client.ts
index 96a46f5d6..69acd7ed8 100644
--- a/plugins/codex-security/mcp-app/src/workbench-client.ts
+++ b/plugins/codex-security/mcp-app/src/workbench-client.ts
@@ -2,7 +2,7 @@ import { execFile } from "node:child_process";
import { join, resolve } from "node:path";
import { promisify } from "node:util";
import type { JsonObject } from "./types.js";
-import { isRecord as isJsonObject } from "./record.js";
+import { isRecord } from "./record.js";
import {
missingPythonHelperMessage,
resolvePythonCommand,
@@ -39,26 +39,32 @@ export async function runWorkbench(
input?: string | Buffer,
options: WorkbenchOptions = {},
): Promise<JsonObject> {
- let pythonCommand: string | undefined;
+ const pythonCommand = await resolvePythonCommand({
+ workingDirectory: PLUGIN_ROOT,
+ });
try {
- pythonCommand = await resolvePythonCommand({
- workingDirectory: PLUGIN_ROOT,
- });
return await executeWorkbench(pythonCommand, args, input, options);
} catch (error) {
- const launchError = pythonCommand
- ? missingPythonHelperMessage(error, pythonCommand)
- : undefined;
- if (launchError) {
- throw new Error(launchError, { cause: error });
- }
- if (isExecError(error) && error.stderr.trim()) {
- throw new Error(error.stderr.trim(), { cause: error });
- }
+ const message =
+ missingPythonHelperMessage(error, pythonCommand) ??
+ workbenchExitMessage(error, args[0]);
+ if (message) throw new Error(message, { cause: error });
throw error;
}
}
+function workbenchExitMessage(error: unknown, command: string) {
+ if (!isRecord(error) || typeof error.stderr !== "string") return;
+ const stderr = error.stderr.trim();
+ if (stderr) return stderr;
+
+ const operation = `Codex Security workbench ${command}`;
+ if (typeof error.signal === "string")
+ return `${operation} was terminated by signal ${error.signal}.`;
+ if (typeof error.code === "number")
+ return `${operation} exited with code ${error.code}.`;
+}
+
async function executeWorkbench(
pythonCommand: string,
args: string[],
@@ -139,7 +145,7 @@ async function executeWorkbench(
throw error;
});
const result = JSON.parse(stdout) as unknown;
- if (!isJsonObject(result)) {
+ if (!isRecord(result)) {
throw new Error("Codex Security workbench helper returned invalid JSON.");
}
return result;
@@ -148,12 +154,3 @@ async function executeWorkbench(
function workbenchScriptPath(): string {
return join(PLUGIN_ROOT, "scripts", "workbench_db.py");
}
-
-function isExecError(error: unknown): error is { stderr: string } {
- return Boolean(
- error &&
- typeof error === "object" &&
- "stderr" in error &&
- typeof error.stderr === "string",
- );
-}
diff --git a/plugins/codex-security/mcp-app/tests/test_start_scan_errors.ts b/plugins/codex-security/mcp-app/tests/test_start_scan_errors.ts
new file mode 100644
index 000000000..ab7fe584b
--- /dev/null
+++ b/plugins/codex-security/mcp-app/tests/test_start_scan_errors.ts
@@ -0,0 +1,89 @@
+import assert from "node:assert/strict";
+import { randomUUID } from "node:crypto";
+import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
+import { join } from "node:path";
+import { test } from "node:test";
+import { buildServer } from "./build-server.ts";
+import { fixturePython } from "./support/python.ts";
+import { startRpcServer } from "./support/rpc-server.ts";
+import { temporaryDirectory } from "./support/temporary-directories.ts";
+
+test("scan startup failures reach the MCP caller", async (t) => {
+ const root = await temporaryDirectory("workbench-start-errors-");
+ t.after(() => rm(root, { recursive: true, force: true }));
+ const bundle = join(root, "server.cjs");
+ await buildServer(bundle, {
+ plugins: [
+ {
+ name: "short-workbench-timeout",
+ setup(build) {
+ build.onLoad(
+ { filter: /workbench-client\.ts$/ },
+ async ({ path }) => {
+ const source = await readFile(path, "utf8");
+ return {
+ loader: "ts",
+ contents: source.replace("300_000", "2_000"),
+ };
+ },
+ );
+ },
+ },
+ ],
+ });
+ await mkdir(join(root, "scripts"));
+ const server = startRpcServer({
+ command: process.execPath,
+ args: [bundle, "--stdio"],
+ env: {
+ ...process.env,
+ CODEX_SECURITY_PLUGIN_ROOT: root,
+ CODEX_SECURITY_PYTHON_COMMAND: fixturePython,
+ },
+ });
+ t.after(() => server.stop());
+ await server.initialize("scan-start-errors");
+ let id = 1;
+ async function start(beforeExit = "pass") {
+ await writeFile(
+ join(root, "scripts/workbench_db.py"),
+ `import os, signal, sys, time
+
+def main():
+ if sys.argv[1] == "resolve-scan-root":
+ print('{"scanRoot": "."}')
+ return
+ assert sys.argv[1] == "start-scan"
+ ${beforeExit}
+ os._exit(23)
+`,
+ );
+ const { result } = await server.callTool(++id, {
+ name: "start_codex_security_scan",
+ arguments: { sessionId: randomUUID() },
+ });
+ assert.equal(result.isError, true);
+ assert.equal(result.content[0].type, "text");
+ return result.content[0].text;
+ }
+ assert.match(
+ await start("time.sleep(10)"),
+ /start-scan timed out after 2 seconds/,
+ );
+ assert.equal(
+ await start(
+ 'os.write(2, b"Could not open the scan database.\\nFixture diagnostic.")',
+ ),
+ "Could not open the scan database.\nFixture diagnostic.",
+ );
+ assert.equal(
+ await start(),
+ "Codex Security workbench start-scan exited with code 23.",
+ );
+ if (process.platform !== "win32") {
+ assert.equal(
+ await start("os.kill(os.getpid(), signal.SIGTERM)"),
+ "Codex Security workbench start-scan was terminated by signal SIGTERM.",
+ );
+ }
+});