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(deep-scan): reproduce the Windows first-launch pin failure (#2385)

Michael D'Angelo63c1e62f40d3
diff --git a/plugins/codex-security/mcp-app/tests/deep_scan_executor_fixture.ts b/plugins/codex-security/mcp-app/tests/deep_scan_executor_fixture.ts
index b856ea04d..58474c0d3 100644
--- a/plugins/codex-security/mcp-app/tests/deep_scan_executor_fixture.ts
+++ b/plugins/codex-security/mcp-app/tests/deep_scan_executor_fixture.ts
@@ -1,15 +1,13 @@
import assert from "node:assert/strict";
-import { readFile } from "node:fs/promises";
import path from "node:path";
-import { fileURLToPath } from "node:url";
import type { CodexSdkWorkerExecutor as Executor } from "../src/deep-scan/executor.js";
import type { CodexWorkerRequest } from "../src/deep-scan/types.js";
import { assertConfigOverrides } from "./assertions.ts";
-import { importModule, importSource } from "./import-module.ts";
+import { importSource } from "./import-module.ts";
+import { executor } from "./support/deep-scan-executor-module.ts";
import { createFakeCodexFixture } from "./support/fake-codex-fixture.ts";
import { createTemporaryDirectories } from "./support/temporary-directories.ts";
-const executorSource = new URL("../src/deep-scan/executor.ts", import.meta.url);
export const {
bundledCodexSdkEnvironment,
}: {
@@ -27,27 +25,13 @@ export const {
),
);
-const executor = await importModule({
- define: {
- "import.meta.url": JSON.stringify(executorSource.href),
- },
- stdin: {
- // Expose the environment snapshot only for tests.
- contents: `${await readFile(executorSource, "utf8")}
-export { snapshotWorkerEnvironment, appendItemDiagnostic };
-export { parseDeepScan } from "./response.js";`,
- loader: "ts",
- resolveDir: path.dirname(fileURLToPath(executorSource)),
- sourcefile: fileURLToPath(executorSource),
- },
-});
-
export const {
resolveCodexPath,
snapshotWorkerEnvironment,
appendItemDiagnostic,
parseDeepScan,
} = executor;
+
export const CodexSdkWorkerExecutor: typeof Executor =
executor.CodexSdkWorkerExecutor;
diff --git a/plugins/codex-security/mcp-app/tests/support/deep-scan-executor-module.ts b/plugins/codex-security/mcp-app/tests/support/deep-scan-executor-module.ts
new file mode 100644
index 000000000..7d70488e1
--- /dev/null
+++ b/plugins/codex-security/mcp-app/tests/support/deep-scan-executor-module.ts
@@ -0,0 +1,28 @@
+import { readFile } from "node:fs/promises";
+import path from "node:path";
+import { fileURLToPath } from "node:url";
+import { importModule } from "../import-module.ts";
+
+const executorSource = new URL(
+ "../../src/deep-scan/executor.ts",
+ import.meta.url,
+);
+export const executor = await importModule({
+ define: {
+ "import.meta.url": JSON.stringify(executorSource.href),
+ },
+ stdin: {
+ // Expose internal worker checks only to the test fixtures.
+ contents: `${await readFile(executorSource, "utf8")}
+export { snapshotWorkerEnvironment, appendItemDiagnostic, verifyBoundCodexExecutable };
+export { parseDeepScan } from "./response.js";`,
+ loader: "ts",
+ resolveDir: path.dirname(fileURLToPath(executorSource)),
+ sourcefile: fileURLToPath(executorSource),
+ },
+});
+
+export const verifyBoundCodexExecutable: (executable: {
+ path: string;
+ identity: import("node:fs").BigIntStats;
+}) => Promise<void> = executor.verifyBoundCodexExecutable;
diff --git a/plugins/codex-security/mcp-app/tests/test_helper_files.ts b/plugins/codex-security/mcp-app/tests/test_helper_files.ts
index a328dd4c5..b7353b23e 100644
--- a/plugins/codex-security/mcp-app/tests/test_helper_files.ts
+++ b/plugins/codex-security/mcp-app/tests/test_helper_files.ts
@@ -5,6 +5,7 @@ import { join, win32 } from "node:path";
import { after, test } from "node:test";
import { importModule, importSource } from "./import-module.ts";
import { createTemporaryDirectories } from "./support/temporary-directories.ts";
+import { registerWorkerExecutableFirstLaunchTests } from "./windows_executable_first_launch_cases.ts";
const { resolvedPathText, isMissingPathError } = (await importSource(
"src/helpers/helper-files.ts",
@@ -195,3 +196,6 @@ test(
}
},
);
+
+// The Windows CI job runs this entry point directly and retains native TAP results.
+registerWorkerExecutableFirstLaunchTests();
diff --git a/plugins/codex-security/mcp-app/tests/windows_executable_first_launch_cases.ts b/plugins/codex-security/mcp-app/tests/windows_executable_first_launch_cases.ts
new file mode 100644
index 000000000..cf79401bc
--- /dev/null
+++ b/plugins/codex-security/mcp-app/tests/windows_executable_first_launch_cases.ts
@@ -0,0 +1,100 @@
+import assert from "node:assert/strict";
+import { execFile } from "node:child_process";
+import { createHash } from "node:crypto";
+import { createReadStream } from "node:fs";
+import { copyFile, rm, stat, utimes, writeFile } from "node:fs/promises";
+import path from "node:path";
+import test from "node:test";
+import { setTimeout as delay } from "node:timers/promises";
+import { promisify } from "node:util";
+import { executablePathForSpawn } from "../src/executable-path.ts";
+import { temporaryDirectory } from "./support/temporary-directories.ts";
+
+async function digest(file: string): Promise<string> {
+ const hash = createHash("sha256");
+ for await (const bytes of createReadStream(file)) hash.update(bytes);
+ return hash.digest("hex");
+}
+
+export function registerWorkerExecutableFirstLaunchTests(): void {
+ test(
+ "the first Windows version launch preserves the executable pin",
+ { skip: process.platform !== "win32" },
+ async (t) => {
+ const { verifyBoundCodexExecutable } =
+ await import("./support/deep-scan-executor-module.ts");
+ const root = await temporaryDirectory("worker-first-launch-", true);
+ t.after(() => rm(root, { recursive: true, force: true }));
+ const executable = path.join(root, "version-fixture.exe");
+ await copyFile(process.execPath, executable);
+ const beforeDigest = await digest(executable);
+ const identity = await stat(executable, { bigint: true });
+ await verifyBoundCodexExecutable({ path: executable, identity });
+ const { stdout } = await promisify(execFile)(
+ executablePathForSpawn(executable),
+ ["--version"],
+ {
+ windowsHide: true,
+ },
+ );
+ assert.equal(stdout.trim(), process.version);
+ const after = await stat(executable, { bigint: true });
+ assert.equal(await digest(executable), beforeDigest);
+ assert.equal(after.dev, identity.dev);
+ assert.equal(after.ino, identity.ino);
+ assert.equal(after.size, identity.size);
+ assert.equal(after.mtimeNs, identity.mtimeNs);
+ try {
+ await verifyBoundCodexExecutable({ path: executable, identity });
+ } catch (error) {
+ if (
+ after.ctimeNs !== identity.ctimeNs &&
+ error instanceof Error &&
+ error.name === "DeepScanNonRetryableError" &&
+ error.cause === undefined
+ ) {
+ t.todo(
+ "The current worker pin rejects an ordinary launch when only Windows change time advances.",
+ );
+ }
+ throw error;
+ }
+ },
+ );
+
+ test("the worker pin rejects a changed synthetic file of the same size", async (t) => {
+ const { verifyBoundCodexExecutable } =
+ await import("./support/deep-scan-executor-module.ts");
+ const root = await temporaryDirectory("worker-changed-pin-", true);
+ t.after(() => rm(root, { recursive: true, force: true }));
+ const executable = path.join(root, "content-fixture.exe");
+ // This inert text fixture represents two ordinary content revisions of equal size.
+ const fixedTime = new Date("2025-01-01T00:00:00Z");
+ await writeFile(executable, "fixture revision A\n");
+ await utimes(executable, fixedTime, fixedTime);
+ const identity = await stat(executable, { bigint: true });
+ await verifyBoundCodexExecutable({ path: executable, identity });
+ // Retry a real equal-length revision on a later tick if the first write is too fast.
+ for (const [wait, revision] of [
+ [0, "B"],
+ [2200, "C"],
+ ] as const) {
+ if (wait) await delay(wait);
+ await writeFile(executable, `fixture revision ${revision}\n`);
+ await utimes(executable, fixedTime, fixedTime);
+ const updated = await stat(executable, { bigint: true });
+ assert.equal(updated.size, identity.size);
+ assert.equal(updated.mtimeNs, identity.mtimeNs);
+ if (updated.ctimeNs !== identity.ctimeNs) {
+ await assert.rejects(
+ verifyBoundCodexExecutable({ path: executable, identity }),
+ { name: "DeepScanNonRetryableError" },
+ );
+ return;
+ }
+ }
+ t.skip(
+ "The fixture filesystem reported the same change time across two separately timed content revisions.",
+ );
+ });
+}