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
test: reuse command helper across CLI and release checks (#739)
mldangelo-oaiabe3a4fdb771
diff --git a/sdk/typescript/tests-ts/api-credentials.test.ts b/sdk/typescript/tests-ts/api-credentials.test.tsindex 148b69656..ae54415ae 100644--- a/sdk/typescript/tests-ts/api-credentials.test.ts+++ b/sdk/typescript/tests-ts/api-credentials.test.ts@@ -311,12 +311,14 @@ describe("CodexSecurity orchestration", () => {startThread: () => ({id: null,async runStreamed() {- expect(- existsSync(- join(credentialHome, ".codex-security-scan.lock"),- ),- ).toBe(false);- if (++scansStarted === 2) releaseScans();+ if (++scansStarted === 2) {+ expect(+ existsSync(+ join(credentialHome, ".codex-security-scan.lock"),+ ),+ ).toBe(false);+ releaseScans();+ }const credentialConfig = parseToml(await readFile(join(credentialHome, "config.toml"),diff --git a/sdk/typescript/tests-ts/api.test.ts b/sdk/typescript/tests-ts/api.test.tsindex e258155f5..af27c2b5a 100644--- a/sdk/typescript/tests-ts/api.test.ts+++ b/sdk/typescript/tests-ts/api.test.ts@@ -6673,9 +6673,10 @@ if ([basename(process.argv[1]), ...process.argv.slice(2)].join(" ") !== "login sconst codexHome = join(root, "codex-home");const fakeCodex = join(root, "codex.mjs");await mkdir(codexHome, { mode: 0o700 });+ // Keep --import pending so Node cannot exit while resolving the login argument.await writeFile(fakeCodex,- 'console.error("Open https://auth.example.test/device");\nconsole.error("User code: ABCD-EFGH");\nprocess.on("SIGTERM", () => {});\nsetInterval(() => {}, 1000);\n',+ 'process.on("SIGTERM", () => {});\nconsole.error("Open https://auth.example.test/device");\nconsole.error("User code: ABCD-EFGH");\nsetInterval(() => {}, 1000);\nawait new Promise(() => {});\n',);const fakeCommand = nodeCodex(fakeCodex);const client = new TestClient(diff --git a/sdk/typescript/tests-ts/attack-path-tool-name.test.ts b/sdk/typescript/tests-ts/attack-path-tool-name.test.tsindex c11a599f2..48dbafa1d 100644--- a/sdk/typescript/tests-ts/attack-path-tool-name.test.ts+++ b/sdk/typescript/tests-ts/attack-path-tool-name.test.ts@@ -3,6 +3,7 @@ import { tmpdir } from "node:os";import { join } from "node:path";import { expect, test } from "bun:test";import { PLUGIN_ROOT } from "./plugin-root.js";+import { runCommand } from "./support/shell.js";test("keeps model-visible attack-path tool names within the Codex limit", async () => {const node = Bun.which("node");@@ -10,26 +11,20 @@ test("keeps model-visible attack-path tool names within the Codex limit", asyncconst state = mkdtempSync(join(tmpdir(), "codex-security-mcp-tools-"));try {- const server = Bun.spawn({- cmd: [node!, join(PLUGIN_ROOT, "mcp", "server.mjs")],- env: { ...process.env, CODEX_SECURITY_STATE_DIR: state },- stdin: new TextEncoder().encode(- [+ const { status, stdout, stderr } = await runCommand(+ node!,+ [join(PLUGIN_ROOT, "mcp", "server.mjs")],+ {+ env: { ...process.env, CODEX_SECURITY_STATE_DIR: state },+ input: ['{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-11-25","capabilities":{},"clientInfo":{"name":"codex-security-test","version":"1.0.0"}}}','{"jsonrpc":"2.0","method":"notifications/initialized","params":{}}','{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}',"",].join("\n"),- ),- stdout: "pipe",- stderr: "pipe",- timeout: 30_000,- });- const [status, stdout, stderr] = await Promise.all([- server.exited,- new Response(server.stdout).text(),- new Response(server.stderr).text(),- ]);+ timeout: 30_000,+ },+ );expect(status, stderr).toBe(0);const tools = stdoutdiff --git a/sdk/typescript/tests-ts/cli-launcher.test.ts b/sdk/typescript/tests-ts/cli-launcher.test.tsindex 5a0e6df2b..35acc1c53 100644--- a/sdk/typescript/tests-ts/cli-launcher.test.ts+++ b/sdk/typescript/tests-ts/cli-launcher.test.ts@@ -1,4 +1,3 @@-import { spawnSync } from "node:child_process";import {copyFile,mkdir,@@ -14,6 +13,7 @@ import { pathToFileURL } from "node:url";import { describe, expect, test } from "bun:test";import { VERSION } from "../src/index.js";import { SYNTHETIC_CREDENTIALS } from "./cli-fixtures.js";+import { runCommand } from "./support/shell.js";const packageRoot = join(import.meta.dir, "..");@@ -27,18 +27,11 @@ describe("CLI launcher", () => {if (process.platform !== "win32") {await symlink(launcher, bin);}- const child = Bun.spawn({- cmd: [process.execPath, bin, "--version"],- stdin: "ignore",- stdout: "pipe",- stderr: "pipe",- timeout: 30_000,- });- const [status, stdout, stderr] = await Promise.all([- child.exited,- new Response(child.stdout).text(),- new Response(child.stderr).text(),- ]);+ const { status, stdout, stderr } = await runCommand(+ process.execPath,+ [bin, "--version"],+ { timeout: 30_000 },+ );expect(status, stderr).toBe(0);expect(stderr).toBe("");@@ -56,24 +49,11 @@ describe("CLI launcher", () => {preload,`Object.defineProperty(process, "cwd", { value() { throw new Error(${JSON.stringify(`working directory is unavailable: ${SYNTHETIC_CREDENTIALS}`)}); } });\n`,);- const child = Bun.spawn({- cmd: [- process.execPath,- "--preload",- preload,- join(packageRoot, "src", "cli.ts"),- "scan",- ],- stdin: "ignore",- stdout: "pipe",- stderr: "pipe",- timeout: 30_000,- });- const [status, stdout, stderr] = await Promise.all([- child.exited,- new Response(child.stdout).text(),- new Response(child.stderr).text(),- ]);+ const { status, stdout, stderr } = await runCommand(+ process.execPath,+ ["--preload", preload, join(packageRoot, "src", "cli.ts"), "scan"],+ { timeout: 30_000 },+ );expect(status, stderr).toBe(2);expect(stdout).toBe("");@@ -98,8 +78,7 @@ describe("CLI launcher", () => {join(root, "dist", "cli.js"),`throw new Error(${JSON.stringify(`failed ${SYNTHETIC_CREDENTIALS}`)});\n`,);- const child = spawnSync("node", [launcher], {- encoding: "utf8",+ const child = await runCommand("node", [launcher], {env: { ...process.env, NODE_NO_WARNINGS: "1" },timeout: 30_000,});@@ -119,7 +98,7 @@ describe("CLI launcher", () => {try {const installed = join(root, "node_modules", "@openai", "codex-security");const dist = join(installed, "dist");- const build = spawnSync(+ const build = await runCommand("node",[join(packageRoot, "node_modules", "typescript", "bin", "tsc"),@@ -128,9 +107,8 @@ describe("CLI launcher", () => {"--outDir",dist,],- { cwd: packageRoot, encoding: "utf8", timeout: 30_000 },+ { cwd: packageRoot, timeout: 30_000 },);- expect(build.error).toBeUndefined();expect(build.status).toBe(0);expect(build.stderr).toBe("");@@ -167,8 +145,7 @@ describe("CLI launcher", () => {"--preserve-symlinks-main --no-experimental-detect-module",NODE_USE_ENV_PROXY: undefined,};- const child = spawnSync("node", [bin, "--version"], {- encoding: "utf8",+ const child = await runCommand("node", [bin, "--version"], {env: launchEnvironment,timeout: 30_000,});@@ -192,11 +169,10 @@ describe("CLI launcher", () => {"});\n",].join("\n"),);- const failed = spawnSync(+ const failed = await runCommand("node",["--import", pathToFileURL(preload).href, bin, "scan"],{- encoding: "utf8",env: launchEnvironment,timeout: 30_000,},diff --git a/sdk/typescript/tests-ts/cli-serve.test.ts b/sdk/typescript/tests-ts/cli-serve.test.tsindex c6a017199..8de6bce04 100644--- a/sdk/typescript/tests-ts/cli-serve.test.ts+++ b/sdk/typescript/tests-ts/cli-serve.test.ts@@ -7,6 +7,7 @@ import { createInterface } from "node:readline";import { expect, test } from "bun:test";import { main } from "../src/cli.js";import { capture, dependencies } from "./cli-fixtures.js";+import { runCommand } from "./support/shell.js";const packageRoot = join(import.meta.dir, "..");const cli = join(packageRoot, "src", "cli.ts");@@ -79,19 +80,14 @@ test("serve reports startup failures with a nonzero exit", async () => {try {const state = join(root, "not-a-directory");await writeFile(state, "synthetic file");- const child = Bun.spawn({- cmd: [process.execPath, cli, "serve"],- env: { ...process.env, CODEX_SECURITY_STATE_DIR: state },- stdin: "ignore",- stdout: "pipe",- stderr: "pipe",- timeout: 20_000,- });- const [status, stdout, stderr] = await Promise.all([- child.exited,- new Response(child.stdout).text(),- new Response(child.stderr).text(),- ]);+ const { status, stdout, stderr } = await runCommand(+ process.execPath,+ [cli, "serve"],+ {+ env: { ...process.env, CODEX_SECURITY_STATE_DIR: state },+ timeout: 20_000,+ },+ );expect(status, stderr).toBe(1);expect(stdout).toBe("");expect(stderr).toContain("Could not access the findings database");diff --git a/sdk/typescript/tests-ts/cli.test.ts b/sdk/typescript/tests-ts/cli.test.tsindex 7a9a6ef42..dcb5d2545 100644--- a/sdk/typescript/tests-ts/cli.test.ts+++ b/sdk/typescript/tests-ts/cli.test.ts@@ -1,4 +1,3 @@-import { execFileSync, spawnSync } from "node:child_process";import {mkdir,mkdtemp,@@ -56,6 +55,7 @@ import {fakeResult,} from "./cli-fixtures.js";import { PLUGIN_ROOT } from "./plugin-root.js";+import { runCommand } from "./support/shell.js";const DEFAULT_SCAN_MODEL_CONFIGURATION =scanModelConfiguration(DEFAULT_CODEX_CONFIG);@@ -79,14 +79,18 @@ async function multiscanInventory(root: string): Promise<void> {"initial",],]) {- expect(spawnSync("git", args, { encoding: "utf8" }).status).toBe(0);+ const result = await runCommand("git", args, { timeout: 10_000 });+ expect(result.status, result.stderr).toBe(0);}- const revision = spawnSync("git", ["-C", repository, "rev-parse", "HEAD"], {- encoding: "utf8",- }).stdout.trim();+ const { status, stdout, stderr } = await runCommand(+ "git",+ ["-C", repository, "rev-parse", "HEAD"],+ { timeout: 10_000 },+ );+ expect(status, stderr).toBe(0);await writeFile(join(root, "repositories.csv"),- `id,repository,revision\nsample,${repository},${revision}\n`,+ `id,repository,revision\nsample,${repository},${stdout.trim()}\n`,);}@@ -411,28 +415,22 @@ describe("CLI", () => {const root = await mkdtemp(join(tmpdir(), "codex-security-deep-defaults-"));try {- const child = Bun.spawn({- cmd: [- python!,+ const { status, stdout, stderr } = await runCommand(+ python!,+ [join(PLUGIN_ROOT, "scripts", "deep_scan_config.py"),"--available-parallelism","12",],- env: {- ...process.env,- CODEX_HOME: join(root, "codex-home"),- PYTHONDONTWRITEBYTECODE: "1",+ {+ env: {+ ...process.env,+ CODEX_HOME: join(root, "codex-home"),+ PYTHONDONTWRITEBYTECODE: "1",+ },+ timeout: 30_000,},- stdin: "ignore",- stdout: "pipe",- stderr: "pipe",- timeout: 30_000,- });- const [status, stdout, stderr] = await Promise.all([- child.exited,- new Response(child.stdout).text(),- new Response(child.stderr).text(),- ]);+ );expect(status, stderr).toBe(0);expect(stderr).toBe("");@@ -592,12 +590,13 @@ describe("CLI", () => {await mkdtemp(join(tmpdir(), "codex-security-cli-pre-commit-")),);try {- execFileSync("git", ["init", "-q", root], { timeout: 10_000 });- execFileSync(- "git",+ for (const args of [+ ["init", "-q", root],["-C", root, "config", "core.hooksPath", ".custom hooks"],- { timeout: 10_000 },- );+ ]) {+ const result = await runCommand("git", args, { timeout: 10_000 });+ expect(result.status, result.stderr).toBe(0);+ }let started = false;const hook = join(root, ".custom hooks", "pre-commit");const deps = dependencies({@@ -699,12 +698,13 @@ describe("CLI", () => {'#!/bin/sh\nprintf "codex-security\\n" > "$CODEX_SECURITY_HOOK_MARKER"\nexit 0\n',{ mode: 0o755 },);- execFileSync(+ const staged = await runCommand("git",["-C", root, "add", "-f", "node_modules/.bin/codex-security"],{ timeout: 10_000 },);- const commit = spawnSync(+ expect(staged.status, staged.stderr).toBe(0);+ const commit = await runCommand("git",["-C",@@ -721,7 +721,6 @@ describe("CLI", () => {"test",],{- encoding: "utf8",env: {...process.env,CODEX_HOME: join(root, "codex-home"),@@ -733,8 +732,7 @@ describe("CLI", () => {timeout: 10_000,},);- expect(commit.error).toBeUndefined();- expect(commit.status).not.toBe(0);+ expect(commit.status, commit.stderr).toBeGreaterThan(0);await expect(stat(maliciousMarker)).rejects.toMatchObject({code: "ENOENT",});@@ -1066,12 +1064,11 @@ describe("CLI", () => {);});- test("exposes only typed, read-only SDK metadata over MCP", () => {- const child = spawnSync(+ test("exposes only typed, read-only SDK metadata over MCP", async () => {+ const child = await runCommand(process.execPath,[join(import.meta.dir, "../src/cli.ts"), "--mcp"],{- encoding: "utf8",input: ['{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-11-25","capabilities":{},"clientInfo":{"name":"codex-security-test","version":"1.0.0"}}}','{"jsonrpc":"2.0","method":"notifications/initialized","params":{}}',@@ -1533,7 +1530,7 @@ describe("CLI", () => {test("registers the scoped package as the MCP command", async () => {const home = await mkdtemp(join(tmpdir(), "codex-security-mcp-home-"));try {- const child = spawnSync(+ const child = await runCommand(process.execPath,[join(import.meta.dir, "../src/cli.ts"),@@ -1544,7 +1541,6 @@ describe("CLI", () => {"--full-output",],{- encoding: "utf8",env: { ...process.env, HOME: home, USERPROFILE: home },timeout: 30_000,},diff --git a/sdk/typescript/tests-ts/release-automation.test.ts b/sdk/typescript/tests-ts/release-automation.test.tsindex 0d9d42f56..c8f11d8ca 100644--- a/sdk/typescript/tests-ts/release-automation.test.ts+++ b/sdk/typescript/tests-ts/release-automation.test.ts@@ -1,4 +1,4 @@-import { execFile, spawnSync } from "node:child_process";+import { spawnSync } from "node:child_process";import { createHash } from "node:crypto";import {mkdirSync,@@ -12,7 +12,7 @@ import { tmpdir } from "node:os";import { join } from "node:path";import { fileURLToPath } from "node:url";import { describe, expect, test } from "bun:test";-import { bashCommand } from "./support/shell.js";+import { bashCommand, runCommand } from "./support/shell.js";type ReleaseMetadata = Record<string, unknown>;@@ -160,34 +160,6 @@ const releaseTagTimeout = process.platform === "win32" ? 20_000 : 10_000;const bash = bashCommand();-function runCommand(- command: string,- args: string[],- {- input,- ...options- }: {- cwd?: string;- env?: NodeJS.ProcessEnv;- input?: string;- timeout: number;- },-): Promise<{ status: number | null; stdout: string; stderr: string }> {- // Avoid Bun's premature synchronous timeouts while keeping pipe reads bounded.- return new Promise((resolve, reject) => {- const child = execFile(- command,- args,- { ...options, encoding: "utf8" },- (_error, stdout, stderr) => {- resolve({ status: child.exitCode, stdout, stderr });- },- );- child.stdin?.on("error", reject);- child.stdin?.end(input);- });-}-const jqMock = ["jq() {",' node -e \'const fs=require("node:fs");const filter=process.argv.at(-1);const value=JSON.parse(fs.readFileSync(0,"utf8"));if(filter==="[.object.type, .object.sha] | @tsv"){const fields=[value.object?.type,value.object?.sha];if(fields.some((field)=>typeof field!=="string"))process.exit(1);process.stdout.write(fields.join("\\t")+"\\n");}else if(filter===".status // empty"){if(value.status!=null)process.stdout.write(String(value.status)+"\\n");}else process.exit(64);\' -- "$@"',diff --git a/sdk/typescript/tests-ts/support/shell.ts b/sdk/typescript/tests-ts/support/shell.tsindex 31427daa1..1f775672d 100644--- a/sdk/typescript/tests-ts/support/shell.ts+++ b/sdk/typescript/tests-ts/support/shell.ts@@ -1,3 +1,4 @@+import { execFile } from "node:child_process";import { existsSync } from "node:fs";import { dirname, join } from "node:path";@@ -8,3 +9,31 @@ export function bashCommand(): string {const gitBash = join(dirname(dirname(git)), "bin", "bash.exe");return existsSync(gitBash) ? gitBash : "bash";}++export function runCommand(+ command: string,+ args: string[],+ {+ input,+ ...options+ }: {+ cwd?: string;+ env?: NodeJS.ProcessEnv;+ input?: string;+ timeout: number;+ },+): Promise<{ status: number | null; stdout: string; stderr: string }> {+ // Avoid Bun's premature synchronous timeouts while keeping pipe reads bounded.+ return new Promise((resolve, reject) => {+ const child = execFile(+ command,+ args,+ { ...options, encoding: "utf8" },+ (_error, stdout, stderr) => {+ resolve({ status: child.exitCode, stdout, stderr });+ },+ );+ child.stdin?.on("error", reject);+ child.stdin?.end(input);+ });+}