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): find system Python for Linux accounts without a home (#2509)

Michael D'Angelo5813bb1b66c8
diff --git a/plugins/codex-security/mcp-app/src/python_command.ts b/plugins/codex-security/mcp-app/src/python_command.ts
index 9e1d2e689..db7675598 100644
--- a/plugins/codex-security/mcp-app/src/python_command.ts
+++ b/plugins/codex-security/mcp-app/src/python_command.ts
@@ -36,9 +36,24 @@ export async function resolvePythonCommand(
const platform = options.platform ?? process.platform;
const pathImplementation = platform === "win32" ? path.win32 : path.posix;
- const cacheDirectory =
- (options.cacheDirectory ?? process.env.XDG_CACHE_HOME) ||
- pathImplementation.join(options.homeDirectory ?? homedir(), ".cache");
+ const systemPython = platform === "win32" ? "python" : "python3";
+ let cacheDirectory = options.cacheDirectory ?? process.env.XDG_CACHE_HOME;
+ if (!cacheDirectory) {
+ try {
+ cacheDirectory = pathImplementation.join(
+ options.homeDirectory ?? homedir(),
+ ".cache",
+ );
+ } catch (error) {
+ // A numeric Linux UID can have no home directory for the managed cache.
+ if (
+ platform === "linux" &&
+ (error as { info?: NodeJS.ErrnoException }).info?.code === "ENOENT"
+ )
+ return systemPython;
+ throw error;
+ }
+ }
const bundledPythonRoot = pathImplementation.join(
cacheDirectory,
"codex-runtimes",
@@ -65,7 +80,7 @@ export async function resolvePythonCommand(
return candidate;
}
}
- return platform === "win32" ? "python" : "python3";
+ return systemPython;
}
/**
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 fa364131b..f29cbcde4 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
@@ -1507,6 +1507,9 @@ async function testWorkerRuntimeSettings() {
"bin",
"python",
);
+ const systemPython = process.platform === "win32" ? "python" : "python3";
+ const workerPython = (index: number) =>
+ index === 0 ? systemPython : `${helperPython}-${index} `;
process.env.PYTHON = python;
const gitEnvironment = workerGitEnvironment(fixture.root);
await writeFile(gitEnvironment.NODE_EXTRA_CA_CERTS, rootCertificates[0]!);
@@ -2074,7 +2077,7 @@ env_key = "SYNTHETIC_GATEWAY_KEY"`,
? ["api", `background jobs ${index}`]
: undefined,
filePaths: index % 2 === 0 ? [`background jobs ${index}`] : [],
- pythonCommand: `${helperPython}-${index} `,
+ pythonCommand: workerPython(index),
},
}),
);
@@ -2499,7 +2502,8 @@ env_key = "SYNTHETIC_GATEWAY_KEY"`,
"mcp_servers.cs_artifacts.command": process.execPath,
"mcp_servers.cs_artifacts.env.CODEX_SECURITY_INCLUDE_PATHS_JSON":
undefined,
- "mcp_servers.cs_artifacts.env.CODEX_SECURITY_PYTHON_COMMAND": `${helperPython}-${index} `,
+ "mcp_servers.cs_artifacts.env.CODEX_SECURITY_PYTHON_COMMAND":
+ workerPython(index),
"mcp_servers.cs_artifacts.args.0": path.join(
fixture.root,
`plugin-${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 f5a86035c..990c8e5eb 100644
--- a/plugins/codex-security/mcp-app/tests/test_python_command.ts
+++ b/plugins/codex-security/mcp-app/tests/test_python_command.ts
@@ -7,8 +7,10 @@ import {
symlink,
writeFile,
} from "node:fs/promises";
-import { tmpdir } from "node:os";
+import { syncBuiltinESMExports } from "node:module";
+import os, { tmpdir } from "node:os";
import path from "node:path";
+import { mock } from "node:test";
import {
isUsablePythonExecutable,
missingPythonHelperMessage,
@@ -62,6 +64,27 @@ assert.equal(
);
const unixHome = "/home/fixture";
+const missingHome = mock.method(os, "homedir", () => {
+ throw Object.assign(new Error("No home directory for this UID"), {
+ code: "ERR_SYSTEM_ERROR",
+ info: { code: "ENOENT" },
+ });
+});
+try {
+ syncBuiltinESMExports();
+ assert.equal(
+ await resolvePythonCommand({
+ configuredPython: "",
+ cacheDirectory: "",
+ platform: "linux",
+ }),
+ "python3",
+ );
+} finally {
+ missingHome.mock.restore();
+ syncBuiltinESMExports();
+}
+
const unixRoot = path.posix.join(
unixHome,
".cache",
diff --git a/sdk/typescript/src/runtime.ts b/sdk/typescript/src/runtime.ts
index 17d08cbf4..301b5ebb1 100644
--- a/sdk/typescript/src/runtime.ts
+++ b/sdk/typescript/src/runtime.ts
@@ -3688,14 +3688,23 @@ export async function resolvePluginPython(
);
}
- const managedRoots = options.managedRuntimeRoots ?? [
- join(
- environmentValue(environment, "XDG_CACHE_HOME", true) ||
- join(options.homeDirectory ?? homedir(), ".cache"),
- "codex-runtimes",
- "codex-primary-runtime",
- ),
- ];
+ let managedRoots = options.managedRuntimeRoots;
+ if (managedRoots === undefined) {
+ try {
+ const cache =
+ environmentValue(environment, "XDG_CACHE_HOME", true) ||
+ join(options.homeDirectory ?? homedir(), ".cache");
+ managedRoots = [join(cache, "codex-runtimes", "codex-primary-runtime")];
+ } catch (error) {
+ // A numeric Linux UID can lack a home for the optional managed cache.
+ if (
+ process.platform !== "linux" ||
+ (error as { info?: NodeJS.ErrnoException }).info?.code !== "ENOENT"
+ )
+ throw error;
+ managedRoots = [];
+ }
+ }
const relativeCandidates =
process.platform === "win32"
? [
diff --git a/sdk/typescript/tests-ts/runtime.test.ts b/sdk/typescript/tests-ts/runtime.test.ts
index 08a2cadbf..1a2b66263 100644
--- a/sdk/typescript/tests-ts/runtime.test.ts
+++ b/sdk/typescript/tests-ts/runtime.test.ts
@@ -7135,6 +7135,39 @@ describe("runtime directories and plugin Python boundary", () => {
expect(result.status, result.stderr).toBe(0);
});
+ test.skipIf(process.platform !== "linux")(
+ "discovers system Python when the account has no home for the managed cache",
+ async () => {
+ const options = {
+ environment: {
+ PATH: process.env["PATH"],
+ LD_LIBRARY_PATH: process.env["LD_LIBRARY_PATH"],
+ },
+ protectedRoot: await temporaryDirectory(),
+ };
+ const systemPython = await resolvePluginPython({
+ ...options,
+ managedRuntimeRoots: [],
+ });
+ const homeError = new Error("home lookup failed");
+ using home = spyOn(await import("node:os"), "homedir").mockImplementation(
+ () => {
+ throw Object.assign(new Error("no home for this UID"), {
+ info: { code: "ENOENT", syscall: "uv_os_homedir" },
+ });
+ },
+ );
+ expect(await resolvePluginPython(options)).toBe(systemPython);
+ await expect(
+ resolvePluginPython({ ...options, environment: { PATH: "" } }),
+ ).rejects.toBeInstanceOf(PluginPythonUnavailableError);
+ home.mockImplementation(() => {
+ throw homeError;
+ });
+ await expect(resolvePluginPython(options)).rejects.toBe(homeError);
+ },
+ );
+
testPosix("uses configured, inherited, and managed Python", async () => {
const root = await temporaryDirectory();
using _home = spyOn(await import("node:os"), "homedir").mockImplementation(