Skip to content

OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security

fix(plugin): include build and test files in scan inputs (#1414)

mldangelo-oai89d56ae64c22
diff --git a/plugins/codex-security/mcp-app/tests/test_artifact_inventory.ts b/plugins/codex-security/mcp-app/tests/test_artifact_inventory.ts
index ec5366ed2..1ba22ef33 100644
--- a/plugins/codex-security/mcp-app/tests/test_artifact_inventory.ts
+++ b/plugins/codex-security/mcp-app/tests/test_artifact_inventory.ts
@@ -220,7 +220,7 @@ async function testPrepareUsesOnlyAuthoritativeDiffChanges() {
);
await fixture.writeRepositoryFile(
"tests/example.ts",
- "export const ignored = true;\n",
+ "export const testSetup = true;\n",
);
await unlink(path.join(fixture.repoRoot, "src/deleted.ts"));
await runGit(fixture.repoRoot, "add", ".");
@@ -235,13 +235,14 @@ async function testPrepareUsesOnlyAuthoritativeDiffChanges() {
};
assert.deepEqual(await inventory.prepareCodexSecurityReviewItems(context), {
- reviewItemsTotal: 3,
+ reviewItemsTotal: 4,
});
assert.deepEqual(await inventory.listCodexSecurityReviewItems(context), {
items: [
{ path: "src/changed.ts" },
{ path: "src/deleted.ts" },
{ path: "src/new.ts" },
+ { path: "tests/example.ts" },
],
});
}
diff --git a/plugins/codex-security/scripts/generate_in_scope_files.py b/plugins/codex-security/scripts/generate_in_scope_files.py
index 3f26ff84f..17bd2fb41 100644
--- a/plugins/codex-security/scripts/generate_in_scope_files.py
+++ b/plugins/codex-security/scripts/generate_in_scope_files.py
@@ -165,39 +165,24 @@ def generate_diff_in_scope_files(
output: Path,
) -> int:
"""Reuse the existing diff selection without generating previews or duplicate worklists."""
- from generate_rank_input import git_changed_paths, path_is_diff_excluded, run_git_changed_paths
+ from generate_rank_input import git_changed_paths
from rank_preview import is_binary_file
rows: list[bytes] = []
try:
- changed = (
- [
- (path, status)
- for path, status, selected_mode in run_git_changed_paths(
- repository, [f"{base}..{head}"]
- )
- if selected_mode != b"120000"
- ]
- if mode == "revisions"
- else git_changed_paths(repository, base, head, mode)
- )
- eligible = [
- (path, status)
- for path, status in changed
- if not path_is_diff_excluded(path.relative_to(repository))
- ]
+ changed = git_changed_paths(repository, base, head, mode)
revision_refs = {
path.relative_to(repository): (
f"{base if status == 'D' else head}:{path.relative_to(repository).as_posix()}"
)
- for path, status in eligible
+ for path, status in changed
if mode == "revisions" or status == "D"
}
revision_samples = dict(
zip(revision_refs, git_blob_samples(repository, list(revision_refs.values())))
)
- for path, status in eligible:
+ for path, status in changed:
relative = path.relative_to(repository)
if mode == "revisions" or status == "D":
sample = revision_samples[relative]
@@ -211,12 +196,17 @@ def generate_diff_in_scope_files(
continue
elif path.is_symlink() or not path.is_file() or is_binary_file(path):
continue
- relative_path = relative.as_posix()
- if "\n" in relative_path or "\r" in relative_path:
+ try:
+ relative_path = relative.as_posix().encode("utf-8")
+ except UnicodeEncodeError as error:
+ raise InventoryError(
+ "Git changes contain a path that cannot be encoded as UTF-8 for the file inventory"
+ ) from error
+ if b"\n" in relative_path or b"\r" in relative_path:
raise InventoryError(
"Git changes contain a path that cannot fit in the file inventory"
)
- rows.append(f"{relative_path}\n".encode())
+ rows.append(relative_path + b"\n")
except (OSError, subprocess.CalledProcessError) as error:
detail = getattr(error, "stderr", None)
if isinstance(detail, bytes):
diff --git a/plugins/codex-security/scripts/generate_rank_input.py b/plugins/codex-security/scripts/generate_rank_input.py
index 09ccb0d32..dd02ec291 100644
--- a/plugins/codex-security/scripts/generate_rank_input.py
+++ b/plugins/codex-security/scripts/generate_rank_input.py
@@ -9,8 +9,8 @@ This script stays deliberately model-free:
worklist from Git changed paths. It supports committed revision diffs and
local working-tree patches.
-Candidate selection uses the existing path exclusions and binary detection,
-not a language-extension allowlist. All formats use bounded source previews.
+Candidate selection uses repository scope, native tool ignore rules, and binary detection,
+not filename or directory classifications. Ranking inputs use bounded source previews.
"""
from __future__ import annotations
@@ -20,6 +20,7 @@ import json
import os
import subprocess
import sys
+from collections.abc import Iterable
from pathlib import Path
# Some plugin hosts launch Python with safe-path isolation enabled.
@@ -33,82 +34,6 @@ from rank_preview import (
)
from workbench_target import git_blob_samples, git_command, git_directory_snapshot_paths
-EXCLUDED_DIRS = {
- ".cache",
- ".circleci",
- ".devcontainer",
- ".git",
- ".github",
- ".idea",
- ".mypy_cache",
- ".pytest_cache",
- ".ruff_cache",
- ".tox",
- ".venv",
- ".vscode",
- "__pycache__",
- "bench",
- "benchmark",
- "bintest",
- "build",
- "build_config",
- "build_configs",
- "build-tools",
- "build_tools",
- "ci",
- "coverage",
- "deps",
- "dev",
- "dist",
- "doc",
- "docs",
- "example",
- "examples",
- "external",
- "extern",
- "fixture",
- "fixtures",
- "generated",
- "node_modules",
- "sample",
- "samples",
- "target",
- "test",
- "tests",
- "testing",
- "third-party",
- "third_party",
- "tmp",
- "vendor",
-}
-
-EXCLUDED_FILENAMES = {
- ".DS_Store",
- "CHANGELOG",
- "CHANGELOG.md",
- "CONTRIBUTING.md",
- "Dockerfile",
- "Gemfile",
- "Gemfile.lock",
- "LICENSE",
- "LICENSE.md",
- "Makefile",
- "NEWS",
- "NEWS.md",
- "NOTICE",
- "README",
- "README.md",
- "README.rst",
- "Rakefile",
- "SECURITY.md",
- "TODO",
- "TODO.md",
- "docker-compose.yml",
- "package-lock.json",
- "pnpm-lock.yaml",
- "yarn.lock",
-}
-
JsonRow = dict[str, object]
@@ -176,21 +101,6 @@ def parse_args() -> argparse.Namespace:
return parser.parse_args()
-def path_is_excluded(path: Path) -> bool:
- if any(part in EXCLUDED_DIRS for part in path.parts):
- return True
- if path.name in EXCLUDED_FILENAMES:
- return True
- return path.name.endswith((".min.js", ".map"))
-
-
-def path_is_diff_excluded(path: Path) -> bool:
- """Apply repository exclusions while retaining changed workflow files."""
- if path.parts[:2] == (".github", "workflows"):
- return False
- return path_is_excluded(path)
-
-
def resolve_scope(
repo: Path,
scope: str,
@@ -257,6 +167,60 @@ def load_scopes_file(scopes_file: Path) -> list[str]:
return loaded
+def scope_candidates(repo: Path, scope_path: Path) -> Iterable[Path]:
+ """Use Git's inventory in worktrees and ripgrep's ignore rules elsewhere.
+
+ Git retains tracked files and applies its standard exclusions to untracked files.
+ Outside Git, ripgrep also honors .ignore and .rgignore alongside .gitignore.
+ Explicit file scopes bypass directory ignore rules.
+ """
+ if scope_path.is_file():
+ return (scope_path,)
+ git_candidates = git_directory_snapshot_paths(scope_path)
+ if git_candidates is not None:
+ return git_candidates
+
+ command = [
+ "rg",
+ "--files",
+ "--hidden",
+ "--no-require-git",
+ "--null",
+ # Also exclude descendants when the scope starts inside .git.
+ "--glob",
+ "!**/.git",
+ "--glob",
+ "!**/.git/**",
+ "--",
+ str(scope_path.relative_to(repo)),
+ ]
+ try:
+ result = subprocess.run(command, cwd=repo, capture_output=True, check=False)
+ except OSError as exc:
+ ignore_names = (".gitignore", ".ignore", ".rgignore")
+ ancestors = (scope_path, *scope_path.parents)
+ has_ignore_rules = (
+ any((ancestor / ".git").exists() for ancestor in (repo, *repo.parents))
+ or any(
+ (ancestor / name).is_file()
+ for ancestor in ancestors
+ if ancestor == repo or repo in ancestor.parents
+ for name in ignore_names
+ )
+ or any(path.name in ignore_names for path in scope_path.rglob("*") if path.is_file())
+ )
+ if has_ignore_rules:
+ raise SystemExit(
+ "Could not safely enumerate ignored scoped files without Git or ripgrep."
+ ) from exc
+ return scope_path.rglob("*")
+
+ if result.returncode not in (0, 1):
+ detail = result.stderr.decode("utf-8", errors="replace").strip()
+ raise SystemExit(f"Could not enumerate scoped repository files: {detail}")
+ return (repo / os.fsdecode(path) for path in result.stdout.split(b"\0") if path)
+
+
def make_repo_rank_input(args: argparse.Namespace) -> None:
repo = Path(args.repo).expanduser().resolve()
if not repo.is_dir():
@@ -276,7 +240,7 @@ def make_repo_rank_input(args: argparse.Namespace) -> None:
for scope_abs in resolved_scopes:
scope_rel = scope_abs.relative_to(repo)
area = args.area or scope_rel.as_posix()
- candidates = (scope_abs,) if scope_abs.is_file() else scope_abs.rglob("*")
+ candidates = scope_candidates(repo, scope_abs)
for path in candidates:
try:
if path.is_symlink() or not path.is_file():
@@ -286,12 +250,7 @@ def make_repo_rank_input(args: argparse.Namespace) -> None:
continue
rel = path.relative_to(repo)
directly_requested = path in directly_requested_files
- excluded_path = (
- path.relative_to(scope_abs if scope_abs.is_dir() else scope_abs.parent)
- if explicit_scopes
- else rel
- )
- if not directly_requested and path_is_excluded(excluded_path):
+ if ".git" in rel.parts:
continue
preview, is_binary = preview_for(path, args.preview_bytes)
@@ -319,58 +278,7 @@ def make_repo_scope_input(args: argparse.Namespace) -> None:
rows_by_path: dict[str, JsonRow] = {}
for scope in scopes:
scope_path = resolve_scope(repo, scope, expand_user=False, reject_symlinks=True)
- if scope_path.is_file():
- candidates = (scope_path,)
- else:
- git_candidates = git_directory_snapshot_paths(scope_path)
- if git_candidates is not None:
- candidates = git_candidates
- else:
- command = [
- "rg",
- "--files",
- "--hidden",
- "--no-require-git",
- "--null",
- # Also exclude descendants when the scope starts inside .git.
- "--glob",
- "!**/.git",
- "--glob",
- "!**/.git/**",
- "--",
- str(scope_path.relative_to(repo)),
- ]
- try:
- result = subprocess.run(command, cwd=repo, capture_output=True, check=False)
- except OSError as exc:
- ignore_names = (".gitignore", ".ignore", ".rgignore")
- ancestors = (scope_path, *scope_path.parents)
- has_ignore_rules = (
- any((ancestor / ".git").exists() for ancestor in (repo, *repo.parents))
- or any(
- (ancestor / name).is_file()
- for ancestor in ancestors
- if ancestor == repo or repo in ancestor.parents
- for name in ignore_names
- )
- or any(
- path.name in ignore_names
- for path in scope_path.rglob("*")
- if path.is_file()
- )
- )
- if has_ignore_rules:
- raise SystemExit(
- "Could not safely enumerate ignored scoped files without Git or ripgrep."
- ) from exc
- candidates = scope_path.rglob("*")
- else:
- if result.returncode not in (0, 1):
- detail = result.stderr.decode("utf-8", errors="replace").strip()
- raise SystemExit(f"Could not enumerate scoped repository files: {detail}")
- candidates = (
- repo / os.fsdecode(path) for path in result.stdout.split(b"\0") if path
- )
+ candidates = scope_candidates(repo, scope_path)
for path in candidates:
try:
if path.is_symlink() or not path.is_file():
@@ -388,15 +296,15 @@ def make_repo_scope_input(args: argparse.Namespace) -> None:
print(f"Wrote {len(rows)} scoped paths to {output}")
-def run_git_changed_paths(repo: Path, diff_args: list[str]) -> list[tuple[Path, str, bytes]]:
- """Return changed paths, statuses, and modes from the selected side of each change."""
+def run_git_changed_paths(repo: Path, diff_args: list[str]) -> list[tuple[Path, str]]:
+ """Return changed regular files from the selected side of each change."""
result = git_command(
repo,
"diff",
"--ignore-submodules=all",
"--raw",
"-z",
- "--diff-filter=ACMRD",
+ "--diff-filter=ACMRDT",
*diff_args,
text=False,
)
@@ -405,7 +313,7 @@ def run_git_changed_paths(repo: Path, diff_args: list[str]) -> list[tuple[Path,
if fields and not fields[-1]:
fields.pop()
- changed: list[tuple[Path, str, bytes]] = []
+ changed: list[tuple[Path, str]] = []
index = 0
while index < len(fields):
metadata = fields[index].split()
@@ -416,15 +324,14 @@ def run_git_changed_paths(repo: Path, diff_args: list[str]) -> list[tuple[Path,
path = repo / os.fsdecode(fields[index])
index += 1
selected_mode = metadata[0].removeprefix(b":") if status == "D" else metadata[1]
- changed.append((path, status, selected_mode))
+ if selected_mode.startswith(b"100"):
+ changed.append((path, status))
return changed
def git_changed_paths(repo: Path, base: str, head: str, mode: str) -> list[tuple[Path, str]]:
if mode == "revisions":
- return [
- (path, status) for path, status, _ in run_git_changed_paths(repo, [f"{base}..{head}"])
- ]
+ return run_git_changed_paths(repo, [f"{base}..{head}"])
if mode == "local-patch":
unstaged = run_git_changed_paths(repo, [base])
staged = run_git_changed_paths(repo, ["--cached", base])
@@ -437,14 +344,18 @@ def git_changed_paths(repo: Path, base: str, head: str, mode: str) -> list[tuple
text=False,
)
untracked.check_returncode()
- combined = {path: status for path, status, _ in staged}
- combined.update((path, status) for path, status, _ in unstaged)
+ combined = dict(staged)
+ combined.update(unstaged)
combined.update(
(repo / os.fsdecode(relative), "A")
for relative in untracked.stdout.split(b"\0")
if relative and not relative.endswith(b"/")
)
- return sorted(combined.items())
+ return sorted(
+ (path, status)
+ for path, status in combined.items()
+ if status == "D" or (not path.is_symlink() and path.is_file())
+ )
raise SystemExit(f"Unknown diff mode: {mode}")
@@ -453,11 +364,7 @@ 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_diff_excluded(path.relative_to(repo))
- ]
+ changed = git_changed_paths(repo, args.base, args.head, args.mode)
revision_refs = {
path.relative_to(repo): (
f"{args.base if status == 'D' else args.head}:{path.relative_to(repo).as_posix()}"
diff --git a/plugins/codex-security/scripts/workbench_target.py b/plugins/codex-security/scripts/workbench_target.py
index 98b73dfb3..c286477ed 100644
--- a/plugins/codex-security/scripts/workbench_target.py
+++ b/plugins/codex-security/scripts/workbench_target.py
@@ -541,6 +541,16 @@ def clean_worktree_content_digest() -> str:
return f"codex-security-snapshot/v1:sha256:{digest.hexdigest()}"
+def _has_linked_parent(scope: Path, path: Path) -> bool:
+ ancestor = scope
+ for component in path.relative_to(scope).parts[:-1]:
+ ancestor /= component
+ metadata = ancestor.lstat()
+ if stat.S_ISLNK(metadata.st_mode) or getattr(metadata, "st_reparse_tag", 0) & 0x20000000:
+ return True
+ return False
+
+
def git_directory_snapshot_paths(target: Path) -> list[Path] | None:
repository_root = git_output(target, "rev-parse", "--show-toplevel")
if repository_root is None:
@@ -595,9 +605,12 @@ def git_directory_snapshot_paths(target: Path) -> list[Path] | None:
continue
path = target.joinpath(*relative.parts[scope_depth:])
try:
+ # The index can retain descendants of a directory replaced by a link.
+ if _has_linked_parent(target, path):
+ continue
metadata = path.lstat()
- except FileNotFoundError:
- # The index can retain a path that was staged and then deleted.
+ except (FileNotFoundError, NotADirectoryError):
+ # The index can retain deleted paths, including directory-to-file replacements.
continue
paths.append(path)
if not stat.S_ISDIR(metadata.st_mode):
diff --git a/plugins/codex-security/skills/finding-discovery/SKILL.md b/plugins/codex-security/skills/finding-discovery/SKILL.md
index 1ee25a3f6..71bc716d5 100644
--- a/plugins/codex-security/skills/finding-discovery/SKILL.md
+++ b/plugins/codex-security/skills/finding-discovery/SKILL.md
@@ -33,7 +33,7 @@ When a running diff scan already supplies its file inventory through `list_codex
For a targeted code diff without an existing compact inventory:
- Read `../security-scan/references/scan-artifacts-and-ledger.md`.
-- Generate `rank_input.jsonl` deterministically from changed text files regardless of extension, subject to the existing path exclusions, with `<python_command> <plugin_dir>/scripts/generate_rank_input.py make-diff-rank-input --repo <repo_root> --base <base> --mode revisions --head <head> --out <discovery_dir>/rank_input.jsonl` for PR, commit, and branch diffs, or `<python_command> <plugin_dir>/scripts/generate_rank_input.py make-diff-rank-input --repo <repo_root> --base <base> --mode local-patch --out <discovery_dir>/rank_input.jsonl` for a local patch.
+- Generate `rank_input.jsonl` deterministically from changed text files regardless of filename, extension, or directory, with `<python_command> <plugin_dir>/scripts/generate_rank_input.py make-diff-rank-input --repo <repo_root> --base <base> --mode revisions --head <head> --out <discovery_dir>/rank_input.jsonl` for PR, commit, and branch diffs, or `<python_command> <plugin_dir>/scripts/generate_rank_input.py make-diff-rank-input --repo <repo_root> --base <base> --mode local-patch --out <discovery_dir>/rank_input.jsonl` for a local patch.
- Copy every diff row into `deep_review_input.jsonl` with `<plugin_dir>/scripts/launch_codex_security_mcp --helper copy-deep-review-input --rank-input <discovery_dir>/rank_input.jsonl --out <discovery_dir>/deep_review_input.jsonl`. On Windows, use the [PowerShell copy command](../security-scan/references/scan-artifacts-and-ledger.md#windows-worklist-copy) to preserve literal paths. Diff scans do not rank or drop changed files before deep review.
- Add directly supporting files required to understand the changed security behavior only when repository evidence shows they are needed. Do not use them to broaden into unrelated repository-wide enumeration.
- Deep-review every file in `deep_review_input.jsonl` using the shared scoped file-review rules.
diff --git a/plugins/codex-security/skills/security-diff-scan/SKILL.md b/plugins/codex-security/skills/security-diff-scan/SKILL.md
index ca63a9fce..0029a4f85 100644
--- a/plugins/codex-security/skills/security-diff-scan/SKILL.md
+++ b/plugins/codex-security/skills/security-diff-scan/SKILL.md
@@ -5,7 +5,7 @@ description: "Review a pull request, commit, branch diff, or working-tree patch
# Security Diff Scan
-Review every changed source file, including deleted files. Follow changed behavior into supporting code without expanding into an unrelated repository audit.
+Review every changed text file, including deleted files. Follow changed behavior into supporting code without expanding into an unrelated repository audit.
## Setup
@@ -21,6 +21,8 @@ Save context changes with `update_codex_security_scan_context`. Advance each sta
Read `../../references/config-preflight.md` before dispatching the `security_diff_scan` capability preflight. When the host explicitly identifies itself as the desktop app, also read `../../references/desktop-config-preflight.md` before running the helper. For a durable scan, use its authoritative scan context, ask before applying actionable remediation, and wait without creating a scan goal or calling `fail_codex_security_scan`. Do not fail automatically for declined or unavailable remediation, helper errors, or a non-ready rerun; preserve the running scan and retry or hand off while recovery may still be possible. Use `cancel_codex_security_scan` only when the user explicitly cancels; call `fail_codex_security_scan` only after documented recovery is exhausted and the blocker is confirmed unrecoverable. Do not treat a config value that differs from a suggested patch as a warning unless the capability requirement itself is unmet.
+The inventory includes changed text files regardless of filename, extension, or directory. Tracked changes remain in scope even when they match ignore rules; local patches also include untracked files that Git does not ignore. Deleted files are classified from the base revision. Assess security relevance from the file's behavior and reachability, including build, deployment, and CI use, rather than its path alone.
+
1. Run `$threat-model` once, or use the supplied model. Preserve a supplied schema-valid canonical `threatModel` object unchanged. Otherwise retain the exact supplied text, or the completed generated Markdown, as `{ "format": "markdown", "content": "<model text>" }`; mark supplied text with `origin: "provided"` and generated text with `origin: "generated"`. Model the repository unless the user requests a narrower scope, and record the model's known scope rather than substituting the changed-file scope. Immediately save a `complete: false` semantic draft with that model, no findings yet, and partial coverage. The host writes `<scan_dir>/threatmodel.md`; later phases retain the same canonical model.
2. Prepare the file list with `prepare_codex_security_review_items` and read all pages from `list_codex_security_review_items`. Inspect deleted files at the baseline revision and unchanged files only when needed to explain the change.
3. Run `$finding-discovery` in compact diff mode across the existing file inventory. Do not create ranked worklists, per-finding ledgers, or discovery reports. Divide large changes among available workers without overlap; review any unassigned files yourself. Keep independently reachable bugs separate and record all candidates once with `record_codex_security_discovery_candidates`.
@@ -34,8 +36,6 @@ For terminal scans without a `scanId`, generate the changed-file list with:
<python_command> <plugin_dir>/scripts/generate_in_scope_files.py --repo <repo_root> --scope . --diff-base <base> --diff-head <head> --diff-mode <revisions|local-patch> --out <discovery_dir>/in_scope_files.txt
```
-The inventory includes changed text files regardless of extension, subject to the existing path exclusions. Deleted files are classified from the base revision.
-
Record candidates with `<plugin_dir>/scripts/launch_codex_security_mcp --helper normalize-candidates --input <candidate-source> --out <discovery_dir>/candidate_ledger.jsonl --repo-root <repo_root> --in-scope-files <discovery_dir>/in_scope_files.txt --allow-missing-in-scope`. Use the `.cmd` launcher on Windows. Add validation and attack-path decisions to that same file. Following `../../references/final-report.md`, assemble unsealed `scan-manifest.json`, `findings.json`, and `coverage.json` before running `finalize_scan_contract.py --scan-dir <scan_dir> --source-root <repo_root>`.
Finish only after every changed file and candidate is accounted for. Return the generated report, actual coverage gaps, and Codex review comments for confirmed findings.
diff --git a/plugins/codex-security/skills/security-scan/references/scan-artifacts-and-ledger.md b/plugins/codex-security/skills/security-scan/references/scan-artifacts-and-ledger.md
index e5493aadc..4b9a581f9 100644
--- a/plugins/codex-security/skills/security-scan/references/scan-artifacts-and-ledger.md
+++ b/plugins/codex-security/skills/security-scan/references/scan-artifacts-and-ledger.md
@@ -56,7 +56,7 @@ The parent agent must reconcile validation and attack-path subagent outputs befo
## Scoped Deep Review
- Use `deep_review_input.jsonl` as the canonical changed-file review worklist for diff scans.
-- For diff-scoped scans, generate `rank_input.jsonl` deterministically from changed text files regardless of extension, subject to the existing path exclusions, with `<python_command> <plugin_dir>/scripts/generate_rank_input.py make-diff-rank-input --repo <repo_root> --base <base> --mode revisions --head <head> --out <discovery_dir>/rank_input.jsonl` for PR, commit, and branch diffs, or `<python_command> <plugin_dir>/scripts/generate_rank_input.py make-diff-rank-input --repo <repo_root> --base <base> --mode local-patch --out <discovery_dir>/rank_input.jsonl` for a local patch, then copy every row into `deep_review_input.jsonl` with `<plugin_dir>/scripts/launch_codex_security_mcp --helper copy-deep-review-input --rank-input <discovery_dir>/rank_input.jsonl --out <discovery_dir>/deep_review_input.jsonl`. On Windows, use the [PowerShell copy command](#windows-worklist-copy) below.
+- For diff-scoped scans, generate `rank_input.jsonl` deterministically from changed text files regardless of filename, extension, or directory, with `<python_command> <plugin_dir>/scripts/generate_rank_input.py make-diff-rank-input --repo <repo_root> --base <base> --mode revisions --head <head> --out <discovery_dir>/rank_input.jsonl` for PR, commit, and branch diffs, or `<python_command> <plugin_dir>/scripts/generate_rank_input.py make-diff-rank-input --repo <repo_root> --base <base> --mode local-patch --out <discovery_dir>/rank_input.jsonl` for a local patch, then copy every row into `deep_review_input.jsonl` with `<plugin_dir>/scripts/launch_codex_security_mcp --helper copy-deep-review-input --rank-input <discovery_dir>/rank_input.jsonl --out <discovery_dir>/deep_review_input.jsonl`. On Windows, use the [PowerShell copy command](#windows-worklist-copy) below.
- Diff-scoped scans do not rank or drop changed files before deep review. Every row in diff `rank_input.jsonl` must be copied into `deep_review_input.jsonl` and receive a full-file review receipt.
- Add directly supporting files required to understand the changed security behavior only when repository evidence shows they are needed; record the add-back reason in the work ledger or per-file result.
- Deep-review every file selected into `deep_review_input.jsonl`.
diff --git a/plugins/codex-security/tests/source_cases.py b/plugins/codex-security/tests/source_cases.py
index 8febfccc3..fbbfc0219 100644
--- a/plugins/codex-security/tests/source_cases.py
+++ b/plugins/codex-security/tests/source_cases.py
@@ -1,7 +1,8 @@
"""Shared source examples for inventory and ranking integration tests.
-Keep these examples representative: selection and previews do not depend on
-language-specific support. Every inventory and ranking mode uses the same cases.
+Keep these examples representative: security-relevant behavior can live in
+build files, dependencies, tests, CI, documentation, and generated code. Selection
+and previews do not depend on language-specific support or directory names.
"""
from __future__ import annotations
@@ -16,21 +17,34 @@ class SourceCase(NamedTuple):
SOURCE_CASES = (
- SourceCase("scripts/entrypoint", "exec service --before", "exec service --after"),
- SourceCase("src/handler.unlisted", "render(before)", "render(after)"),
SourceCase(
- "src/service.py",
- "def handle():\n return before",
- "def handle():\n return after",
+ "Dockerfile",
+ "FROM python:3.13-slim\nCOPY service.py /app/service.py\nRUN chmod 644 /app/service.py",
+ "FROM python:3.13-slim\nCOPY service.py /app/service.py\nRUN chmod 666 /app/service.py",
),
SourceCase(
- "infra/policy.yaml",
- "access:\n enabled: false",
- "access:\n enabled: true",
+ "vendor/handler.unlisted",
+ 'db.execute("SELECT name FROM records WHERE id = ?", [record_id])',
+ 'db.execute("SELECT name FROM records WHERE id = " + record_id)',
),
SourceCase(
- "src/view.ejs",
- "<% const count = 0; %>\n<p><%= count %></p>",
- "<% const count = 1; %>\n<p><%= count %></p>",
+ "tests/service.py",
+ "def request(endpoint):\n return requests.get(endpoint, verify=True)",
+ "def request(endpoint):\n return requests.get(endpoint, verify=False)",
+ ),
+ SourceCase(
+ ".circleci/config.yaml",
+ "jobs:\n build:\n steps:\n - run: printf '%s' \"$BUILD_LABEL\"",
+ 'jobs:\n build:\n steps:\n - run: sh -c "$BUILD_LABEL"',
+ ),
+ SourceCase(
+ "docs/view.ejs",
+ "<p><%= request.query.title %></p>",
+ "<p><%- request.query.title %></p>",
+ ),
+ SourceCase(
+ "generated/client.min.js",
+ "document.body.textContent=location.hash.slice(1);",
+ "document.body.innerHTML=location.hash.slice(1);",
),
)
diff --git a/plugins/codex-security/tests/test_generate_in_scope_files.py b/plugins/codex-security/tests/test_generate_in_scope_files.py
index 1e4625ece..7c2637467 100644
--- a/plugins/codex-security/tests/test_generate_in_scope_files.py
+++ b/plugins/codex-security/tests/test_generate_in_scope_files.py
@@ -513,7 +513,7 @@ def test_diff_inventory_keeps_changed_and_deleted_source_files(tmp_path: Path, m
write_file(repository, "app/routes.py", b"changed = True\n")
write_file(repository, "app/new handler.py", b"handler = True\n")
write_file(repository, "app/binary.py", b"\x00\xff\x01")
- write_file(repository, "tests/demo.py", b"excluded = True\n")
+ write_file(repository, "tests/demo.py", b"changed = True\n")
write_file(repository, ".github/workflows/ci.yml", b"name: CI\n")
write_file(
repository,
@@ -549,6 +549,7 @@ def test_diff_inventory_keeps_changed_and_deleted_source_files(tmp_path: Path, m
"app/new handler.py",
"app/routes.py",
"app/évidence.py",
+ "tests/demo.py",
]
@@ -684,7 +685,7 @@ def test_diff_inventory_includes_bom_marked_utf16_text(tmp_path: Path, mode: str
"app/decoded-nul.ps1",
b"\xff\xfe" + "text\0binary".encode("utf-16-le"),
)
- write_file(repository, "tests/excluded.ps1", b"\xff\xfe" + source.encode("utf-16-le"))
+ write_file(repository, "tests/encoded.ps1", b"\xff\xfe" + source.encode("utf-16-le"))
arguments = ["--diff-base", base, "--diff-mode", mode]
if mode == "revisions":
@@ -700,6 +701,7 @@ def test_diff_inventory_includes_bom_marked_utf16_text(tmp_path: Path, mode: str
"app/utf16-be.ps1",
"app/utf16-le.ps1",
"app/utf8.ps1",
+ "tests/encoded.ps1",
]
@@ -725,3 +727,29 @@ def test_diff_inventory_rejects_a_narrower_scope(tmp_path: Path) -> None:
assert result.returncode == 2
assert "diff scans must use the repository root" in result.stderr
assert output.read_text(encoding="utf-8") == "previous.py\n"
+
+
[email protected](os.name == "nt", reason="Windows paths cannot retain arbitrary non-UTF-8 bytes")
+def test_diff_inventory_rejects_non_utf8_path_and_preserves_output(tmp_path: Path) -> None:
+ repository = make_repository(tmp_path)
+ git(repository, "add", ".")
+ git(repository, "commit", "-qm", "base")
+ base = git(repository, "rev-parse", "HEAD")
+ blob = git(repository, "rev-parse", f"{base}:app/routes.py")
+ subprocess.run(
+ ["git", "-C", str(repository), "update-index", "-z", "--index-info"],
+ input=b"100644 " + blob.encode("ascii") + b"\ttests/caf\xe9.py\0",
+ check=True,
+ capture_output=True,
+ )
+ head = git(
+ repository, "commit-tree", git(repository, "write-tree"), "-p", base, "-m", "Byte path"
+ )
+ output = tmp_path / "in_scope_files.txt"
+ output.write_text("previous.py\n", encoding="utf-8")
+ result = run_inventory(
+ repository, ".", output, arguments=["--diff-base", base, "--diff-head", head]
+ )
+ assert result.returncode == 2
+ assert "cannot be encoded as UTF-8 for the file inventory" in result.stderr
+ assert output.read_text(encoding="utf-8") == "previous.py\n"
diff --git a/plugins/codex-security/tests/test_generate_rank_input.py b/plugins/codex-security/tests/test_generate_rank_input.py
index 47eb468c1..61d2c0420 100644
--- a/plugins/codex-security/tests/test_generate_rank_input.py
+++ b/plugins/codex-security/tests/test_generate_rank_input.py
@@ -65,7 +65,7 @@ def test_make_repo_rank_input_matches_golden_and_filters_noise(tmp_path: Path) -
(repo / "src" / "binary.py").write_bytes(b"value\x00binary")
(repo / "src" / "binary.unlisted").write_bytes(b"x" * (64 * 1024 + 1) + b"\0binary")
(repo / "tests" / "ignored.py").write_text("ignored = True", encoding="utf-8")
- (repo / "README.md").write_text("ignored", encoding="utf-8")
+ (repo / "README.md").write_text("Deployment instructions", encoding="utf-8")
output = tmp_path / "rank_input.jsonl"
run_cli(
@@ -408,7 +408,8 @@ def test_make_repo_rank_input_combines_explicit_files_and_directories(tmp_path:
)
-def test_make_repo_scope_input_preserves_every_requested_directory_file(tmp_path: Path) -> None:
[email protected]("command", ["make-repo-scope-input", "make-repo-rank-input"])
+def test_repo_input_preserves_every_requested_directory_file(tmp_path: Path, command: str) -> None:
repo = tmp_path / "repo"
(repo / "src" / "tests").mkdir(parents=True)
(repo / "src" / "examples").mkdir()
@@ -421,11 +422,11 @@ def test_make_repo_scope_input_preserves_every_requested_directory_file(tmp_path
(repo / "src" / "Dockerfile").write_text("FROM scratch", encoding="utf-8")
(repo / "src" / ".git" / "config").write_text("private", encoding="utf-8")
scopes = tmp_path / "target-paths.json"
- scopes.write_text(json.dumps(["src", "src/runtime.py"]), encoding="utf-8")
+ scopes.write_text(json.dumps(["src", "src/runtime.py", "src/.git/config"]), encoding="utf-8")
output = tmp_path / "scoped-source-input.jsonl"
run_cli(
- "make-repo-scope-input",
+ command,
"--repo",
str(repo),
"--scopes-file",
@@ -434,12 +435,12 @@ def test_make_repo_scope_input_preserves_every_requested_directory_file(tmp_path
str(output),
)
- assert read_jsonl(output) == [
- {"path": "src/Dockerfile"},
- {"path": "src/examples/demo.py"},
- {"path": "src/fixtures/payload.txt"},
- {"path": "src/runtime.py"},
- {"path": "src/tests/handler.py"},
+ assert [row["path"] for row in read_jsonl(output)] == [
+ "src/Dockerfile",
+ "src/examples/demo.py",
+ "src/fixtures/payload.txt",
+ "src/runtime.py",
+ "src/tests/handler.py",
]
@@ -496,7 +497,58 @@ def test_make_repo_scope_input_rejects_explicit_symlink_scopes(tmp_path: Path, s
assert not output.exists()
-def test_make_repo_scope_input_preserves_tracked_ignored_and_binary_files(tmp_path: Path) -> None:
[email protected]("command", ["make-repo-scope-input", "make-repo-rank-input"])
[email protected]("scope", ["src", "."])
[email protected]("replacement", ["link", "loop", "file"])
+def test_repo_input_handles_replaced_tracked_directories(
+ tmp_path: Path, command: str, scope: str, replacement: str
+) -> None:
+ repo = tmp_path / "repo"
+ nested = repo / "src" / "nested"
+ nested.mkdir(parents=True)
+ initialize_repo(repo)
+ (repo / ".gitignore").write_text("private/\n", encoding="utf-8")
+ (repo / "src" / "handler.py").write_text("handler = True\n", encoding="utf-8")
+ (nested / "service.py").write_text("service = True\n", encoding="utf-8")
+ git(repo, "add", ".")
+ git(repo, "commit", "-qm", "base")
+ private = repo / "private"
+ private.mkdir()
+ (private / "service.py").write_text("outside_requested_scope = True\n", encoding="utf-8")
+ (nested / "service.py").unlink()
+ nested.rmdir()
+ if replacement == "link":
+ nested.symlink_to(private, target_is_directory=True)
+ elif replacement == "loop":
+ nested.symlink_to(nested.name, target_is_directory=True)
+ else:
+ nested.write_text("replacement = True\n", encoding="utf-8")
+ scopes = tmp_path / "target-paths.json"
+ scopes.write_text(json.dumps([scope]), encoding="utf-8")
+ output = tmp_path / "input.jsonl"
+
+ run_cli(
+ command,
+ "--repo",
+ str(repo),
+ "--scopes-file",
+ str(scopes),
+ "--out",
+ str(output),
+ )
+
+ expected = ["src/handler.py"]
+ if replacement == "file":
+ expected.append("src/nested")
+ if scope == ".":
+ expected.insert(0, ".gitignore")
+ assert [row["path"] for row in read_jsonl(output)] == expected
+
+
[email protected]("command", ["make-repo-scope-input", "make-repo-rank-input"])
+def test_repo_input_preserves_tracked_ignored_and_binary_files(
+ tmp_path: Path, command: str
+) -> None:
repo = tmp_path / "repo"
source = repo / "src"
vendor = source / "vendor"
@@ -511,7 +563,7 @@ def test_make_repo_scope_input_preserves_tracked_ignored_and_binary_files(tmp_pa
output = tmp_path / "scoped-source-input.jsonl"
run_cli(
- "make-repo-scope-input",
+ command,
"--repo",
str(repo),
"--scopes-file",
@@ -520,13 +572,14 @@ def test_make_repo_scope_input_preserves_tracked_ignored_and_binary_files(tmp_pa
str(output),
)
- assert read_jsonl(output) == [
- {"path": "src/logo.png"},
- {"path": "src/vendor/dependency.py"},
- ]
+ expected = ["src/vendor/dependency.py"]
+ if command == "make-repo-scope-input":
+ expected.insert(0, "src/logo.png")
+ assert [row["path"] for row in read_jsonl(output)] == expected
-def test_make_repo_scope_input_respects_ignored_directory_descendants(tmp_path: Path) -> None:
[email protected]("command", ["make-repo-scope-input", "make-repo-rank-input"])
+def test_repo_input_respects_ignored_directory_descendants(tmp_path: Path, command: str) -> None:
repo = tmp_path / "repo"
(repo / "src" / "node_modules").mkdir(parents=True)
(repo / "src" / "nested").mkdir()
@@ -542,7 +595,7 @@ def test_make_repo_scope_input_respects_ignored_directory_descendants(tmp_path:
output = tmp_path / "scoped-source-input.jsonl"
run_cli(
- "make-repo-scope-input",
+ command,
"--repo",
str(repo),
"--scopes-file",
@@ -558,7 +611,8 @@ def test_make_repo_scope_input_respects_ignored_directory_descendants(tmp_path:
}
-def test_make_repo_scope_input_keeps_explicitly_requested_ignored_file(tmp_path: Path) -> None:
[email protected]("command", ["make-repo-scope-input", "make-repo-rank-input"])
+def test_repo_input_keeps_explicitly_requested_ignored_file(tmp_path: Path, command: str) -> None:
repo = tmp_path / "repo"
(repo / "src").mkdir(parents=True)
(repo / ".gitignore").write_text(".env\n", encoding="utf-8")
@@ -568,7 +622,7 @@ def test_make_repo_scope_input_keeps_explicitly_requested_ignored_file(tmp_path:
output = tmp_path / "scoped-source-input.jsonl"
run_cli(
- "make-repo-scope-input",
+ command,
"--repo",
str(repo),
"--scopes-file",
@@ -577,10 +631,11 @@ def test_make_repo_scope_input_keeps_explicitly_requested_ignored_file(tmp_path:
str(output),
)
- assert read_jsonl(output) == [{"path": "src/.env"}]
+ assert [row["path"] for row in read_jsonl(output)] == ["src/.env"]
-def test_make_repo_scope_input_uses_git_ignore_rules(tmp_path: Path) -> None:
[email protected]("command", ["make-repo-scope-input", "make-repo-rank-input"])
+def test_repo_input_uses_git_ignore_rules(tmp_path: Path, command: str) -> None:
repo = tmp_path / "repo"
(repo / "src").mkdir(parents=True)
initialize_repo(repo)
@@ -592,7 +647,7 @@ def test_make_repo_scope_input_uses_git_ignore_rules(tmp_path: Path) -> None:
output = tmp_path / "scoped-source-input.jsonl"
run_cli(
- "make-repo-scope-input",
+ command,
"--repo",
str(repo),
"--scopes-file",
@@ -601,11 +656,12 @@ def test_make_repo_scope_input_uses_git_ignore_rules(tmp_path: Path) -> None:
str(output),
)
- assert read_jsonl(output) == [{"path": "src/handler.py"}]
+ assert [row["path"] for row in read_jsonl(output)] == ["src/handler.py"]
-def test_make_repo_scope_input_falls_back_without_git_or_ripgrep(
- tmp_path: Path, monkeypatch: pytest.MonkeyPatch
[email protected]("command", ["make-repo-scope-input", "make-repo-rank-input"])
+def test_repo_input_falls_back_without_git_or_ripgrep(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, command: str
) -> None:
repo = tmp_path / "repo"
(repo / "src").mkdir(parents=True)
@@ -616,7 +672,7 @@ def test_make_repo_scope_input_falls_back_without_git_or_ripgrep(
monkeypatch.setenv("PATH", str(tmp_path / "missing-tools"))
run_cli(
- "make-repo-scope-input",
+ command,
"--repo",
str(repo),
"--scopes-file",
@@ -625,11 +681,12 @@ def test_make_repo_scope_input_falls_back_without_git_or_ripgrep(
str(output),
)
- assert read_jsonl(output) == [{"path": "src/handler.py"}]
+ assert [row["path"] for row in read_jsonl(output)] == ["src/handler.py"]
-def test_make_repo_scope_input_fails_closed_when_ignore_rules_cannot_be_applied(
- tmp_path: Path, monkeypatch: pytest.MonkeyPatch
[email protected]("command", ["make-repo-scope-input", "make-repo-rank-input"])
+def test_repo_input_fails_closed_when_ignore_rules_cannot_be_applied(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, command: str
) -> None:
repo = tmp_path / "repo"
(repo / "src").mkdir(parents=True)
@@ -641,7 +698,7 @@ def test_make_repo_scope_input_fails_closed_when_ignore_rules_cannot_be_applied(
monkeypatch.setenv("PATH", str(tmp_path / "missing-tools"))
result = run_cli(
- "make-repo-scope-input",
+ command,
"--repo",
str(repo),
"--scopes-file",
@@ -656,8 +713,9 @@ def test_make_repo_scope_input_fails_closed_when_ignore_rules_cannot_be_applied(
assert not output.exists()
-def test_make_repo_scope_input_fails_closed_for_git_private_excludes_without_tools(
- tmp_path: Path, monkeypatch: pytest.MonkeyPatch
[email protected]("command", ["make-repo-scope-input", "make-repo-rank-input"])
+def test_repo_input_fails_closed_for_git_private_excludes_without_tools(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, command: str
) -> None:
repo = tmp_path / "repo"
(repo / ".git" / "info").mkdir(parents=True)
@@ -670,7 +728,7 @@ def test_make_repo_scope_input_fails_closed_for_git_private_excludes_without_too
monkeypatch.setenv("PATH", str(tmp_path / "missing-tools"))
result = run_cli(
- "make-repo-scope-input",
+ command,
"--repo",
str(repo),
"--scopes-file",
@@ -764,13 +822,15 @@ def test_make_diff_rank_input_keeps_changed_and_deleted_text(tmp_path: Path, mod
deleted_guard.unlink()
deleted_entrypoint.unlink()
deleted_binary.unlink()
- (repo / "README.md").write_text("ignored", encoding="utf-8")
+ (repo / "README.md").write_text("Deployment instructions", encoding="utf-8")
arguments = ["--mode", "local-patch"]
if mode in {"revisions", "staged"}:
git(repo, "add", ".")
git(repo, "update-index", "--force-remove", "removed-module")
git(repo, "update-index", "--add", "--cacheinfo", f"160000,{gitlink},added-module")
if mode == "revisions":
+ link_blob = git(repo, "hash-object", "-w", "--stdin", input="../outside")
+ git(repo, "update-index", "--add", "--cacheinfo", f"120000,{link_blob},tests/link.py")
git(repo, "commit", "-qm", "change")
arguments = ["--head", git(repo, "rev-parse", "HEAD")]
git(repo, "checkout", "-q", base)
@@ -791,6 +851,7 @@ def test_make_diff_rank_input_keeps_changed_and_deleted_text(tmp_path: Path, mod
assert [row["path"] for row in rows] == [
".github/workflows/ci.yml",
".gitmodules",
+ "README.md",
"src/alpha.py",
"src/beta.py",
"src/deleted_guard.py",
@@ -904,7 +965,7 @@ def test_make_diff_rank_input_combines_staged_and_unstaged_patch(tmp_path: Path)
index_only = repo / "src" / "index-only.py"
index_only.write_text("index_only = True", encoding="utf-8")
git(repo, "add", "src/index-only.py")
- index_only.unlink()
+ index_only.unlink() # The selected working tree no longer contains this staged addition.
nested = repo / "nested"
nested.mkdir()
initialize_repo(nested)
@@ -929,9 +990,7 @@ def test_make_diff_rank_input_combines_staged_and_unstaged_patch(tmp_path: Path)
".github/workflows/ci.yaml",
"src/alpha.py",
"src/beta.py",
- "src/index-only.py",
]
- assert read_jsonl(output)[-1]["preview"] == ""
@pytest.mark.parametrize("mode", ["repo", "explicit-file", "revisions", "local-patch"])
diff --git a/sdk/typescript/src/custom-validation-prompt.ts b/sdk/typescript/src/custom-validation-prompt.ts
index e796fe4ac..2c4c7b0d7 100644
--- a/sdk/typescript/src/custom-validation-prompt.ts
+++ b/sdk/typescript/src/custom-validation-prompt.ts
@@ -13,7 +13,7 @@ const SOURCES = {
"skills/security-scan/SKILL.md":
"104e2f93fc965c34e91970f517a89e330d5b29dbb243dcea5d95d83f14d135cf",
"skills/security-diff-scan/SKILL.md":
- "653df6b1127141fb35bd0d6eea4ce5e2e764f5f5cede91b6fc7f3b3eb3a65874",
+ "31d7a4d9c12a92a8701f643c1b37fe1d2581371ab1fd565b74a4211eb3e57fde",
} as const;
const DISABLED_TOOLS = [
diff --git a/sdk/typescript/tests-ts/compact-diff-scan.test.ts b/sdk/typescript/tests-ts/compact-diff-scan.test.ts
index 32740de40..183407515 100644
--- a/sdk/typescript/tests-ts/compact-diff-scan.test.ts
+++ b/sdk/typescript/tests-ts/compact-diff-scan.test.ts
@@ -136,7 +136,7 @@ describe("compact diff scan", () => {
writeSource(repository, "src/handler.py", "value = 2\n");
writeSource(repository, "src/new handler.py", "created = True\n");
writeSource(repository, "src/binary.py", Buffer.from([0, 255, 1]));
- writeSource(repository, "tests/ignored.py", "ignored = True\n");
+ writeSource(repository, "tests/example.py", "test_setup = True\n");
git(repository, "add", ".");
git(repository, "commit", "-qm", "selected changes");
const head = git(repository, "rev-parse", "HEAD");
@@ -162,6 +162,7 @@ describe("compact diff scan", () => {
"src/guard.py",
"src/handler.py",
"src/new handler.py",
+ "tests/example.py",
]);
});
diff --git a/sdk/typescript/tests-ts/diff-rank-input.test.ts b/sdk/typescript/tests-ts/diff-rank-input.test.ts
index 48ce8664d..c0fe64a59 100644
--- a/sdk/typescript/tests-ts/diff-rank-input.test.ts
+++ b/sdk/typescript/tests-ts/diff-rank-input.test.ts
@@ -95,8 +95,6 @@ test("diff previews stay inside the selected repository", () => {
"src/deleted.py",
"src/entry.py",
"src/handler.py",
- // Git lists the untracked POSIX symlink, but not the Windows junction.
- ...(process.platform === "win32" ? [] : ["src/nested"]),
"src/nested/linked.py",
]);
expect(rows.find((row) => row.path === "src/handler.py")?.preview).toBe(
@@ -105,9 +103,6 @@ test("diff previews stay inside the selected repository", () => {
expect(rows.find((row) => row.path === "src/nested/linked.py")?.preview).toBe(
"",
);
- if (process.platform !== "win32") {
- expect(rows.find((row) => row.path === "src/nested")?.preview).toBe("");
- }
});
test("preserves Unicode Git paths and legacy-encoded commit metadata", () => {