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(sdk): serialize patch project trust overrides correctly (#1861)

Michael D'Angelof3aa3b060351
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.ts
index 1f270499c..471b6f39b 100644
--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.ts
+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.ts
@@ -1686,6 +1686,7 @@ env_key = "SYNTHETIC_GATEWAY_KEY"`,
configuration: {
...parsedConfiguration,
...inheritedTelemetry,
+ projects: { [fixture.root]: { trust_level: "trusted" } },
...Object.fromEntries(
Object.entries(nativePaths).map(([key, value]) => [
key,
@@ -1776,6 +1777,7 @@ env_key = "SYNTHETIC_GATEWAY_KEY"`,
...entry.authentication,
...entry.workerTelemetry,
...entry.nativePaths,
+ projects: { [fixture.root]: { trust_level: "untrusted" } },
approval_policy: "on-request",
default_permissions: "parent",
permissions: { parent: { filesystem: { ":root": "write" } } },
@@ -2092,6 +2094,13 @@ env_key = "SYNTHETIC_GATEWAY_KEY"`,
web_search: workerConfigurations[index].webSearch,
"windows.sandbox": workerConfigurations[index].windowsSandbox,
});
+ assert.deepEqual(
+ nativeConfigOverrides(invocation.argv).filter((override) =>
+ /^projects[.=]/.test(override),
+ ),
+ [],
+ "Patch project trust is not forwarded to discovery, reducer or resumed workers.",
+ );
assert.deepEqual(
JSON.parse(
JSON.stringify(
diff --git a/sdk/typescript/src/api.ts b/sdk/typescript/src/api.ts
index 574645f8c..74894b639 100644
--- a/sdk/typescript/src/api.ts
+++ b/sdk/typescript/src/api.ts
@@ -1011,7 +1011,12 @@ export class CodexSecurity {
temporaryRoot,
);
// Pin an untrusted root before --cd can trust it and load repository MCP
- // servers. Raw overrides keep the path as one quoted TOML key.
+ // servers. An inline table keeps the path out of CLI dotted-key parsing.
+ // Codex prefers an exact ASCII-lowercase trust key on Windows.
+ const projectTrustKey =
+ process.platform === "win32"
+ ? repository.replace(/[A-Z]/g, (letter) => letter.toLowerCase())
+ : repository;
const configured = scanModelConfiguration(session.effectiveConfig);
const model = options.model ?? configured.model;
const reasoningEffort =
@@ -1034,7 +1039,7 @@ export class CodexSecurity {
[
`permissions.${SCAN_PERMISSION_PROFILE}.network.enabled=false`,
"project_root_markers=[]",
- `projects.${JSON.stringify(repository)}.trust_level="untrusted"`,
+ `projects=${inlineToml({ [projectTrustKey]: { trust_level: "untrusted" } })}`,
],
);
tracker = new ScanCostTracker({
diff --git a/sdk/typescript/tests-ts/api-patch.test.ts b/sdk/typescript/tests-ts/api-patch.test.ts
index 1c8dfbfa3..3719d37b6 100644
--- a/sdk/typescript/tests-ts/api-patch.test.ts
+++ b/sdk/typescript/tests-ts/api-patch.test.ts
@@ -143,12 +143,17 @@ describe("CodexSecurity headless patching", () => {
repositoryPath: repository,
finding: "Candidate finding",
};
+ const projectTrustKey =
+ process.platform === "win32"
+ ? repository.replace(/[A-Z]/g, (letter) => letter.toLowerCase())
+ : repository;
return {
client,
options,
captured,
workbench,
codexHome,
+ projectTrustKey,
};
}
@@ -388,7 +393,8 @@ describe("CodexSecurity headless patching", () => {
});
test("keeps the patch workspace untrusted across native config layers", async () => {
- const { client, options, captured, codexHome } = await patchClient();
+ const { client, options, captured, codexHome, projectTrustKey } =
+ await patchClient();
await using security = client;
await security.patch(options);
expect(
@@ -406,7 +412,7 @@ describe("CodexSecurity headless patching", () => {
},
},
project_root_markers: [],
- projects: { [options.repositoryPath]: { trust_level: "untrusted" } },
+ projects: { [projectTrustKey]: { trust_level: "untrusted" } },
});
expect(captured.thread?.workingDirectory).toBe(options.repositoryPath);
});
@@ -425,9 +431,8 @@ describe("CodexSecurity headless patching", () => {
refresh_interval_ms: 1000,
},
};
- const { client, options, captured, codexHome } = await patchClient(
- undefined,
- {
+ const { client, options, captured, codexHome, projectTrustKey } =
+ await patchClient(undefined, {
...(selection === "profile"
? {
profile: "review",
@@ -435,8 +440,7 @@ describe("CodexSecurity headless patching", () => {
}
: { model_provider: "synthetic.provider" }),
model_providers: { "synthetic.provider": provider },
- },
- );
+ });
await using security = client;
expect(await security.patch(options)).toMatchObject(verified);
expect(captured.codex?.nativeProfile).toBeDefined();
@@ -465,7 +469,7 @@ describe("CodexSecurity headless patching", () => {
},
},
project_root_markers: [],
- projects: { [options.repositoryPath]: { trust_level: "untrusted" } },
+ projects: { [projectTrustKey]: { trust_level: "untrusted" } },
});
expect(captured.codex?.config).not.toHaveProperty("model_providers");
expect(captured.codex?.apiKey).toBeUndefined();
diff --git a/sdk/typescript/tests-ts/api-provider-isolation.test.ts b/sdk/typescript/tests-ts/api-provider-isolation.test.ts
index 871f3b331..48a286990 100644
--- a/sdk/typescript/tests-ts/api-provider-isolation.test.ts
+++ b/sdk/typescript/tests-ts/api-provider-isolation.test.ts
@@ -1,4 +1,4 @@
-import { codexFactory } from "./support/api-events.js";
+import { codexFactory, completedEvents } from "./support/api-events.js";
import { afterEach, expect, mock, test } from "bun:test";
import { build } from "esbuild";
import { parse as parseToml, stringify as stringifyToml } from "smol-toml";
@@ -141,6 +141,108 @@ test("native worker override tables retain inherited features and MCP servers",
expect(await readFile(join(home, "config.toml"), "utf8")).toBe(original);
});
+test("native patch overrides disable repository MCP while preserving host MCP", async () => {
+ const root = await temporaryDirectory();
+ const home = join(root, "home");
+ const markerRoot = join(root, "Repository.V1 With Spaces Ü");
+ const repository = join(markerRoot, "packages", "app");
+ const projectTrustKey =
+ process.platform === "win32"
+ ? repository.replace(/[A-Z]/g, (letter) => letter.toLowerCase())
+ : repository;
+ await mkdir(home, { mode: 0o700 });
+ await mkdir(join(repository, ".codex"), { recursive: true });
+ await writeFile(join(markerRoot, "package.json"), "{}\n");
+ const hostConfig = stringifyToml({
+ project_root_markers: ["package.json"],
+ projects: {
+ [markerRoot]: { trust_level: "trusted" },
+ [projectTrustKey]: { trust_level: "trusted" },
+ },
+ mcp_servers: { host: { command: "node", args: ["host-server.js"] } },
+ });
+ const repositoryConfig = stringifyToml({
+ mcp_servers: {
+ repository: { command: "node", args: ["repository-server.js"] },
+ },
+ });
+ await writeFile(join(home, "config.toml"), hostConfig);
+ await writeFile(join(repository, ".codex", "config.toml"), repositoryConfig);
+ const environment = { CODEX_HOME: home };
+ const { config: trusted } = await nativeRequest(
+ environment,
+ repository,
+ ["--cd", repository],
+ "config/read",
+ { cwd: repository, includeLayers: false },
+ );
+ expect(trusted.mcp_servers).toHaveProperty("repository");
+
+ await using client = new TestClient(
+ {},
+ {
+ prepareRuntime: async () => preparedRuntime(home),
+ resolvePluginPython: async () => "/managed/python",
+ createCodex: async (options) => {
+ // Exercise CLI parsing: parsing these arguments as a TOML document
+ // incorrectly treats quoted path components as literal keys.
+ const { config } = await nativeRequest(
+ environment,
+ repository,
+ [
+ "--cd",
+ repository,
+ ...[
+ ...profileConfigOverrides(options.config ?? {}),
+ ...(options.configOverrides ?? []),
+ ].flatMap((value) => ["--config", value]),
+ ],
+ "config/read",
+ { cwd: repository, includeLayers: false },
+ );
+ expect(config.mcp_servers).not.toHaveProperty("repository");
+ expect(config.projects[projectTrustKey]).toEqual({
+ trust_level: "untrusted",
+ });
+ expect(config.project_root_markers).toEqual([]);
+ expect(config.mcp_servers.host).toMatchObject({
+ command: "node",
+ args: ["host-server.js"],
+ enabled: true,
+ });
+ return codexFactory(async () => ({
+ events: completedEvents(
+ "patch-thread",
+ (async function* () {
+ yield {
+ type: "item.completed" as const,
+ item: {
+ id: "result",
+ type: "agent_message" as const,
+ text: JSON.stringify({
+ status: "no_change",
+ changedFiles: [],
+ verificationReport: "Synthetic patch result",
+ reason: null,
+ }),
+ },
+ };
+ })(),
+ ),
+ }))();
+ },
+ },
+ );
+ await client.patch({
+ repositoryPath: repository,
+ finding: "Synthetic finding",
+ });
+ expect(await readFile(join(home, "config.toml"), "utf8")).toBe(hostConfig);
+ expect(
+ await readFile(join(repository, ".codex", "config.toml"), "utf8"),
+ ).toBe(repositoryConfig);
+});
+
async function bundleWorkerSettings(root: string) {
const executor = fileURLToPath(
new URL(