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
chore(codex-security): sync public projection
Copyberry615850d57ac6
diff --git a/sdk/typescript/_bundled_plugin/scripts/normalize_candidates.py b/sdk/typescript/_bundled_plugin/scripts/normalize_candidates.pydeleted file mode 100644index d4de4c128..000000000--- a/sdk/typescript/_bundled_plugin/scripts/normalize_candidates.py+++ /dev/null@@ -1,276 +0,0 @@-#!/usr/bin/env python3-"""Validate and combine security-scan candidates into deterministic JSONL."""--from __future__ import annotations--import argparse-import hashlib-import json-import re-import sys-import tempfile-from pathlib import Path, PurePosixPath-from typing import Any--CWE = re.compile(r"(?i)CWE-(\d+)")-ROLES = {- "entrypoint": 0,- "entrypoint/wrapper": 1,- "source": 2,- "root_control": 3,- "sink": 4,- "concrete_implementation": 5,- "evidence": 6,-}-FIELDS = {- "candidate_id",- "cwe_ids",- "locations",- "summary",- "evidence",- "context",- "instance",-}---def text_field(row: dict[str, Any], field: str, *, required: bool = True) -> str | None:- value = row.get(field)- if value is None and not required:- return None- if not isinstance(value, str) or not value.strip():- raise ValueError(f"{field}: expected a non-empty string")- return value.strip()---def cwe_ids(row: dict[str, Any]) -> list[str]:- value = row.get("cwe_ids")- if not isinstance(value, list):- raise ValueError("cwe_ids: expected an array")- found: set[int] = set()- for item in value:- if not isinstance(item, str):- raise ValueError("cwe_ids: expected CWE strings")- match = CWE.fullmatch(item.strip())- if match is None or int(match[1]) < 1:- raise ValueError(f"cwe_ids: unsupported value {item!r}")- found.add(int(match[1]))- return [f"CWE-{number}" for number in sorted(found)]---def relative_file(value: Any, repo_root: Path) -> tuple[str, Path]:- if not isinstance(value, str) or not value or "\0" in value:- raise ValueError("path: expected a non-empty repository-relative path")- raw = value- if sys.platform == "win32":- raw = raw.replace("\\", "/")- path = PurePosixPath(raw)- if (- path.is_absolute()- or ".." in path.parts- or (sys.platform == "win32" and re.match(r"^[A-Za-z]:", raw))- ):- raise ValueError("path: expected a repository-relative path without traversal")- resolved = (repo_root / raw).resolve(strict=True)- try:- relative = resolved.relative_to(repo_root).as_posix()- except ValueError as error:- raise ValueError("path: must resolve inside --repo-root") from error- if not resolved.is_file():- raise ValueError("path: expected a regular file")- return relative, resolved---def positive_line(value: Any, field: str) -> int:- if not isinstance(value, int) or isinstance(value, bool) or value < 1:- raise ValueError(f"{field}: expected a positive integer")- return value---def normalize_locations(- row: dict[str, Any], repo_root: Path, line_counts: dict[Path, int]-) -> list[dict[str, Any]]:- value = row.get("locations")- if not isinstance(value, list) or not value:- raise ValueError("locations: expected a non-empty array")- normalized: dict[tuple[str, int, int, str], dict[str, Any]] = {}- for item in value:- if not isinstance(item, dict):- raise ValueError("locations: expected location objects")- unknown = set(item) - {"path", "start_line", "end_line", "role"}- if unknown:- raise ValueError(f"locations: unsupported fields {', '.join(sorted(unknown))}")- relative, source = relative_file(item.get("path"), repo_root)- start = positive_line(item.get("start_line"), "start_line")- end = positive_line(item.get("end_line", start), "end_line")- if end < start:- raise ValueError("end_line: must be greater than or equal to start_line")- if source not in line_counts:- line_counts[source] = len(source.read_bytes().splitlines())- if end > line_counts[source]:- raise ValueError(f"line range {start}-{end} exceeds {relative}:{line_counts[source]}")- role = item.get("role")- if not isinstance(role, str) or role not in ROLES:- raise ValueError(f"role: unsupported value {role!r}")- key = (relative, start, end, role)- normalized[key] = {- "path": relative,- "start_line": start,- "end_line": end,- "role": role,- }- return sorted(- normalized.values(),- key=lambda item: (- ROLES[item["role"]],- item["path"],- item["start_line"],- item["end_line"],- ),- )---def read_scope(path: Path, repo_root: Path) -> set[str]:- scope: set[str] = set()- for number, line in enumerate(path.read_bytes().decode("utf-8").split("\n"), 1):- if sys.platform == "win32":- line = line.removesuffix("\r")- if not line:- continue- try:- relative, _ = relative_file(line, repo_root)- except (OSError, ValueError) as error:- raise ValueError(f"in-scope file row {number}: {error}") from error- scope.add(relative)- return scope---def normalize_candidate(- row: dict[str, Any], repo_root: Path, scope: set[str], line_counts: dict[Path, int]-) -> dict[str, Any]:- unknown = set(row) - FIELDS- if unknown:- raise ValueError(f"unsupported fields {', '.join(sorted(unknown))}")- if "candidate_id" in row:- text_field(row, "candidate_id")- candidate_locations = normalize_locations(row, repo_root, line_counts)- if not any(item["path"] in scope for item in candidate_locations):- raise ValueError("locations: expected at least one in-scope file")- result: dict[str, Any] = {- "cwe_ids": cwe_ids(row),- "locations": candidate_locations,- "summary": text_field(row, "summary"),- "evidence": text_field(row, "evidence"),- }- context = text_field(row, "context", required=False)- if context is not None:- result["context"] = context- instance = text_field(row, "instance", required=False)- if instance is not None:- result["instance"] = instance- return result---def identity(row: dict[str, Any]) -> str:- return json.dumps(- {- "cwe_ids": row["cwe_ids"],- "locations": row["locations"],- "instance": row.get("instance"),- },- ensure_ascii=False,- separators=(",", ":"),- sort_keys=True,- )---def merged_text(group: list[dict[str, Any]], field: str) -> str:- return "\n".join(sorted({item[field] for item in group if field in item}))---def combine(rows: list[dict[str, Any]]) -> list[dict[str, Any]]:- groups: dict[str, list[dict[str, Any]]] = {}- for row in rows:- groups.setdefault(identity(row), []).append(row)- combined: list[dict[str, Any]] = []- for key, group in sorted(groups.items()):- candidate_id = hashlib.sha256(key.encode()).hexdigest()[:16]-- result = {- "candidate_id": f"candidate-{candidate_id}",- "cwe_ids": group[0]["cwe_ids"],- "locations": group[0]["locations"],- "summary": merged_text(group, "summary"),- "evidence": merged_text(group, "evidence"),- }- context = merged_text(group, "context")- if context:- result["context"] = context- if "instance" in group[0]:- result["instance"] = group[0]["instance"]- combined.append(result)- return combined---def main() -> None:- parser = argparse.ArgumentParser(description=__doc__)- parser.add_argument("--input", nargs="+", required=True, help="Candidate JSONL inputs.")- parser.add_argument("--out", required=True, help="Combined candidate JSONL output.")- parser.add_argument("--repo-root", required=True, help="Repository root for candidate paths.")- parser.add_argument("--in-scope-files", required=True, help="Repository-relative file list.")- args = parser.parse_args()- try:- repo_root = Path(args.repo_root).expanduser().resolve(strict=True)- if not repo_root.is_dir():- raise ValueError("--repo-root: expected a directory")- output = Path(args.out).expanduser().resolve(strict=False)- scope_path = Path(args.in_scope_files).expanduser().resolve(strict=True)- inputs = sorted({Path(value).expanduser().resolve(strict=True) for value in args.input})- if output in inputs:- raise ValueError("--out: must not also be an input")- if output == scope_path:- raise ValueError("--out: must not replace --in-scope-files")- scope = read_scope(scope_path, repo_root)- line_counts: dict[Path, int] = {}- rows: list[dict[str, Any]] = []- for source in inputs:- with source.open(encoding="utf-8") as handle:- for number, line in enumerate(handle, 1):- if not line.strip():- continue- try:- row = json.loads(line)- if not isinstance(row, dict):- raise ValueError("expected a JSON object")- rows.append(normalize_candidate(row, repo_root, scope, line_counts))- except (ValueError, TypeError, OSError) as error:- raise ValueError(f"{source} row {number}: {error}") from error- combined = combine(rows)- output.parent.mkdir(parents=True, exist_ok=True)- payload = "".join(- json.dumps(row, ensure_ascii=False, separators=(",", ":"), sort_keys=True) + "\n"- for row in combined- )- temporary: Path | None = None- try:- with tempfile.NamedTemporaryFile(- mode="w",- encoding="utf-8",- dir=output.parent,- prefix=f".{output.name}.",- suffix=".tmp",- delete=False,- ) as handle:- temporary = Path(handle.name)- handle.write(payload)- temporary.replace(output)- finally:- if temporary is not None:- temporary.unlink(missing_ok=True)- print(f"Combined {len(rows)} candidate rows into {len(combined)} rows in {output}")- except (OSError, ValueError) as error:- print(f"normalize_candidates: {error}", file=sys.stderr)- raise SystemExit(2) from error---if __name__ == "__main__":- main()diff --git a/sdk/typescript/tests-ts/api.test.ts b/sdk/typescript/tests-ts/api.test.tsindex 7f0bc6831..9d2ae101d 100644--- a/sdk/typescript/tests-ts/api.test.ts+++ b/sdk/typescript/tests-ts/api.test.ts@@ -1295,24 +1295,18 @@ describe("CodexSecurity orchestration", () => {},);- // The fake Codex stream has no process handle to keep its unref'ed poll alive.- const keepEventLoopAlive = setTimeout(() => {}, 10_000);- try {- await expect(- client.run(repository, {- maxCostUsd: 0.005,- onCost: (cost) => costs.push(cost.estimatedUsd),- signal: AbortSignal.timeout(5_000),- }),- ).rejects.toMatchObject({- name: ScanCostLimitExceededError.name,+ await expect(+ client.run(repository, {maxCostUsd: 0.005,- scanDir,- cost,- });- } finally {- clearTimeout(keepEventLoopAlive);- }+ onCost: (cost) => costs.push(cost.estimatedUsd),+ signal: AbortSignal.timeout(5_000),+ }),+ ).rejects.toMatchObject({+ name: ScanCostLimitExceededError.name,+ maxCostUsd: 0.005,+ scanDir,+ cost,+ });expect(costs.at(-1)).toBe(0.00625);expect(commands[1]).toEqual(["fail-scan",diff --git a/sdk/typescript/tests-ts/auth.test.ts b/sdk/typescript/tests-ts/auth.test.tsindex 214c50bcf..39281ec2f 100644--- a/sdk/typescript/tests-ts/auth.test.ts+++ b/sdk/typescript/tests-ts/auth.test.ts@@ -1,8 +1,7 @@-import { ChildProcess } from "node:child_process";-import { mkdtemp, rm, writeFile } from "node:fs/promises";+import { mkdtemp, readFile, rm, watch, writeFile } from "node:fs/promises";import { tmpdir } from "node:os";import { join } from "node:path";-import { afterEach, describe, expect, spyOn, test } from "bun:test";+import { afterEach, describe, expect, test } from "bun:test";import {accountStatus,CodexLoginHandle,@@ -126,18 +125,47 @@ describe("Codex authentication process boundary", () => {expect(succeeded).toBe(true);});- test("drains native login stderr before resolving authentication", async () => {- const root = await mkdtemp(join(tmpdir(), "codex-security-auth-stderr-"));+ test("drains inherited stderr before resolving interactive login", async () => {+ const root = await mkdtemp(join(tmpdir(), "codex-security-auth-drain-"));temporaryDirectories.push(root);- const script = join(root, "login-stderr.mjs");+ const script = join(root, "inherited-stderr.mjs");const message = "network timeout while authenticating";+ const grandchildScript = `+process.once("disconnect", () => {+ process.stderr.write(${JSON.stringify(`${message}\n`)});+});+process.send("ready");+`;await writeFile(script,- `process.stderr.write(${JSON.stringify(`${message}\n`)}, (error) => process.exit(error ? 2 : 1));\n`,+ `+import { spawn } from "node:child_process";++const grandchild = spawn(process.execPath, ["-e", ${JSON.stringify(grandchildScript)}], {+ stdio: ["ignore", "ignore", "inherit", "ipc"],+ windowsHide: true,+});+const readyTimeout = setTimeout(() => {+ grandchild.kill();+ console.error("Timed out waiting for the login grandchild.");+ process.exit(1);+}, 10_000);+grandchild.once("message", (message) => {+ if (message === "ready") {+ clearTimeout(readyTimeout);+ process.exit(1);+ }+});+grandchild.once("error", (error) => {+ clearTimeout(readyTimeout);+ console.error(error.message);+ process.exit(1);+});+`,);const handle = new CodexLoginHandle(- { command: "node", prefixArgs: [script] },+ { command: process.execPath, prefixArgs: [script] },["login"],process.env,() => {},@@ -150,162 +178,105 @@ describe("Codex authentication process boundary", () => {});});- test.skipIf(process.platform === "win32")(- "drains inherited stderr before resolving interactive login",- async () => {- const root = await mkdtemp(join(tmpdir(), "codex-security-auth-drain-"));- temporaryDirectories.push(root);- 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";+ test+ .skipIf(process.platform !== "win32")+ .each([+ "releases inherited login pipes when the Windows fallback fires",+ "releases inherited login pipes after a native Windows process exits",+ ])("%s", async () => {+ const root = await mkdtemp(join(tmpdir(), "codex-security-auth-pipes-"));+ temporaryDirectories.push(root);+ const ready = join(root, "grandchild-ready");+ const release = join(root, "release-grandchild");+ const done = join(root, "grandchild-done");+ const script = join(root, "inherited-pipes.mjs");+ const grandchildScript = `+import { existsSync, watch, writeFileSync } from "node:fs";+import { join } from "node:path";-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);- }+const root = process.argv[1];+const release = join(root, "release-grandchild");+const watcher = watch(root, () => {+ if (existsSync(release)) {+ watcher.close();+ writeFileSync(join(root, "grandchild-done"), "done");+ process.exit(0);}- clearInterval(watcher);- clearTimeout(timeout);- writeSync(2, ${JSON.stringify(`${message}\n`)});+});+writeFileSync(join(root, "grandchild-ready"), "ready");+process.send("ready");+if (existsSync(release)) {+ watcher.close();+ writeFileSync(join(root, "grandchild-done"), "done");process.exit(0);-}, 25);-writeFileSync(ready, "ready");+}`;- await writeFile(- script,- `+ 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 },+ ["-e", ${JSON.stringify(grandchildScript)}, ${JSON.stringify(root)}],+ { stdio: ["ignore", "ignore", "inherit", "ipc"], windowsHide: true },);const readyTimeout = setTimeout(() => {- clearInterval(readyWatcher);grandchild.kill();- console.error("Timed out waiting for the login grandchild.");+ console.error("Timed out waiting for the Windows 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("message", (message) => {+ if (message === "ready") {+ clearTimeout(readyTimeout);+ process.exit(0);+ }+});grandchild.once("error", (error) => {- clearInterval(readyWatcher);clearTimeout(readyTimeout);console.error(error.message);process.exit(1);});`,- );-- const handle = new CodexLoginHandle(- { command: "node", prefixArgs: [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")(- "releases native login pipes when the Windows fallback fires",- async () => {- const root = await mkdtemp(join(tmpdir(), "codex-security-auth-pipes-"));- temporaryDirectories.push(root);- const script = join(root, "login-pipes.mjs");- await writeFile(- script,- 'process.stdout.write("ready\\n", () => process.exit(0));\n',- );-- const originalOnce = ChildProcess.prototype.once;- let loginChild: ChildProcess | undefined;- let releaseClose: (() => void) | undefined;- const processObserver = spyOn(ChildProcess.prototype, "once");- processObserver.mockImplementation(function (- this: ChildProcess,- event: string,- listener: (...eventArguments: never[]) => void,- ) {- if (event === "exit") loginChild = this;- if (event === "close") {- return Reflect.apply(originalOnce, this, [- event,- (...eventArguments: unknown[]) => {- releaseClose = () =>- Reflect.apply(listener, this, eventArguments);- },- ]);- }- return Reflect.apply(originalOnce, this, [event, listener]);- });-- let handle: CodexLoginHandle;- try {- handle = new CodexLoginHandle(- { command: "node", prefixArgs: [script] },- ["login"],- process.env,- () => {},- );- } finally {- processObserver.mockRestore();- }+ );+ const completionSignal = AbortSignal.timeout(20_000);+ const grandchildDone = (async () => {try {- const startedAt = Date.now();- const timeout = AbortSignal.timeout(5_000);- const completion = Promise.race([- handle.wait(),- new Promise<never>((_, reject) => {- timeout.addEventListener(- "abort",- () => reject(new Error("The Windows login fallback timed out.")),- { once: true },- );- }),- ]);- await expect(completion).resolves.toMatchObject({- success: true,- exitCode: 0,- });- expect(Date.now() - startedAt).toBeGreaterThanOrEqual(900);- expect(releaseClose).toBeFunction();- expect(loginChild?.stdout?.destroyed).toBe(true);- expect(loginChild?.stderr?.destroyed).toBe(true);- } finally {- releaseClose?.();+ for await (const event of watch(root, {+ signal: completionSignal,+ })) {+ if (event.filename === "grandchild-done") {+ return await readFile(done, "utf8");+ }+ }+ } catch (error) {+ if (completionSignal.aborted) {+ throw new Error("The Windows login grandchild did not exit.", {+ cause: error,+ });+ }+ throw error;}- },- );+ throw new Error("The Windows login grandchild did not exit.");+ })();+ const handle = new CodexLoginHandle(+ { command: process.execPath, prefixArgs: [script] },+ ["login"],+ process.env,+ () => {},+ );+ try {+ await expect(handle.wait()).resolves.toMatchObject({+ success: true,+ exitCode: 0,+ });+ await expect(readFile(ready, "utf8")).resolves.toBe("ready");+ } finally {+ await writeFile(release, "released");+ await expect(grandchildDone).resolves.toBe("done");+ }+ });test("does not report a canceled interactive login as successful", async () => {const root = await mkdtemp(join(tmpdir(), "codex-security-auth-cancel-"));diff --git a/sdk/typescript/tests-ts/cli.test.ts b/sdk/typescript/tests-ts/cli.test.tsindex 2235d9bad..763a9f6b3 100644--- a/sdk/typescript/tests-ts/cli.test.ts+++ b/sdk/typescript/tests-ts/cli.test.ts@@ -11,7 +11,7 @@ import {writeFile,} from "node:fs/promises";import { tmpdir } from "node:os";-import { delimiter, join, normalize } from "node:path";+import { delimiter, join } from "node:path";import { Writable } from "node:stream";import { fileURLToPath, pathToFileURL } from "node:url";import { stripVTControlCharacters } from "node:util";@@ -182,12 +182,10 @@ describe("CLI", () => {deps,),).toBe(0);- const result = JSON.parse(stdout.text()) as {- hook: string;- failOnSeverity: string;- };- expect(normalize(result.hook)).toBe(hook);- expect(result.failOnSeverity).toBe("medium");+ expect(JSON.parse(stdout.text())).toEqual({+ hook,+ failOnSeverity: "medium",+ });}expect(await readFile(hook, "utf8")).toContain("--working-tree --fail-on-severity medium",@@ -208,12 +206,10 @@ describe("CLI", () => {deps,),).toBe(0);- const migrated = JSON.parse(migratedHook.text()) as {- hook: string;- failOnSeverity: string;- };- expect(normalize(migrated.hook)).toBe(hook);- expect(migrated.failOnSeverity).toBe("medium");+ expect(JSON.parse(migratedHook.text())).toEqual({+ hook,+ failOnSeverity: "medium",+ });expect(await readFile(hook, "utf8")).toBe(trustedHook);const existingHook = capture();@@ -306,7 +302,7 @@ describe("CLI", () => {await expect(stat(maliciousMarker)).rejects.toMatchObject({code: "ENOENT",});- expect(trustedHook).not.toMatch(/^exec\s+npx(?:\s|$)/m);+ expect(trustedHook).not.toContain("npx");expect(trustedHook).toContain(await realpath(process.execPath));expect(trustedHook).toContain(await realpath(