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(runtime): honor literal POSIX executable override paths (#2551)

Michael D'Angeloa0fedec4cad8
diff --git a/plugins/codex-security/mcp-app/src/deep-scan/executor.ts b/plugins/codex-security/mcp-app/src/deep-scan/executor.ts
index 07df96f20..7686d9f66 100644
--- a/plugins/codex-security/mcp-app/src/deep-scan/executor.ts
+++ b/plugins/codex-security/mcp-app/src/deep-scan/executor.ts
@@ -1,8 +1,6 @@
import { randomUUID } from "node:crypto";
import {
- accessSync,
type BigIntStats,
- constants as fsConstants,
existsSync,
promises as fs,
readdirSync,
@@ -33,7 +31,11 @@ import {
profileConfigOverrides,
} from "../../../scripts/codex_profile.mjs";
import { DEEP_SCAN_SETTINGS } from "../deep-scan-settings.js";
-import { executablePathForSpawn } from "../executable-path.js";
+import {
+ configuredExecutableValue,
+ executablePathForSpawn,
+ isExecutableFile,
+} from "../executable-path.js";
import { writePrivateFile } from "./artifacts.js";
import { workerTokenUsage } from "./usage-receipt.js";
import {
@@ -1053,11 +1055,12 @@ export function resolveCodexPath(
originalCwd: string = process.cwd(),
): string {
const searchPath = searchPathForPlatform(env, platform);
- const configured = environmentVariable(
- env,
- "CODEX_CLI_PATH",
+ const configured = configuredExecutableValue(
+ environmentVariable(env, "CODEX_CLI_PATH", platform),
platform,
- )?.trim();
+ (value) =>
+ isExecutableFile(absoluteCodexPath(value, platform, originalCwd)),
+ );
if (configured && (platform !== "win32" || !isWindowsAppsPath(configured))) {
if (isBareCommandName(configured)) {
const executableName =
@@ -1256,16 +1259,6 @@ function resolveWindowsCachedBinary(
return selected?.path;
}
-function isExecutableFile(value: string): boolean {
- try {
- if (!statSync(value).isFile()) return false;
- accessSync(value, fsConstants.X_OK);
- return true;
- } catch {
- return false;
- }
-}
-
function absoluteCodexPath(
value: string,
platform: NodeJS.Platform,
diff --git a/plugins/codex-security/mcp-app/src/executable-path.ts b/plugins/codex-security/mcp-app/src/executable-path.ts
index 11eeeec04..5733ebb2b 100644
--- a/plugins/codex-security/mcp-app/src/executable-path.ts
+++ b/plugins/codex-security/mcp-app/src/executable-path.ts
@@ -1,3 +1,4 @@
+import { accessSync, constants, statSync } from "node:fs";
import { win32 } from "node:path";
export function executablePathForSpawn(command: string): string {
@@ -9,3 +10,38 @@ export function executablePathForSpawn(command: string): string {
? command
: win32.toNamespacedPath(command);
}
+
+export function isExecutableFile(value: string): boolean {
+ try {
+ if (!statSync(value).isFile()) return false;
+ accessSync(value, constants.X_OK);
+ return true;
+ } catch {
+ return false;
+ }
+}
+
+export function isExplicitPathWithTrailingWhitespace(
+ value: string,
+ platform: NodeJS.Platform,
+): boolean {
+ // Bare names keep their usual PATH precedence. Leading padding can change the path's root.
+ return (
+ platform !== "win32" &&
+ value.includes("/") &&
+ value.trimStart() === value &&
+ value.trimEnd() !== value
+ );
+}
+
+export function configuredExecutableValue(
+ value: string | undefined,
+ platform: NodeJS.Platform,
+ literalIsExecutable: (value: string) => boolean,
+): string | undefined {
+ return value &&
+ isExplicitPathWithTrailingWhitespace(value, platform) &&
+ literalIsExecutable(value)
+ ? value
+ : value?.trim();
+}
diff --git a/plugins/codex-security/mcp-app/src/python_command.ts b/plugins/codex-security/mcp-app/src/python_command.ts
index db7675598..c3eaae412 100644
--- a/plugins/codex-security/mcp-app/src/python_command.ts
+++ b/plugins/codex-security/mcp-app/src/python_command.ts
@@ -2,6 +2,10 @@ import { spawn } from "node:child_process";
import { constants, promises as fs } from "node:fs";
import { homedir } from "node:os";
import path from "node:path";
+import {
+ configuredExecutableValue,
+ isExecutableFile,
+} from "./executable-path.ts";
type PythonPlatform = NodeJS.Platform;
@@ -11,6 +15,7 @@ interface ResolvePythonCommandOptions {
homeDirectory?: string;
isUsableExecutable?: (candidate: string) => Promise<boolean>;
platform?: PythonPlatform;
+ workingDirectory?: string;
}
const MISSING_PYTHON_HELPER_MESSAGE =
@@ -29,12 +34,19 @@ export async function resolvePythonCommand(
) {
return process.env.CODEX_SECURITY_PYTHON_COMMAND;
}
- const configuredPython = options.configuredPython ?? process.env.PYTHON;
- if (configuredPython?.trim()) {
- return configuredPython.trim();
- }
-
const platform = options.platform ?? process.platform;
+ const configuredPython = configuredExecutableValue(
+ options.configuredPython ?? process.env.PYTHON,
+ platform,
+ (value) =>
+ isExecutableFile(
+ value.startsWith("/")
+ ? value
+ : `${options.workingDirectory ?? process.cwd()}/${value}`,
+ ),
+ );
+ if (configuredPython) return configuredPython;
+
const pathImplementation = platform === "win32" ? path.win32 : path.posix;
const systemPython = platform === "win32" ? "python" : "python3";
let cacheDirectory = options.cacheDirectory ?? process.env.XDG_CACHE_HOME;
diff --git a/plugins/codex-security/mcp-app/src/workbench-client.ts b/plugins/codex-security/mcp-app/src/workbench-client.ts
index cb509af73..96a46f5d6 100644
--- a/plugins/codex-security/mcp-app/src/workbench-client.ts
+++ b/plugins/codex-security/mcp-app/src/workbench-client.ts
@@ -41,7 +41,9 @@ export async function runWorkbench(
): Promise<JsonObject> {
let pythonCommand: string | undefined;
try {
- pythonCommand = await resolvePythonCommand();
+ pythonCommand = await resolvePythonCommand({
+ workingDirectory: PLUGIN_ROOT,
+ });
return await executeWorkbench(pythonCommand, args, input, options);
} catch (error) {
const launchError = pythonCommand
diff --git a/plugins/codex-security/mcp-app/tests/deep_scan_executable_cases.ts b/plugins/codex-security/mcp-app/tests/deep_scan_executable_cases.ts
index 5f0baf339..6e4f4cf85 100644
--- a/plugins/codex-security/mcp-app/tests/deep_scan_executable_cases.ts
+++ b/plugins/codex-security/mcp-app/tests/deep_scan_executable_cases.ts
@@ -7,6 +7,7 @@ import {
realpath,
rename,
rm,
+ symlink,
writeFile,
} from "node:fs/promises";
import { syncBuiltinESMExports } from "node:module";
@@ -177,6 +178,35 @@ export async function testExecutorExecutablePinning({
() => new CodexSdkWorkerExecutor({ parentSandbox }),
);
try {
+ const configuredPaths = fixtures.map((fixture) => fixture.executablePath);
+ if (process.platform !== "win32") {
+ const alias = path.join(
+ fixtures[0]!.root,
+ "codex with trailing space ",
+ );
+ await symlink(fixtures[0]!.executablePath, alias);
+ configuredPaths[0] = alias;
+ const other = fixtures[1]!.executablePath;
+ const bare = path.basename(other) + " ";
+ await symlink(
+ fixtures[0]!.executablePath,
+ path.join(fixtures[0]!.root, bare),
+ );
+ assert.equal(
+ resolveCodexPath(
+ {
+ PATH: [path.dirname(other), fixtures[0]!.root].join(
+ path.delimiter,
+ ),
+ CODEX_CLI_PATH: bare,
+ },
+ process.platform,
+ process.arch,
+ fixtures[0]!.root,
+ ),
+ other,
+ );
+ }
for (const fixture of fixtures) {
await writeFile(
path.join(fixture.root, "prompt.md"),
@@ -188,7 +218,7 @@ export async function testExecutorExecutablePinning({
await Promise.all(
fixtures.map((fixture, index) => {
process.env.CODEX_CLI_PATH =
- fixtures[firstLaunch ? index : 1 - index]!.executablePath;
+ configuredPaths[firstLaunch ? index : 1 - index]!;
process.env.FAKE_CODEX_MARKER = fixture.markerPath;
return runTurn(
executors[index]!,
diff --git a/plugins/codex-security/mcp-app/tests/test_python_command.ts b/plugins/codex-security/mcp-app/tests/test_python_command.ts
index 990c8e5eb..4af57cf6e 100644
--- a/plugins/codex-security/mcp-app/tests/test_python_command.ts
+++ b/plugins/codex-security/mcp-app/tests/test_python_command.ts
@@ -177,8 +177,54 @@ try {
);
const resolvedPython =
process.platform === "win32" ? process.execPath : `${fileCandidate} `;
- if (process.platform !== "win32")
+ if (process.platform !== "win32") {
await symlink(process.execPath, resolvedPython);
+ assert.equal(
+ await resolvePythonCommand({ configuredPython: resolvedPython }),
+ resolvedPython,
+ );
+ assert.equal(
+ await resolvePythonCommand({
+ configuredPython: "./python3 ",
+ workingDirectory: executableFixtureRoot,
+ }),
+ "./python3 ",
+ );
+ const paddedAbsolute = ` ${fileCandidate} `;
+ const shadow = path.join(executableFixtureRoot, paddedAbsolute);
+ await mkdir(path.dirname(shadow), { recursive: true });
+ await symlink(process.execPath, shadow);
+ assert.equal(
+ await resolvePythonCommand({
+ configuredPython: paddedAbsolute,
+ workingDirectory: executableFixtureRoot,
+ }),
+ fileCandidate,
+ );
+ const trusted = path.join(executableFixtureRoot, "tools");
+ await mkdir(trusted);
+ await writeFile(
+ path.join(trusted, "python3"),
+ "#!/bin/sh\n/bin/cat >/dev/null\nprintf trusted",
+ {
+ mode: 0o700,
+ },
+ );
+ const previousPath = process.env.PATH;
+ try {
+ process.env.PATH = [trusted, executableFixtureRoot].join(path.delimiter);
+ const command = await resolvePythonCommand({
+ configuredPython: "python3 ",
+ });
+ assert.equal(
+ await runPythonWithInput(command, [], "", "fixture"),
+ "trusted",
+ );
+ } finally {
+ if (previousPath === undefined) delete process.env.PATH;
+ else process.env.PATH = previousPath;
+ }
+ }
process.env.PYTHON = ` ${fileCandidate} `;
process.env.CODEX_SECURITY_PYTHON_COMMAND = resolvedPython;
assert.equal(await resolvePythonCommand(), resolvedPython);
diff --git a/sdk/typescript/docs/cli.md b/sdk/typescript/docs/cli.md
index 4e0c4d594..c233e6882 100644
--- a/sdk/typescript/docs/cli.md
+++ b/sdk/typescript/docs/cli.md
@@ -1113,6 +1113,10 @@ Python lookup: `--python` on commands that expose interpreter selection or SDK
`pythonPath`, then
`PYTHON`, the managed runtime, and `python3` or `python` on `PATH` (`py` also
works on Windows). `CODEX_SECURITY_STATE_DIR` overrides `CODEX_HOME` for storage.
+On POSIX, `CODEX_CLI_PATH` and `PYTHON` preserve trailing whitespace in an
+explicit path that selects an executable. Use an absolute path or a `./`
+prefix for filenames beginning or ending in whitespace. Bare command names
+are trimmed before lookup.
### Troubleshooting
diff --git a/sdk/typescript/src/executable-path.ts b/sdk/typescript/src/executable-path.ts
index 6bd72d108..83c525a73 100644
--- a/sdk/typescript/src/executable-path.ts
+++ b/sdk/typescript/src/executable-path.ts
@@ -1,4 +1,5 @@
/* Generated from plugin executable-path.ts. Run `pnpm generate:models`. */
+import { accessSync, constants, statSync } from "node:fs";
import { win32 } from "node:path";
export function executablePathForSpawn(command: string): string {
@@ -10,3 +11,38 @@ export function executablePathForSpawn(command: string): string {
? command
: win32.toNamespacedPath(command);
}
+
+export function isExecutableFile(value: string): boolean {
+ try {
+ if (!statSync(value).isFile()) return false;
+ accessSync(value, constants.X_OK);
+ return true;
+ } catch {
+ return false;
+ }
+}
+
+export function isExplicitPathWithTrailingWhitespace(
+ value: string,
+ platform: NodeJS.Platform,
+): boolean {
+ // Bare names keep their usual PATH precedence. Leading padding can change the path's root.
+ return (
+ platform !== "win32" &&
+ value.includes("/") &&
+ value.trimStart() === value &&
+ value.trimEnd() !== value
+ );
+}
+
+export function configuredExecutableValue(
+ value: string | undefined,
+ platform: NodeJS.Platform,
+ literalIsExecutable: (value: string) => boolean,
+): string | undefined {
+ return value &&
+ isExplicitPathWithTrailingWhitespace(value, platform) &&
+ literalIsExecutable(value)
+ ? value
+ : value?.trim();
+}
diff --git a/sdk/typescript/src/runtime.ts b/sdk/typescript/src/runtime.ts
index 7d6ae9731..67092898b 100644
--- a/sdk/typescript/src/runtime.ts
+++ b/sdk/typescript/src/runtime.ts
@@ -1,4 +1,9 @@
-import { executablePathForSpawn } from "./executable-path.js";
+import {
+ configuredExecutableValue,
+ executablePathForSpawn,
+ isExplicitPathWithTrailingWhitespace,
+ isExecutableFile,
+} from "./executable-path.js";
import { retryWindowsRename } from "./retry-windows-rename.js";
export { executablePathForSpawn } from "./executable-path.js";
import {
@@ -3202,7 +3207,11 @@ async function pluginRuntimeProjection(
export function resolveCodexCommand(
environment: ProcessEnvironment = process.env,
): CodexCommand {
- const configured = environmentValue(environment, "CODEX_CLI_PATH");
+ const configured = configuredExecutableValue(
+ environmentValue(environment, "CODEX_CLI_PATH", true),
+ process.platform,
+ (value) => isExecutableFile(expandHomeForLookup(value, environment)),
+ );
const expanded =
configured === undefined
? undefined
@@ -3683,13 +3692,26 @@ export async function resolvePluginPython(
options.signal,
);
}
- const inherited = environmentValue(environment, "PYTHON");
+ const inherited = environmentValue(environment, "PYTHON", true);
if (inherited) {
+ const roots = isPythonPathCandidate(inherited)
+ ? protectedRoot
+ : discoveryRoots;
+ // Check an explicit literal path with the same repository protections as the launch.
+ const candidate =
+ isExplicitPathWithTrailingWhitespace(inherited, process.platform) &&
+ (await resolveTrustedExecutable(
+ expandHomeForLookup(inherited, environment),
+ environment,
+ roots,
+ ))
+ ? inherited
+ : inherited.trim();
return await requirePython(
- inherited,
+ candidate,
"PYTHON",
environment,
- isPythonPathCandidate(inherited) ? protectedRoot : discoveryRoots,
+ roots,
options.signal,
);
}
diff --git a/sdk/typescript/tests-ts/runtime.test.ts b/sdk/typescript/tests-ts/runtime.test.ts
index 7fb339293..00686838d 100644
--- a/sdk/typescript/tests-ts/runtime.test.ts
+++ b/sdk/typescript/tests-ts/runtime.test.ts
@@ -6966,6 +6966,73 @@ describe("runtime directories and plugin Python boundary", () => {
}
});
+ testPosix(
+ "preserves literal executable override names and accepts padding",
+ async () => {
+ const tools = await realpath(await temporaryDirectory());
+ const repository = await temporaryDirectory();
+ const interpreter = pythonWithLauncher();
+ expect(interpreter).not.toBeNull();
+ const ordinary = join(tools, "tool");
+ const literal = ordinary + " ";
+ await symlink(interpreter!, ordinary);
+ await symlink(interpreter!, literal);
+
+ for (const [configured, expected] of [
+ [literal, literal],
+ [` ${ordinary} `, ordinary],
+ ] as const) {
+ expect(
+ resolveCodexCommand({ CODEX_CLI_PATH: configured }).command,
+ ).toBe(expected);
+ await expect(
+ resolvePluginPython({
+ environment: { PYTHON: configured, PATH: tools },
+ protectedRoot: repository,
+ }),
+ ).resolves.toBe(expected);
+ }
+ await expect(
+ resolvePluginPython({
+ environment: { PYTHON: "tool ", PATH: tools },
+ protectedRoot: repository,
+ }),
+ ).resolves.toBe(ordinary);
+
+ const paddedAbsolute = ` ${ordinary} `;
+ const shadow = join(repository, paddedAbsolute);
+ await mkdir(dirname(shadow), { recursive: true });
+ await writeFile(shadow, "#!/bin/sh\nexit 93\n", { mode: 0o700 });
+ const runtime = new URL("../src/runtime.ts", import.meta.url).href;
+ const child = spawnSync(
+ process.execPath,
+ [
+ "-e",
+ `import { resolveCodexCommand } from ${JSON.stringify(runtime)};
+ import { spawnSync } from "node:child_process";
+ const { command } = resolveCodexCommand();
+ process.exit(spawnSync(command, ["-I", "-c", "pass"]).status ?? 1);`,
+ ],
+ {
+ cwd: repository,
+ env: { ...process.env, CODEX_CLI_PATH: paddedAbsolute },
+ },
+ );
+ expect(child.status).toBe(0);
+
+ await rm(literal);
+ expect(resolveCodexCommand({ CODEX_CLI_PATH: literal }).command).toBe(
+ ordinary,
+ );
+ await expect(
+ resolvePluginPython({
+ environment: { PYTHON: literal, PATH: tools },
+ protectedRoot: repository,
+ }),
+ ).resolves.toBe(ordinary);
+ },
+ );
+
test("resolves inherited Python names case-insensitively", async () => {
const discovered = pythonWithLauncher();
expect(discovered).not.toBeNull();