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(runtime): extract plugin ZIPs from the checked descriptor (#1808)

Michael D'Angelo55029e96bb8c
diff --git a/sdk/typescript/src/runtime.ts b/sdk/typescript/src/runtime.ts
index 8a3a13918..45b0e3518 100644
--- a/sdk/typescript/src/runtime.ts
+++ b/sdk/typescript/src/runtime.ts
@@ -12,8 +12,12 @@ import {
import { randomUUID } from "node:crypto";
import {
chmodSync,
+ close as closeCallback,
constants,
createWriteStream,
+ fstat as fstatCallback,
+ open as openCallback,
+ read as readCallback,
type BigIntStats,
type Stats,
} from "node:fs";
@@ -54,7 +58,7 @@ import { promisify } from "node:util";
import { pipeline } from "node:stream/promises";
import { crc32 } from "node:zlib";
import { setTimeout as delay } from "node:timers/promises";
-import { openPromise as openZip } from "yauzl";
+import { fromFdPromise, type ZipFile } from "yauzl";
import { parse } from "smol-toml";
import {
CodexSecurityError,
@@ -2353,11 +2357,10 @@ export async function extractPluginZip(
);
try {
throwIfSignalAborted(signal);
- await rejectBackslashZipNames(archivePath, signal);
let expandedSize = 0;
const paths = new Set<string>();
const checksums: Array<{ path: string; checksum: number }> = [];
- const zip = await openZip(archivePath, { strictFileNames: true });
+ const zip = await openPluginZip(archivePath, signal);
try {
for await (const entry of zip.eachEntry()) {
throwIfSignalAborted(signal);
@@ -2440,19 +2443,23 @@ export async function extractPluginZip(
}
}
-async function rejectBackslashZipNames(
+async function openPluginZip(
path: string,
signal?: AbortSignal,
-): Promise<void> {
- const handle = await open(path, "r");
+): Promise<ZipFile> {
+ const fd = await promisify(openCallback)(
+ path,
+ constants.O_RDONLY |
+ (process.platform === "win32" ? 0 : constants.O_NONBLOCK),
+ );
try {
- const metadata = await handle.stat();
+ const metadata = await promisify(fstatCallback)(fd);
if (!metadata.isFile() || metadata.size < 22) {
throw new Error("missing end of central directory");
}
const tailSize = Math.min(metadata.size, 65_557);
const tail = await readExactly(
- handle,
+ fd,
tailSize,
metadata.size - tailSize,
signal,
@@ -2490,12 +2497,7 @@ async function rejectBackslashZipNames(
if (centralOffset + centralSize > endOffset) {
throw new Error("invalid central directory bounds");
}
- const central = await readExactly(
- handle,
- centralSize,
- centralOffset,
- signal,
- );
+ const central = await readExactly(fd, centralSize, centralOffset, signal);
let offset = 0;
for (let index = 0; index < entries; index += 1) {
if (
@@ -2522,8 +2524,14 @@ async function rejectBackslashZipNames(
if (offset !== central.byteLength) {
throw new Error("invalid central directory size");
}
- } finally {
- await handle.close();
+ // Yauzl owns the descriptor only after construction succeeds.
+ return await fromFdPromise(fd, {
+ autoClose: true,
+ strictFileNames: true,
+ });
+ } catch (error) {
+ await promisify(closeCallback)(fd);
+ throw error;
}
}
@@ -3401,7 +3409,7 @@ async function copyPluginTree(
}
async function readExactly(
- handle: Awaited<ReturnType<typeof open>>,
+ fd: number,
length: number,
position: number,
signal?: AbortSignal,
@@ -3410,7 +3418,8 @@ async function readExactly(
let offset = 0;
while (offset < length) {
throwIfSignalAborted(signal);
- const { bytesRead } = await handle.read(
+ const { bytesRead } = await promisify(readCallback)(
+ fd,
buffer,
offset,
length - offset,
diff --git a/sdk/typescript/tests-ts/fixtures/runtime-zip-descriptor.mjs b/sdk/typescript/tests-ts/fixtures/runtime-zip-descriptor.mjs
new file mode 100644
index 000000000..e16a24c77
--- /dev/null
+++ b/sdk/typescript/tests-ts/fixtures/runtime-zip-descriptor.mjs
@@ -0,0 +1,179 @@
+import assert from "node:assert/strict";
+import { createRequire, syncBuiltinESMExports } from "node:module";
+import { join } from "node:path";
+
+const fs = createRequire(import.meta.url)("node:fs");
+const [root, scenario] = process.argv.slice(2);
+const archive = join(root, "plugin.zip");
+const destination = join(root, "extracted");
+const controller = new AbortController();
+const operationError = new Error("Synthetic ZIP operation failure.");
+const closeError = new Error("Synthetic ZIP close failure.");
+const abortError = new Error("Synthetic ZIP cancellation.");
+const original = {
+ open: fs.open,
+ read: fs.read,
+ close: fs.close,
+ createWriteStream: fs.createWriteStream,
+ promiseOpen: fs.promises.open,
+};
+const descriptors = new Set();
+let replaced = false;
+let injected = false;
+
+if (scenario === "initial-fifo") {
+ fs.renameSync(join(root, "replacement.fifo"), archive);
+} else if (scenario === "constructor-error") {
+ const bytes = fs.readFileSync(archive);
+ bytes.writeUInt16LE(1, bytes.length - 22 + 4);
+ fs.writeFileSync(archive, bytes);
+}
+
+function afterRead(buffer, bytesRead) {
+ if (!injected && (scenario === "read-error" || scenario === "close-error")) {
+ injected = true;
+ throw operationError;
+ }
+ if (
+ !replaced &&
+ scenario.startsWith("replace-") &&
+ bytesRead >= 4 &&
+ buffer.readUInt32LE(0) === 0x02014b50
+ ) {
+ // Move the open archive aside so replacement also works on Windows.
+ fs.renameSync(archive, join(root, "checked.zip"));
+ fs.renameSync(
+ join(
+ root,
+ scenario === "replace-fifo" ? "replacement.fifo" : "replacement.zip",
+ ),
+ archive,
+ );
+ replaced = true;
+ }
+}
+
+// Instrument both APIs so the regression also exercises the previous opener.
+fs.promises.open = async function (path, ...args) {
+ const handle = await original.promiseOpen.call(this, path, ...args);
+ if (path !== archive) return handle;
+ const descriptor = handle.fd;
+ descriptors.add(descriptor);
+ const read = handle.read.bind(handle);
+ const close = handle.close.bind(handle);
+ handle.read = async (...readArgs) => {
+ const result = await read(...readArgs);
+ afterRead(result.buffer, result.bytesRead);
+ return result;
+ };
+ handle.close = async () => {
+ await close();
+ descriptors.delete(descriptor);
+ if (scenario === "close-error") throw closeError;
+ };
+ return handle;
+};
+fs.open = function (path, flags, ...args) {
+ const callback = args.pop();
+ return original.open.call(this, path, flags, ...args, (error, fd) => {
+ if (!error && path === archive) descriptors.add(fd);
+ callback(error, fd);
+ });
+};
+fs.read = function (fd, buffer, ...args) {
+ const callback = args.pop();
+ return original.read.call(
+ this,
+ fd,
+ buffer,
+ ...args,
+ (error, bytesRead, output) => {
+ if (!error && descriptors.has(fd)) {
+ try {
+ afterRead(output, bytesRead);
+ } catch (failure) {
+ callback(failure);
+ return;
+ }
+ }
+ callback(error, bytesRead, output);
+ },
+ );
+};
+for (const symbol of Object.getOwnPropertySymbols(original.read)) {
+ Object.defineProperty(
+ fs.read,
+ symbol,
+ Object.getOwnPropertyDescriptor(original.read, symbol),
+ );
+}
+fs.close = function (fd, callback) {
+ const owned = descriptors.has(fd);
+ return original.close.call(this, fd, (error) => {
+ if (!error) descriptors.delete(fd);
+ callback(owned && scenario === "close-error" ? closeError : error);
+ });
+};
+fs.createWriteStream = function (...args) {
+ const stream = original.createWriteStream.apply(this, args);
+ if (scenario === "abort" || scenario === "write-error") {
+ stream.once("pipe", () => {
+ injected = true;
+ queueMicrotask(() => {
+ if (scenario === "abort") controller.abort(abortError);
+ else stream.destroy(operationError);
+ });
+ });
+ }
+ return stream;
+};
+syncBuiltinESMExports();
+
+const { extractPluginZip } = await import("../../src/runtime.ts");
+let result;
+let failure;
+try {
+ result = await extractPluginZip(archive, destination, controller.signal);
+} catch (error) {
+ failure = error;
+}
+if (scenario.startsWith("replace-")) {
+ if (failure !== undefined) throw failure;
+ assert.equal(replaced, true);
+ const manifest = JSON.parse(
+ fs.readFileSync(join(result, ".codex-plugin/plugin.json"), "utf8"),
+ );
+ assert.equal(manifest.version, "1.2.3");
+} else {
+ assert.ok(failure);
+ assert.equal(fs.existsSync(destination), false);
+ if (scenario === "abort") {
+ assert.equal(failure, abortError);
+ } else {
+ assert.equal(failure.name, "PluginBootstrapError");
+ if (scenario === "initial-fifo") {
+ assert.match(failure.cause.message, /missing end of central directory/);
+ } else if (scenario === "constructor-error") {
+ assert.match(
+ failure.cause.message,
+ /multi-disk zip files are not supported/,
+ );
+ } else {
+ assert.equal(injected, true);
+ assert.equal(
+ failure.cause,
+ scenario === "close-error" ? closeError : operationError,
+ );
+ }
+ }
+}
+assert.equal(
+ fs
+ .readdirSync(root)
+ .some((name) => name.startsWith(".codex-security-plugin-")),
+ false,
+);
+process.once("beforeExit", () => {
+ assert.equal(descriptors.size, 0);
+ process.stdout.write(JSON.stringify({ scenario, passed: true }) + "\n");
+});
diff --git a/sdk/typescript/tests-ts/runtime-zip-descriptor.test.ts b/sdk/typescript/tests-ts/runtime-zip-descriptor.test.ts
new file mode 100644
index 000000000..3c81706e6
--- /dev/null
+++ b/sdk/typescript/tests-ts/runtime-zip-descriptor.test.ts
@@ -0,0 +1,59 @@
+import { spawnSync } from "node:child_process";
+import { writeFile } from "node:fs/promises";
+import { join } from "node:path";
+import { fileURLToPath } from "node:url";
+import { afterEach, expect, test } from "bun:test";
+import { strToU8, zipSync } from "fflate";
+import { createApiTestFixtures } from "./support/temporary-directories.js";
+
+const { temporaryDirectory, cleanup } = createApiTestFixtures("runtime-zip-");
+afterEach(cleanup);
+
+for (const scenario of [
+ "replace-regular",
+ "replace-fifo",
+ "initial-fifo",
+ "constructor-error",
+ "read-error",
+ "close-error",
+ "abort",
+ "write-error",
+]) {
+ test.skipIf(process.platform === "win32" && scenario.includes("fifo"))(
+ `retains and closes the checked plugin ZIP: ${scenario}`,
+ async () => {
+ const root = await temporaryDirectory();
+ for (const [name, version] of [
+ ["plugin.zip", "1.2.3"],
+ ["replacement.zip", "9.8.7"],
+ ] as const) {
+ await writeFile(
+ join(root, name),
+ zipSync({
+ "release/.codex-plugin/plugin.json": strToU8(
+ JSON.stringify({ name: "codex-security", version }),
+ ),
+ }),
+ );
+ }
+ if (scenario.includes("fifo")) {
+ const fifo = spawnSync("mkfifo", [join(root, "replacement.fifo")]);
+ expect(fifo.status, fifo.stderr?.toString()).toBe(0);
+ }
+ const child = spawnSync(
+ process.execPath,
+ [
+ fileURLToPath(
+ new URL("./fixtures/runtime-zip-descriptor.mjs", import.meta.url),
+ ),
+ root,
+ scenario,
+ ],
+ { encoding: "utf8", timeout: 10_000, killSignal: "SIGKILL" },
+ );
+ expect(child.error, child.stderr).toBeUndefined();
+ expect(child.status, child.stderr).toBe(0);
+ expect(JSON.parse(child.stdout)).toEqual({ scenario, passed: true });
+ },
+ );
+}