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

test(sdk): resolve a usable Python interpreter for Windows workbench tests (#2493)

Michael D'Angelo9ee5983b300c
diff --git a/sdk/typescript/scripts/package-dist-files.mjs b/sdk/typescript/scripts/package-dist-files.mjs
index 7646a8382..7a4df1aa1 100644
--- a/sdk/typescript/scripts/package-dist-files.mjs
+++ b/sdk/typescript/scripts/package-dist-files.mjs
@@ -120,6 +120,7 @@ const distModules = [
"owner-evidence",
"patch-publication",
"patch-tui",
+ "plugin-python-probe",
"publication",
"publication-events",
"publication-store",
diff --git a/sdk/typescript/src/plugin-python-probe.ts b/sdk/typescript/src/plugin-python-probe.ts
new file mode 100644
index 000000000..724082435
--- /dev/null
+++ b/sdk/typescript/src/plugin-python-probe.ts
@@ -0,0 +1,10 @@
+export const PLUGIN_PYTHON_READY = "codex-security-python-ok";
+// Cold Python startup can exceed five seconds on busy Windows hosts.
+export const PLUGIN_PYTHON_PROBE_TIMEOUT_MS = 30_000;
+
+export const PLUGIN_PYTHON_PROBE = [
+ "import importlib.util,sys",
+ "if sys.version_info < (3, 10): raise SystemExit(1)",
+ "if sys.version_info < (3, 11) and importlib.util.find_spec('tomli') is None: raise SystemExit(1)",
+ `print('${PLUGIN_PYTHON_READY}')`,
+].join("\n");
diff --git a/sdk/typescript/src/runtime.ts b/sdk/typescript/src/runtime.ts
index 37a814796..706c77cbf 100644
--- a/sdk/typescript/src/runtime.ts
+++ b/sdk/typescript/src/runtime.ts
@@ -1,5 +1,10 @@
import { executablePathForSpawn } from "./executable-path.js";
export { executablePathForSpawn } from "./executable-path.js";
+import {
+ PLUGIN_PYTHON_PROBE,
+ PLUGIN_PYTHON_PROBE_TIMEOUT_MS,
+ PLUGIN_PYTHON_READY,
+} from "./plugin-python-probe.js";
import { resolveBundledCodexExecutable } from "./codex-sdk-environment.js";
import { gitProtectionRoots } from "./targets.js";
import { isNonEmptyString, notify } from "./value.js";
@@ -3813,23 +3818,16 @@ async function usablePython(
try {
const { stdout } = await execFile(
command.executable,
- [
- "-I",
- "-c",
- "import importlib.util,sys\nif sys.version_info < (3, 10): raise SystemExit(1)\nif sys.version_info < (3, 11) and importlib.util.find_spec('tomli') is None: raise SystemExit(1)\nprint('codex-security-python-ok')",
- ],
+ ["-I", "-c", PLUGIN_PYTHON_PROBE],
{
env: command.environment,
encoding: "utf8",
- // Cold Python startup can exceed five seconds on busy Windows hosts.
- timeout: 30_000,
+ timeout: PLUGIN_PYTHON_PROBE_TIMEOUT_MS,
windowsHide: true,
signal,
},
);
- return stdout.trim() === "codex-security-python-ok"
- ? command.executable
- : null;
+ return stdout.trim() === PLUGIN_PYTHON_READY ? command.executable : null;
} catch (error) {
if (signal?.aborted) throw error;
return null;
diff --git a/sdk/typescript/tests-ts/api-file-profile-replay.test.ts b/sdk/typescript/tests-ts/api-file-profile-replay.test.ts
index b7d224d04..d28a56b61 100644
--- a/sdk/typescript/tests-ts/api-file-profile-replay.test.ts
+++ b/sdk/typescript/tests-ts/api-file-profile-replay.test.ts
@@ -30,9 +30,11 @@ import { PLUGIN_ROOT } from "./plugin-root.js";
import { TestClient, mockWorkbench } from "./support/api-client.js";
import { preparedRuntime } from "./support/api-events.js";
import { rejecting } from "./support/errors.js";
-import { pythonExecutable } from "./support/python.js";
+import { useTestPython } from "./support/python.js";
import { createApiTestFixtures } from "./support/temporary-directories.js";
+const pythonExecutable = useTestPython();
+
const { temporaryDirectory, cleanup } = createApiTestFixtures(
"file-profile-replay-",
);
diff --git a/sdk/typescript/tests-ts/api.test.ts b/sdk/typescript/tests-ts/api.test.ts
index 050b9e3d5..87d081bb6 100644
--- a/sdk/typescript/tests-ts/api.test.ts
+++ b/sdk/typescript/tests-ts/api.test.ts
@@ -122,10 +122,13 @@ import { importScan } from "../src/import-scan.js";
import { restoreScanKnowledge, scanInputIdentity } from "../src/scan-inputs.js";
import { VERSION } from "../src/version.js";
import { createProviderProfile } from "../src/provider-profile.js";
-import { pythonExecutable, nodeCommand, gitText } from "./support/shell.js";
+import { nodeCommand, gitText } from "./support/shell.js";
+import { useTestPython } from "./support/python.js";
import { fail, rejecting, throwing } from "./support/errors.js";
import { mockFs, restoreFs } from "./support/module-mocks.js";
+const pythonExecutable = useTestPython();
+
async function registerEightFileScan(
_options: unknown,
args: readonly string[],
diff --git a/sdk/typescript/tests-ts/diff-rank-input.test.ts b/sdk/typescript/tests-ts/diff-rank-input.test.ts
index be967dd03..ab50f1df5 100644
--- a/sdk/typescript/tests-ts/diff-rank-input.test.ts
+++ b/sdk/typescript/tests-ts/diff-rank-input.test.ts
@@ -1,5 +1,5 @@
import { runNodePython } from "./support/python-probe.js";
-import { pythonExecutable } from "./support/python.js";
+import { useTestPython } from "./support/python.js";
import { nodeCommand } from "./support/shell.js";
import { windowsHelperFixture } from "./windows-helper-command.js";
import { createTemporaryDirectoriesSync } from "./support/temporary-directories.js";
@@ -19,6 +19,8 @@ import { basename, dirname, join } from "node:path";
import { afterEach, expect, test } from "bun:test";
import { PLUGIN_ROOT } from "./plugin-root.js";
+const pythonExecutable = useTestPython();
+
const temporaryRoots = createTemporaryDirectoriesSync(true);
const testPosix = process.platform === "win32" ? test.skip : test;
diff --git a/sdk/typescript/tests-ts/hosted-scan.test.ts b/sdk/typescript/tests-ts/hosted-scan.test.ts
index 97e0495a0..60cc00f4b 100644
--- a/sdk/typescript/tests-ts/hosted-scan.test.ts
+++ b/sdk/typescript/tests-ts/hosted-scan.test.ts
@@ -40,7 +40,7 @@ import { capture, dependencies } from "./cli-fixtures.js";
import { normalizeTarget } from "../src/targets.js";
import { runWorkbench } from "../src/runtime.js";
import { readSavedScanLifecycle } from "../src/saved-scan-bootstrap.js";
-import { pythonExecutable } from "./support/python.js";
+import { useTestPython } from "./support/python.js";
import { git } from "./git-fixture.js";
import { PLUGIN_ROOT } from "./plugin-root.js";
import {
@@ -51,6 +51,8 @@ import { parseJsonLines, readJson, writeJson } from "./support/json.js";
import { rejecting } from "./support/errors.js";
import { writeSource } from "./support/shell.js";
+const pythonExecutable = useTestPython();
+
const { severityRubric } = hostedContract.run.params;
const { cleanup, temporaryDirectory } = createApiTestFixtures("hosted-scan-");
afterEach(cleanup);
diff --git a/sdk/typescript/tests-ts/python-executable.test.ts b/sdk/typescript/tests-ts/python-executable.test.ts
new file mode 100644
index 000000000..36d76470c
--- /dev/null
+++ b/sdk/typescript/tests-ts/python-executable.test.ts
@@ -0,0 +1,149 @@
+import { afterEach, expect, spyOn, test } from "bun:test";
+import { spawnSync } from "node:child_process";
+import { copyFile, readFile, writeFile } from "node:fs/promises";
+import { delimiter, join } from "node:path";
+import { PLUGIN_PYTHON_PROBE } from "../src/plugin-python-probe.js";
+import * as pythonProbe from "./support/python-probe.js";
+import { useTestPython } from "./support/python.js";
+import { createTemporaryDirectories } from "./support/temporary-directories.js";
+
+const pythonExecutable = useTestPython();
+const pythonOnPath = useTestPython(false);
+const directories = createTemporaryDirectories(true);
+afterEach(directories.cleanup);
+
+test.skipIf(process.platform !== "win32")(
+ "prepares slow PATH discovery outside the test deadline and bypasses it for an override",
+ async () => {
+ const root = await directories.create("codex-security-python-discovery-");
+ const python = pythonExecutable()!;
+ const venv = join(root, "venv");
+ const created = pythonProbe.runNodePython(python, [
+ "-m",
+ "venv",
+ "--without-pip",
+ "--system-site-packages",
+ venv,
+ ]);
+ expect(created.status, created.stderr).toBe(0);
+ const bin = join(venv, "Scripts");
+ const fallback = join(bin, "python3.exe");
+ await copyFile(join(bin, "python.exe"), fallback);
+ const marker = join(root, "probes.txt");
+ await writeFile(
+ join(venv, "Lib", "site-packages", "sitecustomize.py"),
+ [
+ "import os, sys, time",
+ "from pathlib import Path",
+ "if Path(sys.executable).name.lower() == 'python.exe':",
+ " with open(os.environ['PYTHON_FIXTURE_MARKER'], 'a', encoding='utf-8') as record: record.write('probe\\n')",
+ " time.sleep(1.5)",
+ " os._exit(49)",
+ ].join("\n"),
+ );
+ const fixture = join(root, "python.test.ts");
+ const helper = new URL("./support/python.ts", import.meta.url).href;
+ await writeFile(
+ fixture,
+ [
+ 'import { expect, test } from "bun:test";',
+ `import { useTestPython } from ${JSON.stringify(helper)};`,
+ "const resolve = useTestPython();",
+ 'test("uses the prepared Python", () => {',
+ " expect(resolve()).toBe(process.env.EXPECTED_PYTHON);",
+ " if (!process.env.PYTHON) {",
+ " const path = process.env.PATH;",
+ " process.env.PATH = process.cwd();",
+ " expect(resolve()).toBeNull();",
+ " process.env.PATH = path;",
+ " expect(resolve()).toBe(process.env.EXPECTED_PYTHON);",
+ " }",
+ "});",
+ ].join("\n"),
+ );
+ for (const configured of [python, ""]) {
+ const result = spawnSync(
+ process.execPath,
+ ["test", "--timeout", "300", fixture],
+ {
+ cwd: root,
+ env: {
+ ...process.env,
+ PATH: [bin, process.env["PATH"]].join(delimiter),
+ PYTHON: configured,
+ EXPECTED_PYTHON: configured || fallback,
+ PYTHON_FIXTURE_MARKER: marker,
+ },
+ encoding: "utf8",
+ windowsHide: true,
+ },
+ );
+ expect(result.status, result.stderr).toBe(0);
+ if (configured) expect(await Bun.file(marker).exists()).toBe(false);
+ }
+ expect((await readFile(marker, "utf8")).split(/\r?\n/u)).toEqual([
+ "probe",
+ "",
+ ]);
+ },
+);
+
+test("uses a configured Python executable and discovers one for an empty override", () => {
+ const previous = process.env["PYTHON"];
+ try {
+ const discovered = pythonOnPath();
+ process.env["PYTHON"] = "";
+ expect(pythonExecutable()).toBe(discovered);
+ process.env["PYTHON"] = " synthetic python ";
+ expect(pythonExecutable()).toBe("synthetic python");
+ expect(pythonOnPath()).toBe(discovered);
+ } finally {
+ if (previous === undefined) delete process.env["PYTHON"];
+ else process.env["PYTHON"] = previous;
+ }
+});
+
+test.skipIf(process.platform !== "win32")(
+ "falls back to the Python launcher after a timeout and an unusable Windows alias",
+ () => {
+ const python = pythonExecutable();
+ expect(python).not.toBeNull();
+ using _which = spyOn(Bun, "which").mockImplementation((name) =>
+ name === "py" ? python : process.execPath,
+ );
+ using probes = spyOn(pythonProbe, "runNodePython").mockReturnValueOnce({
+ pid: 0,
+ output: [],
+ stdout: "",
+ stderr: "",
+ signal: "SIGTERM",
+ status: null,
+ error: Object.assign(new Error("Timed out"), { code: "ETIMEDOUT" }),
+ });
+ expect(pythonOnPath()).toBe(python);
+ expect(probes.mock.calls.map((call) => call[2]?.timeout)).toEqual([
+ 30_000, 30_000, 30_000,
+ ]);
+ },
+);
+
+test("the Python probe rejects unsupported versions and checks tomli on Python 3.10", () => {
+ const python = pythonExecutable();
+ expect(python).not.toBeNull();
+ for (const [minor, tomli, status] of [
+ [9, true, 1],
+ [10, false, 1],
+ [10, true, 0],
+ [11, false, 0],
+ ] as const) {
+ const program = [
+ "import importlib.util, sys",
+ `sys.version_info = (3, ${minor})`,
+ `importlib.util.find_spec = lambda name: object() if name == 'tomli' and ${tomli ? "True" : "False"} else None`,
+ PLUGIN_PYTHON_PROBE,
+ ].join("\n");
+ expect(pythonProbe.runPython(python!, ["-c", program]).exitCode).toBe(
+ status,
+ );
+ }
+});
diff --git a/sdk/typescript/tests-ts/scan-recovery.test.ts b/sdk/typescript/tests-ts/scan-recovery.test.ts
index 7d33fc088..c0502326b 100644
--- a/sdk/typescript/tests-ts/scan-recovery.test.ts
+++ b/sdk/typescript/tests-ts/scan-recovery.test.ts
@@ -8,6 +8,7 @@ import {
workbenchCommand,
} from "./support/workbench-command.js";
import { runNodePython } from "./support/python-probe.js";
+import { useTestPython } from "./support/python.js";
import { copyCompletedScanFixture, PLUGIN_ROOT } from "./plugin-root.js";
import { createApiTestFixtures } from "./support/temporary-directories.js";
import { readJson } from "./support/json.js";
@@ -16,6 +17,8 @@ import { ScanInterruptedError } from "../src/errors.js";
import { dependencies, fakeSecurity } from "./cli-fixtures.js";
import { createCliTest } from "./support/cli-run.js";
+const pythonExecutable = useTestPython();
+
type FindingsDocument = {
findings: Array<{
identity: { anchor: string };
@@ -70,8 +73,7 @@ async function startDraftScan(
recipeFromStdin = false,
) {
const root = await temporaryDirectory();
- const python =
- process.env["PYTHON"] ?? Bun.which("python3") ?? Bun.which("python");
+ const python = pythonExecutable();
expect(python).not.toBeNull();
const target = join(
diff --git a/sdk/typescript/tests-ts/sdk-scan-prompts.test.ts b/sdk/typescript/tests-ts/sdk-scan-prompts.test.ts
index 1b12c67c6..d66b032f4 100644
--- a/sdk/typescript/tests-ts/sdk-scan-prompts.test.ts
+++ b/sdk/typescript/tests-ts/sdk-scan-prompts.test.ts
@@ -19,6 +19,9 @@ import {
scanInputIdentity,
} from "../src/scan-inputs.js";
import { workbenchCommand } from "./support/workbench-command.js";
+import { useTestPython } from "./support/python.js";
+
+const pythonExecutable = useTestPython(false);
const { cleanup, temporaryDirectory } = createApiTestFixtures();
afterEach(cleanup);
@@ -36,7 +39,7 @@ test("rerun recognizes unchanged context after the workbench serializes its reci
undefined,
await readKnowledgeBaseSnapshot([document]),
);
- const python = Bun.which("python3") ?? Bun.which("python");
+ const python = pythonExecutable();
expect(python).not.toBeNull();
const command = workbenchCommand(python!, join(root, "state"));
const saved = await command(
diff --git a/sdk/typescript/tests-ts/support/python.ts b/sdk/typescript/tests-ts/support/python.ts
index d5c3bb734..a47485136 100644
--- a/sdk/typescript/tests-ts/support/python.ts
+++ b/sdk/typescript/tests-ts/support/python.ts
@@ -1,8 +1,52 @@
-export function pythonExecutable(inheritEnvironment = true): string | null {
- return (
- (inheritEnvironment ? process.env["PYTHON"] : undefined) ??
- Bun.which("python3") ??
- Bun.which("python") ??
- Bun.which("py")
+import { beforeAll } from "bun:test";
+import {
+ PLUGIN_PYTHON_PROBE,
+ PLUGIN_PYTHON_PROBE_TIMEOUT_MS,
+ PLUGIN_PYTHON_READY,
+} from "../../src/plugin-python-probe.js";
+import { runNodePython } from "./python-probe.js";
+
+// Tests can change and restore PATH. Reuse the probes for each ordered set of candidates.
+const windowsPythonByCandidates = new Map<string, string | null>();
+
+function findWindowsPython(): string | null {
+ const candidates = ["python", "python3", "py"].map((name) =>
+ Bun.which(name, { PATH: process.env["PATH"] ?? "" }),
);
+ const key = JSON.stringify(candidates);
+ const cached = windowsPythonByCandidates.get(key);
+ if (cached !== undefined) return cached;
+ // Windows Store aliases can resolve on PATH even without an interpreter.
+ const executable =
+ candidates.find((candidate) => {
+ if (candidate === null) return false;
+ const result = runNodePython(candidate, ["-c", PLUGIN_PYTHON_PROBE], {
+ timeout: PLUGIN_PYTHON_PROBE_TIMEOUT_MS,
+ windowsHide: true,
+ });
+ return (
+ result.status === 0 && result.stdout.trim() === PLUGIN_PYTHON_READY
+ );
+ }) ?? null;
+ windowsPythonByCandidates.set(key, executable);
+ return executable;
+}
+
+export function useTestPython(inheritEnvironment = true): () => string | null {
+ function resolve() {
+ const configured = inheritEnvironment
+ ? process.env["PYTHON"]?.trim()
+ : undefined;
+ if (configured) return configured;
+ return process.platform === "win32"
+ ? findWindowsPython()
+ : (Bun.which("python3") ?? Bun.which("python") ?? Bun.which("py"));
+ }
+ if (process.platform === "win32") {
+ // Three bounded probes fit in the Windows CI budget, outside each test's deadline.
+ beforeAll(() => {
+ resolve();
+ }, 120_000);
+ }
+ return resolve;
}
diff --git a/sdk/typescript/tests-ts/support/shell.ts b/sdk/typescript/tests-ts/support/shell.ts
index 1225514d8..965e425b2 100644
--- a/sdk/typescript/tests-ts/support/shell.ts
+++ b/sdk/typescript/tests-ts/support/shell.ts
@@ -69,8 +69,6 @@ export function runCommand(
});
}
-export { pythonExecutable } from "./python.js";
-
export function writeSource(
repository: string,
path: string,
diff --git a/sdk/typescript/tests-ts/workbench-compressed-usage.test.ts b/sdk/typescript/tests-ts/workbench-compressed-usage.test.ts
index a263dfcf8..db2df5903 100644
--- a/sdk/typescript/tests-ts/workbench-compressed-usage.test.ts
+++ b/sdk/typescript/tests-ts/workbench-compressed-usage.test.ts
@@ -4,9 +4,11 @@ import { execFileSync } from "node:child_process";
import { copyFile, mkdir, writeFile } from "node:fs/promises";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
-import { pythonExecutable } from "./support/python.js";
+import { useTestPython } from "./support/python.js";
import { createApiTestFixtures } from "./support/temporary-directories.js";
+const pythonExecutable = useTestPython();
+
const { temporaryDirectory, cleanup } = createApiTestFixtures(
"workbench-compressed-usage-",
);
diff --git a/sdk/typescript/tests-ts/workbench-nested-git-utf8.test.ts b/sdk/typescript/tests-ts/workbench-nested-git-utf8.test.ts
index 239014e2b..d55b08bba 100644
--- a/sdk/typescript/tests-ts/workbench-nested-git-utf8.test.ts
+++ b/sdk/typescript/tests-ts/workbench-nested-git-utf8.test.ts
@@ -1,5 +1,5 @@
import { createTemporaryDirectoriesSync } from "./support/temporary-directories.js";
-import { pythonExecutable } from "./support/python.js";
+import { useTestPython } from "./support/python.js";
import { spawnSync } from "node:child_process";
import { mkdirSync, readFileSync } from "node:fs";
import { join } from "node:path";
@@ -7,6 +7,8 @@ import { afterEach, expect, test } from "bun:test";
import { PLUGIN_ROOT } from "./plugin-root.js";
import { runNodePython } from "./support/python-probe.js";
+const pythonExecutable = useTestPython();
+
const temporaryDirectories = createTemporaryDirectoriesSync(true);
afterEach(temporaryDirectories.cleanup);
diff --git a/sdk/typescript/tests-ts/workbench-node-runtime.test.ts b/sdk/typescript/tests-ts/workbench-node-runtime.test.ts
index 7859bb9dd..bbedc15fc 100644
--- a/sdk/typescript/tests-ts/workbench-node-runtime.test.ts
+++ b/sdk/typescript/tests-ts/workbench-node-runtime.test.ts
@@ -6,9 +6,11 @@ import { basename, dirname, join, sep } from "node:path";
import { fileURLToPath } from "node:url";
import { promisify } from "node:util";
import { PLUGIN_ROOT } from "./plugin-root.js";
-import { pythonExecutable } from "./support/python.js";
+import { useTestPython } from "./support/python.js";
import { createApiTestFixtures } from "./support/temporary-directories.js";
+const pythonExecutable = useTestPython();
+
const { temporaryDirectory, cleanup } =
createApiTestFixtures("workbench-node-");
afterEach(cleanup);
diff --git a/sdk/typescript/tests-ts/workbench-windows-compatibility.test.ts b/sdk/typescript/tests-ts/workbench-windows-compatibility.test.ts
index 6d5dd2ea9..cd74cba82 100644
--- a/sdk/typescript/tests-ts/workbench-windows-compatibility.test.ts
+++ b/sdk/typescript/tests-ts/workbench-windows-compatibility.test.ts
@@ -2,14 +2,16 @@ import { join } from "node:path";
import { afterEach, expect, test } from "bun:test";
import { PLUGIN_ROOT } from "./plugin-root.js";
import { runPython } from "./support/python-probe.js";
+import { useTestPython } from "./support/python.js";
import { createTemporaryDirectories } from "./support/temporary-directories.js";
+const pythonExecutable = useTestPython();
const directories = createTemporaryDirectories(true);
afterEach(directories.cleanup);
function probe(program: string, ...args: string[]): void {
- const python = Bun.which("python3") ?? Bun.which("python");
+ const python = pythonExecutable();
if (python === null)
throw new Error("Python is required for workbench tests.");
const result = runPython(python, [