Skip to content

OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security

fix(windows): reuse verified generated plugin cache content (#2538)

Michael D'Angelofbfba97201ba
diff --git a/sdk/typescript/src/runtime.ts b/sdk/typescript/src/runtime.ts
index de319933a..9540c8a77 100644
--- a/sdk/typescript/src/runtime.ts
+++ b/sdk/typescript/src/runtime.ts
@@ -20,7 +20,7 @@ import {
type ChildProcessWithoutNullStreams,
type SpawnOptionsWithoutStdio,
} from "node:child_process";
-import { randomUUID } from "node:crypto";
+import { createHash, randomUUID } from "node:crypto";
import { shortArchivePrefix } from "./scan-archive-path.js";
import {
chmodSync,
@@ -41,6 +41,7 @@ import {
lstat,
mkdir,
mkdtemp,
+ open,
readFile,
readdir,
realpath,
@@ -3321,6 +3322,35 @@ export async function bootstrapPlugin(
);
const stagedMatches =
staged?.version === version && (await matchesSelected(stagedRoot));
+ const matchesNativeInstall = async (
+ installedRoot: string,
+ allowRuntimeFiles = false,
+ ) =>
+ (await matchesSelected(stagedRoot)) &&
+ // Ordinary reuse permits runtime files when all selected bytes are present.
+ // The native receipt still verifies the complete generated command output.
+ ((allowRuntimeFiles && (await matchesSelected(installedRoot, true))) ||
+ (await matchesSelected(
+ installedRoot,
+ false,
+ migratedCommands(installedRoot),
+ )));
+ const nativeMigration = (installedRoot: string, allowRuntimeFiles = false) =>
+ verifiedCommandMigration(
+ installedRoot,
+ (path) => matchesNativeInstall(path, allowRuntimeFiles),
+ options.signal,
+ );
+ const matchesReceipt = async (installedRoot: string, migration: unknown) => {
+ if (
+ typeof migration === "string" &&
+ options.runCodex === undefined &&
+ (await isBundledPluginInstaller(command))
+ ) {
+ return (await nativeMigration(installedRoot, true)) === migration;
+ }
+ return matchesSelected(installedRoot, true);
+ };
if (!stagedMatches) {
if (existing !== null) {
@@ -3399,7 +3429,10 @@ export async function bootstrapPlugin(
isRecord(previous) &&
typeof previous["installedPath"] === "string" &&
previous["version"] === version &&
- (await matchesSelected(previous["installedPath"], true))
+ (await matchesReceipt(
+ previous["installedPath"],
+ previous["nativeCommandMigration"],
+ ))
) {
return {
pluginRoot: root,
@@ -3425,13 +3458,7 @@ export async function bootstrapPlugin(
} catch (first) {
if (!isRolledBackPluginActivation(first)) throw first;
throwIfSignalAborted(options.signal);
- let bundled: string;
- try {
- bundled = resolveBundledCodexExecutable();
- } catch {
- throw first;
- }
- if (!(await sameFile(command.command, bundled))) throw first;
+ if (!(await isBundledPluginInstaller(command))) throw first;
// The bundled CLI restored the old cache. A short-lived Windows reader
// of the new tree can release it before a fresh installation attempt.
try {
@@ -3472,22 +3499,28 @@ export async function bootstrapPlugin(
);
}
- if (retried) {
- const installedRoot = installed["installedPath"];
- // The pinned local installer converts plugin commands into skills here.
- const migratedCommands = join(
- installedRoot,
- ".codex-plugin",
- "migrated-command-skills",
+ // The bundled installer removes stale command skills before regenerating them.
+ const bundledInstaller =
+ retried ||
+ (options.runCodex === undefined &&
+ (await isBundledPluginInstaller(command)));
+ const hasSourceMigration =
+ bundledInstaller &&
+ (
+ await lstat(migratedCommands(root)).catch(nullIfMissingFileError)
+ )?.isDirectory() === true;
+ const nativeCommandMigration = hasSourceMigration
+ ? await nativeMigration(installed["installedPath"])
+ : undefined;
+ if (
+ retried &&
+ !(hasSourceMigration
+ ? nativeCommandMigration !== undefined
+ : await matchesNativeInstall(installed["installedPath"]))
+ ) {
+ throw new PluginBootstrapError(
+ "The retried Codex plugin install did not install the selected plugin contents.",
);
- if (
- !(await matchesSelected(stagedRoot)) ||
- !(await matchesSelected(installedRoot, false, migratedCommands))
- ) {
- throw new PluginBootstrapError(
- "The retried Codex plugin install did not install the selected plugin contents.",
- );
- }
}
await writeFile(
@@ -3495,6 +3528,7 @@ export async function bootstrapPlugin(
JSON.stringify({
installedPath: installed["installedPath"],
version,
+ ...(nativeCommandMigration ? { nativeCommandMigration } : {}),
}),
{ mode: 0o600, signal: options.signal },
);
@@ -3509,6 +3543,111 @@ export async function bootstrapPlugin(
};
}
+async function isBundledPluginInstaller(
+ command: CodexCommand,
+): Promise<boolean> {
+ try {
+ return await sameFile(command.command, resolveBundledCodexExecutable());
+ } catch {
+ return false;
+ }
+}
+
+function migratedCommands(plugin: string): string {
+ return join(plugin, ".codex-plugin", "migrated-command-skills");
+}
+
+// Bind a receipt to the generated files observed in this installation.
+// Entry names come from its directories, never from the saved digest.
+async function verifiedCommandMigration(
+ installedRoot: string,
+ matchesSelected: (installedRoot: string) => Promise<boolean>,
+ signal?: AbortSignal,
+): Promise<string | undefined> {
+ const digest = createHash("sha256");
+ const record = (...fields: (string | number)[]) =>
+ digest.update(JSON.stringify(fields)).update("\n");
+ async function visit(path: string, name: string): Promise<boolean> {
+ throwIfSignalAborted(signal);
+ const metadata = await lstat(path, { bigint: true });
+ if (
+ metadata.isSymbolicLink() ||
+ relative(path, await realpath(path)) !== ""
+ )
+ return false;
+ if (metadata.isDirectory()) {
+ record("directory", name);
+ for (const entry of (await readdir(path)).sort()) {
+ const child = join(path, entry);
+ if (!(await visit(child, name ? `${name}/${entry}` : entry)))
+ return false;
+ }
+ return sameInode(metadata, await lstat(path, { bigint: true }));
+ }
+ if (!metadata.isFile()) return false;
+ const file = await open(
+ path,
+ constants.O_RDONLY |
+ (constants.O_NOFOLLOW ?? 0) |
+ (constants.O_NONBLOCK ?? 0),
+ );
+ try {
+ const opened = await file.stat({ bigint: true });
+ if (!opened.isFile()) return false;
+ if (!sameInode(metadata, opened)) {
+ if (process.platform !== "win32") return false;
+ // Some Windows Node versions report a different device on an open handle.
+ const { sameCheckedFileDevice } = await import("./contract.js");
+ if (!(await sameCheckedFileDevice(file, { path, metadata }, opened)))
+ return false;
+ }
+ const contents = createHash("sha256")
+ .update(await file.readFile({ signal }))
+ .digest("hex");
+ record("file", name, Number(opened.mode & 0o111n), contents);
+ } finally {
+ await file.close();
+ }
+ return true;
+ }
+ throwIfSignalAborted(signal);
+ const installation = await lstat(installedRoot, { bigint: true }).catch(
+ nullIfMissingFileError,
+ );
+ if (
+ !installation?.isDirectory() ||
+ installation.ino === 0n ||
+ !(await matchesSelected(installedRoot))
+ )
+ return;
+ try {
+ const directory = migratedCommands(await realpath(installedRoot));
+ const parent = dirname(directory);
+ if (
+ !(await lstat(parent)).isDirectory() ||
+ relative(parent, await realpath(parent)) !== ""
+ )
+ return;
+ const root = await lstat(directory, { bigint: true }).catch(
+ nullIfMissingFileError,
+ );
+ if (root && (!root.isDirectory() || !(await visit(directory, "")))) return;
+ // Native activation replaces the installed directory even at the same version.
+ if (!sameInode(installation, await lstat(installedRoot, { bigint: true })))
+ return;
+ throwIfSignalAborted(signal);
+ return root ? `sha256:${digest.digest("hex")}` : "absent";
+ } catch (error) {
+ throwIfSignalAborted(signal);
+ if (
+ !["ENOENT", "ENOTDIR", "ELOOP", "EISDIR", "EACCES", "EPERM"].includes(
+ nodeErrorCode(error) ?? "",
+ )
+ )
+ throw error;
+ }
+}
+
async function pluginContentsMatch(
source: string,
destination: string,
diff --git a/sdk/typescript/tests-ts/runtime-plugin-activation.test.ts b/sdk/typescript/tests-ts/runtime-plugin-activation.test.ts
index 19d441325..770998524 100644
--- a/sdk/typescript/tests-ts/runtime-plugin-activation.test.ts
+++ b/sdk/typescript/tests-ts/runtime-plugin-activation.test.ts
@@ -1,5 +1,15 @@
import { afterEach, expect, spyOn, test } from "bun:test";
-import { cp, mkdir, readFile, readdir, writeFile } from "node:fs/promises";
+import * as files from "node:fs/promises";
+import {
+ cp,
+ mkdir,
+ readFile,
+ readdir,
+ rename,
+ rm,
+ symlink,
+ writeFile,
+} from "node:fs/promises";
import { join } from "node:path";
import * as timers from "node:timers/promises";
import * as executable from "../src/codex-sdk-environment.js";
@@ -57,6 +67,7 @@ const fakeCli = `
process.exit(step.exit);
}
const installedPath = join(home, "installed");
+ fs.rmSync(installedPath, { recursive: true, force: true });
fs.cpSync(staged, installedPath, { recursive: true });
if (step.stale) fs.writeFileSync(join(installedPath, "marker.txt"), "previous");
if (step.extraSkill === "installed") addSkill(installedPath, "skills/unselected");
@@ -118,6 +129,7 @@ async function fixture(
const command = nodeCommand().command;
return {
command,
+ selected,
previous,
record,
previousReceipt,
@@ -204,10 +216,272 @@ test
result.installedRoot,
);
expect(await f.remaining()).toHaveLength(0);
+ expect((await f.install()).installedRoot).toBe(result.installedRoot);
expect(resolver).toHaveBeenCalled();
},
);
+test("only the bundled installer reuses its regenerated command output", async () => {
+ const f = await fixture([{}, {}, {}], "regenerated");
+ using resolver = spyOn(
+ executable,
+ "resolveBundledCodexExecutable",
+ ).mockReturnValue(f.previous);
+ await f.install();
+ expect(await f.remaining()).toHaveLength(2);
+
+ resolver.mockReturnValue(f.command);
+ const installed = await f.install();
+ expect(await f.remaining()).toHaveLength(1);
+ expect((await f.install()).installedRoot).toBe(installed.installedRoot);
+ expect(await f.remaining()).toHaveLength(1);
+
+ resolver.mockReturnValue(f.previous);
+ await f.install();
+ expect(await f.remaining()).toHaveLength(0);
+});
+
+test("rechecks the full marketplace selection and the recovered installed files", async () => {
+ const f = await fixture([{}, {}, {}], "regenerated");
+ using resolver = spyOn(
+ executable,
+ "resolveBundledCodexExecutable",
+ ).mockReturnValue(f.command);
+ const { installedRoot } = await f.install();
+ const staleSkill = join(
+ f.selected,
+ ".codex-plugin",
+ "migrated-command-skills",
+ "source-command-retired",
+ "SKILL.md",
+ );
+ await writeFile(staleSkill, "revised stale source");
+ await f.install();
+ expect(await f.remaining()).toHaveLength(1);
+
+ await mkdir(join(installedRoot, "skills", "other-selection"), {
+ recursive: true,
+ });
+ await writeFile(
+ join(installedRoot, "skills", "other-selection", "SKILL.md"),
+ "other",
+ );
+ await f.install();
+ expect(await f.remaining()).toHaveLength(0);
+ expect(resolver).toHaveBeenCalled();
+});
+
+test("preserves runtime files when the selection already contains the native command output", async () => {
+ const f = await fixture([{}, {}, {}, {}]);
+ using resolver = spyOn(
+ executable,
+ "resolveBundledCodexExecutable",
+ ).mockReturnValue(f.command);
+ const { installedRoot } = await f.install();
+ const migration = join(".codex-plugin", "migrated-command-skills");
+ const generated = join(installedRoot, migration);
+ await cp(generated, join(f.selected, migration), { recursive: true });
+ await f.install();
+ const receipt = await readFile(f.record, "utf8");
+ expect(JSON.parse(receipt).nativeCommandMigration).toMatch(/^sha256:/u);
+ const bytecode = join(installedRoot, "scripts", "__pycache__");
+ await mkdir(bytecode, { recursive: true });
+ const cached = join(bytecode, "helper.cpython-312.pyc");
+ await writeFile(cached, "runtime-generated");
+
+ expect((await f.install()).installedRoot).toBe(installedRoot);
+ expect(await f.remaining()).toHaveLength(2);
+ expect(await readFile(cached, "utf8")).toBe("runtime-generated");
+ expect(await readFile(f.record, "utf8")).toBe(receipt);
+
+ const skill = join(generated, "source-command-sample", "SKILL.md");
+ await rm(skill);
+ await f.install();
+ expect(await f.remaining()).toHaveLength(1);
+ expect(await readFile(skill, "utf8")).toBe("synthetic skill");
+ await mkdir(join(generated, "foreign-command"));
+ await writeFile(join(generated, "foreign-command", "SKILL.md"), "foreign");
+ await f.install();
+ expect(await f.remaining()).toHaveLength(0);
+ expect(await readdir(generated)).toEqual(["source-command-sample"]);
+ expect(resolver).toHaveBeenCalled();
+});
+
+test("a link replacing the installed command migration directory requires reinstall", async () => {
+ const f = await fixture([{}, {}], "regenerated");
+ using resolver = spyOn(
+ executable,
+ "resolveBundledCodexExecutable",
+ ).mockReturnValue(f.command);
+ const { installedRoot } = await f.install();
+ const migration = join(
+ installedRoot,
+ ".codex-plugin",
+ "migrated-command-skills",
+ );
+ const outside = await temporaryDirectory();
+ await writeFile(join(outside, "marker.txt"), "unrelated");
+ await rm(migration, { recursive: true });
+ await symlink(
+ outside,
+ migration,
+ process.platform === "win32" ? "junction" : "dir",
+ );
+ await f.install();
+ expect(await f.remaining()).toHaveLength(0);
+ expect(await readFile(join(outside, "marker.txt"), "utf8")).toBe("unrelated");
+ expect(resolver).toHaveBeenCalled();
+});
+
+test.each([
+ "missing file",
+ "changed bytes",
+ "renamed skill",
+ "changed file type",
+ "restored source with changed output",
+ "extra output",
+ "missing directory",
+ "nested link",
+ "previously absent",
+] as const)(
+ "repairs %s in a recorded native command migration",
+ async (damage) => {
+ const absent = damage === "previously absent";
+ const f = await fixture([{}, {}], absent ? "removed" : "regenerated");
+ using resolver = spyOn(
+ executable,
+ "resolveBundledCodexExecutable",
+ ).mockReturnValue(f.command);
+ const { installedRoot } = await f.install();
+ const migration = join(
+ installedRoot,
+ ".codex-plugin",
+ "migrated-command-skills",
+ );
+ const skill = join(migration, "source-command-sample");
+ const file = join(skill, "SKILL.md");
+ const outside = await temporaryDirectory();
+ const sentinel = join(outside, "SKILL.md");
+ await writeFile(sentinel, "outside");
+ switch (damage) {
+ case "missing file":
+ await rm(file);
+ break;
+ case "restored source with changed output":
+ await cp(
+ join(f.selected, ".codex-plugin", "migrated-command-skills"),
+ migration,
+ { recursive: true },
+ );
+ await writeFile(file, "different skill");
+ break;
+ case "changed bytes":
+ await writeFile(file, "different skill");
+ break;
+ case "renamed skill":
+ await rename(skill, join(migration, "other-command-name"));
+ break;
+ case "changed file type":
+ await rm(file);
+ await mkdir(file);
+ break;
+ case "extra output":
+ await writeFile(join(skill, "extra.md"), "unselected");
+ break;
+ case "missing directory":
+ await rm(migration, { recursive: true });
+ break;
+ case "nested link":
+ await rm(skill, { recursive: true });
+ await symlink(
+ outside,
+ skill,
+ process.platform === "win32" ? "junction" : "dir",
+ );
+ break;
+ case "previously absent":
+ await mkdir(migration);
+ break;
+ }
+ await f.install();
+ expect(await f.remaining()).toHaveLength(0);
+ if (absent) {
+ await expect(readdir(migration)).rejects.toMatchObject({
+ code: "ENOENT",
+ });
+ } else {
+ expect(await readdir(migration)).toEqual(["source-command-sample"]);
+ expect(await readdir(skill)).toEqual(["SKILL.md"]);
+ expect(await readFile(file, "utf8")).toBe("synthetic skill");
+ }
+ expect(await readFile(sentinel, "utf8")).toBe("outside");
+ expect(resolver).toHaveBeenCalled();
+ },
+);
+
+test("canceling a generated skill read leaves its native receipt and caller reason intact", async () => {
+ const f = await fixture([{}], "regenerated");
+ using resolver = spyOn(
+ executable,
+ "resolveBundledCodexExecutable",
+ ).mockReturnValue(f.command);
+ const { installedRoot } = await f.install();
+ const generated = join(
+ installedRoot,
+ ".codex-plugin",
+ "migrated-command-skills",
+ "source-command-sample",
+ "SKILL.md",
+ );
+ const receipt = await readFile(f.record);
+ const controller = new AbortController();
+ const reason = new Error(
+ "synthetic cancellation while reading generated skill",
+ );
+ const prior = files.open;
+ using opening = spyOn(files, "open").mockImplementation(async (...args) => {
+ const result = await prior(...args);
+ if (String(args[0]) === generated) controller.abort(reason);
+ return result;
+ });
+ await expect(f.install(controller.signal)).rejects.toBe(reason);
+ expect(await f.remaining()).toHaveLength(0);
+ expect(await readFile(f.record)).toEqual(receipt);
+ expect(opening).toHaveBeenCalled();
+ expect(resolver).toHaveBeenCalled();
+});
+
+test("checks the generated output and selected files in the same installation", async () => {
+ const f = await fixture([{}, {}], "regenerated");
+ using resolver = spyOn(
+ executable,
+ "resolveBundledCodexExecutable",
+ ).mockReturnValue(f.command);
+ const { installedRoot } = await f.install();
+ const previous = join(await temporaryDirectory(), "previous");
+ const original = files.realpath;
+ let replaced = false;
+ using resolving = spyOn(files, "realpath").mockImplementation((async (
+ ...args: Parameters<typeof original>
+ ) => {
+ if (!replaced && args[0] === installedRoot) {
+ replaced = true;
+ await rename(installedRoot, previous);
+ await cp(previous, installedRoot, { recursive: true });
+ await writeFile(join(installedRoot, "marker.txt"), "other selection");
+ }
+ return original(...args);
+ }) as typeof original);
+ await f.install();
+ expect(replaced).toBe(true);
+ expect(await f.remaining()).toHaveLength(0);
+ expect(await readFile(join(installedRoot, "marker.txt"), "utf8")).toBe(
+ "selected",
+ );
+ expect(resolving).toHaveBeenCalled();
+ expect(resolver).toHaveBeenCalled();
+});
+
test.skipIf(process.platform !== "win32").each([
[
"persistent denial",