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): discover Python for targets when launched from filesystem root (#2569)

Michael D'Angelobf9853020059
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 4f9de0a89..b441c402c 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
@@ -1503,14 +1503,17 @@ async function testWorkerRuntimeSettings() {
);
const browserNode = path.join(fixture.root, "browser node");
process.env.CODEX_BROWSER_USE_NODE_PATH = browserNode;
- const python = path.join(fixture.root, "selected venv", "bin", "python");
+ const systemPython = process.platform === "win32" ? "python" : "python3";
+ const python =
+ configuration === "" && process.platform !== "win32"
+ ? systemPython
+ : path.join(fixture.root, "selected venv", "bin", "python");
const helperPython = path.join(
fixture.root,
"helper venv",
"bin",
"python",
);
- const systemPython = process.platform === "win32" ? "python" : "python3";
const workerPython = (index: number) =>
index === 0 ? systemPython : `${helperPython}-${index} `;
process.env.PYTHON = python;
diff --git a/sdk/typescript/src/runtime.ts b/sdk/typescript/src/runtime.ts
index e898679a4..d4bf700d4 100644
--- a/sdk/typescript/src/runtime.ts
+++ b/sdk/typescript/src/runtime.ts
@@ -3940,13 +3940,11 @@ export async function resolvePluginPython(
options: PluginPythonOptions = {},
): Promise<string> {
const environment = options.environment ?? process.env;
- const requestedRoots = options.protectedRoot ?? process.cwd();
+ const cwd = process.cwd();
+ const requestedRoots = options.protectedRoot ?? cwd;
const protectedRoot =
typeof requestedRoots === "string" ? [requestedRoots] : [...requestedRoots];
- const callerDirectories = [
- process.cwd(),
- options.currentDirectory ?? process.cwd(),
- ];
+ const callerDirectories = new Set([cwd, options.currentDirectory ?? cwd]);
// Preserve target and enclosing-checkout protection for every interpreter.
for (const directory of new Set([...callerDirectories, ...protectedRoot])) {
for (const checkout of await gitProtectionRoots(directory, options.signal))
@@ -3954,7 +3952,21 @@ export async function resolvePluginPython(
}
// Named interpreters are ambient PATH discovery, even when PYTHON names one.
// Explicit trusted paths and managed runtimes retain their existing precedence.
- const discoveryRoots = [...protectedRoot, ...callerDirectories];
+ const discoveryRoots = [...protectedRoot];
+ for (const directory of callerDirectories) {
+ const physical =
+ directory === cwd
+ ? cwd
+ : await realpath(directory).catch(() => undefined);
+ // A filesystem-root launch directory also contains the operator's installed tools.
+ // Explicit scan roots and enclosing checkouts are already in protectedRoot.
+ if (
+ protectedRoot.length === 0 ||
+ physical === undefined ||
+ dirname(physical) !== physical
+ )
+ discoveryRoots.push(directory);
+ }
if (options.configuredPath !== undefined) {
return await requirePython(
options.configuredPath,
diff --git a/sdk/typescript/tests-ts/runtime.test.ts b/sdk/typescript/tests-ts/runtime.test.ts
index ad0100d0a..d120d6abd 100644
--- a/sdk/typescript/tests-ts/runtime.test.ts
+++ b/sdk/typescript/tests-ts/runtime.test.ts
@@ -7022,6 +7022,61 @@ describe("runtime directories and plugin Python boundary", () => {
}
});
+ testPosix(
+ "discovers Python outside explicit targets from a filesystem-root working directory",
+ async () => {
+ if (
+ runTestInSubprocess(
+ fileURLToPath(import.meta.url),
+ "discovers Python outside explicit targets from a filesystem-root working directory",
+ {},
+ "/",
+ )
+ )
+ return;
+ const target = await temporaryDirectory();
+ const tools = await temporaryDirectory();
+ const system = pythonWithLauncher();
+ expect(system).not.toBeNull();
+ const name = "fixture-python";
+ await symlink(system!, join(tools, name));
+ await symlink(system!, join(target, name));
+ const alias = join(tools, "root-directory");
+ await symlink("/", alias, "dir");
+ expect(process.cwd()).toBe("/");
+ const environment = { PATH: [target, tools].join(":"), PYTHON: name };
+ for (const caller of ["/", alias]) {
+ const selected = await resolvePluginPython({
+ currentDirectory: caller,
+ protectedRoot: target,
+ environment,
+ });
+ expect(selected).toBe(join(tools, name));
+ }
+ await expect(
+ resolvePluginPython({
+ currentDirectory: `/${basename(target)}-missing/..`,
+ protectedRoot: target,
+ environment,
+ }),
+ ).rejects.toThrow("unavailable");
+ await expect(resolvePluginPython({ environment })).rejects.toThrow(
+ "unavailable",
+ );
+ for (const protectedRoot of [[], "/"]) {
+ await expect(
+ resolvePluginPython({ protectedRoot, environment }),
+ ).rejects.toThrow("unavailable");
+ }
+ await expect(
+ resolvePluginPython({
+ protectedRoot: target,
+ environment: { PATH: target, PYTHON: name },
+ }),
+ ).rejects.toThrow("unavailable");
+ },
+ );
+
testPosix(
"preserves literal executable override names and accepts padding",
async () => {
diff --git a/sdk/typescript/tests-ts/support/test-subprocess.ts b/sdk/typescript/tests-ts/support/test-subprocess.ts
index 1b653c215..2584938ab 100644
--- a/sdk/typescript/tests-ts/support/test-subprocess.ts
+++ b/sdk/typescript/tests-ts/support/test-subprocess.ts
@@ -5,6 +5,7 @@ export function runTestInSubprocess(
file: string,
name: string,
environment: NodeJS.ProcessEnv = {},
+ cwd: string | URL = new URL("../../", import.meta.url),
): boolean {
const identity = `${file}::${name}`;
if (process.env["CODEX_SECURITY_ISOLATED_TEST"] === identity) return false;
@@ -15,7 +16,7 @@ export function runTestInSubprocess(
process.execPath,
["test", "--timeout", timeout, "--test-name-pattern", pattern, file],
{
- cwd: new URL("../../", import.meta.url),
+ cwd,
encoding: "utf8",
env: {
...process.env,