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: isolate SDK fixtures and simplify Windows shards (#504)

mldangelo-oai357aadf7f3e5
diff --git a/.github/workflows/node-ci.yml b/.github/workflows/node-ci.yml
index f04481a9f..c6398851d 100644
--- a/.github/workflows/node-ci.yml
+++ b/.github/workflows/node-ci.yml
@@ -140,14 +140,7 @@ jobs:
- name: Prepare private Windows test root
id: windows-temp
shell: pwsh
- run: |
- $ErrorActionPreference = 'Stop'
- $path = Join-Path $env:USERPROFILE '.codex-security-ci-temp'
- New-Item -ItemType Directory -Path $path -Force | Out-Null
- $sid = (whoami /user /fo csv /nh | ConvertFrom-Csv -Header Name, Sid).Sid
- & icacls $path /inheritance:r /grant:r "*${sid}:(OI)(CI)F" '*S-1-5-18:(OI)(CI)F' '*S-1-5-32-544:(OI)(CI)F' | Out-Null
- if ($LASTEXITCODE -ne 0) { throw 'Could not secure the Windows test root' }
- "path=$path" >> $env:GITHUB_OUTPUT
+ run: ./sdk/typescript/scripts/prepare-windows-test-root.ps1
- name: Test shard ${{ matrix.shard }}
timeout-minutes: 10
@@ -155,9 +148,20 @@ jobs:
TEMP: ${{ steps.windows-temp.outputs.path }}
TMP: ${{ steps.windows-temp.outputs.path }}
TMPDIR: ${{ steps.windows-temp.outputs.path }}
- CODEX_SECURITY_ALLOW_MACHINE_POLICY_TEST: ${{ runner.environment == 'github-hosted' && 'true' || 'false' }}
+ CODEX_SECURITY_ALLOW_MACHINE_POLICY_TEST: "false"
run: node sdk/typescript/scripts/run-windows-ci-tests.mjs ${{ matrix.shard }}
+ - name: Test machine-wide PowerShell policy
+ if: matrix.shard == 3 && runner.environment == 'github-hosted'
+ timeout-minutes: 5
+ working-directory: sdk/typescript
+ env:
+ TEMP: ${{ steps.windows-temp.outputs.path }}
+ TMP: ${{ steps.windows-temp.outputs.path }}
+ TMPDIR: ${{ steps.windows-temp.outputs.path }}
+ CODEX_SECURITY_ALLOW_MACHINE_POLICY_TEST: "true"
+ run: bun test --timeout 120000 ./tests-ts/windows-machine-policy.test.ts
+
- name: Typecheck
if: matrix.shard == 7
working-directory: sdk/typescript
diff --git a/sdk/typescript/scripts/prepare-windows-test-root.ps1 b/sdk/typescript/scripts/prepare-windows-test-root.ps1
new file mode 100644
index 000000000..e6c9ed9af
--- /dev/null
+++ b/sdk/typescript/scripts/prepare-windows-test-root.ps1
@@ -0,0 +1,7 @@
+$ErrorActionPreference = 'Stop'
+$path = Join-Path $env:USERPROFILE '.codex-security-ci-temp'
+New-Item -ItemType Directory -Path $path -Force | Out-Null
+$sid = (whoami /user /fo csv /nh | ConvertFrom-Csv -Header Name, Sid).Sid
+& icacls $path /inheritance:r /grant:r "*${sid}:(OI)(CI)F" '*S-1-5-18:(OI)(CI)F' '*S-1-5-32-544:(OI)(CI)F' | Out-Null
+if ($LASTEXITCODE -ne 0) { throw 'Could not secure the Windows test root' }
+"path=$path" >> $env:GITHUB_OUTPUT
diff --git a/sdk/typescript/scripts/run-windows-ci-tests.mjs b/sdk/typescript/scripts/run-windows-ci-tests.mjs
index 2f7a7ee4d..83b792e81 100644
--- a/sdk/typescript/scripts/run-windows-ci-tests.mjs
+++ b/sdk/typescript/scripts/run-windows-ci-tests.mjs
@@ -1,45 +1,25 @@
import { spawn } from "node:child_process";
-import { readFile, readdir } from "node:fs/promises";
+import { readdir } from "node:fs/promises";
import { fileURLToPath } from "node:url";
const testsDirectory = new URL("../tests-ts/", import.meta.url);
const packageDirectory = fileURLToPath(new URL("../", import.meta.url));
const tests = (await readdir(testsDirectory))
- .filter((file) => file.endsWith(".test.ts"))
+ .filter(
+ (file) =>
+ file.endsWith(".test.ts") && file !== "windows-machine-policy.test.ts",
+ )
.sort();
-const slowApiTestNames = [
- "keeps a private preflight snapshot isolated from persistent credentials",
- "reuses keyring-compatible credentials across separate scan clients",
- "runs parallel ChatGPT scans with isolated mutable configuration",
- "reuses the managed runtime when scan authentication changes",
- "does not reimport ambient credentials after an explicit logout",
-];
-const apiTestSource = await readFile(
- new URL("../tests-ts/api.test.ts", import.meta.url),
- "utf8",
-);
-for (const testName of slowApiTestNames) {
- if (!apiTestSource.includes(`test("${testName}"`)) {
- throw new Error("Windows CI slow API shard references a missing test.");
- }
-}
-const slowApiTests = slowApiTestNames.join("|");
const shardSeeds = [
- {
- files: ["api.test.ts"],
- testNamePattern: slowApiTests,
- },
- {
- files: ["api.test.ts"],
- testNamePattern: `^(?!.*(?:${slowApiTests})).*$`,
- },
- { files: ["runtime.test.ts"] },
- { files: ["cli-authentication.test.ts"] },
- { files: ["scan-recovery.test.ts"] },
- { files: [] },
- { files: [] },
+ ["api-credentials.test.ts"],
+ ["api.test.ts"],
+ ["runtime.test.ts"],
+ ["cli-authentication.test.ts"],
+ ["scan-recovery.test.ts"],
+ [],
+ [],
];
-const assigned = new Set(shardSeeds.flatMap(({ files }) => files));
+const assigned = new Set(shardSeeds.flat());
for (const file of assigned) {
if (!tests.includes(file)) {
throw new Error("Windows CI test shard references a missing file: " + file);
@@ -52,28 +32,19 @@ const slowRemainderFiles = new Set([
"scan-comparison.test.ts",
]);
for (const [index, file] of unassigned.entries()) {
- shardSeeds[slowRemainderFiles.has(file) ? 6 : 5 + (index % 2)].files.push(
- file,
- );
+ shardSeeds[slowRemainderFiles.has(file) ? 6 : 5 + (index % 2)].push(file);
}
-const assignments = new Map();
-for (const { files } of shardSeeds) {
- for (const file of files) {
- assignments.set(file, (assignments.get(file) ?? 0) + 1);
- }
-}
-for (const file of tests) {
- const expectedAssignments = file === "api.test.ts" ? 2 : 1;
- if (assignments.get(file) !== expectedAssignments) {
- throw new Error("Windows CI test shards must run every test file.");
- }
+const assignments = shardSeeds.flat();
+if (
+ assignments.length !== tests.length ||
+ new Set(assignments).size !== tests.length
+) {
+ throw new Error("Windows CI test shards must run every test file once.");
}
const requestedShard =
- process.argv[2] === undefined
- ? undefined
- : Number.parseInt(process.argv[2], 10);
+ process.argv[2] === undefined ? undefined : Number(process.argv[2]);
if (
requestedShard !== undefined &&
(!Number.isSafeInteger(requestedShard) ||
@@ -84,12 +55,12 @@ if (
}
const selectedShards =
requestedShard === undefined
- ? shardSeeds.map((shard, index) => ({ ...shard, index }))
- : [{ ...shardSeeds[requestedShard - 1], index: requestedShard - 1 }];
+ ? shardSeeds.map((files, index) => ({ files, index }))
+ : [{ files: shardSeeds[requestedShard - 1], index: requestedShard - 1 }];
const results = await Promise.all(
selectedShards.map(
- ({ files, index, testNamePattern }) =>
+ ({ files, index }) =>
new Promise((resolve, reject) => {
const paths = files.map((file) => "./tests-ts/" + file);
console.log(
@@ -98,19 +69,11 @@ const results = await Promise.all(
"/" +
shardSeeds.length +
": " +
- paths.join(" ") +
- (testNamePattern === undefined
- ? ""
- : " --test-name-pattern " + testNamePattern),
+ paths.join(" "),
);
// Native Windows credential and document checks can exceed 30 seconds.
// The workflow still bounds each complete shard to ten minutes.
- const args = ["test", "--timeout", "120000"];
- if (testNamePattern !== undefined) {
- args.push("--test-name-pattern", testNamePattern);
- }
- args.push(...paths);
- const child = spawn("bun", args, {
+ const child = spawn("bun", ["test", "--timeout", "120000", ...paths], {
cwd: packageDirectory,
stdio: "inherit",
windowsHide: true,
diff --git a/sdk/typescript/tests-ts/api-credentials.test.ts b/sdk/typescript/tests-ts/api-credentials.test.ts
new file mode 100644
index 000000000..e8935a636
--- /dev/null
+++ b/sdk/typescript/tests-ts/api-credentials.test.ts
@@ -0,0 +1,476 @@
+import { execFileSync } from "node:child_process";
+import { existsSync } from "node:fs";
+import { mkdir, readFile, stat, writeFile } from "node:fs/promises";
+import { join } from "node:path";
+import type { CodexOptions } from "@openai/codex-sdk";
+import { afterEach, describe, expect, test } from "bun:test";
+import { parse as parseToml } from "smol-toml";
+import { initialCredentialsAvailable } from "../src/api.js";
+import { setCodexSecurityCredentialLogout } from "../src/runtime.js";
+import { PLUGIN_ROOT } from "./plugin-root.js";
+import { shellEnvironmentReference, TestClient } from "./support/api-client.js";
+import {
+ completedEvents,
+ createApiTestFixtures,
+} from "./support/api-events.js";
+
+const { cleanup, copyCompletedScan, temporaryDirectory } =
+ createApiTestFixtures();
+afterEach(cleanup);
+
+describe("CodexSecurity orchestration", () => {
+ test("keeps a private preflight snapshot isolated from persistent credentials", async () => {
+ const root = await temporaryDirectory();
+ const repository = join(root, "repository");
+ const ambientHome = join(root, "ambient-codex-home");
+ const scanDir = join(root, "scan");
+ await mkdir(repository);
+ await mkdir(ambientHome);
+ await mkdir(scanDir, { mode: 0o700 });
+ await writeFile(join(ambientHome, "auth.json"), "{}\n");
+ const interpreter =
+ Bun.which("python3") ?? Bun.which("python") ?? Bun.which("py");
+ expect(interpreter).not.toBeNull();
+ let capturedConfigPath: string | undefined;
+ let capturedCodexHome: string | undefined;
+ const unrelatedProjects = Object.fromEntries(
+ Array.from({ length: 256 }, (_, index) => [
+ join(root, `unrelated-project-${index}`),
+ { trust_level: "untrusted" },
+ ]),
+ );
+ const client = new TestClient(
+ {
+ pluginPath: PLUGIN_ROOT,
+ codexOverrides: {
+ approval_policy: "never",
+ features: { goals: true },
+ projects: {
+ ...unrelatedProjects,
+ [repository]: { trust_level: "trusted" },
+ },
+ mcp_servers: {
+ private: {
+ command: "echo",
+ env: { PRIVATE_TOKEN: "RUNTIME_MCP_SECRET" },
+ },
+ },
+ shell_environment_policy: {
+ set: { PRIVATE_TOKEN: "RUNTIME_SHELL_SECRET" },
+ },
+ responses_api_metadata: {
+ request_trace: "preserve-configured-metadata",
+ },
+ },
+ },
+ {
+ environment: { CODEX_HOME: ambientHome },
+ resolvePluginPython: async () => interpreter!,
+ prepareOutputDir: async () => scanDir,
+ repositoryRevision: async () => "deadbeef",
+ createCodex: (options: CodexOptions) => ({
+ startThread: () => ({
+ id: null,
+ async runStreamed(input: string) {
+ const configPath = options.env?.["CODEX_SECURITY_CONFIG_PATH"];
+ const codexHome = options.env?.["CODEX_HOME"];
+ expect(typeof configPath).toBe("string");
+ expect(typeof codexHome).toBe("string");
+ capturedConfigPath = configPath;
+ capturedCodexHome = codexHome;
+ expect(configPath!.startsWith(`${codexHome!}/`)).toBe(false);
+ expect(
+ parseToml(
+ await readFile(join(codexHome!, "config.toml"), "utf8"),
+ ),
+ ).toMatchObject({
+ approval_policy: "never",
+ permissions: {
+ codex_security_scan: {
+ filesystem: {
+ ":root": "read",
+ ":workspace_roots": "write",
+ [join(ambientHome, "state", "plugins", "codex-security")]:
+ "write",
+ [join(
+ ambientHome,
+ "state",
+ "plugins",
+ "codex-security",
+ "codex-home",
+ )]: "read",
+ },
+ },
+ },
+ });
+ const codexConfig = await readFile(
+ join(codexHome!, "config.toml"),
+ "utf8",
+ );
+ expect(options.env?.["CODEX_SECURITY_SURFACE"]).toBe("sdk");
+ expect(codexConfig).not.toContain("model_reasoning_summary");
+ expect(codexConfig).not.toContain("show_raw_agent_reasoning");
+ expect(options.config).not.toHaveProperty("projects");
+ expect(options.config).not.toHaveProperty("permissions");
+ expect(options.config).toMatchObject({
+ default_permissions: "codex_security_scan",
+ allow_login_shell: false,
+ model_reasoning_summary: "detailed",
+ show_raw_agent_reasoning: true,
+ windows: { sandbox: "unelevated" },
+ mcp_servers: {
+ private: {
+ command: "echo",
+ env: { PRIVATE_TOKEN: "RUNTIME_MCP_SECRET" },
+ },
+ },
+ shell_environment_policy: {
+ set: { PRIVATE_TOKEN: "RUNTIME_SHELL_SECRET" },
+ },
+ responses_api_metadata: {
+ request_trace: "preserve-configured-metadata",
+ codex_security_surface: "sdk",
+ },
+ });
+ if (process.platform !== "win32") {
+ expect((await stat(configPath!)).mode & 0o777).toBe(0o600);
+ }
+ const serialized = await readFile(configPath!, "utf8");
+ expect(serialized).not.toContain("RUNTIME_MCP_SECRET");
+ expect(serialized).not.toContain("RUNTIME_SHELL_SECRET");
+ expect(serialized).not.toContain("mcp_servers");
+ expect(serialized).not.toContain("shell_environment_policy");
+ expect(parseToml(serialized)).toMatchObject({
+ projects: {
+ [repository]: { trust_level: "trusted" },
+ },
+ });
+ expect(input).toContain(
+ `--config ${shellEnvironmentReference("CODEX_SECURITY_CONFIG_PATH")}`,
+ );
+ expect(input).toContain("--effective-config");
+ const shellEnvironment = options.env as Record<string, string>;
+ const helper = execFileSync(
+ interpreter!,
+ [
+ join(PLUGIN_ROOT, "scripts", "config_preflight.py"),
+ "--skill",
+ "security-scan",
+ "--config",
+ shellEnvironment["CODEX_SECURITY_CONFIG_PATH"]!,
+ "--cwd",
+ repository,
+ "--multi-agent-runtime-owner",
+ "native",
+ "--multi-agent-runtime-version",
+ "v2",
+ "--multi-agent-session-cap",
+ "12",
+ "--multi-agent-runtime-provenance",
+ "tool-surface",
+ "--runtime-check",
+ "delegation_available=true",
+ "--runtime-check",
+ "goal_tools_available=true",
+ "--effective-config",
+ "features.goals=true",
+ ],
+ {
+ env: {
+ PATH: process.env["PATH"],
+ CODEX_HOME: join(root, "denied"),
+ },
+ encoding: "utf8",
+ },
+ );
+ const preflight = JSON.parse(helper) as Record<string, unknown>;
+ expect(preflight["status"]).toBe("ready");
+ expect(preflight["config_resolution"]).toBe("manual-layers");
+ expect(preflight["config_paths"]).toEqual([configPath]);
+ await copyCompletedScan(root);
+ const manifestPath = join(scanDir, "scan-manifest.json");
+ const manifest = JSON.parse(
+ await readFile(manifestPath, "utf8"),
+ ) as { scan: { producer: { version: string } } };
+ const pluginManifest = JSON.parse(
+ await readFile(
+ join(PLUGIN_ROOT, ".codex-plugin", "plugin.json"),
+ "utf8",
+ ),
+ ) as { version: string };
+ manifest.scan.producer.version = pluginManifest.version;
+ await writeFile(manifestPath, JSON.stringify(manifest));
+ return { events: completedEvents() };
+ },
+ }),
+ }),
+ },
+ );
+
+ try {
+ await client.run(repository);
+ expect(capturedConfigPath).toBeDefined();
+ expect(capturedCodexHome).toBeDefined();
+ } finally {
+ await client.close();
+ }
+ expect(existsSync(capturedConfigPath!)).toBe(false);
+ expect(capturedCodexHome).toBe(
+ join(ambientHome, "state", "plugins", "codex-security", "codex-home"),
+ );
+ expect(existsSync(capturedCodexHome!)).toBe(true);
+ });
+
+ test("reuses keyring-compatible credentials across separate scan clients", async () => {
+ const root = await temporaryDirectory();
+ const repository = join(root, "repository");
+ const ambientHome = join(root, "ambient-codex-home");
+ const stateDirectory = join(root, "state");
+ const credentialHome = join(stateDirectory, "codex-home");
+ const runtimeHomes: string[] = [];
+ await mkdir(repository);
+ await mkdir(ambientHome);
+ await writeFile(join(ambientHome, "auth.json"), "{}\n");
+
+ for (const index of [0, 1]) {
+ const scanDir = join(root, `scan-${index}`);
+ await mkdir(scanDir, { mode: 0o700 });
+ const client = new TestClient(
+ { pluginPath: PLUGIN_ROOT },
+ {
+ environment: {
+ CODEX_HOME: ambientHome,
+ CODEX_SECURITY_STATE_DIR: stateDirectory,
+ },
+ resolvePluginPython: async () => "/managed/python",
+ prepareOutputDir: async () => scanDir,
+ repositoryRevision: async () => "deadbeef",
+ createCodex: (options: CodexOptions) => {
+ runtimeHomes.push(options.env?.["CODEX_HOME"] ?? "");
+ throw new Error("persistent credential scan reached");
+ },
+ },
+ );
+
+ try {
+ await expect(client.run(repository)).rejects.toThrow(
+ "persistent credential scan reached",
+ );
+ } finally {
+ await client.close();
+ }
+ expect(existsSync(credentialHome)).toBe(true);
+ }
+
+ expect(runtimeHomes).toEqual([credentialHome, credentialHome]);
+ });
+
+ test("runs parallel ChatGPT scans with isolated mutable configuration", async () => {
+ const root = await temporaryDirectory();
+ const repository = join(root, "repository");
+ const ambientHome = join(root, "ambient-codex-home");
+ const stateDirectory = join(root, "state");
+ const credentialHome = join(stateDirectory, "codex-home");
+ await mkdir(repository);
+ await mkdir(ambientHome);
+ await writeFile(join(ambientHome, "auth.json"), "{}\n");
+ let scansStarted = 0;
+ const deepScanConfigPaths = new Set<string>();
+ let releaseScans!: () => void;
+ const concurrentScans = new Promise<void>((resolve) => {
+ releaseScans = resolve;
+ });
+
+ const clients = await Promise.all(
+ [0, 1].map(async (index) => {
+ const scanDir = join(root, `parallel-scan-${index}`);
+ await mkdir(scanDir, { mode: 0o700 });
+ return new TestClient(
+ {
+ pluginPath: PLUGIN_ROOT,
+ codexOverrides: {
+ model: index === 0 ? "gpt-5.6-sol" : "gpt-5.6-terra",
+ },
+ },
+ {
+ environment: {
+ CODEX_HOME: ambientHome,
+ CODEX_SECURITY_STATE_DIR: stateDirectory,
+ },
+ resolvePluginPython: async () => "/managed/python",
+ prepareOutputDir: async () => scanDir,
+ repositoryRevision: async () => "deadbeef",
+ createCodex: (options: CodexOptions) => {
+ expect(options.env?.["CODEX_HOME"]).toBe(credentialHome);
+ const expectedModel =
+ index === 0 ? "gpt-5.6-sol" : "gpt-5.6-terra";
+ expect(options.config?.["model"]).toBe(expectedModel);
+ const deepScanConfigPath =
+ options.env?.["CODEX_SECURITY_DEEP_SCAN_CONFIG_PATH"];
+ expect(typeof deepScanConfigPath).toBe("string");
+ deepScanConfigPaths.add(deepScanConfigPath!);
+ return {
+ startThread: () => ({
+ id: null,
+ async runStreamed() {
+ expect(
+ existsSync(
+ join(credentialHome, ".codex-security-scan.lock"),
+ ),
+ ).toBe(false);
+ if (++scansStarted === 2) releaseScans();
+ const credentialConfig = parseToml(
+ await readFile(
+ join(credentialHome, "config.toml"),
+ "utf8",
+ ),
+ );
+ expect(credentialConfig["model"]).toBeUndefined();
+ const before = parseToml(
+ await readFile(deepScanConfigPath!, "utf8"),
+ );
+ expect(before["deep_scan"]).toMatchObject({
+ workers: index + 2,
+ });
+ await concurrentScans;
+ const after = parseToml(
+ await readFile(deepScanConfigPath!, "utf8"),
+ );
+ expect(after["deep_scan"]).toMatchObject({
+ workers: index + 2,
+ });
+ throw new Error("parallel managed scan reached");
+ },
+ }),
+ };
+ },
+ },
+ );
+ }),
+ );
+
+ try {
+ const results = await Promise.allSettled(
+ clients.map((client, index) =>
+ client
+ .run(repository, { mode: "deep", workers: index + 2 })
+ .finally(releaseScans),
+ ),
+ );
+ for (const result of results) {
+ expect(result).toMatchObject({
+ status: "rejected",
+ reason: expect.objectContaining({
+ message: "parallel managed scan reached",
+ }),
+ });
+ }
+ expect(existsSync(credentialHome)).toBe(true);
+ expect(scansStarted).toBe(2);
+ expect(deepScanConfigPaths.size).toBe(2);
+ const pluginConfiguration = JSON.parse(
+ await readFile(join(PLUGIN_ROOT, ".mcp.json"), "utf8"),
+ ) as { mcpServers: Record<string, { env_vars: string[] }> };
+ expect(
+ pluginConfiguration.mcpServers["codex-security"]?.env_vars.includes(
+ "CODEX_SECURITY_DEEP_SCAN_CONFIG_PATH",
+ ),
+ ).toBe(true);
+ } finally {
+ releaseScans();
+ await Promise.all(clients.map(async (client) => await client.close()));
+ }
+ });
+
+ test("reuses the managed runtime when scan authentication changes", async () => {
+ const root = await temporaryDirectory();
+ const repository = join(root, "repository");
+ const ambientHome = join(root, "ambient-codex-home");
+ const stateDirectory = join(root, "state");
+ const dedicatedHome = join(stateDirectory, "codex-home");
+ const scanDir = join(root, "scan");
+ const ambientAuthentication = '{"auth_mode":"chatgpt"}\n';
+ await mkdir(repository);
+ await mkdir(ambientHome);
+ await mkdir(scanDir, { mode: 0o700 });
+ await writeFile(join(ambientHome, "auth.json"), ambientAuthentication);
+ const runs: Array<{ home: string; apiKey?: string }> = [];
+ const client = new TestClient(
+ { pluginPath: PLUGIN_ROOT },
+ {
+ environment: {
+ CODEX_HOME: ambientHome,
+ CODEX_SECURITY_STATE_DIR: stateDirectory,
+ OPENAI_API_KEY: "synthetic-transient-key",
+ },
+ resolvePluginPython: async () => "/managed/python",
+ prepareOutputDir: async () => scanDir,
+ repositoryRevision: async () => "deadbeef",
+ createCodex: (options: CodexOptions) => {
+ runs.push({
+ home: options.env?.["CODEX_HOME"] ?? "",
+ ...(options.apiKey === undefined ? {} : { apiKey: options.apiKey }),
+ });
+ throw new Error("authentication-selected scan reached");
+ },
+ },
+ );
+
+ try {
+ await expect(client.run(repository, { auth: "api-key" })).rejects.toThrow(
+ "authentication-selected scan reached",
+ );
+ expect(runs[0]?.home).toBe(dedicatedHome);
+ expect(runs[0]?.apiKey).toBe("synthetic-transient-key");
+ expect(existsSync(join(dedicatedHome, "auth.json"))).toBe(false);
+
+ await expect(client.run(repository, { auth: "chatgpt" })).rejects.toThrow(
+ "authentication-selected scan reached",
+ );
+ expect(runs[1]).toEqual({ home: dedicatedHome });
+ expect(await readFile(join(dedicatedHome, "auth.json"), "utf8")).toBe(
+ ambientAuthentication,
+ );
+ await expect(client.run(repository, { auth: "api-key" })).rejects.toThrow(
+ "authentication-selected scan reached",
+ );
+ expect(runs[2]?.home).toBe(dedicatedHome);
+ expect(runs[2]?.apiKey).toBe("synthetic-transient-key");
+ expect(await readFile(join(dedicatedHome, "auth.json"), "utf8")).toBe(
+ ambientAuthentication,
+ );
+ } finally {
+ await client.close();
+ }
+ expect(existsSync(dedicatedHome)).toBe(true);
+ });
+
+ test("does not reimport ambient credentials after an explicit logout", async () => {
+ const root = await temporaryDirectory();
+ const ambientHome = join(root, "ambient-home");
+ const credentialHome = join(root, "credential-home");
+ await mkdir(ambientHome);
+ await mkdir(credentialHome, { mode: 0o700 });
+ await writeFile(join(ambientHome, "auth.json"), '{"token":"ambient"}\n');
+ await setCodexSecurityCredentialLogout(credentialHome, true);
+ let imported = false;
+
+ await expect(
+ initialCredentialsAvailable({}, ambientHome, credentialHome, async () => {
+ imported = true;
+ return true;
+ }),
+ ).resolves.toBe(false);
+ expect(imported).toBe(false);
+
+ await setCodexSecurityCredentialLogout(credentialHome, false);
+ await expect(
+ initialCredentialsAvailable(
+ {},
+ ambientHome,
+ credentialHome,
+ async () => true,
+ ),
+ ).resolves.toBe(true);
+ });
+});
diff --git a/sdk/typescript/tests-ts/api-post-scan.test.ts b/sdk/typescript/tests-ts/api-post-scan.test.ts
index c60563558..1c17fe13b 100644
--- a/sdk/typescript/tests-ts/api-post-scan.test.ts
+++ b/sdk/typescript/tests-ts/api-post-scan.test.ts
@@ -3,7 +3,7 @@ import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
import { dirname, join } from "node:path";
import type { ThreadEvent } from "@openai/codex-sdk";
import { afterEach, describe, expect, test } from "bun:test";
-import { CodexSecurity } from "../src/index.js";
+import { TestClient } from "./support/api-client.js";
import {
completedEvents,
createApiTestFixtures,
@@ -15,31 +15,6 @@ const { cleanup, copyCompletedScan, temporaryDirectory } =
afterEach(cleanup);
-const TestClient = CodexSecurity as unknown as new (
- config: Record<string, unknown>,
- dependencies: Record<string, unknown>,
-) => CodexSecurity;
-
-function runWorkbench(_options: unknown, args: readonly string[]) {
- if (args[0] === "register-cli-scan") {
- return Promise.resolve({
- scanId: "scan_example_001",
- targetId: "target_sha256_example",
- targetRevision: "deadbeef",
- scanDir: args[args.indexOf("--scan-dir") + 1],
- contract: { target: { allowedKinds: ["git_revision"] } },
- });
- }
- if (args[0] === "get-scan-feedback") {
- return Promise.resolve({
- scanId: "scan_example_001",
- targetId: "target_sha256_example",
- falsePositives: [],
- });
- }
- return Promise.resolve({});
-}
-
describe("completed scan follow-up instructions", () => {
test.each([
["missing report", "report.md", undefined],
@@ -66,7 +41,6 @@ describe("completed scan follow-up instructions", () => {
resolvePluginPython: async () => "/managed/python",
prepareOutputDir: async () => scanDir,
repositoryRevision: async () => "deadbeef",
- runWorkbench,
createCodex: () => ({
startThread: () => ({
id: "thread-1",
diff --git a/sdk/typescript/tests-ts/api-surface.test.ts b/sdk/typescript/tests-ts/api-surface.test.ts
index 96face64c..86d11320e 100644
--- a/sdk/typescript/tests-ts/api-surface.test.ts
+++ b/sdk/typescript/tests-ts/api-surface.test.ts
@@ -3,6 +3,7 @@ import { join } from "node:path";
import type { CodexOptions } from "@openai/codex-sdk";
import { afterEach, describe, expect, test } from "bun:test";
import { CodexSecurity } from "../src/index.js";
+import { mockWorkbench } from "./support/api-client.js";
import {
completedEvents,
createApiTestFixtures,
@@ -16,29 +17,7 @@ const InternalCodexSecurity = CodexSecurity as unknown as new (
runtimeOptions?: { surface: "cli" | "sdk" },
) => CodexSecurity;
-afterEach(async () => {
- await fixtures.cleanup();
-});
-
-function mockWorkbench(args: readonly string[]) {
- if (args[0] === "register-cli-scan") {
- return {
- scanId: "scan_example_001",
- targetId: "target_sha256_example",
- targetRevision: "deadbeef",
- scanDir: args[args.indexOf("--scan-dir") + 1],
- contract: { target: { allowedKinds: ["git_revision"] } },
- };
- }
- if (args[0] === "get-scan-feedback") {
- return {
- scanId: "scan_example_001",
- targetId: "target_sha256_example",
- falsePositives: [],
- };
- }
- return {};
-}
+afterEach(fixtures.cleanup);
async function scanResponseSurface(runtimeOptions?: {
surface: "cli" | "sdk";
diff --git a/sdk/typescript/tests-ts/api.test.ts b/sdk/typescript/tests-ts/api.test.ts
index d198909fa..e1db152a6 100644
--- a/sdk/typescript/tests-ts/api.test.ts
+++ b/sdk/typescript/tests-ts/api.test.ts
@@ -3,7 +3,6 @@ import {
copyFile,
cp,
mkdir,
- mkdtemp,
readFile,
readdir,
realpath,
@@ -16,7 +15,6 @@ import * as fsPromises from "node:fs/promises";
import { execFileSync } from "node:child_process";
import { createHash } from "node:crypto";
import { existsSync } from "node:fs";
-import { tmpdir } from "node:os";
import { basename, join } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import { Codex, type CodexOptions, type ThreadEvent } from "@openai/codex-sdk";
@@ -46,16 +44,24 @@ import {
type JsonObject,
} from "../src/config.js";
import { estimateScanCost, type ScanCost } from "../src/cost.js";
-import {
- resolveCodexCommand,
- runWorkbench,
- setCodexSecurityCredentialLogout,
-} from "../src/runtime.js";
+import { resolveCodexCommand, runWorkbench } from "../src/runtime.js";
import { normalizeTarget } from "../src/targets.js";
import { SYNTHETIC_CREDENTIALS } from "./cli-fixtures.js";
import { INTEGRATION_TARGET, PLUGIN_ROOT } from "./plugin-root.js";
-import { preparedRuntime } from "./support/api-events.js";
-import { runMockInSubprocess } from "./support/isolated-mock.js";
+import {
+ mockScanRegistration,
+ mockWorkbench,
+ shellEnvironmentReference,
+ SHELL_ENVIRONMENT_PREFIX,
+ TestClient,
+ TEST_SNAPSHOT_DIGEST,
+} from "./support/api-client.js";
+import {
+ completedEvents,
+ createApiTestFixtures,
+ preparedRuntime,
+} from "./support/api-events.js";
+import { runTestInSubprocess } from "./support/test-subprocess.js";
type ScanObserverName = Parameters<
NonNullable<ScanOptions["onObserverError"]>
@@ -63,13 +69,9 @@ type ScanObserverName = Parameters<
const REPOSITORY_ROOT = fileURLToPath(new URL("../../..", import.meta.url));
const EXAMPLE = join(PLUGIN_ROOT, "examples", "completed-scan");
-const temporaryDirectories: string[] = [];
-const TEST_SNAPSHOT_DIGEST = `codex-security-snapshot/v1:sha256:${"a".repeat(64)}`;
-const SHELL_ENVIRONMENT_PREFIX = process.platform === "win32" ? "$env:" : "$";
-
-function shellEnvironmentReference(name: string, suffix = ""): string {
- return `"${SHELL_ENVIRONMENT_PREFIX}${name}${suffix}"`;
-}
+const { cleanup, copyCompletedScan, temporaryDirectory } =
+ createApiTestFixtures();
+afterEach(cleanup);
const EXTERNAL_PROVIDER_CASES = [
[
@@ -131,79 +133,6 @@ const BEDROCK_AUTHENTICATION_CASES = [
],
["default AWS credential chain", {}, "default_credential_chain"],
] as const;
-const TestClientBase = CodexSecurity as unknown as new (
- config: Record<string, unknown>,
- dependencies: Record<string, unknown>,
-) => CodexSecurity;
-
-function mockScanRegistration(args: readonly string[]) {
- const recipe = JSON.parse(args[args.indexOf("--recipe-json") + 1]!) as {
- repositoryRevision?: string;
- target: { kind: string };
- };
- const kind =
- recipe.target.kind === "refs" || recipe.target.kind === "working_tree"
- ? "git_diff"
- : recipe.repositoryRevision === undefined
- ? "directory_snapshot"
- : "git_revision";
-
- return {
- scanId: "scan_example_001",
- targetId: "target_sha256_example",
- targetRevision: recipe.repositoryRevision ?? "unversioned",
- scanDir: args[args.indexOf("--scan-dir") + 1],
- contract: {
- target: {
- allowedKinds: [kind],
- ...(kind === "directory_snapshot"
- ? { requiredSnapshotDigest: TEST_SNAPSHOT_DIGEST }
- : {}),
- },
- },
- };
-}
-
-class TestClient extends TestClientBase {
- public constructor(
- config: Record<string, unknown>,
- dependencies: Record<string, unknown>,
- ) {
- super(config, {
- runWorkbench: async (_options: unknown, args: readonly string[]) =>
- mockWorkbench(args),
- ...dependencies,
- });
- }
-}
-
-function mockWorkbench(args: readonly string[]) {
- if (args[0] === "register-cli-scan") return mockScanRegistration(args);
- if (args[0] === "get-scan-feedback") {
- return {
- scanId: "scan_example_001",
- targetId: "target_sha256_example",
- falsePositives: [],
- };
- }
- return {};
-}
-
-afterEach(async () => {
- await Promise.all(
- temporaryDirectories
- .splice(0)
- .map((path) => rm(path, { recursive: true, force: true })),
- );
-});
-
-async function temporaryDirectory(): Promise<string> {
- const path = await realpath(
- await mkdtemp(join(tmpdir(), "codex-security-api-")),
- );
- temporaryDirectories.push(path);
- return path;
-}
function nodeCodex(script: string): {
command: { command: string };
@@ -219,13 +148,6 @@ function nodeCodex(script: string): {
};
}
-async function copyCompletedScan(root: string): Promise<string> {
- const scanDir = join(root, "scan");
- await cp(EXAMPLE, scanDir, { recursive: true });
- await writeFile(join(scanDir, "report.md"), "# Scan report\n");
- return scanDir;
-}
-
async function writeUsageSession(
codexHome: string,
threadId: string,
@@ -258,25 +180,6 @@ async function writeUsageSession(
);
}
-async function* completedEvents(): AsyncGenerator<ThreadEvent> {
- yield { type: "thread.started", thread_id: "thread-1" };
- yield { type: "turn.started" };
- yield {
- type: "item.completed",
- item: { id: "message-1", type: "agent_message", text: "scan complete" },
- };
- yield {
- type: "turn.completed",
- usage: {
- input_tokens: 10,
- cached_input_tokens: 2,
- cache_write_input_tokens: 0,
- output_tokens: 3,
- reasoning_output_tokens: 1,
- },
- };
-}
-
describe("CodexSecurity orchestration", () => {
test("distinguishes local workbench and database errors from model transport failures", () => {
for (const message of [
@@ -323,7 +226,7 @@ describe("CodexSecurity orchestration", () => {
prepareRuntime: async () => preparedRuntime(codexHome),
resolvePluginPython: async () => "/managed/python",
repositoryRevision: async () => null,
- runWorkbench: async (_options: unknown, args: readonly string[]) => {
+ runWorkbench: async (_options, args): Promise<JsonObject> => {
if (args[0] === "register-cli-scan") {
recipe = JSON.parse(args[args.indexOf("--recipe-json") + 1]!);
}
@@ -673,12 +576,13 @@ describe("CodexSecurity orchestration", () => {
const repository = join(root, "repository");
await mkdir(repository);
- for (const codexOverrides of [
+ const invalidSettings: JsonObject[] = [
{ model: "" },
{ model: 42 },
{ model_reasoning_effort: "" },
{ model_reasoning_effort: false },
- ]) {
+ ];
+ for (const codexOverrides of invalidSettings) {
const client = new TestClient({ codexOverrides }, { environment: {} });
await expect(client.preflight(repository)).rejects.toThrow(
/model|reasoning effort/u,
@@ -823,10 +727,8 @@ describe("CodexSecurity orchestration", () => {
codex_api_key: "synthetic-forwarded-codex-key",
},
}),
- resolvePluginPython: async (options: {
- environment?: Record<string, string | undefined>;
- }) => {
- pythonEnvironment = options.environment;
+ resolvePluginPython: async (options) => {
+ pythonEnvironment = options?.environment;
return "/managed/python";
},
prepareOutputDir: async () => scanDir,
@@ -1140,7 +1042,7 @@ describe("CodexSecurity orchestration", () => {
resolvePluginPython: async () => "/managed/python",
prepareOutputDir: async () => scanDir,
repositoryRevision: async () => "deadbeef",
- runWorkbench: async (_options: unknown, args: readonly string[]) => {
+ runWorkbench: async (_options, args): Promise<JsonObject> => {
if (args[0] === "register-cli-scan") {
savedRecipe = JSON.parse(args[args.indexOf("--recipe-json") + 1]!);
}
@@ -1427,7 +1329,7 @@ describe("CodexSecurity orchestration", () => {
prepareRuntime: async () => preparedRuntime(codexHome),
resolvePluginPython: async () => "/managed/python",
repositoryRevision: async () => null,
- runWorkbench: async (_options: unknown, args: readonly string[]) => {
+ runWorkbench: async (_options, args): Promise<JsonObject> => {
if (args[0] === "get-scan-feedback") {
return {
scanId: "scan_example_001",
@@ -1785,6 +1687,13 @@ describe("CodexSecurity orchestration", () => {
});
test("keeps a relative repository stable if runtime initialization changes cwd", async () => {
+ if (
+ runTestInSubprocess(
+ fileURLToPath(import.meta.url),
+ "keeps a relative repository stable if runtime initialization changes cwd",
+ )
+ )
+ return;
const root = await temporaryDirectory();
const initial = join(root, "initial");
const elsewhere = join(root, "elsewhere");
@@ -1870,7 +1779,7 @@ describe("CodexSecurity orchestration", () => {
resolvePluginPython: async () => "/managed/python",
prepareOutputDir: async () => scanDir,
repositoryRevision: async () => "deadbeef",
- runWorkbench: async (_options: unknown, args: readonly string[]) => {
+ runWorkbench: async (_options, args): Promise<JsonObject> => {
commands.push(args);
if (args[0] === "register-cli-scan") {
return mockScanRegistration(args);
@@ -2078,7 +1987,7 @@ describe("CodexSecurity orchestration", () => {
resolvePluginPython: async () => "/managed/python",
prepareOutputDir: async () => scanDir,
repositoryRevision: async () => "deadbeef",
- runWorkbench: async (_options: unknown, args: readonly string[]) => {
+ runWorkbench: async (_options, args): Promise<JsonObject> => {
if (args[0] === "register-cli-scan") {
return {
...mockScanRegistration(args),
@@ -2158,7 +2067,7 @@ describe("CodexSecurity orchestration", () => {
resolvePluginPython: async () => "/managed/python",
prepareOutputDir: async () => scanDir,
repositoryRevision: async () => "deadbeef",
- runWorkbench: async (_options: unknown, args: readonly string[]) => {
+ runWorkbench: async (_options, args): Promise<JsonObject> => {
if (args[0] !== "register-cli-scan") {
return {
scanId: "scan_example_001",
@@ -2354,7 +2263,7 @@ describe("CodexSecurity orchestration", () => {
resolvePluginPython: async () => "/managed/python",
prepareOutputDir: async () => scanDir,
repositoryRevision: async () => "deadbeef",
- runWorkbench: async (_options: unknown, args: readonly string[]) =>
+ runWorkbench: async (_options, args): Promise<JsonObject> =>
args[0] === "register-cli-scan"
? {
...mockScanRegistration(args),
@@ -2391,7 +2300,7 @@ describe("CodexSecurity orchestration", () => {
resolvePluginPython: async () => "/managed/python",
prepareOutputDir: async () => scanDir,
repositoryRevision: async () => "deadbeef",
- runWorkbench: async (_options: unknown, args: readonly string[]) => {
+ runWorkbench: async (_options, args): Promise<JsonObject> => {
commands.push(args[0]!);
if (args[0] === "register-cli-scan") {
return mockScanRegistration(args);
@@ -2455,7 +2364,7 @@ describe("CodexSecurity orchestration", () => {
resolvePluginPython: async () => "/managed/python",
prepareOutputDir: async () => scanDir,
repositoryRevision: async () => "deadbeef",
- runWorkbench: async (_options: unknown, args: readonly string[]) => {
+ runWorkbench: async (_options, args): Promise<JsonObject> => {
commands.push(args[0]!);
return mockWorkbench(args);
},
@@ -2507,7 +2416,7 @@ describe("CodexSecurity orchestration", () => {
resolvePluginPython: async () => "/managed/python",
prepareOutputDir: async () => scanDir,
repositoryRevision: async () => "deadbeef",
- runWorkbench: async (_options: unknown, args: readonly string[]) =>
+ runWorkbench: async (_options, args): Promise<JsonObject> =>
args[0] === "register-cli-scan"
? { ...mockScanRegistration(args), scopeFileCount: 4_207 }
: mockWorkbench(args),
@@ -2585,7 +2494,7 @@ describe("CodexSecurity orchestration", () => {
resolvePluginPython: async () => "/managed/python",
prepareOutputDir: async () => scanDir,
repositoryRevision: async () => "deadbeef",
- runWorkbench: async (_options: unknown, args: readonly string[]) =>
+ runWorkbench: async (_options, args): Promise<JsonObject> =>
args[0] === "register-cli-scan"
? { ...mockScanRegistration(args), scopeFileCount: 1_258 }
: mockWorkbench(args),
@@ -2723,7 +2632,7 @@ describe("CodexSecurity orchestration", () => {
resolvePluginPython: async () => "/managed/python",
prepareOutputDir: async () => scanDir,
repositoryRevision: async () => "deadbeef",
- runWorkbench: async (_options: unknown, args: readonly string[]) => {
+ runWorkbench: async (_options, args): Promise<JsonObject> => {
commands.push(args);
if (args[0] === "register-cli-scan") {
return mockScanRegistration(args);
@@ -2822,7 +2731,7 @@ describe("CodexSecurity orchestration", () => {
prepareRuntime: async () => ({
...runtime,
plugin: {
- ...(runtime["plugin"] as Record<string, unknown>),
+ ...runtime.plugin,
pluginRoot,
marketplaceRoot: pluginRoot,
installedRoot: pluginRoot,
@@ -2831,7 +2740,7 @@ describe("CodexSecurity orchestration", () => {
resolvePluginPython: async () => "/managed/python",
prepareOutputDir: async () => scanDir,
repositoryRevision: async () => "deadbeef",
- runWorkbench: async (_options: unknown, args: readonly string[]) => {
+ runWorkbench: async (_options, args): Promise<JsonObject> => {
commands.push(args[0]!);
return args[0] === "register-cli-scan"
? mockScanRegistration(args)
@@ -2872,7 +2781,7 @@ describe("CodexSecurity orchestration", () => {
resolvePluginPython: async () => "/managed/python",
prepareOutputDir: async () => scanDir,
repositoryRevision: async () => "deadbeef",
- runWorkbench: async (_options: unknown, args: readonly string[]) => {
+ runWorkbench: async (_options, args): Promise<JsonObject> => {
if (args[0] === "register-cli-scan") {
return { ...mockScanRegistration(args), scanId };
}
@@ -2968,7 +2877,7 @@ describe("CodexSecurity orchestration", () => {
resolvePluginPython: async () => "/managed/python",
prepareOutputDir: async () => scanDir,
repositoryRevision: async () => "deadbeef",
- runWorkbench: async (_options: unknown, args: readonly string[]) => {
+ runWorkbench: async (_options, args): Promise<JsonObject> => {
commands.push(args);
if (args[0] === "get-scan-feedback") {
return {
@@ -3097,7 +3006,7 @@ describe("CodexSecurity orchestration", () => {
resolvePluginPython: async () => "/managed/python",
prepareOutputDir: async () => scanDir,
repositoryRevision: async () => "deadbeef",
- runWorkbench: async (_options: unknown, args: readonly string[]) => {
+ runWorkbench: async (_options, args): Promise<JsonObject> => {
if (args[0] === "register-cli-scan") {
return mockScanRegistration(args);
}
@@ -3158,7 +3067,7 @@ describe("CodexSecurity orchestration", () => {
resolvePluginPython: async () => "/managed/python",
prepareOutputDir: async () => scanDir,
repositoryRevision: async () => "deadbeef",
- runWorkbench: async (_options: unknown, args: readonly string[]) => {
+ runWorkbench: async (_options, args): Promise<JsonObject> => {
commands.push(args);
if (args[0] === "register-cli-scan") {
return mockScanRegistration(args);
@@ -3224,7 +3133,7 @@ describe("CodexSecurity orchestration", () => {
resolvePluginPython: async () => "/managed/python",
prepareOutputDir: async () => scanDir,
repositoryRevision: async () => "deadbeef",
- runWorkbench: async (_options: unknown, args: readonly string[]) => {
+ runWorkbench: async (_options, args): Promise<JsonObject> => {
commands.push(args);
return mockWorkbench(args);
},
@@ -3391,7 +3300,7 @@ describe("CodexSecurity orchestration", () => {
resolvePluginPython: async () => "/managed/python",
prepareOutputDir: async () => scanDir,
repositoryRevision: async () => "deadbeef",
- runWorkbench: async (_options: unknown, args: readonly string[]) => {
+ runWorkbench: async (_options, args): Promise<JsonObject> => {
commands.push(args);
if (args[0] === "register-cli-scan") {
return mockScanRegistration(args);
@@ -3520,7 +3429,7 @@ describe("CodexSecurity orchestration", () => {
resolvePluginPython: async () => "/managed/python",
prepareOutputDir: async () => scanDir,
repositoryRevision: async () => "deadbeef",
- runWorkbench: async (_options: unknown, args: readonly string[]) => {
+ runWorkbench: async (_options, args): Promise<JsonObject> => {
commands.push(args);
if (args[0] !== "complete-budget-exhausted-scan") {
return mockWorkbench(args);
@@ -3552,7 +3461,7 @@ describe("CodexSecurity orchestration", () => {
.digest("hex");
await writeFile(manifestPath, `${JSON.stringify(manifest)}\n`);
return {
- scan: { warnings: [args[args.indexOf("--message") + 1]] },
+ scan: { warnings: [args[args.indexOf("--message") + 1]!] },
};
},
createCodex: () => ({
@@ -3652,7 +3561,7 @@ describe("CodexSecurity orchestration", () => {
resolvePluginPython: async () => "/managed/python",
prepareOutputDir: async () => scanDir,
repositoryRevision: async () => "deadbeef",
- runWorkbench: async (_options: unknown, args: readonly string[]) => {
+ runWorkbench: async (_options, args): Promise<JsonObject> => {
commands.push(args);
if (args[0] === "register-cli-scan") {
return mockScanRegistration(args);
@@ -3736,7 +3645,7 @@ describe("CodexSecurity orchestration", () => {
resolvePluginPython: async () => "/managed/python",
prepareOutputDir: async () => scanDir,
repositoryRevision: async () => "deadbeef",
- runWorkbench: async (_options: unknown, args: readonly string[]) => {
+ runWorkbench: async (_options, args): Promise<JsonObject> => {
if (args[0] === "get-scan-feedback") {
return {
scanId: "scan_example_001",
@@ -3841,7 +3750,7 @@ describe("CodexSecurity orchestration", () => {
const python = Bun.which("python3") ?? Bun.which("python");
expect(python).not.toBeNull();
const environment = {
- PATH: process.env["PATH"],
+ PATH: process.env["PATH"] ?? "",
CODEX_SECURITY_STATE_DIR: stateDirectory,
};
const commands: Array<readonly string[]> = [];
@@ -3859,7 +3768,7 @@ describe("CodexSecurity orchestration", () => {
runWorkbench: async (
options: Parameters<typeof runWorkbench>[0],
args: readonly string[],
- ) => {
+ ): Promise<JsonObject> => {
commands.push(args);
const result = await runWorkbench(options, args);
if (args[0] === "fail-scan") {
@@ -3914,7 +3823,7 @@ describe("CodexSecurity orchestration", () => {
const python = Bun.which("python3") ?? Bun.which("python");
expect(python).not.toBeNull();
const environment = {
- PATH: process.env["PATH"],
+ PATH: process.env["PATH"] ?? "",
CODEX_SECURITY_STATE_DIR: stateDirectory,
};
const commands: Array<readonly string[]> = [];
@@ -3937,7 +3846,7 @@ describe("CodexSecurity orchestration", () => {
runWorkbench: async (
options: Parameters<typeof runWorkbench>[0],
args: readonly string[],
- ) => {
+ ): Promise<JsonObject> => {
commands.push(args);
return await runWorkbench(options, args);
},
@@ -4087,252 +3996,6 @@ describe("CodexSecurity orchestration", () => {
await client.close();
});
- test("keeps a private preflight snapshot isolated from persistent credentials", async () => {
- const root = await temporaryDirectory();
- const repository = join(root, "repository");
- const ambientHome = join(root, "ambient-codex-home");
- const scanDir = join(root, "scan");
- await mkdir(repository);
- await mkdir(ambientHome);
- await mkdir(scanDir, { mode: 0o700 });
- await writeFile(join(ambientHome, "auth.json"), "{}\n");
- const interpreter =
- Bun.which("python3") ?? Bun.which("python") ?? Bun.which("py");
- expect(interpreter).not.toBeNull();
- let capturedConfigPath: string | undefined;
- let capturedCodexHome: string | undefined;
- const unrelatedProjects = Object.fromEntries(
- Array.from({ length: 256 }, (_, index) => [
- join(root, `unrelated-project-${index}`),
- { trust_level: "untrusted" },
- ]),
- );
- const client = new TestClient(
- {
- pluginPath: PLUGIN_ROOT,
- codexOverrides: {
- approval_policy: "never",
- features: { goals: true },
- projects: {
- ...unrelatedProjects,
- [repository]: { trust_level: "trusted" },
- },
- mcp_servers: {
- private: {
- command: "echo",
- env: { PRIVATE_TOKEN: "RUNTIME_MCP_SECRET" },
- },
- },
- shell_environment_policy: {
- set: { PRIVATE_TOKEN: "RUNTIME_SHELL_SECRET" },
- },
- responses_api_metadata: {
- request_trace: "preserve-configured-metadata",
- },
- },
- },
- {
- environment: { CODEX_HOME: ambientHome },
- resolvePluginPython: async () => interpreter!,
- prepareOutputDir: async () => scanDir,
- repositoryRevision: async () => "deadbeef",
- createCodex: (options: CodexOptions) => ({
- startThread: () => ({
- id: null,
- async runStreamed(input: string) {
- const configPath = options.env?.["CODEX_SECURITY_CONFIG_PATH"];
- const codexHome = options.env?.["CODEX_HOME"];
- expect(typeof configPath).toBe("string");
- expect(typeof codexHome).toBe("string");
- capturedConfigPath = configPath;
- capturedCodexHome = codexHome;
- expect(configPath!.startsWith(`${codexHome!}/`)).toBe(false);
- expect(
- parseToml(
- await readFile(join(codexHome!, "config.toml"), "utf8"),
- ),
- ).toMatchObject({
- approval_policy: "never",
- permissions: {
- codex_security_scan: {
- filesystem: {
- ":root": "read",
- ":workspace_roots": "write",
- [join(ambientHome, "state", "plugins", "codex-security")]:
- "write",
- [join(
- ambientHome,
- "state",
- "plugins",
- "codex-security",
- "codex-home",
- )]: "read",
- },
- },
- },
- });
- const codexConfig = await readFile(
- join(codexHome!, "config.toml"),
- "utf8",
- );
- expect(options.env?.["CODEX_SECURITY_SURFACE"]).toBe("sdk");
- expect(codexConfig).not.toContain("model_reasoning_summary");
- expect(codexConfig).not.toContain("show_raw_agent_reasoning");
- expect(options.config).not.toHaveProperty("projects");
- expect(options.config).not.toHaveProperty("permissions");
- expect(options.config).toMatchObject({
- default_permissions: "codex_security_scan",
- allow_login_shell: false,
- model_reasoning_summary: "detailed",
- show_raw_agent_reasoning: true,
- windows: { sandbox: "unelevated" },
- mcp_servers: {
- private: {
- command: "echo",
- env: { PRIVATE_TOKEN: "RUNTIME_MCP_SECRET" },
- },
- },
- shell_environment_policy: {
- set: { PRIVATE_TOKEN: "RUNTIME_SHELL_SECRET" },
- },
- responses_api_metadata: {
- request_trace: "preserve-configured-metadata",
- codex_security_surface: "sdk",
- },
- });
- if (process.platform !== "win32") {
- expect((await stat(configPath!)).mode & 0o777).toBe(0o600);
- }
- const serialized = await readFile(configPath!, "utf8");
- expect(serialized).not.toContain("RUNTIME_MCP_SECRET");
- expect(serialized).not.toContain("RUNTIME_SHELL_SECRET");
- expect(serialized).not.toContain("mcp_servers");
- expect(serialized).not.toContain("shell_environment_policy");
- expect(parseToml(serialized)).toMatchObject({
- projects: {
- [repository]: { trust_level: "trusted" },
- },
- });
- expect(input).toContain(
- `--config ${shellEnvironmentReference("CODEX_SECURITY_CONFIG_PATH")}`,
- );
- expect(input).toContain("--effective-config");
- const shellEnvironment = options.env as Record<string, string>;
- const helper = execFileSync(
- interpreter!,
- [
- join(PLUGIN_ROOT, "scripts", "config_preflight.py"),
- "--skill",
- "security-scan",
- "--config",
- shellEnvironment["CODEX_SECURITY_CONFIG_PATH"]!,
- "--cwd",
- repository,
- "--multi-agent-runtime-owner",
- "native",
- "--multi-agent-runtime-version",
- "v2",
- "--multi-agent-session-cap",
- "12",
- "--multi-agent-runtime-provenance",
- "tool-surface",
- "--runtime-check",
- "delegation_available=true",
- "--runtime-check",
- "goal_tools_available=true",
- "--effective-config",
- "features.goals=true",
- ],
- {
- env: {
- PATH: process.env["PATH"],
- CODEX_HOME: join(root, "denied"),
- },
- encoding: "utf8",
- },
- );
- const preflight = JSON.parse(helper) as Record<string, unknown>;
- expect(preflight["status"]).toBe("ready");
- expect(preflight["config_resolution"]).toBe("manual-layers");
- expect(preflight["config_paths"]).toEqual([configPath]);
- await copyCompletedScan(root);
- const manifestPath = join(scanDir, "scan-manifest.json");
- const manifest = JSON.parse(
- await readFile(manifestPath, "utf8"),
- ) as { scan: { producer: { version: string } } };
- const pluginManifest = JSON.parse(
- await readFile(
- join(PLUGIN_ROOT, ".codex-plugin", "plugin.json"),
- "utf8",
- ),
- ) as { version: string };
- manifest.scan.producer.version = pluginManifest.version;
- await writeFile(manifestPath, JSON.stringify(manifest));
- return { events: completedEvents() };
- },
- }),
- }),
- },
- );
-
- try {
- await client.run(repository);
- expect(capturedConfigPath).toBeDefined();
- expect(capturedCodexHome).toBeDefined();
- } finally {
- await client.close();
- }
- expect(existsSync(capturedConfigPath!)).toBe(false);
- expect(capturedCodexHome).toBe(
- join(ambientHome, "state", "plugins", "codex-security", "codex-home"),
- );
- expect(existsSync(capturedCodexHome!)).toBe(true);
- });
-
- test("reuses keyring-compatible credentials across separate scan clients", async () => {
- const root = await temporaryDirectory();
- const repository = join(root, "repository");
- const ambientHome = join(root, "ambient-codex-home");
- const stateDirectory = join(root, "state");
- const credentialHome = join(stateDirectory, "codex-home");
- const runtimeHomes: string[] = [];
- await mkdir(repository);
- await mkdir(ambientHome);
- await writeFile(join(ambientHome, "auth.json"), "{}\n");
-
- for (const index of [0, 1]) {
- const scanDir = join(root, `scan-${index}`);
- await mkdir(scanDir, { mode: 0o700 });
- const client = new TestClient(
- { pluginPath: PLUGIN_ROOT },
- {
- environment: {
- CODEX_HOME: ambientHome,
- CODEX_SECURITY_STATE_DIR: stateDirectory,
- },
- resolvePluginPython: async () => "/managed/python",
- prepareOutputDir: async () => scanDir,
- repositoryRevision: async () => "deadbeef",
- createCodex: (options: CodexOptions) => {
- runtimeHomes.push(options.env?.["CODEX_HOME"] ?? "");
- throw new Error("persistent credential scan reached");
- },
- },
- );
-
- try {
- await expect(client.run(repository)).rejects.toThrow(
- "persistent credential scan reached",
- );
- } finally {
- await client.close();
- }
- expect(existsSync(credentialHome)).toBe(true);
- }
-
- expect(runtimeHomes).toEqual([credentialHome, credentialHome]);
- });
-
test.each([
["OpenAI", undefined, "OPENAI_API_KEY", "gpt-5.6-sol", undefined],
...EXTERNAL_PROVIDER_CASES,
@@ -4365,7 +4028,7 @@ describe("CodexSecurity orchestration", () => {
? {}
: {
model_provider: provider,
- model_providers: { [provider]: providerConfig },
+ model_providers: { [provider]: providerConfig! },
}),
},
},
@@ -4447,10 +4110,12 @@ describe("CodexSecurity orchestration", () => {
});
const clients = await Promise.all(
- [
- ["OPENAI_API_KEY", "gpt-5.6-sol", undefined],
- ["OPENROUTER_API_KEY", "anthropic/claude-sonnet-4.5", "openrouter"],
- ].map(async ([apiKey, model, provider], index) => {
+ (
+ [
+ ["OPENAI_API_KEY", "gpt-5.6-sol", undefined],
+ ["OPENROUTER_API_KEY", "anthropic/claude-sonnet-4.5", "openrouter"],
+ ] as const
+ ).map(async ([apiKey, model, provider], index) => {
const scanDir = join(root, `parallel-api-key-scan-${index}`);
await mkdir(scanDir, { mode: 0o700 });
return new TestClient(
@@ -4524,123 +4189,6 @@ describe("CodexSecurity orchestration", () => {
}
});
- test("runs parallel ChatGPT scans with isolated mutable configuration", async () => {
- const root = await temporaryDirectory();
- const repository = join(root, "repository");
- const ambientHome = join(root, "ambient-codex-home");
- const stateDirectory = join(root, "state");
- const credentialHome = join(stateDirectory, "codex-home");
- await mkdir(repository);
- await mkdir(ambientHome);
- await writeFile(join(ambientHome, "auth.json"), "{}\n");
- let scansStarted = 0;
- const deepScanConfigPaths = new Set<string>();
- let releaseScans!: () => void;
- const concurrentScans = new Promise<void>((resolve) => {
- releaseScans = resolve;
- });
-
- const clients = await Promise.all(
- [0, 1].map(async (index) => {
- const scanDir = join(root, `parallel-scan-${index}`);
- await mkdir(scanDir, { mode: 0o700 });
- return new TestClient(
- {
- pluginPath: PLUGIN_ROOT,
- codexOverrides: {
- model: index === 0 ? "gpt-5.6-sol" : "gpt-5.6-terra",
- },
- },
- {
- environment: {
- CODEX_HOME: ambientHome,
- CODEX_SECURITY_STATE_DIR: stateDirectory,
- },
- resolvePluginPython: async () => "/managed/python",
- prepareOutputDir: async () => scanDir,
- repositoryRevision: async () => "deadbeef",
- createCodex: (options: CodexOptions) => {
- expect(options.env?.["CODEX_HOME"]).toBe(credentialHome);
- const expectedModel =
- index === 0 ? "gpt-5.6-sol" : "gpt-5.6-terra";
- expect(options.config?.["model"]).toBe(expectedModel);
- const deepScanConfigPath =
- options.env?.["CODEX_SECURITY_DEEP_SCAN_CONFIG_PATH"];
- expect(typeof deepScanConfigPath).toBe("string");
- deepScanConfigPaths.add(deepScanConfigPath!);
- return {
- startThread: () => ({
- id: null,
- async runStreamed() {
- expect(
- existsSync(
- join(credentialHome, ".codex-security-scan.lock"),
- ),
- ).toBe(false);
- if (++scansStarted === 2) releaseScans();
- const credentialConfig = parseToml(
- await readFile(
- join(credentialHome, "config.toml"),
- "utf8",
- ),
- );
- expect(credentialConfig["model"]).toBeUndefined();
- const before = parseToml(
- await readFile(deepScanConfigPath!, "utf8"),
- );
- expect(before["deep_scan"]).toMatchObject({
- workers: index + 2,
- });
- await concurrentScans;
- const after = parseToml(
- await readFile(deepScanConfigPath!, "utf8"),
- );
- expect(after["deep_scan"]).toMatchObject({
- workers: index + 2,
- });
- throw new Error("parallel managed scan reached");
- },
- }),
- };
- },
- },
- );
- }),
- );
-
- try {
- const results = await Promise.allSettled(
- clients.map((client, index) =>
- client
- .run(repository, { mode: "deep", workers: index + 2 })
- .finally(releaseScans),
- ),
- );
- for (const result of results) {
- expect(result).toMatchObject({
- status: "rejected",
- reason: expect.objectContaining({
- message: "parallel managed scan reached",
- }),
- });
- }
- expect(existsSync(credentialHome)).toBe(true);
- expect(scansStarted).toBe(2);
- expect(deepScanConfigPaths.size).toBe(2);
- const pluginConfiguration = JSON.parse(
- await readFile(join(PLUGIN_ROOT, ".mcp.json"), "utf8"),
- ) as { mcpServers: Record<string, { env_vars: string[] }> };
- expect(
- pluginConfiguration.mcpServers["codex-security"]?.env_vars.includes(
- "CODEX_SECURITY_DEEP_SCAN_CONFIG_PATH",
- ),
- ).toBe(true);
- } finally {
- releaseScans();
- await Promise.all(clients.map(async (client) => await client.close()));
- }
- });
-
test("keeps legacy custom-plugin Deep Scan settings under the credential lock", async () => {
const root = await temporaryDirectory();
const repository = join(root, "repository");
@@ -4725,10 +4273,7 @@ describe("CodexSecurity orchestration", () => {
prepareRuntime: async () => ({
...preparedRuntime(codexHome),
plugin: {
- ...(preparedRuntime(codexHome)["plugin"] as Record<
- string,
- unknown
- >),
+ ...preparedRuntime(codexHome).plugin,
pluginRoot,
installedRoot: pluginRoot,
},
@@ -4841,7 +4386,7 @@ describe("CodexSecurity orchestration", () => {
return {
...runtime,
plugin: {
- ...(runtime["plugin"] as Record<string, unknown>),
+ ...runtime.plugin,
installedRoot: join(
codexHome,
"plugins",
@@ -4965,7 +4510,7 @@ describe("CodexSecurity orchestration", () => {
{
cwd: root,
env: {
- PATH: process.env["PATH"],
+ PATH: process.env["PATH"] ?? "",
HOME: process.env["HOME"],
...environment,
CODEX_SECURITY_TARGET_PATHS_FILE: capturedTargetPathsFile,
@@ -5549,12 +5094,9 @@ process.exit(2);
credentialsAvailable: false,
}),
resolveCodexCommand: () => fakeCommand.command,
- resolvePluginPython: async (options: {
- environment?: Record<string, string | undefined>;
- protectedRoot?: string;
- }) => {
- pythonEnvironment = options.environment;
- pythonProtectedRoot = options.protectedRoot;
+ resolvePluginPython: async (options) => {
+ pythonEnvironment = options?.environment;
+ pythonProtectedRoot = options?.protectedRoot;
return "/managed/python";
},
prepareOutputDir: async () => scanDir,
@@ -5659,69 +5201,6 @@ if ([basename(process.argv[1]), ...process.argv.slice(2)].join(" ") !== "login s
}
});
- test("reuses the managed runtime when scan authentication changes", async () => {
- const root = await temporaryDirectory();
- const repository = join(root, "repository");
- const ambientHome = join(root, "ambient-codex-home");
- const stateDirectory = join(root, "state");
- const dedicatedHome = join(stateDirectory, "codex-home");
- const scanDir = join(root, "scan");
- const ambientAuthentication = '{"auth_mode":"chatgpt"}\n';
- await mkdir(repository);
- await mkdir(ambientHome);
- await mkdir(scanDir, { mode: 0o700 });
- await writeFile(join(ambientHome, "auth.json"), ambientAuthentication);
- const runs: Array<{ home: string; apiKey?: string }> = [];
- const client = new TestClient(
- { pluginPath: PLUGIN_ROOT },
- {
- environment: {
- CODEX_HOME: ambientHome,
- CODEX_SECURITY_STATE_DIR: stateDirectory,
- OPENAI_API_KEY: "synthetic-transient-key",
- },
- resolvePluginPython: async () => "/managed/python",
- prepareOutputDir: async () => scanDir,
- repositoryRevision: async () => "deadbeef",
- createCodex: (options: CodexOptions) => {
- runs.push({
- home: options.env?.["CODEX_HOME"] ?? "",
- ...(options.apiKey === undefined ? {} : { apiKey: options.apiKey }),
- });
- throw new Error("authentication-selected scan reached");
- },
- },
- );
-
- try {
- await expect(client.run(repository, { auth: "api-key" })).rejects.toThrow(
- "authentication-selected scan reached",
- );
- expect(runs[0]?.home).toBe(dedicatedHome);
- expect(runs[0]?.apiKey).toBe("synthetic-transient-key");
- expect(existsSync(join(dedicatedHome, "auth.json"))).toBe(false);
-
- await expect(client.run(repository, { auth: "chatgpt" })).rejects.toThrow(
- "authentication-selected scan reached",
- );
- expect(runs[1]).toEqual({ home: dedicatedHome });
- expect(await readFile(join(dedicatedHome, "auth.json"), "utf8")).toBe(
- ambientAuthentication,
- );
- await expect(client.run(repository, { auth: "api-key" })).rejects.toThrow(
- "authentication-selected scan reached",
- );
- expect(runs[2]?.home).toBe(dedicatedHome);
- expect(runs[2]?.apiKey).toBe("synthetic-transient-key");
- expect(await readFile(join(dedicatedHome, "auth.json"), "utf8")).toBe(
- ambientAuthentication,
- );
- } finally {
- await client.close();
- }
- expect(existsSync(dedicatedHome)).toBe(true);
- });
-
test("does not cache an environment key as reusable file authentication", async () => {
let imported = false;
await expect(
@@ -5776,35 +5255,6 @@ if ([basename(process.argv[1]), ...process.argv.slice(2)].join(" ") !== "login s
);
});
- test("does not reimport ambient credentials after an explicit logout", async () => {
- const root = await temporaryDirectory();
- const ambientHome = join(root, "ambient-home");
- const credentialHome = join(root, "credential-home");
- await mkdir(ambientHome);
- await mkdir(credentialHome, { mode: 0o700 });
- await writeFile(join(ambientHome, "auth.json"), '{"token":"ambient"}\n');
- await setCodexSecurityCredentialLogout(credentialHome, true);
- let imported = false;
-
- await expect(
- initialCredentialsAvailable({}, ambientHome, credentialHome, async () => {
- imported = true;
- return true;
- }),
- ).resolves.toBe(false);
- expect(imported).toBe(false);
-
- await setCodexSecurityCredentialLogout(credentialHome, false);
- await expect(
- initialCredentialsAvailable(
- {},
- ambientHome,
- credentialHome,
- async () => true,
- ),
- ).resolves.toBe(true);
- });
-
test("restores stored ChatGPT credentials when switching from an API-key scan", async () => {
const root = await temporaryDirectory();
const repository = join(root, "repository");
@@ -5966,14 +5416,16 @@ if ([basename(process.argv[1]), ...process.argv.slice(2)].join(" ") !== "login s
const codexHome = join(root, "codex-home");
await mkdir(repository);
await mkdir(codexHome);
- let releaseRuntime!: (runtime: Record<string, unknown>) => void;
+ let releaseRuntime!: (runtime: ReturnType<typeof preparedRuntime>) => void;
let preparationStarted!: () => void;
const started = new Promise<void>((resolve) => {
preparationStarted = resolve;
});
- const prepared = new Promise<Record<string, unknown>>((resolve) => {
- releaseRuntime = resolve;
- });
+ const prepared = new Promise<ReturnType<typeof preparedRuntime>>(
+ (resolve) => {
+ releaseRuntime = resolve;
+ },
+ );
let createCodexCalled = false;
const client = new TestClient(
{},
@@ -6018,14 +5470,16 @@ if ([basename(process.argv[1]), ...process.argv.slice(2)].join(" ") !== "login s
await mkdir(repository);
await mkdir(codexHome);
await mkdir(scanDir, { mode: 0o700 });
- let releaseRuntime!: (runtime: Record<string, unknown>) => void;
+ let releaseRuntime!: (runtime: ReturnType<typeof preparedRuntime>) => void;
let preparationStarted!: () => void;
const started = new Promise<void>((resolve) => {
preparationStarted = resolve;
});
- const prepared = new Promise<Record<string, unknown>>((resolve) => {
- releaseRuntime = resolve;
- });
+ const prepared = new Promise<ReturnType<typeof preparedRuntime>>(
+ (resolve) => {
+ releaseRuntime = resolve;
+ },
+ );
const client = new TestClient(
{},
{
@@ -6226,7 +5680,7 @@ if ([basename(process.argv[1]), ...process.argv.slice(2)].join(" ") !== "login s
test("cleans the bootstrap workspace when credential-home cleanup fails", async () => {
if (
- runMockInSubprocess(
+ runTestInSubprocess(
import.meta.path,
"cleans the bootstrap workspace when credential-home cleanup fails",
)
@@ -6289,7 +5743,7 @@ if ([basename(process.argv[1]), ...process.argv.slice(2)].join(" ") !== "login s
test("attempts both preparation cleanups and preserves the preparation and cleanup failures", async () => {
if (
- runMockInSubprocess(
+ runTestInSubprocess(
import.meta.path,
"attempts both preparation cleanups and preserves the preparation and cleanup failures",
)
diff --git a/sdk/typescript/tests-ts/cli-skills.test.ts b/sdk/typescript/tests-ts/cli-skills.test.ts
index 60c778245..23700df80 100644
--- a/sdk/typescript/tests-ts/cli-skills.test.ts
+++ b/sdk/typescript/tests-ts/cli-skills.test.ts
@@ -13,7 +13,7 @@ import {
} from "../src/cli.js";
import type { LinearClientFactory } from "../src/linear.js";
import { capture, dependencies } from "./cli-fixtures.js";
-import { runMockInSubprocess } from "./support/isolated-mock.js";
+import { runTestInSubprocess } from "./support/test-subprocess.js";
function linearIssue(identifier: string) {
return {
@@ -462,7 +462,7 @@ describe("CLI skill commands", () => {
test("rejects input replacements whose numeric file IDs collide", async () => {
if (
- runMockInSubprocess(
+ runTestInSubprocess(
import.meta.path,
"rejects input replacements whose numeric file IDs collide",
)
@@ -549,7 +549,7 @@ describe("CLI skill commands", () => {
: ["symbolic link", "FIFO"],
)("rejects finding files replaced with a %s", async (replacement) => {
if (
- runMockInSubprocess(
+ runTestInSubprocess(
import.meta.path,
`rejects finding files replaced with a ${replacement}`,
)
diff --git a/sdk/typescript/tests-ts/multiscan.test.ts b/sdk/typescript/tests-ts/multiscan.test.ts
index 33676192a..2c62c0422 100644
--- a/sdk/typescript/tests-ts/multiscan.test.ts
+++ b/sdk/typescript/tests-ts/multiscan.test.ts
@@ -18,6 +18,7 @@ import {
import * as filesystem from "node:fs/promises";
import { hostname, tmpdir } from "node:os";
import { join } from "node:path";
+import { fileURLToPath } from "node:url";
import { afterEach, describe, expect, spyOn, test } from "bun:test";
import { main } from "../src/cli.js";
import { ScanCostLimitExceededError } from "../src/errors.js";
@@ -25,6 +26,7 @@ import type { ScanResult } from "../src/result.js";
import { buildGitHubCredentialArgs, runMultiscan } from "../src/multiscan.js";
import { resolveTrustedExecutable } from "../src/trusted-executable.js";
import { capture, dependencies, fakeResult } from "./cli-fixtures.js";
+import { runTestInSubprocess } from "./support/test-subprocess.js";
type MultiscanOptions = Parameters<typeof runMultiscan>[0];
type SecurityClient = ReturnType<MultiscanOptions["createSecurity"]>;
@@ -1540,6 +1542,13 @@ describe("multiscan", () => {
});
test("ignores repository-local Git shims while preserving credential configuration", async () => {
+ if (
+ runTestInSubprocess(
+ fileURLToPath(import.meta.url),
+ "ignores repository-local Git shims while preserving credential configuration",
+ )
+ )
+ return;
const paths = await fixture();
const source = await repository(paths.root, "private");
await writeFile(
diff --git a/sdk/typescript/tests-ts/runtime.test.ts b/sdk/typescript/tests-ts/runtime.test.ts
index 20e2035bb..5f6505e08 100644
--- a/sdk/typescript/tests-ts/runtime.test.ts
+++ b/sdk/typescript/tests-ts/runtime.test.ts
@@ -27,7 +27,6 @@ import {
relative,
sep,
} from "node:path";
-import { fileURLToPath, pathToFileURL } from "node:url";
import { promisify } from "node:util";
import { brotliDecompressSync } from "node:zlib";
import { afterEach, describe, expect, mock, test } from "bun:test";
@@ -74,7 +73,7 @@ import {
verifyStableWindowsCredentialDescendants,
} from "../src/runtime.js";
import { loadBundledRuntime, PLUGIN_ROOT } from "./plugin-root.js";
-import { runMockInSubprocess } from "./support/isolated-mock.js";
+import { runTestInSubprocess } from "./support/test-subprocess.js";
const temporaryDirectories: string[] = [];
const testPosix = process.platform === "win32" ? test.skip : test;
@@ -890,7 +889,7 @@ describe("plugin runtime preparation", () => {
test("cancels configured plugin directory discovery", async () => {
if (
- runMockInSubprocess(
+ runTestInSubprocess(
import.meta.path,
"cancels configured plugin directory discovery",
)
@@ -1014,7 +1013,7 @@ describe("plugin runtime preparation", () => {
"rejects a queued plugin directory replaced with a symlink",
async () => {
if (
- runMockInSubprocess(
+ runTestInSubprocess(
import.meta.path,
"rejects a queued plugin directory replaced with a symlink",
)
@@ -1327,7 +1326,7 @@ describe("plugin runtime preparation", () => {
test("imports ambient auth when credential files do not support hard links", async () => {
if (
- runMockInSubprocess(
+ runTestInSubprocess(
import.meta.path,
"imports ambient auth when credential files do not support hard links",
)
@@ -2259,7 +2258,7 @@ describe("runtime directories and plugin Python boundary", () => {
test("retries Windows credential verification when a descendant disappears", async () => {
if (
- runMockInSubprocess(
+ runTestInSubprocess(
import.meta.path,
"retries Windows credential verification when a descendant disappears",
)
@@ -2304,7 +2303,7 @@ describe("runtime directories and plugin Python boundary", () => {
test("rejects Windows credential descendants that repeatedly disappear", async () => {
if (
- runMockInSubprocess(
+ runTestInSubprocess(
import.meta.path,
"rejects Windows credential descendants that repeatedly disappear",
)
@@ -3362,193 +3361,6 @@ describe("runtime directories and plugin Python boundary", () => {
},
);
- test.skipIf(
- process.platform !== "win32" ||
- process.env["GITHUB_ACTIONS"] !== "true" ||
- process.env["RUNNER_ENVIRONMENT"] !== "github-hosted" ||
- process.env["CODEX_SECURITY_ALLOW_MACHINE_POLICY_TEST"] !== "true",
- )(
- "prepares managed credential homes under constrained PowerShell",
- async () => {
- const root = await temporaryDirectory();
- const powershell = join(
- process.env["SystemRoot"] ?? "C:\\Windows",
- "System32",
- "WindowsPowerShell",
- "v1.0",
- "powershell.exe",
- );
- const constrainedEnvironment = {
- ...process.env,
- CODEX_SECURITY_STATE_DIR: join(root, "state"),
- PSModulePath: join(root, "untrusted-or-incompatible-modules"),
- PSMODULEPATH: join(root, "uppercase-untrusted-modules"),
- };
- const registry = join(dirname(dirname(dirname(powershell))), "reg.exe");
- const policyKey =
- "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment";
- const policyName = "__PSLockdownPolicy";
- const original = spawnSync(
- registry,
- ["query", policyKey, "/v", policyName],
- { encoding: "utf8", timeout: 15_000, windowsHide: true },
- );
- expect(original.status === 0 || original.status === 1).toBe(true);
- const originalEntry =
- original.status === 0
- ? /^\s*__PSLockdownPolicy\s+(REG_[A-Z_]+)\s*(.*?)\s*$/mu.exec(
- original.stdout,
- )
- : null;
- if (original.status === 0) expect(originalEntry).not.toBeNull();
- const originalPolicy =
- originalEntry === null
- ? null
- : { type: originalEntry[1]!, value: originalEntry[2]! };
- const enabled = spawnSync(
- registry,
- ["add", policyKey, "/v", policyName, "/t", "REG_SZ", "/d", "4", "/f"],
- { encoding: "utf8", timeout: 15_000, windowsHide: true },
- );
- expect(enabled.status).toBe(0);
-
- try {
- const mode = spawnSync(
- powershell,
- [
- "-NoLogo",
- "-NoProfile",
- "-NonInteractive",
- "-Command",
- "$ExecutionContext.SessionState.LanguageMode",
- ],
- {
- encoding: "utf8",
- env: constrainedEnvironment,
- timeout: 15_000,
- windowsHide: true,
- },
- );
- expect(mode.status).toBe(0);
- expect(mode.stdout.trim()).toBe("ConstrainedLanguage");
-
- const oldImplementation = spawnSync(
- powershell,
- [
- "-NoLogo",
- "-NoProfile",
- "-NonInteractive",
- "-Command",
- "$ErrorActionPreference = 'Stop'; New-Object System.Security.AccessControl.DirectorySecurity",
- ],
- {
- encoding: "utf8",
- env: constrainedEnvironment,
- timeout: 15_000,
- windowsHide: true,
- },
- );
- expect(oldImplementation.status).not.toBe(0);
-
- const trustedPowerShellEnvironment = {
- ...Object.fromEntries(
- Object.entries(process.env).filter(
- ([name]) => name.toUpperCase() !== "PSMODULEPATH",
- ),
- ),
- PSModulePath: join(dirname(powershell), "Modules"),
- };
- const guest = spawnSync(
- powershell,
- [
- "-NoLogo",
- "-NoProfile",
- "-NonInteractive",
- "-Command",
- [
- "Microsoft.PowerShell.Utility\\ConvertFrom-SddlString -Sddl 'O:LGG:SYD:(A;;GA;;;SY)'",
- "Microsoft.PowerShell.Utility\\Select-Object -ExpandProperty RawDescriptor",
- "Microsoft.PowerShell.Utility\\Select-Object -ExpandProperty Owner",
- "Microsoft.PowerShell.Utility\\Select-Object -ExpandProperty Value",
- ].join(" | "),
- ],
- {
- encoding: "utf8",
- env: trustedPowerShellEnvironment,
- timeout: 15_000,
- windowsHide: true,
- },
- );
- expect(guest.status).toBe(0);
- expect(guest.stdout.trim()).toMatch(/^S-1-(?:\d+-)*501$/u);
- const home = join(root, "state", "codex-home");
- await mkdir(home, { recursive: true });
- const foreignGrant = spawnSync(
- join(dirname(dirname(dirname(powershell))), "icacls.exe"),
- [home, "/grant", `*${guest.stdout.trim()}:(OI)(CI)R`],
- { encoding: "utf8", timeout: 15_000, windowsHide: true },
- );
- expect(foreignGrant.status).toBe(0);
-
- const fixtureModule = join(root, "runtime-node-fixture.mjs");
- const build = spawnSync(
- process.execPath,
- [
- "build",
- fileURLToPath(new URL("../src/runtime.ts", import.meta.url)),
- "--target=node",
- "--format=esm",
- `--outfile=${fixtureModule}`,
- ],
- { encoding: "utf8", timeout: 30_000, windowsHide: true },
- );
- expect(build.status).toBe(0);
- const selectedNode = spawnSync("node", ["-p", "process.execPath"], {
- encoding: "utf8",
- timeout: 15_000,
- windowsHide: true,
- });
- expect(selectedNode.status).toBe(0);
- const fixture = spawnSync(
- selectedNode.stdout.trim(),
- [
- "--input-type=module",
- "--eval",
- `import { prepareCodexSecurityCredentialHome } from ${JSON.stringify(pathToFileURL(fixtureModule).href)}; await prepareCodexSecurityCredentialHome();`,
- ],
- {
- encoding: "utf8",
- env: constrainedEnvironment,
- timeout: 30_000,
- windowsHide: true,
- },
- );
- expect(fixture.stderr).toBe("");
- expect(fixture.status).toBe(0);
- expect(existsSync(home)).toBe(true);
- } finally {
- const restore = spawnSync(
- registry,
- originalPolicy === null
- ? ["delete", policyKey, "/v", policyName, "/f"]
- : [
- "add",
- policyKey,
- "/v",
- policyName,
- "/t",
- originalPolicy.type,
- "/d",
- originalPolicy.value,
- "/f",
- ],
- { encoding: "utf8", timeout: 15_000, windowsHide: true },
- );
- expect(restore.status).toBe(0);
- }
- },
- );
-
test("derives persistent state from the ambient home or explicit override", async () => {
const root = await temporaryDirectory();
expect(codexSecurityStateDirectory({ CODEX_HOME: root })).toBe(
diff --git a/sdk/typescript/tests-ts/skeleton.test.ts b/sdk/typescript/tests-ts/skeleton.test.ts
index 807ecc753..f8d2bbec8 100644
--- a/sdk/typescript/tests-ts/skeleton.test.ts
+++ b/sdk/typescript/tests-ts/skeleton.test.ts
@@ -63,7 +63,7 @@ describe("TypeScript package skeleton", () => {
}
});
- test("randomizes tests with the default timeout across CI platforms", async () => {
+ test("randomizes tests and keeps the default and Windows CI timeouts", async () => {
const packageJson = JSON.parse(
await readFile(new URL("../package.json", import.meta.url), "utf8"),
);
@@ -82,6 +82,9 @@ describe("TypeScript package skeleton", () => {
expect(ciWorkflow).toContain(
"run: node sdk/typescript/scripts/run-windows-ci-tests.mjs ${{ matrix.shard }}",
);
+ expect(ciWorkflow).toContain(
+ "run: bun test --timeout 120000 ./tests-ts/windows-machine-policy.test.ts",
+ );
expect(ciWorkflow).toContain(
"name: windows-latest / node-${{ matrix.node == '22.13.0' && '22' || matrix.node }}",
);
diff --git a/sdk/typescript/tests-ts/support/api-client.ts b/sdk/typescript/tests-ts/support/api-client.ts
new file mode 100644
index 000000000..79e3b595c
--- /dev/null
+++ b/sdk/typescript/tests-ts/support/api-client.ts
@@ -0,0 +1,73 @@
+import { CodexSecurity } from "../../src/api.js";
+import type { JsonObject } from "../../src/config.js";
+
+type ClientArguments = ConstructorParameters<typeof CodexSecurity>;
+
+export const TEST_SNAPSHOT_DIGEST = `codex-security-snapshot/v1:sha256:${"a".repeat(64)}`;
+
+export function mockScanRegistration(args: readonly string[]) {
+ const recipe = JSON.parse(args[args.indexOf("--recipe-json") + 1]!) as {
+ repositoryRevision?: string;
+ target: { kind: string };
+ };
+ const kind =
+ recipe.target.kind === "refs" || recipe.target.kind === "working_tree"
+ ? "git_diff"
+ : recipe.repositoryRevision === undefined
+ ? "directory_snapshot"
+ : "git_revision";
+
+ return {
+ scanId: "scan_example_001",
+ targetId: "target_sha256_example",
+ targetRevision: recipe.repositoryRevision ?? "unversioned",
+ scanDir: args[args.indexOf("--scan-dir") + 1]!,
+ contract: {
+ target: {
+ allowedKinds: [kind],
+ ...(kind === "directory_snapshot"
+ ? { requiredSnapshotDigest: TEST_SNAPSHOT_DIGEST }
+ : {}),
+ },
+ },
+ };
+}
+
+export function mockWorkbench(args: readonly string[]): JsonObject {
+ if (args[0] === "register-cli-scan") return mockScanRegistration(args);
+ if (args[0] === "get-scan-feedback") {
+ return {
+ scanId: "scan_example_001",
+ targetId: "target_sha256_example",
+ falsePositives: [],
+ };
+ }
+ return {};
+}
+
+export class TestClient extends CodexSecurity {
+ public constructor(
+ config: ClientArguments[0],
+ dependencies: Partial<ClientArguments[1]>,
+ ) {
+ super(
+ config,
+ {
+ createCodex: () => {
+ throw new Error("Unexpected Codex invocation in test");
+ },
+ environment: {},
+ runWorkbench: async (_options, args) => mockWorkbench(args),
+ ...dependencies,
+ },
+ { surface: "sdk" },
+ );
+ }
+}
+
+export const SHELL_ENVIRONMENT_PREFIX =
+ process.platform === "win32" ? "$env:" : "$";
+
+export function shellEnvironmentReference(name: string, suffix = ""): string {
+ return `"${SHELL_ENVIRONMENT_PREFIX}${name}${suffix}"`;
+}
diff --git a/sdk/typescript/tests-ts/support/api-events.ts b/sdk/typescript/tests-ts/support/api-events.ts
index a73f8aa0e..2b5d501cd 100644
--- a/sdk/typescript/tests-ts/support/api-events.ts
+++ b/sdk/typescript/tests-ts/support/api-events.ts
@@ -2,11 +2,19 @@ import { chmod, cp, mkdtemp, realpath, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import type { ThreadEvent } from "@openai/codex-sdk";
-import { runScanEvents } from "../../src/api.js";
+import { CodexSecurity, runScanEvents } from "../../src/api.js";
import type { ScanOptions } from "../../src/index.js";
import { PLUGIN_ROOT } from "../plugin-root.js";
-export function preparedRuntime(codexHome: string): Record<string, unknown> {
+type PreparedRuntime = Awaited<
+ ReturnType<
+ NonNullable<
+ ConstructorParameters<typeof CodexSecurity>[1]["prepareRuntime"]
+ >
+ >
+>;
+
+export function preparedRuntime(codexHome: string): PreparedRuntime {
return {
codexHome,
plugin: {
diff --git a/sdk/typescript/tests-ts/support/isolated-mock.ts b/sdk/typescript/tests-ts/support/isolated-mock.ts
deleted file mode 100644
index 4ec8fb599..000000000
--- a/sdk/typescript/tests-ts/support/isolated-mock.ts
+++ /dev/null
@@ -1,18 +0,0 @@
-import { spawnSync } from "node:child_process";
-import { expect } from "bun:test";
-
-export function runMockInSubprocess(file: string, name: string): boolean {
- if (process.env["CODEX_SECURITY_ISOLATED_MOCK"] === "1") return false;
-
- const result = spawnSync(
- process.execPath,
- ["test", "--timeout", "30000", "--test-name-pattern", name, file],
- {
- encoding: "utf8",
- env: { ...process.env, CODEX_SECURITY_ISOLATED_MOCK: "1" },
- windowsHide: true,
- },
- );
- expect(result.status, result.stderr).toBe(0);
- return true;
-}
diff --git a/sdk/typescript/tests-ts/support/test-subprocess.ts b/sdk/typescript/tests-ts/support/test-subprocess.ts
new file mode 100644
index 000000000..dfe4588f5
--- /dev/null
+++ b/sdk/typescript/tests-ts/support/test-subprocess.ts
@@ -0,0 +1,22 @@
+import { spawnSync } from "node:child_process";
+import { fileURLToPath } from "node:url";
+import { expect } from "bun:test";
+
+export function runTestInSubprocess(file: string, name: string): boolean {
+ const identity = `${file}::${name}`;
+ if (process.env["CODEX_SECURITY_ISOLATED_TEST"] === identity) return false;
+ const pattern = `${name.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&")}$`;
+
+ const result = spawnSync(
+ process.execPath,
+ ["test", "--timeout", "30000", "--test-name-pattern", pattern, file],
+ {
+ cwd: fileURLToPath(new URL("../../", import.meta.url)),
+ encoding: "utf8",
+ env: { ...process.env, CODEX_SECURITY_ISOLATED_TEST: identity },
+ windowsHide: true,
+ },
+ );
+ expect(result.status, result.stderr || result.error?.message).toBe(0);
+ return true;
+}
diff --git a/sdk/typescript/tests-ts/windows-machine-policy.test.ts b/sdk/typescript/tests-ts/windows-machine-policy.test.ts
new file mode 100644
index 000000000..13983049a
--- /dev/null
+++ b/sdk/typescript/tests-ts/windows-machine-policy.test.ts
@@ -0,0 +1,206 @@
+import { spawnSync } from "node:child_process";
+import { existsSync } from "node:fs";
+import { mkdir, mkdtemp, realpath, rm } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import { dirname, join } from "node:path";
+import { fileURLToPath, pathToFileURL } from "node:url";
+import { afterEach, describe, expect, test } from "bun:test";
+
+let temporaryRoot: string | undefined;
+afterEach(async () => {
+ if (temporaryRoot !== undefined)
+ await rm(temporaryRoot, { recursive: true, force: true });
+ temporaryRoot = undefined;
+});
+
+describe("runtime directories and plugin Python boundary", () => {
+ test.skipIf(
+ process.platform !== "win32" ||
+ process.env["GITHUB_ACTIONS"] !== "true" ||
+ process.env["RUNNER_ENVIRONMENT"] !== "github-hosted" ||
+ process.env["CODEX_SECURITY_ALLOW_MACHINE_POLICY_TEST"] !== "true",
+ )(
+ "prepares managed credential homes under constrained PowerShell",
+ async () => {
+ const root = await realpath(
+ await mkdtemp(join(tmpdir(), "codex-security-policy-")),
+ );
+ temporaryRoot = root;
+ const powershell = join(
+ process.env["SystemRoot"] ?? "C:\\Windows",
+ "System32",
+ "WindowsPowerShell",
+ "v1.0",
+ "powershell.exe",
+ );
+ const constrainedEnvironment = {
+ ...process.env,
+ CODEX_SECURITY_STATE_DIR: join(root, "state"),
+ PSModulePath: join(root, "untrusted-or-incompatible-modules"),
+ PSMODULEPATH: join(root, "uppercase-untrusted-modules"),
+ };
+ const registry = join(dirname(dirname(dirname(powershell))), "reg.exe");
+ const policyKey =
+ "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment";
+ const policyName = "__PSLockdownPolicy";
+ const original = spawnSync(
+ registry,
+ ["query", policyKey, "/v", policyName],
+ { encoding: "utf8", timeout: 15_000, windowsHide: true },
+ );
+ expect(original.status === 0 || original.status === 1).toBe(true);
+ const originalEntry =
+ original.status === 0
+ ? /^\s*__PSLockdownPolicy\s+(REG_[A-Z_]+)\s*(.*?)\s*$/mu.exec(
+ original.stdout,
+ )
+ : null;
+ if (original.status === 0) expect(originalEntry).not.toBeNull();
+ const originalPolicy =
+ originalEntry === null
+ ? null
+ : { type: originalEntry[1]!, value: originalEntry[2]! };
+ const enabled = spawnSync(
+ registry,
+ ["add", policyKey, "/v", policyName, "/t", "REG_SZ", "/d", "4", "/f"],
+ { encoding: "utf8", timeout: 15_000, windowsHide: true },
+ );
+ expect(enabled.status).toBe(0);
+
+ try {
+ const mode = spawnSync(
+ powershell,
+ [
+ "-NoLogo",
+ "-NoProfile",
+ "-NonInteractive",
+ "-Command",
+ "$ExecutionContext.SessionState.LanguageMode",
+ ],
+ {
+ encoding: "utf8",
+ env: constrainedEnvironment,
+ timeout: 15_000,
+ windowsHide: true,
+ },
+ );
+ expect(mode.status).toBe(0);
+ expect(mode.stdout.trim()).toBe("ConstrainedLanguage");
+
+ const oldImplementation = spawnSync(
+ powershell,
+ [
+ "-NoLogo",
+ "-NoProfile",
+ "-NonInteractive",
+ "-Command",
+ "$ErrorActionPreference = 'Stop'; New-Object System.Security.AccessControl.DirectorySecurity",
+ ],
+ {
+ encoding: "utf8",
+ env: constrainedEnvironment,
+ timeout: 15_000,
+ windowsHide: true,
+ },
+ );
+ expect(oldImplementation.status).not.toBe(0);
+
+ const trustedPowerShellEnvironment = {
+ ...Object.fromEntries(
+ Object.entries(process.env).filter(
+ ([name]) => name.toUpperCase() !== "PSMODULEPATH",
+ ),
+ ),
+ PSModulePath: join(dirname(powershell), "Modules"),
+ };
+ const guest = spawnSync(
+ powershell,
+ [
+ "-NoLogo",
+ "-NoProfile",
+ "-NonInteractive",
+ "-Command",
+ [
+ "Microsoft.PowerShell.Utility\\ConvertFrom-SddlString -Sddl 'O:LGG:SYD:(A;;GA;;;SY)'",
+ "Microsoft.PowerShell.Utility\\Select-Object -ExpandProperty RawDescriptor",
+ "Microsoft.PowerShell.Utility\\Select-Object -ExpandProperty Owner",
+ "Microsoft.PowerShell.Utility\\Select-Object -ExpandProperty Value",
+ ].join(" | "),
+ ],
+ {
+ encoding: "utf8",
+ env: trustedPowerShellEnvironment,
+ timeout: 15_000,
+ windowsHide: true,
+ },
+ );
+ expect(guest.status).toBe(0);
+ expect(guest.stdout.trim()).toMatch(/^S-1-(?:\d+-)*501$/u);
+ const home = join(root, "state", "codex-home");
+ await mkdir(home, { recursive: true });
+ const foreignGrant = spawnSync(
+ join(dirname(dirname(dirname(powershell))), "icacls.exe"),
+ [home, "/grant", `*${guest.stdout.trim()}:(OI)(CI)R`],
+ { encoding: "utf8", timeout: 15_000, windowsHide: true },
+ );
+ expect(foreignGrant.status).toBe(0);
+
+ const fixtureModule = join(root, "runtime-node-fixture.mjs");
+ const build = spawnSync(
+ process.execPath,
+ [
+ "build",
+ fileURLToPath(new URL("../src/runtime.ts", import.meta.url)),
+ "--target=node",
+ "--format=esm",
+ `--outfile=${fixtureModule}`,
+ ],
+ { encoding: "utf8", timeout: 30_000, windowsHide: true },
+ );
+ expect(build.status).toBe(0);
+ const selectedNode = spawnSync("node", ["-p", "process.execPath"], {
+ encoding: "utf8",
+ timeout: 15_000,
+ windowsHide: true,
+ });
+ expect(selectedNode.status).toBe(0);
+ const fixture = spawnSync(
+ selectedNode.stdout.trim(),
+ [
+ "--input-type=module",
+ "--eval",
+ `import { prepareCodexSecurityCredentialHome } from ${JSON.stringify(pathToFileURL(fixtureModule).href)}; await prepareCodexSecurityCredentialHome();`,
+ ],
+ {
+ encoding: "utf8",
+ env: constrainedEnvironment,
+ timeout: 30_000,
+ windowsHide: true,
+ },
+ );
+ expect(fixture.stderr).toBe("");
+ expect(fixture.status).toBe(0);
+ expect(existsSync(home)).toBe(true);
+ } finally {
+ const restore = spawnSync(
+ registry,
+ originalPolicy === null
+ ? ["delete", policyKey, "/v", policyName, "/f"]
+ : [
+ "add",
+ policyKey,
+ "/v",
+ policyName,
+ "/t",
+ originalPolicy.type,
+ "/d",
+ originalPolicy.value,
+ "/f",
+ ],
+ { encoding: "utf8", timeout: 15_000, windowsHide: true },
+ );
+ expect(restore.status).toBe(0);
+ }
+ },
+ );
+});