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): isolate Git display settings in content snapshots (#2568)
Michael D'Angeloba6702440e7c
diff --git a/plugins/codex-security/mcp-app/tests/support/fake-codex-source.ts b/plugins/codex-security/mcp-app/tests/support/fake-codex-source.tsindex 9e2a92cae..51ae582c3 100644--- a/plugins/codex-security/mcp-app/tests/support/fake-codex-source.ts+++ b/plugins/codex-security/mcp-app/tests/support/fake-codex-source.ts@@ -21,7 +21,7 @@ const accountResult = ${JSON.stringify(accountResult)};const preflightMarkerPath = process.env.FAKE_CODEX_PREFLIGHT_MARKER ?? ${JSON.stringify(preflightMarkerPath)};const temporaryEnvironment = Object.fromEntries(['TEMP', 'TMP'].map(name => [name, process.env[name]]));if (process.argv.includes('app-server')) {- const preflight = { executablePath: process.argv[1], argv: process.argv.slice(2), configPath: process.env.CODEX_SECURITY_CONFIG_PATH, stateDirectory: process.env.CODEX_SECURITY_STATE_DIR, temporaryEnvironment, runnerTrackingId: process.env.RUNNER_TRACKING_ID, libraryPath: process.env["LD_LIBRARY_PATH"], cwd: process.cwd(), codexHome: process.env.CODEX_HOME, providerKey: process.env.SYNTHETIC_GATEWAY_KEY, providerHeaders: Object.fromEntries(['SYNTHETIC_HEADER_VALUE', 'OPENROUTER_API_KEY', 'FIREWORKS_API_KEY'].map(name => [name, process.env[name]])), providerHeader: process.env.SYNTHETIC_HEADER_VALUE, cacheDirectory: process.env.XDG_CACHE_HOME, gitEnvironment: Object.fromEntries(['PATH', 'HOME', 'NODE_OPTIONS', 'NODE_DEBUG', 'NODE_EXTRA_CA_CERTS', 'CODEX_MCP_NODE_PATH', 'CODEX_SECURITY_GIT', 'GIT_SSH_COMMAND', 'GIT_CONFIG_GLOBAL', 'GIT_GLOB_PATHSPECS', 'GIT_ICASE_PATHSPECS'].map(name => [name, process.env[name]])), requests: [] };+ const preflight = { executablePath: process.argv[1], argv: process.argv.slice(2), configPath: process.env.CODEX_SECURITY_CONFIG_PATH, stateDirectory: process.env.CODEX_SECURITY_STATE_DIR, temporaryEnvironment, runnerTrackingId: process.env.RUNNER_TRACKING_ID, libraryPath: process.env["LD_LIBRARY_PATH"], cwd: process.cwd(), codexHome: process.env.CODEX_HOME, providerKey: process.env.SYNTHETIC_GATEWAY_KEY, providerHeaders: Object.fromEntries(['SYNTHETIC_HEADER_VALUE', 'OPENROUTER_API_KEY', 'FIREWORKS_API_KEY'].map(name => [name, process.env[name]])), providerHeader: process.env.SYNTHETIC_HEADER_VALUE, cacheDirectory: process.env.XDG_CACHE_HOME, gitEnvironment: Object.fromEntries(['PATH', 'HOME', 'NODE_OPTIONS', 'NODE_DEBUG', 'NODE_EXTRA_CA_CERTS', 'CODEX_MCP_NODE_PATH', 'CODEX_SECURITY_GIT', 'GIT_SSH_COMMAND', 'GIT_CONFIG_GLOBAL', 'GIT_DIFF_OPTS', 'GIT_GLOB_PATHSPECS', 'GIT_ICASE_PATHSPECS'].map(name => [name, process.env[name]])), requests: [] };writeFileSync(preflightMarkerPath, JSON.stringify(preflight));${jsonLineInputSource(` if (message.method === 'initialized') continue;if (message.method === 'config/read' || message.method === 'permissionProfile/list') {@@ -80,7 +80,7 @@ const pythonRuntime = pythonProbe ? JSON.parse(pythonProbe.stdout) : undefined;const knowledgePath = stdin.includes('synthetic worker configuration fixture') ? process.env.CODEX_SECURITY_KNOWLEDGE_BASE : undefined;const knowledgeDocuments = knowledgePath === undefined ? undefined : Object.fromEntries(readdirSync(knowledgePath).map(name => [name, readFileSync(join(knowledgePath, name), 'utf8')]));const deepConfig = process.env.CODEX_SECURITY_DEEP_SCAN_CONFIG_PATH ? readFileSync(process.env.CODEX_SECURITY_DEEP_SCAN_CONFIG_PATH, 'utf8') : undefined;-writeFileSync(process.env.FAKE_CODEX_MARKER, JSON.stringify({ executablePath: process.argv[1], ...(scopeInput === undefined ? {} : { scopeInput }), argv: process.argv.slice(2), stdin, cwd: process.cwd(), knowledgePath, knowledgeDocuments, deepConfig, codexHome: process.env.CODEX_HOME, temporaryEnvironment, cacheDirectory: process.env.XDG_CACHE_HOME, gitEnvironment: Object.fromEntries(['PATH', 'HOME', 'NODE_OPTIONS', 'NODE_DEBUG', 'NODE_EXTRA_CA_CERTS', 'CODEX_MCP_NODE_PATH', 'CODEX_SECURITY_GIT', 'GIT_SSH_COMMAND', 'GIT_CONFIG_GLOBAL', 'GIT_GLOB_PATHSPECS', 'GIT_ICASE_PATHSPECS'].map(name => [name, process.env[name]])), configPath: process.env.CODEX_SECURITY_CONFIG_PATH, deepConfigPath: process.env.CODEX_SECURITY_DEEP_SCAN_CONFIG_PATH, python: process.env.PYTHON, mcpNodePath: process.env.CODEX_MCP_NODE_PATH, bundledTool: toolProbe?.stdout.trim(), pythonPrefix: pythonRuntime?.[0], pythonLibraryPath: pythonRuntime?.[1], runtimeEnvironment, codexConfig, providerHeaders: Object.fromEntries(['SYNTHETIC_HEADER_VALUE', 'OPENROUTER_API_KEY', 'FIREWORKS_API_KEY'].map(name => [name, process.env[name]])), providerKey: process.env.SYNTHETIC_GATEWAY_KEY, providerHeader: process.env.SYNTHETIC_HEADER_VALUE, originator: process.env.CODEX_INTERNAL_ORIGINATOR_OVERRIDE, ...(profileContents === undefined ? {} : { profileContents }), ...(openaiAuthentication ? { openaiAuthentication } : {}), ...(bedrockAuthentication ? { bedrockAuthentication } : {}) }));+writeFileSync(process.env.FAKE_CODEX_MARKER, JSON.stringify({ executablePath: process.argv[1], ...(scopeInput === undefined ? {} : { scopeInput }), argv: process.argv.slice(2), stdin, cwd: process.cwd(), knowledgePath, knowledgeDocuments, deepConfig, codexHome: process.env.CODEX_HOME, temporaryEnvironment, cacheDirectory: process.env.XDG_CACHE_HOME, gitEnvironment: Object.fromEntries(['PATH', 'HOME', 'NODE_OPTIONS', 'NODE_DEBUG', 'NODE_EXTRA_CA_CERTS', 'CODEX_MCP_NODE_PATH', 'CODEX_SECURITY_GIT', 'GIT_SSH_COMMAND', 'GIT_CONFIG_GLOBAL', 'GIT_DIFF_OPTS', 'GIT_GLOB_PATHSPECS', 'GIT_ICASE_PATHSPECS'].map(name => [name, process.env[name]])), configPath: process.env.CODEX_SECURITY_CONFIG_PATH, deepConfigPath: process.env.CODEX_SECURITY_DEEP_SCAN_CONFIG_PATH, python: process.env.PYTHON, mcpNodePath: process.env.CODEX_MCP_NODE_PATH, bundledTool: toolProbe?.stdout.trim(), pythonPrefix: pythonRuntime?.[0], pythonLibraryPath: pythonRuntime?.[1], runtimeEnvironment, codexConfig, providerHeaders: Object.fromEntries(['SYNTHETIC_HEADER_VALUE', 'OPENROUTER_API_KEY', 'FIREWORKS_API_KEY'].map(name => [name, process.env[name]])), providerKey: process.env.SYNTHETIC_GATEWAY_KEY, providerHeader: process.env.SYNTHETIC_HEADER_VALUE, originator: process.env.CODEX_INTERNAL_ORIGINATOR_OVERRIDE, ...(profileContents === undefined ? {} : { profileContents }), ...(openaiAuthentication ? { openaiAuthentication } : {}), ...(bedrockAuthentication ? { bedrockAuthentication } : {}) }));if (stdin.includes('COMPLETE_THEN_HANG')) process.on('SIGTERM', () => { if (!stdin.includes('IGNORE_TERMINATION')) setTimeout(() => process.exit(0), 100); });if (stdin.includes('THREAD_START_CONFIG_ERROR')) { console.error('Error: thread/start: thread/start failed: agents.max_threads cannot be set when features.multi_agent_v2 is enabled (code -32600)'); process.exit(1); }@@ -140,6 +140,7 @@ export function workerGitEnvironment(root: string) {),CODEX_SECURITY_GIT: path.join(root, "selected tools", "git"),GIT_SSH_COMMAND: "synthetic-ssh --fixture",+ GIT_DIFF_OPTS: "--unified=0",GIT_GLOB_PATHSPECS: "1",GIT_ICASE_PATHSPECS: "1",GIT_CONFIG_GLOBAL: path.join(root, "operator.gitconfig"),diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.tsindex b441c402c..adf48e7d4 100644--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.ts+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.ts@@ -1457,6 +1457,7 @@ async function testWorkerRuntimeSettings() {"CODEX_SECURITY_GIT","GIT_SSH_COMMAND","GIT_CONFIG_GLOBAL",+ "GIT_DIFF_OPTS","GIT_GLOB_PATHSPECS","GIT_ICASE_PATHSPECS","CODEX_CLI_PATH",diff --git a/plugins/codex-security/scripts/workbench_db.py b/plugins/codex-security/scripts/workbench_db.pyindex 716b2589d..d06eca4de 100644--- a/plugins/codex-security/scripts/workbench_db.py+++ b/plugins/codex-security/scripts/workbench_db.py@@ -669,7 +669,8 @@ def require_working_tree_unchanged(target: Path, diff_target: dict[str, str] | Nand diff_target["kind"] == "working_tree"and (git_revision(target) != diff_target["headRevision"]- or worktree_content_digest(target) != diff_target["contentDigest"]+ or worktree_content_digest(target, expected_digest=diff_target["contentDigest"])+ != diff_target["contentDigest"])):raise SystemExit("The selected scan target changed while the scan was starting. Try again.")@@ -2362,7 +2363,9 @@ def require_reviewed_patch_applied() -> str | None:target = require_scan_target_identity(scan)_, content_digest = remediation_checkout_snapshot(- scan, expected_revision=remediation["base_revision"]+ scan,+ expected_revision=remediation["base_revision"],+ expected_digest=remediation["base_content_digest"],)if content_digest == remediation["base_content_digest"]:raise SystemExit(@@ -2421,6 +2424,7 @@ def require_reviewed_patch_applied(pathspec or ".",git_dir=short or metadata,work_tree=checkout_root,+ expected_digest=remediation["base_content_digest"],))if reverted_digest != remediation["base_content_digest"] and unversioned:@@ -2454,9 +2458,6 @@ def require_remediation_checkout_unchanged(require_applied_content: bool = False,require_base_content: bool = False,) -> None:- _, content_digest = remediation_checkout_snapshot(- scan, expected_revision=remediation["base_revision"]- )expected_digest = (remediation["applied_content_digest"]if require_applied_content@@ -2464,6 +2465,9 @@ def require_remediation_checkout_unchanged(if require_base_contentelse None)+ _, content_digest = remediation_checkout_snapshot(+ scan, expected_revision=remediation["base_revision"], expected_digest=expected_digest+ )if expected_digest is not None and content_digest != expected_digest:raise SystemExit("Working-tree contents changed. Regenerate the remediation patch against the current checkout."diff --git a/plugins/codex-security/scripts/workbench_scan_history.py b/plugins/codex-security/scripts/workbench_scan_history.pyindex f87ba5d86..a26a7bb75 100644--- a/plugins/codex-security/scripts/workbench_scan_history.py+++ b/plugins/codex-security/scripts/workbench_scan_history.py@@ -80,8 +80,11 @@ def _windows_resume_checkout(scan: sqlite3.Row) -> Path:except SystemExit as exc:raise SystemExit(replaced) from excwith verified_windows_checkout(scan, repository, replaced_message=replaced):- snapshot = (scan["target_revision"], scan["target_snapshot_digest"])- if scan_target_identity(repository, None)[:2] != snapshot:+ expected = (scan["target_revision"], scan["target_snapshot_digest"])+ current = scan_target_identity(+ repository, None, expected_digest=scan["target_snapshot_digest"]+ )+ if current[:2] != expected:raise SystemExit("Cannot resume: the original checkout revision or contents changed.")return repository@@ -127,7 +130,9 @@ def cli_scan_resume(raise SystemExit("Cannot resume: the original checkout is missing or was replaced.") from exc- if scan_target_identity(repository, None) != (+ if scan_target_identity(+ repository, None, expected_digest=scan["target_snapshot_digest"]+ ) != (scan["target_revision"],scan["target_snapshot_digest"],scan["target_device"],diff --git a/plugins/codex-security/scripts/workbench_scan_start.py b/plugins/codex-security/scripts/workbench_scan_start.pyindex df73b5c71..d81a7ed9c 100644--- a/plugins/codex-security/scripts/workbench_scan_start.py+++ b/plugins/codex-security/scripts/workbench_scan_start.py@@ -65,6 +65,7 @@ def scan_target_identity(diff_target: dict[str, str] | None,*,metadata: os.stat_result | None = None,+ expected_digest: str | None = None,) -> tuple[str, str | None, int | str, int | str]:"""Return revision, content digest, device ID, and inode for scan registration.@@ -79,7 +80,7 @@ def scan_target_identity(snapshot_digest = (directory_content_digest(target)if revision == "unversioned"- else worktree_content_digest(target)+ else worktree_content_digest(target, expected_digest=expected_digest))return (revision,diff --git a/plugins/codex-security/scripts/workbench_setup.py b/plugins/codex-security/scripts/workbench_setup.pyindex 6117ce609..dfb371e4d 100644--- a/plugins/codex-security/scripts/workbench_setup.py+++ b/plugins/codex-security/scripts/workbench_setup.py@@ -76,7 +76,7 @@ def require_diff_target(if kind == "working_tree":base = resolve_git_commit(target, base_revision or "HEAD", "Working-tree base")head = resolve_git_commit(target, head_revision or current_head, "Working-tree HEAD")- current_digest = worktree_content_digest(target)+ current_digest = worktree_content_digest(target, expected_digest=content_digest)if base != current_head or head != current_head:raise SystemExit("Repository HEAD changed after these working-tree changes were selected. "diff --git a/plugins/codex-security/scripts/workbench_target.py b/plugins/codex-security/scripts/workbench_target.pyindex 612ec0107..3b6ed3dbb 100644--- a/plugins/codex-security/scripts/workbench_target.py+++ b/plugins/codex-security/scripts/workbench_target.py@@ -585,6 +585,56 @@ def _visible_git_character(char: str) -> str:return ascii(char)[1:-1]+def _canonical_patch_chunks(output: BinaryIO) -> Iterator[bytes]:+ """Drop Git's optional function label while preserving hunk coordinates and content."""+ line_start = True+ # Limit each read even when source lines are arbitrarily large.+ for part in iter(lambda: output.readline(1024 * 1024), b""):+ next_line_start = part.endswith(b"\n")+ if line_start and part.startswith(b"@@ "):+ coordinates, end, _ = part.partition(b" @@")+ if end:+ part = coordinates + end + b"\n"+ yield part+ line_start = next_line_start+++@contextmanager+def _diff_driver_config(+ target: Path, git_dir: Path | None, work_tree: Path | None+) -> Iterator[tuple[str, ...]]:+ names = git_command(+ target,+ "config",+ "--name-only",+ "--null",+ "--get-regexp",+ r"^diff\..+\.binary$",+ text=False,+ git_dir=git_dir,+ work_tree=work_tree,+ environment_overrides={"LC_ALL": "C"},+ )+ if names.returncode == 1: # Git returns 1 when there are no matching keys.+ yield ()+ return+ if names.returncode:+ raise SystemExit(+ os.fsdecode(names.stderr) or "Could not inspect Git diff-driver configuration."+ )+ # A private command-scope include preserves case and arbitrary driver-name bytes+ # without putting an unbounded number of keys on the command line.+ with tempfile.TemporaryDirectory(prefix="codex-security-diff-") as directory:+ config = Path(directory) / "drivers"+ with config.open("wb") as stream:+ for key in names.stdout.split(b"\0"):+ if key:+ driver = key.removeprefix(b"diff.").removesuffix(b".binary")+ escaped = driver.replace(b"\\", b"\\\\").replace(b'"', b'\\"')+ stream.write(b'[diff "' + escaped + b'"]\n\tbinary = auto\n')+ yield "-c", f"include.path={config}"++def _update_digest_field_from_git(digest: Any,label: bytes,@@ -592,15 +642,24 @@ def _update_digest_field_from_git(*args: str,git_dir: Path | None = None,work_tree: Path | None = None,+ environment_overrides: dict[str, str] | None = None,+ stable_display: bool = False,) -> None:- with tempfile.TemporaryFile() as output:+ with tempfile.TemporaryFile() as output, ExitStack() as stack:+ driver_config = (+ stack.enter_context(_diff_driver_config(target, git_dir, work_tree))+ if stable_display+ else ()+ )completed = git_command(target,+ *driver_config,*args,text=False,git_dir=git_dir,work_tree=work_tree,stdout_file=output,+ environment_overrides=environment_overrides,)if completed.returncode != 0:detail = completed.stderr.decode("utf-8", errors="backslashreplace").replace(@@ -610,25 +669,36 @@ def _update_digest_field_from_git(raise SystemExit(f"Could not snapshot the selected working-tree changes.\n{detail}".rstrip("\n"))- output.seek(0, os.SEEK_END)- value_size = output.tell()+ if stable_display:+ output.seek(0)+ value_size = sum(len(part) for part in _canonical_patch_chunks(output))+ else:+ value_size = output.seek(0, os.SEEK_END)_update_digest_field_header(digest, label, value_size)output.seek(0)- for chunk in iter(lambda: output.read(1024 * 1024), b""):+ chunks = (+ _canonical_patch_chunks(output)+ if stable_display+ else iter(lambda: output.read(1024 * 1024), b"")+ )+ for chunk in chunks:digest.update(chunk)-def worktree_content_digest(target: Path) -> str:+def worktree_content_digest(target: Path, *, expected_digest: str | None = None) -> str:"""Return the worktree digest for the selected path after checking the state of its submodules."""require_clean_submodule_worktrees(target)repository, pathspec = git_worktree_context(target)- return worktree_content_digest_for_context(repository, pathspec)+ return worktree_content_digest_for_context(+ repository, pathspec, expected_digest=expected_digest+ )def remediation_checkout_snapshot(scan: sqlite3.Row,*,expected_revision: str | None = None,+ expected_digest: str | None = None,head_changed_message: str = ("Repository HEAD changed. Regenerate the remediation patch against the current checkout."),@@ -640,7 +710,7 @@ def remediation_checkout_snapshot(if revision != required_revision:raise SystemExit(head_changed_message)content_digest = (- worktree_content_digest(target)+ worktree_content_digest(target, expected_digest=expected_digest)if revision != "unversioned"else directory_content_digest(target, excluded=(Path(scan["scan_dir"]),)))@@ -661,12 +731,16 @@ def require_scan_target_unchanged(scan: sqlite3.Row) -> None:raise SystemExit(head_changed_message)require_clean_submodule_worktrees(target)return- _, current_digest = remediation_checkout_snapshot(- scan, head_changed_message=head_changed_message- )if scan["diff_target_kind"] == "working_tree":expected_digest = scan["diff_content_digest"]elif scan["diff_target_kind"] in {"commit", "range"}:+ expected_digest = clean_worktree_content_digest()+ else:+ expected_digest = scan["target_snapshot_digest"]+ _, current_digest = remediation_checkout_snapshot(+ scan, expected_digest=expected_digest, head_changed_message=head_changed_message+ )+ if scan["diff_target_kind"] in {"commit", "range"}:target = Path(scan["target_path"])tracked = git_bytes(target, "ls-files", "-v", "-z")if tracked is None:@@ -679,9 +753,6 @@ def require_scan_target_unchanged(scan: sqlite3.Row) -> None:)if any(entry.startswith(b"h ") for entry in entries):verify_assumed_unchanged_files(target)- expected_digest = clean_worktree_content_digest()- else:- expected_digest = scan["target_snapshot_digest"]if current_digest != expected_digest:raise SystemExit("Scan target contents changed since the scan. "@@ -728,6 +799,28 @@ def worktree_content_digest_for_context(*,git_dir: Path | None = None,work_tree: Path | None = None,+ expected_digest: str | None = None,+) -> str:+ def calculate(stable_display: bool) -> str:+ return _git_content_digest(repository, pathspec, git_dir, work_tree, stable_display)++ current = calculate(True)+ # Older sealed snapshots may have included the user's Git display settings.+ if (+ expected_digest is not None+ and current != expected_digest+ and calculate(False) == expected_digest+ ):+ return expected_digest+ return current+++def _git_content_digest(+ repository: Path,+ pathspec: str,+ git_dir: Path | None,+ work_tree: Path | None,+ stable_display: bool,) -> str:digest = hashlib.sha256()update_digest_field(digest, b"format", b"codex-security-snapshot/v1")@@ -736,12 +829,44 @@ def worktree_content_digest_for_context(":(top,glob,exclude)**/.svn",":(top,glob,exclude)**/.svn/**",]+ config = (+ (+ "-c",+ "core.quotePath=true",+ "-c",+ "diff.suppressBlankEmpty=false",+ "-c",+ "core.compression=1", # Git uses the fastest zlib level for binary patches by default.+ )+ if stable_display+ else ()+ )+ display = (+ (+ "--no-color",+ "--src-prefix=a/",+ "--dst-prefix=b/",+ "--unified=3",+ "--diff-algorithm=myers",+ "--indent-heuristic",+ "--inter-hunk-context=0",+ "--find-renames=50%",+ "-l1000", # Git's default inexact rename limit.+ "--submodule=short",+ "-O",+ os.devnull,+ )+ if stable_display+ else ()+ )_update_digest_field_from_git(digest,b"tracked-diff",repository,+ *config,"--no-literal-pathspecs","diff",+ *display,"--binary","--full-index","--no-ext-diff",@@ -752,6 +877,9 @@ def worktree_content_digest_for_context(*source_pathspecs,git_dir=git_dir,work_tree=work_tree,+ # GIT_DIFF_OPTS overrides even an explicit --unified option.+ environment_overrides={"GIT_DIFF_OPTS": ""} if stable_display else None,+ stable_display=stable_display,)untracked = git_bytes(repository,@@ -1570,7 +1698,7 @@ def scan_target_warning(scan: sqlite3.Row) -> str | None:expected_digest = (scan["diff_content_digest"] if working_tree else scan["target_snapshot_digest"])- if worktree_content_digest(target) != expected_digest:+ if worktree_content_digest(target, expected_digest=expected_digest) != expected_digest:return ("Working-tree contents changed while the scan was running; ""results were saved for the original snapshot."diff --git a/plugins/codex-security/tests/test_workbench_target.py b/plugins/codex-security/tests/test_workbench_target.pyindex 5f2b0494e..806d6445c 100644--- a/plugins/codex-security/tests/test_workbench_target.py+++ b/plugins/codex-security/tests/test_workbench_target.py@@ -5,6 +5,7 @@ from __future__ import annotationsimport difflibimport errnoimport hashlib+import ioimport osimport runpyimport shutil@@ -1570,6 +1571,184 @@ def test_directory_content_digest_skips_missing_cached_paths(tmp_path: Path) ->assert directory_content_digest(target) == original_digest[email protected]_platform+ ("option", "value"),+ [+ ("color.ui", "always"),+ ("color.diff", "always"),+ ("diff.noprefix", "true"),+ ("diff.mnemonicPrefix", "true"),+ ("diff.context", "0"),+ ("diff.algorithm", "histogram"),+ ("diff.indentHeuristic", "false"),+ ("diff.interHunkContext", "20"),+ ("diff.suppressBlankEmpty", "true"),+ ("diff.orderFile", "order"),+ ("diff.renames", "false"),+ ("diff.renameLimit", "1"),+ ("core.quotePath", "false"),+ ("GIT_DIFF_OPTS", "--unified=0"),+ ],+)+def test_git_diff_display_settings_preserve_selected_working_content(+ tmp_path: Path,+ monkeypatch: pytest.MonkeyPatch,+ workbench_api: dict[str, Any],+ option: str,+ value: str,+) -> None:+ target = tmp_path / "source"+ initialize_git_repository(target)+ path = target / "café.txt"+ path.write_bytes(b"heading\none\nC\nB\nB\nthree\n")+ lines = target / "z-lines.txt"+ original_lines = ["start", "", *(f"line {n}" for n in range(20)), "end"]+ lines.write_text("\n".join(original_lines) + "\n", encoding="utf-8")+ indent = target / "indent.txt"+ indent.write_bytes(b"\ndone()\ndef sample():\n")+ moved = [("first.txt", "new-first.txt"), ("second.txt", "new-second.txt")]+ for name, _ in moved:+ (target / name).write_text(+ "".join(f"{name} line {n}\n" for n in range(20)), encoding="utf-8"+ )+ subprocess.run(["git", "add", "--", "."], cwd=target, check=True)+ subprocess.run(["git", "commit", "-qm", "Track fixture"], cwd=target, check=True)+ indent.write_bytes(b"\ndone()\ndone()\ndef sample():\n")+ for name, destination in moved:+ subprocess.run(["git", "mv", name, destination], cwd=target, check=True)+ with (target / destination).open("ab") as stream:+ stream.write(b"added line\n")+ subprocess.run(["git", "add", "--", destination], cwd=target, check=True)+ path.write_bytes(b"heading\none\nB\nC\nC\nthree\n")+ original_lines[0] = "changed start"+ original_lines[-1] = "changed end"+ lines.write_text("\n".join(original_lines) + "\n", encoding="utf-8")+ if option == "diff.orderFile":+ order_file = tmp_path / "diff order"+ order_file.write_text(lines.name + "\n", encoding="utf-8", newline="\n")+ value = str(order_file)+ selected = workbench_api["require_diff_target"](target, "working_tree", None, None, None)+ if option == "GIT_DIFF_OPTS":+ monkeypatch.setenv(option, value)+ else:+ subprocess.run(["git", "config", "--local", option, value], cwd=target, check=True)++ # Reproduce a stored digest generated by a previous version with this setting.+ old_patch = subprocess.check_output(+ ["git", "diff", "--binary", "--full-index", "--no-ext-diff", "--no-textconv", "HEAD"],+ cwd=target,+ )+ old_hash = hashlib.sha256()+ update_digest_field(old_hash, b"format", b"codex-security-snapshot/v1")+ update_digest_field(old_hash, b"tracked-diff", old_patch)+ legacy = f"codex-security-snapshot/v1:sha256:{old_hash.hexdigest()}"+ assert legacy != selected["contentDigest"]++ def validate(digest: str | None) -> str:+ return workbench_api["require_diff_target"](target, "working_tree", None, None, digest)[+ "contentDigest"+ ]++ assert validate(None) == selected["contentDigest"]+ assert validate(selected["contentDigest"]) == selected["contentDigest"]+ assert validate(legacy) == legacy++ path.write_bytes(b"new source content\n")+ for digest in (selected["contentDigest"], legacy):+ with pytest.raises(SystemExit, match="contents changed"):+ validate(digest)++[email protected]_platform[email protected]("display", ["log", "diff"])+def test_git_submodule_display_preserves_selected_working_content(+ tmp_path: Path, workbench_api: dict[str, Any], display: str+) -> None:+ target = tmp_path / "parent"+ initialize_git_repository(target)+ child = target / "submodule"+ initial = initialize_git_repository(child)+ add_submodule_gitlink(target, initial, ".")+ subprocess.run(["git", "commit", "-qm", "Pin child"], cwd=target, check=True)++ def advance_child(content: str) -> None:+ (child / "README.md").write_text(content + "\n", encoding="utf-8")+ subprocess.run(["git", "commit", "-qam", content], cwd=child, check=True)+ subprocess.run(["git", "add", "--", "submodule"], cwd=target, check=True)++ advance_child("Selected child")+ require_target = workbench_api["require_diff_target"]+ selected = require_target(target, "working_tree", None, None, None)["contentDigest"]+ diff = ["git", "diff", "HEAD", "--", "submodule"]+ original = subprocess.check_output(diff, cwd=target)+ subprocess.run(["git", "config", "--local", "diff.submodule", display], cwd=target, check=True)+ configured = subprocess.check_output(diff, cwd=target)+ assert configured != original+ assert require_target(target, "working_tree", None, None, selected)["contentDigest"] == selected++ advance_child("Changed child")+ with pytest.raises(SystemExit, match="contents changed"):+ require_target(target, "working_tree", None, None, selected)++[email protected]_platform+ ("key", "value", "binary"),+ [+ ("diff.python.xfuncname", "^class.*$", False),+ ("diff.python.binary", "true", False),+ ("diff.Mixed=driver.binary", "true", False),+ ("diff.python.binary", "false", True),+ ("core.compression", "0", True),+ ],+)+def test_git_driver_settings_preserve_selected_working_content(+ tmp_path: Path, workbench_api: dict[str, Any], key: str, value: str, binary: bool+) -> None:+ target = tmp_path / "source"+ initialize_git_repository(target)+ driver = "Mixed=driver" if key == "diff.Mixed=driver.binary" else "python"+ (target / ".gitattributes").write_bytes(f"*.py diff={driver}\n".encode())+ source = target / "example.py"+ initial = (+ b"class Example:\n def sample(self):\n"+ + b"".join(f" a = {n}\n".encode() for n in range(24))+ + (b"\0" + b"abcde" * 4096 if binary else b"")+ )+ source.write_bytes(initial)+ subprocess.run(["git", "add", "--", "."], cwd=target, check=True)+ subprocess.run(["git", "commit", "-qm", "Track example"], cwd=target, check=True)+ source.write_bytes(initial.replace(b"a = 15", b"a = 42"))+ require_target = workbench_api["require_diff_target"]+ selected = require_target(target, "working_tree", None, None, None)["contentDigest"]+ args = ["git", "diff", "--binary", "--full-index", "HEAD"]+ original = subprocess.check_output(args, cwd=target)+ subprocess.run(["git", "config", "--local", key, value], cwd=target, check=True)+ configured = subprocess.check_output(args, cwd=target)+ assert configured != original+ assert require_target(target, "working_tree", None, None, selected)["contentDigest"] == selected+ assert require_target(target, "working_tree", None, None, None)["contentDigest"] == selected+ legacy_hash = hashlib.sha256()+ update_digest_field(legacy_hash, b"format", b"codex-security-snapshot/v1")+ update_digest_field(legacy_hash, b"tracked-diff", configured)+ legacy = f"codex-security-snapshot/v1:sha256:{legacy_hash.hexdigest()}"+ assert require_target(target, "working_tree", None, None, legacy)["contentDigest"] == legacy+ source.write_bytes(initial + b"actual change\n")+ for digest in (selected, legacy):+ with pytest.raises(SystemExit, match="contents changed"):+ require_target(target, "working_tree", None, None, digest)+++def test_snapshot_keeps_hunk_coordinates_and_long_source_lines() -> None:+ header = b"@@ -12,4 +12,5 @@"+ # The next read starts with a header-shaped string in the middle of an added line.+ long_line = b"+" + b"x" * (1024 * 1024 - 1) + header + b" literal content\n"+ patch = header + b" def sample():\n" + long_line + header + b"\n"+ chunks = WORKBENCH_TARGET["_canonical_patch_chunks"](io.BytesIO(patch))+ assert b"".join(chunks) == header + b"\n" + long_line + header + b"\n"++def test_worktree_content_digest_streams_tracked_binary_patch(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: