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(scan): keep diff previews inside the repository (#372)
mldangelo-oai7d62b4ed84aa
diff --git a/sdk/typescript/_bundled_plugin/scripts/generate_in_scope_files.py b/sdk/typescript/_bundled_plugin/scripts/generate_in_scope_files.pyindex 5c9d11885..d32397117 100644--- a/sdk/typescript/_bundled_plugin/scripts/generate_in_scope_files.py+++ b/sdk/typescript/_bundled_plugin/scripts/generate_in_scope_files.py@@ -114,7 +114,7 @@ def generate_diff_in_scope_files() -> int:"""Reuse the existing diff selection without generating previews or duplicate worklists."""sys.path.insert(0, str(Path(__file__).resolve().parent))- from generate_rank_input import git_changed_paths, path_is_excluded, run_git_changed_paths+ from generate_rank_input import git_changed_paths, path_is_excludedfrom rank_preview import (DEFAULT_PREVIEW_BYTES,TEXT_CODE_EXTENSIONS,@@ -125,22 +125,7 @@ def generate_diff_in_scope_files(rows: list[bytes] = []try:- if mode == "local-patch":- changed = run_git_changed_paths(repository, [base])- untracked = subprocess.run(- ["git", "-C", str(repository), "ls-files", "--others", "--exclude-standard", "-z"],- capture_output=True,- text=True,- check=True,- )- changed.extend(- (repository / relative, "A")- for relative in untracked.stdout.split("\0")- if relative- )- else:- changed = git_changed_paths(repository, base, head, mode)-+ changed = git_changed_paths(repository, base, head, mode)eligible = [(path, status)for path, status in changeddiff --git a/sdk/typescript/_bundled_plugin/scripts/generate_rank_input.py b/sdk/typescript/_bundled_plugin/scripts/generate_rank_input.pyindex 81b370959..92018d74b 100644--- a/sdk/typescript/_bundled_plugin/scripts/generate_rank_input.py+++ b/sdk/typescript/_bundled_plugin/scripts/generate_rank_input.py@@ -646,8 +646,19 @@ def git_changed_paths(repo: Path, base: str, head: str, mode: str) -> list[tupleif mode == "local-patch":unstaged = run_git_changed_paths(repo, [base])staged = run_git_changed_paths(repo, ["--cached", base])+ untracked = subprocess.run(+ ["git", "-C", str(repo), "ls-files", "--others", "--exclude-standard", "-z"],+ capture_output=True,+ text=True,+ check=True,+ )combined = dict(staged)combined.update(unstaged)+ combined.update(+ (repo / relative, "A")+ for relative in untracked.stdout.split("\0")+ if relative+ )return sorted(combined.items())raise SystemExit(f"Unknown diff mode: {mode}")@@ -693,10 +704,17 @@ def make_diff_rank_input(args: argparse.Namespace) -> None:preview, is_binary = preview_for_bytes(rel, content, args.preview_bytes)if is_binary:continue+ elif path.is_symlink():+ preview = ""elif path.is_file():- preview, is_binary = preview_for(path, args.preview_bytes)- if is_binary:- continue+ try:+ path.resolve(strict=True).relative_to(repo)+ except (OSError, ValueError):+ preview = ""+ else:+ preview, is_binary = preview_for(path, args.preview_bytes)+ if is_binary:+ continueelse:preview = ""rows.append({"path": rel.as_posix(), "area": args.area, "preview": preview})diff --git a/sdk/typescript/tests-ts/compact-diff-scan.test.ts b/sdk/typescript/tests-ts/compact-diff-scan.test.tsindex 83a7736a7..afcf871a8 100644--- a/sdk/typescript/tests-ts/compact-diff-scan.test.ts+++ b/sdk/typescript/tests-ts/compact-diff-scan.test.ts@@ -244,7 +244,7 @@ describe("compact diff scan", () => {]);});- test("includes staged, unstaged, and untracked working-tree changes", () => {+ test("keeps staged, unstaged, and untracked working-tree inputs aligned", () => {const { root, repository } = createRepository();writeSource(repository, "src/handler.py", "value = 1\n");git(repository, "add", ".");@@ -276,6 +276,27 @@ describe("compact diff scan", () => {"src/staged.py","src/untracked.py",]);++ const reviewOutput = join(root, "rank-input.jsonl");+ const review = python(+ "generate_rank_input.py",+ "make-diff-rank-input",+ "--repo",+ repository,+ "--base",+ "HEAD",+ "--mode",+ "local-patch",+ "--out",+ reviewOutput,+ );+ expect(review.status, review.stderr).toBe(0);+ expect(+ readFileSync(reviewOutput, "utf8")+ .trim()+ .split("\n")+ .map((row) => (JSON.parse(row) as { path: string }).path),+ ).toEqual(["src/handler.py", "src/staged.py", "src/untracked.py"]);});test("keeps deleted inventory paths without accepting unsafe candidates", () => {diff --git a/sdk/typescript/tests-ts/diff-rank-input.test.ts b/sdk/typescript/tests-ts/diff-rank-input.test.tsnew file mode 100644index 000000000..d4ec49f0c--- /dev/null+++ b/sdk/typescript/tests-ts/diff-rank-input.test.ts@@ -0,0 +1,123 @@+import { execFileSync, spawnSync } from "node:child_process";+import {+ mkdirSync,+ mkdtempSync,+ readFileSync,+ realpathSync,+ rmSync,+ symlinkSync,+ writeFileSync,+} from "node:fs";+import { tmpdir } from "node:os";+import { join } from "node:path";+import { afterEach, expect, test } from "bun:test";+import { PLUGIN_ROOT } from "./plugin-root.js";++const temporaryRoots: string[] = [];++afterEach(() => {+ for (const root of temporaryRoots.splice(0)) {+ rmSync(root, { recursive: true, force: true });+ }+});++function git(repository: string, ...args: string[]): string {+ return execFileSync(+ "git",+ [+ "-c",+ "user.name=Fixture",+ "-c",+ "[email protected]",+ ...args,+ ],+ { cwd: repository, encoding: "utf8" },+ ).trim();+}++test("diff previews stay inside the selected repository", () => {+ const root = realpathSync(+ mkdtempSync(join(tmpdir(), "codex-security-diff-rank-")),+ );+ temporaryRoots.push(root);+ const repository = join(root, "repository");+ const nested = join(repository, "src", "nested");+ mkdirSync(nested, { recursive: true });+ git(repository, "init", "-q");+ writeFileSync(join(repository, "src", "handler.py"), "value = 1\n");+ writeFileSync(join(repository, "src", "deleted.py"), "removed = True\n");+ writeFileSync(join(repository, "src", "entry.py"), "handler.py");+ writeFileSync(join(nested, "linked.py"), "value = 1\n");+ git(repository, "add", ".");+ const originalLink = git(repository, "hash-object", "src/entry.py");+ git(+ repository,+ "update-index",+ "--cacheinfo",+ `120000,${originalLink},src/entry.py`,+ );+ git(repository, "commit", "-qm", "base");+ const base = git(repository, "rev-parse", "HEAD");++ writeFileSync(join(repository, "src", "handler.py"), "value = 2\n");+ writeFileSync(join(repository, "src", "entry.py"), "nested/linked.py");+ writeFileSync(join(nested, "linked.py"), "value = 2\n");+ rmSync(join(repository, "src", "deleted.py"));+ git(repository, "add", ".");+ const updatedLink = git(repository, "hash-object", "src/entry.py");+ git(+ repository,+ "update-index",+ "--cacheinfo",+ `120000,${updatedLink},src/entry.py`,+ );+ git(repository, "commit", "-qm", "selected changes");+ const head = git(repository, "rev-parse", "HEAD");++ const externalFixture = join(root, "synthetic-fixture");+ mkdirSync(externalFixture);+ writeFileSync(join(externalFixture, "linked.py"), "synthetic = True\n");+ rmSync(nested, { recursive: true });+ symlinkSync(externalFixture, nested, "junction");++ const python = Bun.which("python3") ?? Bun.which("python") ?? Bun.which("py");+ expect(python).not.toBeNull();+ const output = join(root, "rank-input.jsonl");+ const result = spawnSync(+ python!,+ [+ "-B",+ join(PLUGIN_ROOT, "scripts", "generate_rank_input.py"),+ "make-diff-rank-input",+ "--repo",+ repository,+ "--base",+ base,+ "--head",+ head,+ "--mode",+ "local-patch",+ "--out",+ output,+ ],+ { encoding: "utf8" },+ );++ expect(result.status, result.stderr).toBe(0);+ const rows = readFileSync(output, "utf8")+ .trim()+ .split("\n")+ .map((row) => JSON.parse(row) as { path: string; preview: string });+ expect(rows.map((row) => row.path)).toEqual([+ "src/deleted.py",+ "src/entry.py",+ "src/handler.py",+ "src/nested/linked.py",+ ]);+ expect(rows.find((row) => row.path === "src/handler.py")?.preview).toBe(+ "value = 2",+ );+ expect(rows.find((row) => row.path === "src/nested/linked.py")?.preview).toBe(+ "",+ );+});