OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security
TypeScript ai-securityapplication-securityclicode-scanningcodexcodex-securitycybersecuritydevsecopsnodejsnpmopenaisdksecuritytypescriptvulnerability-scanning
import { nodeCommand } from "./support/shell.js";
import { chmod, mkdir, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { setTimeout as delay } from "node:timers/promises";
import { pathToFileURL } from "node:url";
import { afterEach, describe, expect, test, mock } from "bun:test";
import {
accountStatus,
CodexLoginHandle,
loginApiKey,
logout,
readCodexHomeConfig,
} from "../src/auth.js";
import { PluginBootstrapError } from "../src/index.js";
import { runCodexCommand } from "../src/runtime.js";
import { createApiTestFixtures } from "./support/temporary-directories.js";
import { throwing } from "./support/errors.js";
const { temporaryDirectory, cleanup } = createApiTestFixtures(
"codex-security-auth-",
false,
);
afterEach(cleanup);
async function fakeCodex() {
const root = await temporaryDirectory();
const script = join(root, "codex.mjs");
await writeFile(
script,
`
import { basename } from "node:path";
const args = [basename(process.argv[1]), ...process.argv.slice(2)];
if (args.join(" ") === "login --with-api-key") {
const input = await process.stdin.reduce((text, chunk) => text + chunk, "");
if (input.trim() !== "secret-key") {
console.error("wrong key");
process.exitCode = 2;
}
} else if (args.join(" ") === "login status") {
console.log("Logged in using ChatGPT");
} else if (args[0] === "login") {
console.error("Listening on http://localhost:1455.");
console.error("Listening on http://localhost.:1455.");
console.error("Listening on http://callback.localhost:1455.");
console.error("Listening on http://127.0.0.2:1455.");
console.error("Listening on http://2130706433:1455.");
console.error("Listening on http://0x7f000001:1455.");
console.error("Listening on http://0.0.0.0:1455.");
console.error("Listening on http://[::1]:1455.");
console.error("Listening on http://[::]:1455.");
console.error("Listening on http://[::ffff:127.0.0.1]:1455.");
console.error("Listening on http://[::ffff:0.0.0.0]:1455.");
console.error("Listening on http://[::127.0.0.1]:1455.");
console.error('Open "\\u001b[32mhttps://127.auth.example.test/device\\u001b[0m"');
console.error("Enter this one-time code");
console.error("\\u001b[36m8356-V2EGR\\u001b[0m");
} else if (args.join(" ") !== "logout") {
console.error("unexpected args: " + args.join(" "));
process.exitCode = 3;
}
process.exit(process.exitCode ?? 0);
`,
);
return {
command: nodeCommand(),
environment: {
...process.env,
NODE_OPTIONS: `--import=${pathToFileURL(script).href}`,
},
};
}
describe("Codex authentication process boundary", () => {
test("preserves configuration parse details and allows absent configuration", async () => {
const home = await temporaryDirectory();
const environment = { CODEX_HOME: home };
await expect(readCodexHomeConfig(environment)).resolves.toEqual({});
await writeFile(
join(home, "config.toml"),
'[model_providers.synthetic\nwire_api = "responses"\n',
);
await expect(readCodexHomeConfig(environment)).rejects.toMatchObject({
name: "CodexSecurityError",
message: expect.stringContaining("illegal character in key"),
cause: expect.objectContaining({
message: expect.stringContaining("model_providers.synthetic"),
}),
});
const controller = new AbortController();
const canceled = new Error("Synthetic configuration cancellation");
controller.abort(canceled);
await expect(
readCodexHomeConfig(environment, controller.signal),
).rejects.toBe(canceled);
});
test.skipIf(process.platform === "win32" || process.getuid?.() === 0)(
"preserves native configuration permission failures",
async () => {
const home = await temporaryDirectory();
const file = join(home, "config.toml");
await writeFile(file, 'model = "synthetic"\n', { mode: 0o000 });
try {
await expect(
readCodexHomeConfig({ CODEX_HOME: home }),
).rejects.toMatchObject({
message: expect.stringContaining("EACCES"),
cause: expect.objectContaining({ code: "EACCES", path: file }),
});
} finally {
await chmod(file, 0o600);
}
},
);
test("persists API keys through the exact public Codex executable", async () => {
const { command, environment } = await fakeCodex();
await expect(loginApiKey(command, environment, "")).rejects.toBeInstanceOf(
PluginBootstrapError,
);
await expect(
loginApiKey(command, environment, "secret-key"),
).resolves.toMatchObject({
success: true,
exitCode: 0,
});
});
test("handles a child closing API-key stdin before the write completes", async () => {
const root = await temporaryDirectory("codex-security-auth-epipe-");
const script = join(root, "exit.mjs");
await writeFile(script, "process.exit(1);\n");
const result = await runCodexCommand(
{ command: process.execPath },
[script],
process.env,
"x".repeat(16 * 1024 * 1024),
);
expect(result).toMatchObject({ success: false, exitCode: 1 });
});
test("retains large noninteractive authentication output", async () => {
const root = await temporaryDirectory("codex-security-auth-output-");
const output = "verbose authentication output ".repeat(3_000);
for (const stream of ["stdout", "stderr"] as const) {
const script = join(root, `${stream}.mjs`);
await writeFile(
script,
`process.${stream}.write(${JSON.stringify(output)}, () => process.exit(0));\n`,
);
const result = await runCodexCommand(
{ command: process.execPath },
[script],
process.env,
);
expect(result.success).toBe(true);
expect(result[stream]).toBe(output);
}
});
test("reports account state and performs logout", async () => {
const { command, environment } = await fakeCodex();
await expect(accountStatus(command, environment)).resolves.toMatchObject({
authenticated: true,
details: "Logged in using ChatGPT",
});
await expect(logout(command, environment)).resolves.toBeUndefined();
});
test("captures quoted interactive login metadata and completion", async () => {
const { command, environment } = await fakeCodex();
const observeSucceeded = mock();
const handle = new CodexLoginHandle(
command,
["login", "--device-auth"],
environment,
observeSucceeded,
);
await expect(handle.wait()).resolves.toMatchObject({ success: true });
expect(handle.loginId).toBeNull();
expect(handle.verificationUrl).toBe("https://127.auth.example.test/device");
expect(handle.userCode).toBe("8356-V2EGR");
expect(observeSucceeded).toHaveBeenCalled();
});
test.each([
["Open https://[2001:db8::1].", "https://[2001:db8::1]"],
[
"Open [https://auth.example.test/device]",
"https://auth.example.test/device",
],
["Open [https://[2001:db8::]]", "https://[2001:db8::]"],
[
"Open [https://auth.example.test/device.]",
"https://auth.example.test/device",
],
["Open [https://[2001:db8::3].]", "https://[2001:db8::3]"],
[
"Open [https://[2001:db8::4]/device?challenge=ABCD.]",
"https://[2001:db8::4]/device?challenge=ABCD",
],
["Open [https://[2001:db8::5]:8443]", "https://[2001:db8::5]:8443"],
["Open [https://[::1]]", null],
["Open [https://[::ffff:127.0.0.1]]", null],
["Open http://[2001:db8::1]", null],
] as const)(
"distinguishes IPv6 host brackets from surrounding punctuation: %s",
async (output, expected) => {
const handle = new CodexLoginHandle(
nodeCommand(),
["-e", "console.error(process.argv[1])", output],
{},
() => {},
);
await expect(handle.wait()).resolves.toMatchObject({ success: true });
expect(handle.verificationUrl).toBe(expected);
},
);
test.each(["sync", "async"])(
"rejects wait when required login completion fails: %s",
async (mode) => {
const { command, environment } = await fakeCodex();
const failure = new Error("Synthetic credential persistence failed");
const handle = new CodexLoginHandle(
command,
["login", "--device-auth"],
environment,
() => {
if (mode === "sync") throw failure;
return Promise.reject(failure);
},
);
await expect(handle.wait()).rejects.toBe(failure);
},
);
test.each(["User code: RIGHT-CODE", "Code: RIGHT-CODE", "RIGHT-CODE"])(
"ignores URL parameters when reading device instructions: %s",
async (instruction) => {
const root = await temporaryDirectory("codex-security-auth-code-");
const script = join(root, "login.mjs");
const url = "https://auth.example.test/device?code=WRONG-CODE";
await writeFile(
script,
`process.stderr.write(${JSON.stringify(`Open ${url}\n${instruction}\n`)}, () => process.exit(0));\n`,
);
const handle = new CodexLoginHandle(
nodeCommand(),
[script],
process.env,
() => {},
);
await expect(handle.wait()).resolves.toMatchObject({ success: true });
expect(handle.verificationUrl).toBe(url);
expect(handle.userCode).toBe("RIGHT-CODE");
},
);
test.each([
["HTTP-only output", "", null],
[
"HTTP followed by HTTPS",
"Open https://auth.example.test/device\n",
"https://auth.example.test/device",
],
] as const)(
"ignores external plaintext HTTP authentication URLs: %s",
async (_description, httpsOutput, verificationUrl) => {
const root = await temporaryDirectory("codex-security-auth-http-");
const script = join(root, "login.mjs");
await writeFile(
script,
`process.stderr.write(${JSON.stringify(`Open http://auth.example.test/device\n${httpsOutput}User code: ABCD-EFGH\n`)}, () => process.exit(0));\n`,
);
const handle = new CodexLoginHandle(
nodeCommand(),
[script],
process.env,
() => {},
);
await expect(handle.wait()).resolves.toMatchObject({ success: true });
expect(handle.verificationUrl).toBe(verificationUrl);
expect(handle.userCode).toBe("ABCD-EFGH");
},
);
test("reads login URLs from hyperlinks terminated with ST", async () => {
const url = "https://auth.example.test/device";
const st = "\u001b\\";
const link = `\u001b]8;;https://link.example.test/${st}${url}\u001b]8;;${st}`;
const output = `Open ${link}\nUser code: ABCD-EFGH\n`;
const handle = new CodexLoginHandle(
nodeCommand(),
["-e", "process.stderr.write(process.argv[1])", output],
process.env,
() => {},
);
await expect(handle.wait()).resolves.toMatchObject({ success: true });
expect(handle.verificationUrl).toBe(url);
expect(handle.userCode).toBe("ABCD-EFGH");
});
test("retains large interactive output and login instructions", async () => {
const root = await temporaryDirectory("codex-security-auth-output-");
const script = join(root, "login.mjs");
const output = "verbose authentication output ".repeat(3_000);
await writeFile(
script,
`
console.error("Open https://auth.example.test/device");
console.error("User code: ABCD-EFGH");
setTimeout(() => {
process.stderr.write(${JSON.stringify(output)}, () => process.exit(0));
}, 10);
`,
);
const observeSucceeded = mock();
const handle = new CodexLoginHandle(
{ command: process.execPath },
[script, "login", "--device-auth"],
process.env,
observeSucceeded,
);
await handle.waitForInstructions({ deviceCode: true });
expect(handle.verificationUrl).toBe("https://auth.example.test/device");
expect(handle.userCode).toBe("ABCD-EFGH");
const result = await handle.wait();
expect(result).toMatchObject({ success: true, exitCode: 0, stdout: "" });
expect(result.stderr).toContain(output);
expect(observeSucceeded).toHaveBeenCalled();
});
test("drains native login stderr before resolving authentication", async () => {
const root = await temporaryDirectory("codex-security-auth-stderr-");
const script = join(root, "login-stderr.mjs");
const message = "network timeout while authenticating";
await writeFile(
script,
`process.stderr.write(${JSON.stringify(`${message}\n`)}, (error) => process.exit(error ? 2 : 1));\n`,
);
const handle = new CodexLoginHandle(
{ command: process.execPath },
[script, "login"],
process.env,
() => {},
);
await expect(handle.waitForInstructions()).rejects.toThrow(message);
await expect(handle.wait()).resolves.toMatchObject({
success: false,
exitCode: 1,
stderr: expect.stringContaining(message),
});
});
test.skipIf(process.platform === "win32")(
"drains inherited stderr before resolving interactive login",
async () => {
const root = await temporaryDirectory("codex-security-auth-drain-");
const script = join(root, "inherited-stderr.mjs");
const ready = join(root, "grandchild-ready");
const release = join(root, "release-grandchild");
const message = "network timeout while authenticating";
const grandchildScript = `
import { existsSync, writeFileSync, writeSync } from "node:fs";
const ready = process.argv[1];
const release = process.argv[2];
const parentPid = Number(process.argv[3]);
const timeout = setTimeout(() => process.exit(1), 10_000);
const watcher = setInterval(() => {
if (!existsSync(release)) return;
try {
process.kill(parentPid, 0);
return;
} catch (error) {
if (error?.code !== "ESRCH") {
clearInterval(watcher);
clearTimeout(timeout);
process.exit(1);
}
}
clearInterval(watcher);
clearTimeout(timeout);
writeSync(2, ${JSON.stringify(`${message}\n`)});
process.exit(0);
}, 25);
writeFileSync(ready, "ready");
`;
await writeFile(
script,
`
import { spawn } from "node:child_process";
import { existsSync, writeFileSync } from "node:fs";
const ready = ${JSON.stringify(ready)};
const release = ${JSON.stringify(release)};
const grandchild = spawn(
process.execPath,
["-e", ${JSON.stringify(grandchildScript)}, ready, release, String(process.pid)],
{ stdio: ["ignore", "ignore", "inherit"], windowsHide: true },
);
const readyTimeout = setTimeout(() => {
clearInterval(readyWatcher);
grandchild.kill();
console.error("Timed out waiting for the login grandchild.");
process.exit(1);
}, 10_000);
const readyWatcher = setInterval(() => {
if (!existsSync(ready)) return;
clearInterval(readyWatcher);
clearTimeout(readyTimeout);
writeFileSync(release, "released");
process.exit(1);
}, 25);
grandchild.once("error", (error) => {
clearInterval(readyWatcher);
clearTimeout(readyTimeout);
console.error(error.message);
process.exit(1);
});
`,
);
const handle = new CodexLoginHandle(
{ command: process.execPath },
[script, "login"],
process.env,
() => {},
);
await expect(handle.waitForInstructions()).rejects.toThrow(message);
await expect(handle.wait()).resolves.toMatchObject({
success: false,
exitCode: 1,
stderr: expect.stringContaining(message),
});
},
);
test("escalates cancellation when a login child ignores SIGTERM", async () => {
const root = await temporaryDirectory("codex-security-auth-sigkill-");
const script = join(root, "codex.mjs");
await writeFile(
script,
`
console.error("Open https://auth.example.test/device");
console.error("User code: ABCD-EFGH");
process.on("SIGTERM", () => {});
setInterval(() => {}, 1000);
`,
);
const observeSucceeded = mock();
const handle = new CodexLoginHandle(
{ command: process.execPath },
[script, "login", "--device-auth"],
process.env,
observeSucceeded,
);
await handle.waitForInstructions({ deviceCode: true });
handle.cancel();
await expect(
Promise.race([
handle.wait(),
delay(5_000).then(throwing("Login cancellation did not settle.")),
]),
).resolves.toMatchObject({ success: false });
expect(observeSucceeded).not.toHaveBeenCalled();
});
test.skipIf(process.platform === "win32")(
"cancels login after the parent exits while a descendant holds stderr",
async () => {
const root = await temporaryDirectory("codex-security-auth-exited-");
const script = join(root, "login.mjs");
const ready = join(root, "ready");
const release = join(root, "release");
const descendant = `
import { existsSync, writeFileSync } from "node:fs";
const [parent, ready, release] = process.argv.slice(1);
setTimeout(() => process.exit(1), 10_000);
let announced = false;
setInterval(() => {
if (existsSync(release)) process.exit(0);
if (announced) return;
try { process.kill(Number(parent), 0); return; }
catch (error) { if (error.code !== "ESRCH") process.exit(1); }
announced = true;
console.error("Open https://auth.example.test/device");
console.error("User code: ABCD-EFGH");
}, 25);
writeFileSync(ready, "ready");
`;
await writeFile(
script,
`
import { spawn } from "node:child_process";
import { existsSync } from "node:fs";
const child = spawn(process.execPath, ["-e", ${JSON.stringify(descendant)}, String(process.pid), ${JSON.stringify(ready)}, ${JSON.stringify(release)}], {
stdio: ["ignore", "ignore", "inherit"], windowsHide: true,
});
child.on("error", () => process.exit(1));
setInterval(() => { if (existsSync(${JSON.stringify(ready)})) process.exit(0); }, 25);
setTimeout(() => { child.kill(); process.exit(1); }, 10_000);
`,
);
let succeeded = false;
const handle = new CodexLoginHandle(
nodeCommand(),
[script],
process.env,
() => {
succeeded = true;
},
);
const deadline = new AbortController();
try {
await handle.waitForInstructions({ deviceCode: true });
handle.cancel();
await expect(
Promise.race([
handle.wait(),
delay(5_000, undefined, { signal: deadline.signal }).then(() => {
throw new Error("Canceled login waited for inherited stderr.");
}),
]),
).resolves.toMatchObject({ success: false, exitCode: 0 });
expect(succeeded).toBe(false);
} finally {
deadline.abort();
await writeFile(release, "released");
await handle.wait();
}
},
);
test("does not report a canceled interactive login as successful", async () => {
const root = await temporaryDirectory("codex-security-auth-cancel-");
const script = join(root, "codex.mjs");
await writeFile(
script,
`
console.error("Open https://auth.example.test/device");
console.error("User code: ABCD-EFGH");
process.on("SIGTERM", () => process.exit(0));
setInterval(() => {}, 1000);
`,
);
const observeSucceeded = mock();
const handle = new CodexLoginHandle(
{ command: process.execPath },
[script, "login", "--device-auth"],
process.env,
observeSucceeded,
);
await handle.waitForInstructions({ deviceCode: true });
handle.cancel();
await expect(handle.wait()).resolves.toMatchObject({ success: false });
expect(observeSucceeded).not.toHaveBeenCalled();
});
});
test("retains the Codex configuration path and parsing error", async () => {
const root = join(
await temporaryDirectory(),
"configuration-\u009b-sk-proj-SYNTHETIC_SECRET",
);
await mkdir(root);
const path = join(root, "config.toml");
await writeFile(path, "invalid = [");
try {
await readCodexHomeConfig({ CODEX_HOME: root });
throw new Error("Expected invalid configuration to fail");
} catch (error) {
expect(String(error)).toContain(path);
expect(String(error)).toContain("Invalid TOML");
}
});