OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security
TypeScript ai-securityapplication-securityclicode-scanningcodexcodex-securitycybersecuritydevsecopsnodejsnpmopenaisdksecuritytypescriptvulnerability-scanning
test(windows): resolve usable Python in scan fixtures (#2553)
Michael D'Angelo629d3a18ad97
diff --git a/sdk/typescript/tests-ts/api-severity-rubric.test.ts b/sdk/typescript/tests-ts/api-severity-rubric.test.tsindex 346dc0f86..67316ac24 100644--- a/sdk/typescript/tests-ts/api-severity-rubric.test.ts+++ b/sdk/typescript/tests-ts/api-severity-rubric.test.ts@@ -7,6 +7,9 @@ import { bundledSeverityRubric } from "../src/severity-rubric.js";import { mockWorkbench, TestClient } from "./support/api-client.js";import { codexFactory, preparedRuntime } from "./support/api-events.js";import { createApiTestFixtures } from "./support/temporary-directories.js";+import { useTestPython } from "./support/python.js";++const pythonExecutable = useTestPython();const { temporaryDirectory, cleanup } = createApiTestFixtures();afterEach(cleanup);@@ -28,7 +31,7 @@ for (const mode of ["standard", "deep"] as const) {CODEX_SECURITY_ENABLE_REMOTE_SEVERITY_RUBRIC:source === "bundled" ? undefined : "1",};- const python = Bun.which("python3") ?? Bun.which("python");+ const python = pythonExecutable();if (!python) throw new Error("Python is required for this test.");const markdown = `Use this synthetic ${source} policy.`;const rubricPath = join(root, "policy.md");diff --git a/sdk/typescript/tests-ts/contract.test.ts b/sdk/typescript/tests-ts/contract.test.tsindex 7bd9ac95e..f109d9efe 100644--- a/sdk/typescript/tests-ts/contract.test.ts+++ b/sdk/typescript/tests-ts/contract.test.ts@@ -24,11 +24,14 @@ import {} from "../src/contract.js";import type { NormalizedTarget, ScanExpectation } from "../src/index.js";import { PLUGIN_ROOT } from "./plugin-root.js";+import { useTestPython } from "./support/python.js";import { runPython } from "./support/python-probe.js";import { createContractTestFixtures } from "./support/contract-fixture.js";import { readJson as readJsonFile, writeJson } from "./support/json.js";import { runTestInSubprocess } from "./support/test-subprocess.js";+const pythonExecutable = useTestPython();+const EXAMPLE = join(PLUGIN_ROOT, "examples", "legacy-completed-scan");const { temporaryDirectory, cleanup, copyExample } =createContractTestFixtures();@@ -50,7 +53,7 @@ async function reseal(scanDir: string): Promise<void> {}function pythonExport(scanDir: string) {- return runPython(process.env["PYTHON"] ?? Bun.which("python3") ?? "python", [+ return runPython(pythonExecutable() ?? "python", [join(PLUGIN_ROOT, "scripts", "finalize_scan_contract.py"),"--scan-dir",scanDir,@@ -89,14 +92,11 @@ describe("canonical scan contract", () => {}await writeJson(manifestPath, manifest);await loadContract(scanDir, { pluginRoot: PLUGIN_ROOT });- const result = runPython(- process.env["PYTHON"] ?? Bun.which("python3") ?? "python",- [- join(PLUGIN_ROOT, "scripts", "finalize_scan_contract.py"),- "--scan-dir",- scanDir,- ],- );+ const result = runPython(pythonExecutable() ?? "python", [+ join(PLUGIN_ROOT, "scripts", "finalize_scan_contract.py"),+ "--scan-dir",+ scanDir,+ ]);expect(result.exitCode, new TextDecoder().decode(result.stderr)).toBe(0);for (const path of projections)expect(await readFile(join(scanDir, path), "utf8")).toBe(path);@@ -127,15 +127,12 @@ describe("canonical scan contract", () => {await writeJson(findingsPath, findings);await writeJson(manifestPath, manifest);await reseal(scanDir);- const result = runPython(- process.env["PYTHON"] ?? Bun.which("python3") ?? "python",- [- "-c",- "import sys; from pathlib import Path; sys.path.insert(0, sys.argv[1]); from finalize_scan_contract import _prepare_scan_finalization; _prepare_scan_finalization(Path(sys.argv[2]))",- join(PLUGIN_ROOT, "scripts"),- scanDir,- ],- );+ const result = runPython(pythonExecutable() ?? "python", [+ "-c",+ "import sys; from pathlib import Path; sys.path.insert(0, sys.argv[1]); from finalize_scan_contract import _prepare_scan_finalization; _prepare_scan_finalization(Path(sys.argv[2]))",+ join(PLUGIN_ROOT, "scripts"),+ scanDir,+ ]);expect(result.exitCode, new TextDecoder().decode(result.stderr)).not.toBe(0,);@@ -512,14 +509,11 @@ describe("canonical scan contract", () => {manifest["scan"]["target"]["kind"] = kind;manifest["scan"]["target"]["revision"] = "";await writeJson(manifestPath, manifest);- const result = runPython(- process.env["PYTHON"] ?? Bun.which("python3") ?? "python",- [- join(PLUGIN_ROOT, "scripts", "finalize_scan_contract.py"),- "--scan-dir",- scanDir,- ],- );+ const result = runPython(pythonExecutable() ?? "python", [+ join(PLUGIN_ROOT, "scripts", "finalize_scan_contract.py"),+ "--scan-dir",+ scanDir,+ ]);expect(result.exitCode, new TextDecoder().decode(result.stderr)).toBe(0);await loadContract(scanDir, { pluginRoot: PLUGIN_ROOT });},@@ -895,8 +889,7 @@ describe("canonical scan contract", () => {});test("keeps bundled finalizer paths portable to Windows", () => {- const python =- process.env["PYTHON"] ?? Bun.which("python3") ?? Bun.which("python");+ const python = pythonExecutable();expect(python).not.toBeNull();const program = ["import json, sys",@@ -1109,8 +1102,7 @@ describe("canonical scan contract", () => {loadContract(scanDir, { pluginRoot: PLUGIN_ROOT }),).rejects.toThrow("duplicate artifact path");- const python =- process.env["PYTHON"] ?? Bun.which("python3") ?? Bun.which("python");+ const python = pythonExecutable();expect(python).not.toBeNull();const result = runPython(python!, [join(PLUGIN_ROOT, "scripts", "finalize_scan_contract.py"),diff --git a/sdk/typescript/tests-ts/finding-preview.test.ts b/sdk/typescript/tests-ts/finding-preview.test.tsindex fc8b187b6..979a26ef6 100644--- a/sdk/typescript/tests-ts/finding-preview.test.ts+++ b/sdk/typescript/tests-ts/finding-preview.test.ts@@ -2,9 +2,14 @@ import { join } from "node:path";import { fileURLToPath } from "node:url";import { describe, expect, test } from "bun:test";import { PLUGIN_ROOT } from "./plugin-root.js";+import { useTestPython } from "./support/python.js";++const pythonExecutable = useTestPython();+// Source-scope scenarios exercise many native Git commands on Windows.+const previewTimeout = process.platform === "win32" ? 180_000 : 30_000;function checkSourceScope(scenario: string, expected: unknown = null): void {- const python = Bun.which("python3") ?? Bun.which("python") ?? Bun.which("py");+ const python = pythonExecutable();expect(python).not.toBeNull();const result = Bun.spawnSync([@@ -22,114 +27,170 @@ function checkSourceScope(scenario: string, expected: unknown = null): void {}describe("bundled finding previews", () => {- test("reads only the immutable source objects selected for the scan", () => {- checkSourceScope("boundaries", {- selected: "1 public source",- outside: null,- additional: "1 selected file",- repository: "1 private source",- fileDescendant: null,- traversal: null,- absolute: null,- redirected: null,- escaped: null,- legacyScoped: "1 public source",- legacyUnmarkedFile: "1 selected file",- legacyUnmarkedFileDescendant: null,- legacyRoot: "1 private source",- legacyKnownDirectory: "1 public source",- legacyKnownFile: "1 selected file",- legacyFileDescendant: null,- emptyAuthority: null,- dirty: null,- fallback: "1 public source",- rootControl: "1 public source",- replacedFile: "1 selected file",- replacedFileDescendant: null,- removedDirectory: "1 nested source",- offline: "1 public source",- missingObject: null,- });- }, 30_000);-- test("keeps captured filesystem aliases usable after their source paths disappear", () => {- checkSourceScope("aliases", {- case: expect.any(Boolean),- unicode: expect.any(Boolean),- nonAscii: expect.any(Boolean),- collisionChecked: true,- });- }, 30_000);-- test("requires distinct filesystem witnesses for colliding descendants", () => {- checkSourceScope("descendant_aliases");- }, 30_000);-- test("keeps saved immutable objects readable after replacement refs change", () => {- checkSourceScope("replacements");- }, 30_000);-- test("does not run working-tree filters when registering a committed-ref scan", () => {- checkSourceScope("replacement_filters");- }, 30_000);-- test("omits source authority when replacement refs changed the scanned tree", () => {- checkSourceScope("replacement_snapshot");- }, 30_000);-- test("omits stale committed excerpts for working-tree diff scans", () => {- checkSourceScope("working_tree_excerpt", {- authorityOmitted: true,- currentExcerptOmitted: true,- legacyExcerptOmitted: true,- rangeExcerptPreserved: true,- });- }, 30_000);-- test("indexes Git names once for large explicit scope lists", () => {- checkSourceScope("indexed_scopes", 256);- }, 30_000);-- test("selects source excerpts from the displayed finding locations", () => {- checkSourceScope("display_locations", 8);- }, 30_000);-- test("does not grant source scope through a selected directory link", () => {- checkSourceScope("selected_redirects");- }, 30_000);-- test("rejects unsafe finding locations before invoking Git", () => {- checkSourceScope("unsafe_locations");- }, 30_000);-- test("does not treat links or reparse points as filesystem alias evidence", () => {- checkSourceScope("alias_evidence", {- ordinary: true,- hardlink: false,- symlink: false,- reparse: false,- });- }, 30_000);-- test("keeps subdirectory and linked-worktree targets bound to their selected tree", () => {- checkSourceScope("worktrees");- }, 30_000);+ test(+ "reads only the immutable source objects selected for the scan",+ () => {+ checkSourceScope("boundaries", {+ selected: "1 public source",+ outside: null,+ additional: "1 selected file",+ repository: "1 private source",+ fileDescendant: null,+ traversal: null,+ absolute: null,+ redirected: null,+ escaped: null,+ legacyScoped: "1 public source",+ legacyUnmarkedFile: "1 selected file",+ legacyUnmarkedFileDescendant: null,+ legacyRoot: "1 private source",+ legacyKnownDirectory: "1 public source",+ legacyKnownFile: "1 selected file",+ legacyFileDescendant: null,+ emptyAuthority: null,+ dirty: null,+ fallback: "1 public source",+ rootControl: "1 public source",+ replacedFile: "1 selected file",+ replacedFileDescendant: null,+ removedDirectory: "1 nested source",+ offline: "1 public source",+ missingObject: null,+ });+ },+ previewTimeout,+ );++ test(+ "keeps captured filesystem aliases usable after their source paths disappear",+ () => {+ checkSourceScope("aliases", {+ case: expect.any(Boolean),+ unicode: expect.any(Boolean),+ nonAscii: expect.any(Boolean),+ collisionChecked: true,+ });+ },+ previewTimeout,+ );++ test(+ "requires distinct filesystem witnesses for colliding descendants",+ () => {+ checkSourceScope("descendant_aliases");+ },+ previewTimeout,+ );++ test(+ "keeps saved immutable objects readable after replacement refs change",+ () => {+ checkSourceScope("replacements");+ },+ previewTimeout,+ );++ test(+ "does not run working-tree filters when registering a committed-ref scan",+ () => {+ checkSourceScope("replacement_filters");+ },+ previewTimeout,+ );++ test(+ "omits source authority when replacement refs changed the scanned tree",+ () => {+ checkSourceScope("replacement_snapshot");+ },+ previewTimeout,+ );++ test(+ "omits stale committed excerpts for working-tree diff scans",+ () => {+ checkSourceScope("working_tree_excerpt", {+ authorityOmitted: true,+ currentExcerptOmitted: true,+ legacyExcerptOmitted: true,+ rangeExcerptPreserved: true,+ });+ },+ previewTimeout,+ );++ test(+ "indexes Git names once for large explicit scope lists",+ () => {+ checkSourceScope("indexed_scopes", 256);+ },+ previewTimeout,+ );++ test(+ "selects source excerpts from the displayed finding locations",+ () => {+ checkSourceScope("display_locations", 8);+ },+ previewTimeout,+ );++ test(+ "does not grant source scope through a selected directory link",+ () => {+ checkSourceScope("selected_redirects");+ },+ previewTimeout,+ );++ test(+ "rejects unsafe finding locations before invoking Git",+ () => {+ checkSourceScope("unsafe_locations");+ },+ previewTimeout,+ );++ test(+ "does not treat links or reparse points as filesystem alias evidence",+ () => {+ checkSourceScope("alias_evidence", {+ ordinary: true,+ hardlink: false,+ symlink: false,+ reparse: false,+ });+ },+ previewTimeout,+ );++ test(+ "keeps subdirectory and linked-worktree targets bound to their selected tree",+ () => {+ checkSourceScope("worktrees");+ },+ previewTimeout,+ );test.each(["workspace", "prompt", "headless", "deep", "cli"])("records source authority through the %s scan-start path",(writer) => {checkSourceScope(`writer_${writer}`, writer);},- 30_000,+ previewTimeout,);- test("preserves legacy scans and separately owned migration history", () => {- checkSourceScope("migration");- }, 30_000);+ test(+ "preserves legacy scans and separately owned migration history",+ () => {+ checkSourceScope("migration");+ },+ previewTimeout,+ );});test("bundled finding previews normalize details without changing stored findings", () => {- const python = Bun.which("python3") ?? Bun.which("python") ?? Bun.which("py");+ const python = pythonExecutable();expect(python).not.toBeNull();const original = {attackPath: { likelihood: "medium" },diff --git a/sdk/typescript/tests-ts/import-scan.test.ts b/sdk/typescript/tests-ts/import-scan.test.tsindex be4a99b7d..b2971f607 100644--- a/sdk/typescript/tests-ts/import-scan.test.ts+++ b/sdk/typescript/tests-ts/import-scan.test.ts@@ -24,11 +24,14 @@ import { ScanResult } from "../src/result.js";import { runWorkbench } from "../src/runtime.js";import { VERSION } from "../src/version.js";import { PLUGIN_ROOT } from "./plugin-root.js";+import { useTestPython } from "./support/python.js";import { runTestInSubprocess } from "./support/test-subprocess.js";import { createApiTestFixtures } from "./support/temporary-directories.js";import { rejecting } from "./support/errors.js";import { TestClient } from "./support/api-client.js";+const pythonExecutable = useTestPython();+const { temporaryDirectories: roots, cleanup } = createApiTestFixtures();afterEach(cleanup);@@ -86,7 +89,7 @@ async function fixture(format: "csv" | "json" = "csv") {join(await realpath(tmpdir()), "import-scan-test-"),);roots.track(root);- const python = Bun.which("python3") ?? Bun.which("python");+ const python = pythonExecutable();expect(python).not.toBeNull();const stateDirectory = join(root, "state");const environment = {@@ -170,6 +173,7 @@ test.each(["completed", "failed", "interrupted", "preview"] as const)(runTestInSubprocess(import.meta.path,`scratch cleanup failure preserves the ${outcome} import outcome`,+ { PYTHON: pythonExecutable() ?? undefined },))return;@@ -634,6 +638,7 @@ test.each(["regular file", "symbolic link"])(runTestInSubprocess(import.meta.path,`import rejects a source replaced with a ${replacement} before reading`,+ { PYTHON: pythonExecutable() ?? undefined },))return;diff --git a/sdk/typescript/tests-ts/python-executable.test.ts b/sdk/typescript/tests-ts/python-executable.test.tsindex 36d76470c..5595564fb 100644--- a/sdk/typescript/tests-ts/python-executable.test.ts+++ b/sdk/typescript/tests-ts/python-executable.test.ts@@ -43,11 +43,14 @@ test.skipIf(process.platform !== "win32")();const fixture = join(root, "python.test.ts");const helper = new URL("./support/python.ts", import.meta.url).href;+ const isolated = new URL("./support/test-subprocess.ts", import.meta.url)+ .href;await writeFile(fixture,['import { expect, test } from "bun:test";',`import { useTestPython } from ${JSON.stringify(helper)};`,+ `import { runTestInSubprocess } from ${JSON.stringify(isolated)};`,"const resolve = useTestPython();",'test("uses the prepared Python", () => {'," expect(resolve()).toBe(process.env.EXPECTED_PYTHON);",@@ -59,6 +62,10 @@ test.skipIf(process.platform !== "win32")(" expect(resolve()).toBe(process.env.EXPECTED_PYTHON);"," }","});",+ 'test("isolated child reuses the prepared Python", () => {',+ ' if (runTestInSubprocess(import.meta.path, "isolated child reuses the prepared Python", { PYTHON: resolve() ?? undefined })) return;',+ " expect(resolve()).toBe(process.env.EXPECTED_PYTHON);",+ "}, 30000);",].join("\n"),);for (const configured of [python, ""]) {diff --git a/sdk/typescript/tests-ts/stopped-scan-results.test.ts b/sdk/typescript/tests-ts/stopped-scan-results.test.tsindex 3a8868a35..83c83b59e 100644--- a/sdk/typescript/tests-ts/stopped-scan-results.test.ts+++ b/sdk/typescript/tests-ts/stopped-scan-results.test.ts@@ -2,8 +2,10 @@ import { createTemporaryDirectoriesSync } from "./support/temporary-directories.import { afterEach, expect, test } from "bun:test";import { PLUGIN_ROOT } from "./plugin-root.js";import { runNodePython } from "./support/python-probe.js";+import { useTestPython } from "./support/python.js";const temporaryDirectories = createTemporaryDirectoriesSync();+const pythonExecutable = useTestPython();afterEach(temporaryDirectories.cleanup);@@ -171,7 +173,7 @@ function runStoppedScanProbe(prefix: string,terminalStatus?: string,) {- const python = Bun.which("python3") ?? Bun.which("python");+ const python = pythonExecutable();expect(python).not.toBeNull();const root = temporaryDirectories.create(prefix);const result = runNodePython(python!, [diff --git a/sdk/typescript/tests-ts/support/test-subprocess.ts b/sdk/typescript/tests-ts/support/test-subprocess.tsindex d25121e9c..1b653c215 100644--- a/sdk/typescript/tests-ts/support/test-subprocess.ts+++ b/sdk/typescript/tests-ts/support/test-subprocess.ts@@ -1,7 +1,11 @@import { spawnSync } from "node:child_process";import { expect } from "bun:test";-export function runTestInSubprocess(file: string, name: string): boolean {+export function runTestInSubprocess(+ file: string,+ name: string,+ environment: NodeJS.ProcessEnv = {},+): boolean {const identity = `${file}::${name}`;if (process.env["CODEX_SECURITY_ISOLATED_TEST"] === identity) return false;const timeout = process.env["CODEX_SECURITY_TEST_TIMEOUT_MS"] ?? "30000";@@ -13,7 +17,11 @@ export function runTestInSubprocess(file: string, name: string): boolean {{cwd: new URL("../../", import.meta.url),encoding: "utf8",- env: { ...process.env, CODEX_SECURITY_ISOLATED_TEST: identity },+ env: {+ ...process.env,+ ...environment,+ CODEX_SECURITY_ISOLATED_TEST: identity,+ },windowsHide: true,},);