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
fix: ensure committed diff scans read the requested head revision (#368)
tdullien1ecc608e2fab9
diff --git a/sdk/typescript/_bundled_plugin/scripts/generate_in_scope_files.py b/sdk/typescript/_bundled_plugin/scripts/generate_in_scope_files.pyindex b69c6b087..5c9d11885 100644--- a/sdk/typescript/_bundled_plugin/scripts/generate_in_scope_files.py+++ b/sdk/typescript/_bundled_plugin/scripts/generate_in_scope_files.py@@ -121,6 +121,7 @@ def generate_diff_in_scope_files(is_binary_sample,preview_for,)+ from workbench_target import git_blob_bytesrows: list[bytes] = []try:@@ -140,24 +141,36 @@ def generate_diff_in_scope_files(else:changed = git_changed_paths(repository, base, head, mode)- for path, status in changed:+ eligible = [+ (path, status)+ for path, status in changed+ if not path_is_excluded(path.relative_to(repository))+ and path.suffix.lower() in TEXT_CODE_EXTENSIONS+ ]+ revision_paths = [+ path.relative_to(repository)+ for path, status in eligible+ if mode == "revisions" and status != "D"+ ]+ revision_blobs = dict(+ zip(+ revision_paths,+ git_blob_bytes(+ repository,+ [f"{head}:{path.as_posix()}" for path in revision_paths],+ ),+ )+ )++ for path, status in eligible:relative = path.relative_to(repository)- if path_is_excluded(relative) or path.suffix.lower() not in TEXT_CODE_EXTENSIONS:- continueif status != "D":if mode == "revisions":- contents = subprocess.run(- [- "git",- "-C",- str(repository),- "cat-file",- "blob",- f"{head}:{relative.as_posix()}",- ],- capture_output=True,- check=True,- ).stdout+ contents = revision_blobs[relative]+ if contents is None:+ raise InventoryError(+ f"could not read committed diff blob: {head}:{relative.as_posix()}"+ )if is_binary_sample(contents):continueelif (diff --git a/sdk/typescript/_bundled_plugin/scripts/generate_rank_input.py b/sdk/typescript/_bundled_plugin/scripts/generate_rank_input.pyindex 535fe3410..81b370959 100644--- a/sdk/typescript/_bundled_plugin/scripts/generate_rank_input.py+++ b/sdk/typescript/_bundled_plugin/scripts/generate_rank_input.py@@ -37,8 +37,13 @@ from pathlib import Path# Some plugin hosts launch Python with safe-path isolation enabled.sys.path.insert(0, str(Path(__file__).resolve().parent))-from rank_preview import DEFAULT_PREVIEW_BYTES, TEXT_CODE_EXTENSIONS, preview_for-from workbench_target import git_directory_snapshot_paths+from rank_preview import (+ DEFAULT_PREVIEW_BYTES,+ TEXT_CODE_EXTENSIONS,+ preview_for,+ preview_for_bytes,+)+from workbench_target import git_blob_bytes, git_directory_snapshot_pathsEXCLUDED_DIRS = {".cache",@@ -652,14 +657,42 @@ def make_diff_rank_input(args: argparse.Namespace) -> None:if not repo.is_dir():raise SystemExit(f"Repo path not found: {repo}")+ changed = [+ (path, status)+ for path, status in git_changed_paths(repo, args.base, args.head, args.mode)+ if not path_is_excluded(path.relative_to(repo))+ and path.suffix.lower() in TEXT_CODE_EXTENSIONS+ ]+ revision_paths = [+ path.relative_to(repo)+ for path, status in changed+ if args.mode == "revisions" and status != "D"+ ]+ revision_blobs = dict(+ zip(+ revision_paths,+ git_blob_bytes(+ repo,+ [f"{args.head}:{path.as_posix()}" for path in revision_paths],+ ),+ )+ )+rows: list[JsonRow] = []- for path, status in git_changed_paths(repo, args.base, args.head, args.mode):+ for path, status in changed:rel = path.relative_to(repo)- if path_is_excluded(rel) or path.suffix.lower() not in TEXT_CODE_EXTENSIONS:- continueif status == "D":preview = ""+ elif args.mode == "revisions":+ content = revision_blobs[rel]+ if content is None:+ raise SystemExit(+ f"Unable to read committed diff blob: {args.head}:{rel.as_posix()}"+ )+ preview, is_binary = preview_for_bytes(rel, content, args.preview_bytes)+ if is_binary:+ continueelif path.is_file():preview, is_binary = preview_for(path, args.preview_bytes)if is_binary:diff --git a/sdk/typescript/_bundled_plugin/scripts/rank_preview.py b/sdk/typescript/_bundled_plugin/scripts/rank_preview.pyindex 88256f95f..edaee27f0 100644--- a/sdk/typescript/_bundled_plugin/scripts/rank_preview.py+++ b/sdk/typescript/_bundled_plugin/scripts/rank_preview.py@@ -949,6 +949,10 @@ def preview_for(data = sample + remainingexcept OSError:return "", True+ return preview_for_bytes(path, data, preview_bytes)+++def preview_for_bytes(path: Path, data: bytes, preview_bytes: int) -> tuple[str, bool]:if is_binary_sample(data):return "", Truetext = data.decode("utf-8", errors="ignore")diff --git a/sdk/typescript/_bundled_plugin/scripts/workbench_target.py b/sdk/typescript/_bundled_plugin/scripts/workbench_target.pyindex bd19fb882..05faf6e03 100644--- a/sdk/typescript/_bundled_plugin/scripts/workbench_target.py+++ b/sdk/typescript/_bundled_plugin/scripts/workbench_target.py@@ -40,10 +40,91 @@ def git_bytes(return completed.stdout if completed.returncode == 0 else None+def git_blob_bytes(+ target: Path,+ object_names: list[str],+ *,+ git_dir: Path | None = None,+ work_tree: Path | None = None,+) -> list[bytes | None]:+ """Read ordered raw blobs with one NUL-framed ``git cat-file --batch`` call."""+ if not object_names:+ return []++ request = b"\0".join(os.fsencode(name) for name in object_names) + b"\0"+ completed = git_command(+ target,+ "cat-file",+ "--batch",+ "-Z",+ text=False,+ input_data=request,+ git_dir=git_dir,+ work_tree=work_tree,+ )+ if completed.returncode != 0:+ return [None] * len(object_names)++ try:+ return _decode_git_batch_blobs(completed.stdout, len(object_names))+ except ValueError:+ return [None] * len(object_names)+++def _decode_git_batch_blobs(output: bytes, count: int) -> list[bytes | None]:+ """Decode ordered ``cat-file --batch -Z`` records without scanning blob bytes."""+ blobs: list[bytes | None] = []+ offset = 0+ for _ in range(count):+ header, offset = _read_nul_field(output, offset)+ size = _git_batch_blob_size(header)+ if size is None:+ blobs.append(None)+ continue+ blob, offset = _read_sized_nul_field(output, offset, size)+ blobs.append(blob)+ return blobs+++def _read_nul_field(output: bytes, offset: int) -> tuple[bytes, int]:+ """Read one NUL-terminated protocol field and return the next offset."""+ end = output.find(b"\0", offset)+ if end < 0:+ raise ValueError("missing NUL terminator")+ return output[offset:end], end + 1+++def _git_batch_blob_size(header: bytes) -> int | None:+ """Return a blob header's byte count, or ``None`` for a non-blob record."""+ fields = header.rsplit(b" ", 2)+ if len(fields) != 3 or fields[1] != b"blob":+ return None+ try:+ size = int(fields[2])+ except ValueError as error:+ raise ValueError("invalid blob size") from error+ if size < 0:+ raise ValueError("invalid blob size")+ return size+++def _read_sized_nul_field(+ output: bytes,+ offset: int,+ size: int,+) -> tuple[bytes, int]:+ """Read exactly ``size`` blob bytes followed by one NUL record terminator."""+ end = offset + size+ if output[end : end + 1] != b"\0":+ raise ValueError("missing blob terminator")+ return output[offset:end], end + 1++def git_command(target: Path,*args: str,text: bool,+ input_data: str | bytes | None = None,git_dir: Path | None = None,work_tree: Path | None = None,) -> subprocess.CompletedProcess[str] | subprocess.CompletedProcess[bytes]:@@ -65,6 +146,7 @@ def git_command(capture_output=True,env=environment,text=text,+ input=input_data,)except FileNotFoundError:# Git is optional for Codebase scans. Treat an unavailable executable likediff --git a/sdk/typescript/src/api.ts b/sdk/typescript/src/api.tsindex 209ae6f68..9bcac10de 100644--- a/sdk/typescript/src/api.ts+++ b/sdk/typescript/src/api.ts@@ -113,6 +113,7 @@ import {type ScanMode,type ScanTarget,validatedGitEnvironment,+ validateCommittedDiffCheckout,validateMode,} from "./targets.js";@@ -1654,6 +1655,8 @@ export class CodexSecurity {throwIfAborted(signal);const mode = options.mode ?? "standard";validateMode(normalized, mode);+ await validateCommittedDiffCheckout(repo, normalized, signal);+ throwIfAborted(signal);const protectedRoot =(await enclosingGitWorktreeRoot(repo, signal)) ?? repo;const requestedOutput = await validateOutputDir(diff --git a/sdk/typescript/src/targets.ts b/sdk/typescript/src/targets.tsindex 0cd582065..e16fe7bb9 100644--- a/sdk/typescript/src/targets.ts+++ b/sdk/typescript/src/targets.ts@@ -273,6 +273,45 @@ export async function normalizeTarget(return { kind: "paths", paths };}+export async function validateCommittedDiffCheckout(+ repository: string,+ target: NormalizedTarget,+ signal?: AbortSignal,+): Promise<void> {+ if (target.kind !== "refs") return;++ const checkoutHead = await resolveGitRef(repository, "HEAD", signal);+ if (checkoutHead !== target.head) {+ throw new InvalidTargetError(+ `Committed-diff scans require the repository checkout to match the requested head revision. Checkout HEAD is ${checkoutHead}; requested head is ${target.head}. Check out the requested head and retry.`,+ );+ }++ const status = await gitOutput(+ repository,+ [+ "-c",+ "core.fsmonitor=false",+ "status",+ "--porcelain=v1",+ "--untracked-files=all",+ ],+ signal,+ );+ if (status.length !== 0) {+ throw new InvalidTargetError(+ "Committed-diff scans require a clean repository checkout. Commit, stash, or remove local changes and retry. Use `git stash --include-untracked` to stash untracked files.",+ );+ }++ const tracked = await gitOutput(repository, ["ls-files", "-t", "-z"], signal);+ if (tracked.split("\0").some((entry) => entry.startsWith("S "))) {+ throw new InvalidTargetError(+ "Committed-diff scans require a full repository checkout. Sparse checkouts are not supported; materialize skipped tracked files and retry.",+ );+ }+}+export function validateMode(target: NormalizedTarget, mode: ScanMode): void {if (mode !== "standard" && mode !== "deep") {throw new InvalidTargetError(`Unsupported scan mode: ${String(mode)}`);diff --git a/sdk/typescript/tests-ts/api.test.ts b/sdk/typescript/tests-ts/api.test.tsindex ff530b689..517220710 100644--- a/sdk/typescript/tests-ts/api.test.ts+++ b/sdk/typescript/tests-ts/api.test.ts@@ -4975,6 +4975,75 @@ describe("CodexSecurity orchestration", () => {await client.close();});+ test("rejects committed diffs when checkout bytes can differ from head", async () => {+ const root = await temporaryDirectory();+ const repository = join(root, "repository");+ await mkdir(repository);+ await writeFile(+ join(repository, "tracked.ts"),+ "export const value = 'base';\n",+ );+ await mkdir(join(repository, "src"));+ await writeFile(join(repository, "src", "context.ts"), "export {};\n");+ const git = (...args: string[]) =>+ execFileSync("git", args, { cwd: repository, stdio: "pipe" });+ git("init", "-q", "-b", "main");+ git("add", ".");+ git(+ "-c",+ "user.name=Codex Security",+ "-c",+ "[email protected]",+ "commit",+ "-qm",+ "base",+ );+ git("checkout", "-qb", "feature");+ await writeFile(+ join(repository, "tracked.ts"),+ "export const value = 'head';\n",+ );+ git("add", "tracked.ts");+ git(+ "-c",+ "user.name=Codex Security",+ "-c",+ "[email protected]",+ "commit",+ "-qm",+ "head",+ );+ git("checkout", "-q", "main");++ const client = new TestClient({}, { environment: {} });+ await expect(+ client.run(repository, {+ target: DiffTarget.refs({ base: "main", head: "feature" }),+ }),+ ).rejects.toThrow("checkout to match the requested head revision");++ git("checkout", "-q", "feature");+ await writeFile(+ join(repository, "tracked.ts"),+ "export const value = 'dirty';\n",+ );+ await expect(+ client.run(repository, {+ target: DiffTarget.refs({ base: "main", head: "feature" }),+ }),+ ).rejects.toThrow("clean repository checkout");++ git("restore", "tracked.ts");+ git("update-index", "--skip-worktree", "src/context.ts");+ await rm(join(repository, "src", "context.ts"));+ await expect(+ client.run(repository, {+ target: DiffTarget.refs({ base: "main", head: "feature" }),+ }),+ ).rejects.toThrow("Sparse checkouts are not supported");+ await client.close();+ });+test("reports effective ambient API-key authentication", async () => {const root = await temporaryDirectory();const codexHome = join(root, "codex-home");diff --git a/sdk/typescript/tests-ts/compact-diff-scan.test.ts b/sdk/typescript/tests-ts/compact-diff-scan.test.tsindex 87fcae936..0488354b6 100644--- a/sdk/typescript/tests-ts/compact-diff-scan.test.ts+++ b/sdk/typescript/tests-ts/compact-diff-scan.test.ts@@ -164,6 +164,45 @@ async function startMcp(root: string) {}describe("compact diff scan", () => {+ test("reads committed diff previews from the selected head revision", () => {+ const { root, repository } = createRepository();+ writeSource(repository, "src/feature.ts", "const marker = 'base';\n");+ git(repository, "add", ".");+ git(repository, "commit", "-qm", "base");+ const base = git(repository, "rev-parse", "HEAD");++ writeSource(repository, "src/feature.ts", "const marker = 'head';\n");+ git(repository, "add", ".");+ git(repository, "commit", "-qm", "head");+ const head = git(repository, "rev-parse", "HEAD");+ git(repository, "checkout", "--detach", base);++ const output = join(root, "rank-input.jsonl");+ const result = python(+ "generate_rank_input.py",+ "make-diff-rank-input",+ "--repo",+ repository,+ "--base",+ base,+ "--head",+ head,+ "--mode",+ "revisions",+ "--out",+ output,+ );++ expect(result.status, result.stderr).toBe(0);+ const row = JSON.parse(readFileSync(output, "utf8")) as {+ path: string;+ preview: string;+ };+ expect(row.path).toBe("src/feature.ts");+ expect(row.preview).toContain("head");+ expect(row.preview).not.toContain("base");+ });+test("uses the selected Git revisions and keeps deleted source files", () => {const { root, repository } = createRepository();writeSource(repository, "src/guard.py", "allowed = True\n");