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(package): prevent stale SDK archives on long Windows paths (#2511)

Michael D'Angelo025fd5be3ed5
diff --git a/sdk/typescript/package.json b/sdk/typescript/package.json
index 908a5f9e5..c2097f980 100644
--- a/sdk/typescript/package.json
+++ b/sdk/typescript/package.json
@@ -73,7 +73,7 @@
"generate:external-import-models": "node scripts/generate-external-import-models.cjs",
"generate:external-import-models:check": "node scripts/generate-external-import-models.cjs --check",
"lint": "tsc --noEmit",
- "prepack": "node --run build && node --run build:plugin",
+ "prepack": "node scripts/check-prepack-path.mjs && node --run build && node --run build:plugin",
"test": "node --run build:plugin && bun test --timeout 30000 ./tests-ts",
"test:ci": "node -e \"require('node:fs').mkdirSync('reports',{recursive:true})\" && pnpm run test --coverage --coverage-reporter=text --coverage-reporter=lcov --reporter=junit --reporter-outfile=reports/junit.xml",
"test:mcp": "node --run build:plugin && pnpm --dir ../../plugins/codex-security/mcp-app run test:mcp",
diff --git a/sdk/typescript/scripts/check-prepack-path.mjs b/sdk/typescript/scripts/check-prepack-path.mjs
new file mode 100644
index 000000000..037613437
--- /dev/null
+++ b/sdk/typescript/scripts/check-prepack-path.mjs
@@ -0,0 +1,10 @@
+import { join } from "node:path";
+
+const manifest = join(process.cwd(), "package.json");
+// Windows Node --run can skip the SDK manifest at MAX_PATH and run an ancestor's build.
+if (process.platform === "win32" && manifest.length >= 260) {
+ console.error(
+ `Cannot pack the SDK from ${manifest}. Windows Node --run may select an ancestor package.json at this length. Use a shorter checkout path.`,
+ );
+ process.exitCode = 1;
+}
diff --git a/sdk/typescript/tests-ts/package-tar-listing.test.ts b/sdk/typescript/tests-ts/package-tar-listing.test.ts
index 2a6aa1d16..80badc42f 100644
--- a/sdk/typescript/tests-ts/package-tar-listing.test.ts
+++ b/sdk/typescript/tests-ts/package-tar-listing.test.ts
@@ -2,6 +2,7 @@ import { execFile, spawnSync } from "node:child_process";
import { createHash } from "node:crypto";
import {
chmodSync,
+ copyFileSync,
existsSync,
mkdirSync,
mkdtempSync,
@@ -279,6 +280,94 @@ function runPackageCommand(
}
describe("npm package tar listings", () => {
+ const prefix = join(realpathSync(tmpdir()), "p");
+ const shortestManifest = join(`${prefix}XXXXXX`, "p", "package.json");
+ for (const length of [259, 260]) {
+ test.skipIf(shortestManifest.length > length)(
+ `packs the SDK's fresh output or fails before running an ancestor at manifest length ${length}`,
+ async () => {
+ const root = mkdtempSync(prefix);
+ try {
+ const sdk = join(
+ root,
+ "p".repeat(length - join(root, "package.json").length - 1),
+ );
+ const manifest = join(sdk, "package.json");
+ expect(manifest.length).toBe(length);
+ const output = join(root, "packages");
+ mkdirSync(output);
+ mkdirSync(join(sdk, "scripts"), { recursive: true });
+ mkdirSync(join(sdk, "dist"));
+ copyFileSync(
+ new URL("../scripts/check-prepack-path.mjs", import.meta.url),
+ join(sdk, "scripts", "check-prepack-path.mjs"),
+ );
+ writeFileSync(join(sdk, "dist", "built.txt"), "stale");
+ writeFileSync(
+ manifest,
+ JSON.stringify({
+ name: "sdk-prepack-fixture",
+ version: "1.0.0",
+ files: ["dist"],
+ scripts: {
+ prepack: packageJson.scripts.prepack,
+ build: `node -e "require('node:fs').writeFileSync('dist/built.txt','sdk')"`,
+ "build:plugin": `node -e "require('node:fs').appendFileSync('dist/built.txt','+plugin')"`,
+ },
+ }),
+ );
+ writeFileSync(
+ join(root, "package.json"),
+ JSON.stringify({
+ private: true,
+ scripts: {
+ build: `node -e "require('node:fs').writeFileSync('ancestor-ran','yes')"`,
+ "build:plugin": "node --version",
+ },
+ }),
+ );
+ const npm = await (fixtureNpm ??= resolveFixtureNpm());
+ const result = await runPackageCommand(
+ sdk,
+ npm.command,
+ [
+ ...npm.args,
+ "pack",
+ "--offline",
+ "--foreground-scripts",
+ "--ignore-scripts=false",
+ "--pack-destination",
+ output,
+ ],
+ {
+ ...process.env,
+ npm_config_cache: join(root, "npm-cache"),
+ npm_config_userconfig: join(root, "empty-npmrc"),
+ },
+ );
+ expect(existsSync(join(root, "ancestor-ran"))).toBe(false);
+ if (process.platform === "win32" && length === 260) {
+ expect(result.status).not.toBe(0);
+ expect(result.stderr).toContain("Use a shorter checkout path");
+ expect(readdirSync(output)).toEqual([]);
+ } else {
+ expect(result.status, result.stderr).toBe(0);
+ const archives = readdirSync(output);
+ expect(archives).toHaveLength(1);
+ const { stdout } = await execFileAsync("tar", [
+ "-xOf",
+ join(output, archives[0]!),
+ "package/dist/built.txt",
+ ]);
+ expect(stdout).toBe("sdk+plugin");
+ }
+ } finally {
+ rmSync(root, { recursive: true, force: true });
+ }
+ },
+ );
+ }
+
test("accepts regular entries with Unix or Windows line endings", () => {
const file = "-rw-r--r-- package/package.json";
const directory = "drwxr-xr-x package/dist/";
diff --git a/sdk/typescript/tests-ts/skeleton.test.ts b/sdk/typescript/tests-ts/skeleton.test.ts
index d6204c9f7..598b7011a 100644
--- a/sdk/typescript/tests-ts/skeleton.test.ts
+++ b/sdk/typescript/tests-ts/skeleton.test.ts
@@ -507,7 +507,7 @@ describe("TypeScript package skeleton", () => {
"node scripts/check-plugin-source.mjs",
);
expect(packageJson.scripts.prepack).toBe(
- "node --run build && node --run build:plugin",
+ "node scripts/check-prepack-path.mjs && node --run build && node --run build:plugin",
);
expect(packageJson.scripts.types).not.toContain("check:plugin-source");
expect(packageJson.scripts["audit:prod"]).toBe(