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

perf: avoid duplicate plugin setup inspection and defer CLI YAML (#2149)

Michael D'Angelo1b9523585522
diff --git a/plugins/codex-security/mcp-app/server.ts b/plugins/codex-security/mcp-app/server.ts
index 41abf0fd8..ccba371fd 100644
--- a/plugins/codex-security/mcp-app/server.ts
+++ b/plugins/codex-security/mcp-app/server.ts
@@ -7,7 +7,8 @@ import {
import type { JsonObject } from "./src/types.js";
import { isRecord as isJsonObject } from "./src/record.js";
import { randomUUID } from "node:crypto";
-import { resolve } from "node:path";
+import { realpath, stat } from "node:fs/promises";
+import { isAbsolute, resolve } from "node:path";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import * as z from "zod/v4";
import { resolvePythonCommand } from "./src/python_command.js";
@@ -829,6 +830,7 @@ export function createCodexSecurityServer(): McpServer {
const selected = await deepScanProjectSetup(
targetPath,
projectConfigPath,
+ "preview",
);
const setup = await runWorkbench([
"inspect-setup",
@@ -870,6 +872,7 @@ export function createCodexSecurityServer(): McpServer {
const selected = await deepScanProjectSetup(
targetPath,
projectConfigPath,
+ "save",
);
return workspaceResult(
(await runWorkbench(
@@ -2034,22 +2037,51 @@ async function startHeadlessStandardScan(
async function deepScanProjectSetup(
targetPath: string,
- projectConfigPath?: string,
+ projectConfigPath: string | undefined,
+ action: "preview" | "save",
) {
if (projectConfigPath === undefined) return { targetPath };
+ const inspectWithSetup =
+ action === "preview" && (await isCanonicalDirectory(targetPath));
+ if (!inspectWithSetup) targetPath = await inspectProjectTarget(targetPath);
+ try {
+ const project = await readDeepScanProjectConfig(
+ projectConfigPath,
+ targetPath,
+ );
+ return { targetPath, projectConfigJson: JSON.stringify(project) };
+ } catch (error) {
+ // Target or Git errors take precedence over an invalid project file.
+ if (inspectWithSetup) await inspectProjectTarget(targetPath);
+ throw error;
+ }
+}
+
+async function isCanonicalDirectory(targetPath: string): Promise<boolean> {
+ // Leave home expansion, UNC paths and drive-relative paths to the workbench.
+ if (
+ process.platform === "win32"
+ ? !/^[A-Za-z]:[/\\]/.test(targetPath)
+ : !isAbsolute(targetPath)
+ )
+ return false;
+ try {
+ return (
+ (await realpath(targetPath)) === targetPath &&
+ (await stat(targetPath)).isDirectory()
+ );
+ } catch {
+ return false;
+ }
+}
+
+async function inspectProjectTarget(targetPath: string): Promise<string> {
const inspected = await runWorkbench(
workbenchArgs("inspect-target", { targetPath }),
);
if (typeof inspected.targetPath !== "string")
throw new Error("Missing scan target path.");
- const project = await readDeepScanProjectConfig(
- projectConfigPath,
- inspected.targetPath,
- );
- return {
- targetPath: inspected.targetPath,
- projectConfigJson: JSON.stringify(project),
- };
+ return inspected.targetPath;
}
function wholeTargetScope(
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_project_config.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_project_config.ts
index cc857be79..2d7783f00 100644
--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_project_config.ts
+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_project_config.ts
@@ -1,5 +1,5 @@
import assert from "node:assert/strict";
-import { mkdir, rm, symlink, writeFile } from "node:fs/promises";
+import { mkdir, readFile, rm, symlink, writeFile } from "node:fs/promises";
import { join, resolve, sep } from "node:path";
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
@@ -113,6 +113,7 @@ await test("built MCP setup previews and saves the selected project before scan
const targetPath = join(root, "repository");
const projectConfigPath = join(targetPath, "selected.yaml");
const legacyConfigPath = join(root, "defaults.toml");
+ const workbenchCalls = join(root, "workbench-calls");
const client = new Client({
name: "deep-project-config-test",
version: "1.0.0",
@@ -129,6 +130,14 @@ await test("built MCP setup previews and saves the selected project before scan
"[deep_scan]\nworkers=7\nmax_time_hours=4\n",
);
await buildServer(bundle, {
+ banner: {
+ js: `require("node:diagnostics_channel").subscribe("child_process", ({ process: child }) => {
+ child.once("spawn", () => {
+ if (child.spawnargs.some(arg => arg.endsWith("workbench_db.py")))
+ require("node:fs").appendFileSync(${JSON.stringify(workbenchCalls)}, "call\\n");
+ });
+ });`,
+ },
logOverride: { "empty-import-meta": "silent" },
});
await client.connect(
@@ -158,17 +167,33 @@ await test("built MCP setup previews and saves the selected project before scan
return result.structuredContent as Record<string, any>;
};
const selection = {
- targetPath: "~/repository",
+ targetPath,
scope: ".",
mode: "deep",
projectConfigPath: "selected.yaml",
};
const preview = (await call("inspect_codex_security_setup", selection))
.setup.deepScanConfig;
+ assert.equal(await readFile(workbenchCalls, "utf8"), "call\n");
assert.equal(preview.settings.maxTimeHours, 0.5);
assert.equal(preview.settings.workers, 2);
assert.equal(preview.sources.workers, "project");
assert.equal(preview.projectConfigPath, projectConfigPath);
+ const homeTarget = await call("inspect_codex_security_setup", {
+ ...selection,
+ targetPath: "~/repository",
+ });
+ assert.deepEqual(homeTarget.setup.deepScanConfig, preview);
+ const invalid = await client.callTool({
+ name: "inspect_codex_security_setup",
+ arguments: {
+ ...selection,
+ targetPath: "repository",
+ projectConfigPath: "missing.yaml",
+ },
+ });
+ assert.equal(invalid.isError, true);
+ assert.match(JSON.stringify(invalid.content), /absolute local directory/);
if (process.platform === "win32" && /^[A-Za-z]:/.test(targetPath)) {
selection.projectConfigPath = `${targetPath.slice(0, 2)}selected.yaml`;
const driveRelative = (
diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.ts
index 9740f5dbf..5de5409e9 100644
--- a/sdk/typescript/src/cli.ts
+++ b/sdk/typescript/src/cli.ts
@@ -74,7 +74,6 @@ import {
} from "./artifact-export.js";
export { exportEnvironment } from "./artifact-export.js";
import { parse as parseToml } from "smol-toml";
-import { parse as parseYaml } from "yaml";
import {
classifyConnectionFailure,
CodexSecurity,
@@ -7110,7 +7109,7 @@ export async function main(
} else {
if (exitCode !== 0) return exitCode;
errorOutput.write(
- `codex-security: ${diagnosticLines(incurErrorMessage(frameworkOutput))}\n`,
+ `codex-security: ${diagnosticLines(await incurErrorMessage(frameworkOutput))}\n`,
);
return 2;
}
@@ -9151,11 +9150,12 @@ export function skillCommandFailure(
return detail || `${command} failed with exit code ${status}.`;
}
-function incurErrorMessage(output: string): string {
+async function incurErrorMessage(output: string): Promise<string> {
// YAML folds long strings across lines; JSON output keeps its serialized form.
if (!output.startsWith("{")) {
+ const { parse } = await import("yaml");
try {
- const parsed: unknown = parseYaml(output);
+ const parsed: unknown = parse(output);
if (isJsonObject(parsed) && typeof parsed["message"] === "string")
return parsed["message"];
} catch {
diff --git a/sdk/typescript/src/project-config.ts b/sdk/typescript/src/project-config.ts
index 0ca1950a4..11689f2cb 100644
--- a/sdk/typescript/src/project-config.ts
+++ b/sdk/typescript/src/project-config.ts
@@ -9,7 +9,6 @@ import {
} from "node:path";
import { pathToFileURL } from "node:url";
import Ajv from "ajv";
-import { parseDocument } from "yaml";
import {
DEFAULT_CODEX_CONFIG,
mergeCodexOverrides,
@@ -149,6 +148,7 @@ export async function readProjectConfig(
if (extension === ".json") {
value = JSON.parse(text.replace(/^\uFEFF/u, ""));
} else {
+ const { parseDocument } = await import("yaml");
const document = parseDocument(text, { prettyErrors: false });
if (document.errors.length > 0) throw document.errors[0];
// Keep YAML's expansion guard while allowing repeated native profiles.