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(workbench): register nested Windows scans with long Git metadata paths (#2534)
Michael D'Angelo9ea8f5b56f8a
diff --git a/plugins/codex-security/scripts/windows_scan_local_files.py b/plugins/codex-security/scripts/windows_scan_local_files.pyindex 46f3cb191..3c80911db 100644--- a/plugins/codex-security/scripts/windows_scan_local_files.py+++ b/plugins/codex-security/scripts/windows_scan_local_files.py@@ -1010,14 +1010,19 @@ def scan_root_identity(scan_dir: Path) -> tuple[Path, tuple[int, int]]:@contextlib.contextmanager-def pinned_root_git_marker(cwd: Path, metadata: Path) -> Iterator[Path | None]:- """Bind a canonical worktree beneath a fixed-drive root to its exact .git marker."""+def pinned_root_git_marker(+ cwd: Path, metadata: Path, *, work_tree: Path | None = None+) -> Iterator[Path | None]:+ """Return a short explicit worktree marker while its cwd and Git metadata are pinned."""+ if work_tree is None:+ work_tree = cwdif (not cwd.is_absolute()or not metadata.is_absolute()or len(cwd.drive) != 2or cwd.drive != metadata.drive- or len(cwd.parts) < 2+ or len(work_tree.parts) < 2+ or cwd.parts[: len(work_tree.parts)] != work_tree.partsor len(metadata.parts) < 2or _GetDriveTypeW(cwd.anchor) != _DRIVE_FIXED):@@ -1037,9 +1042,11 @@ def pinned_root_git_marker(cwd: Path, metadata: Path) -> Iterator[Path | None]:assert handle.value is not Noneif _final_path(handle.value) != _extended_path(path):raise ValueError("the selected Git directory spelling is not canonical")- if str(cwd / ".git") != str(metadata):+ root_marker = work_tree / ".git"+ reference = Path(os.path.relpath(root_marker, cwd))+ if str(root_marker) != str(metadata):# The literal canonical root is already pinned before opening its regular file.- fd, leaf = open_read_fd_with_path(cwd, ".git", "Git metadata pointer")+ fd, leaf = open_read_fd_with_path(work_tree, ".git", "Git metadata pointer")pointer = pins.enter_context(os.fdopen(fd, "rb"))if leaf != ".git":raise ValueError("the root marker name does not match .git")@@ -1047,7 +1054,10 @@ def pinned_root_git_marker(cwd: Path, metadata: Path) -> Iterator[Path | None]:value = pointer.read(len(expected) + 3).removesuffix(b"\n").removesuffix(b"\r")if value.replace(b"/", b"\\") != expected:raise ValueError("the root marker does not name the selected Git directory")- marker = Path(".git")+ # A deep scope can make the relative spelling longer than the gitfile itself.+ if len(os.fsencode(root_marker)) < len(os.fsencode(reference)):+ reference = root_marker+ marker = referenceexcept (OSError, ValueError):pins.close()yield markerdiff --git a/plugins/codex-security/scripts/workbench_target.py b/plugins/codex-security/scripts/workbench_target.pyindex 4c395e92f..b6a836ea7 100644--- a/plugins/codex-security/scripts/workbench_target.py+++ b/plugins/codex-security/scripts/workbench_target.py@@ -869,7 +869,7 @@ def _windows_inventory_query(input_data: bytes | None = None,environment_overrides: dict[str, str] | None = None,) -> subprocess.CompletedProcess[bytes]:- """Retry a root inventory's metadata length error with its verified literal marker."""+ """Retry an inventory's metadata length error with its verified worktree marker."""def query(directory: Path, environment: dict[str, str] | None@@ -891,7 +891,7 @@ def _windows_inventory_query(if (result.returncode != 128or result.stdout- or str(target) != str(work_tree)+ or not target.is_relative_to(work_tree)or b"GIT_DIR" not in result.stderr):return result@@ -901,7 +901,7 @@ def _windows_inventory_query(probe = query(git_dir, {**(environment_overrides or {}), "LC_ALL": "C", "LANGUAGE": "C"})if probe.returncode != 128 or probe.stdout or diagnostic not in probe.stderr.splitlines():return result- with pinned_root_git_marker(target, git_dir) as marker:+ with pinned_root_git_marker(target, git_dir, work_tree=work_tree) as marker:if marker is not None:retry = query(marker, environment_overrides)if retry.returncode == 0 or (retry.returncode == 1 and "check-ignore" in args):diff --git a/plugins/codex-security/tests/test_workbench_target.py b/plugins/codex-security/tests/test_workbench_target.pyindex 512e67480..3d311fdc7 100644--- a/plugins/codex-security/tests/test_workbench_target.py+++ b/plugins/codex-security/tests/test_workbench_target.py@@ -1582,6 +1582,8 @@ def test_copy_git_worktree_preserves_dangling_windows_directory_symlink(tmp_path"case-marker","relative-pointer","conditional",+ "scoped-conditional",+ "deep-gitfile",],)def test_windows_root_inventory_retries_the_selected_git_directory(@@ -1608,12 +1610,29 @@ def test_windows_root_inventory_retries_the_selected_git_directory(marker_name = ".GIT" if case == "case-marker" else ".git"(worktree / marker_name).write_text(f"gitdir: {spelling}\n", encoding="utf-8")cwd = worktree- if case == "scoped":- cwd = worktree / "subdir"- cwd.mkdir()+ expected = b"README.md\0"+ scoped = case in {"scoped", "scoped-conditional"}+ if scoped:+ scope = Path("component[ab]", "nested")+ cwd = worktree / scope+ cwd.mkdir(parents=True)+ (source / scope).mkdir(parents=True)+ (source / scope / "selected.txt").write_text("selected\n", encoding="utf-8")+ subprocess.run(+ ["git", "add", "--", (scope / "selected.txt").as_posix()], cwd=source, check=True+ )+ expected = b"selected.txt\0"+ if case == "deep-gitfile":+ while len(os.fsencode(os.path.relpath(worktree / ".git", cwd))) <= len(+ os.fsencode(worktree / ".git")+ ):+ cwd /= "d"+ cwd.mkdir(parents=True)native = workbench_target.git_commandargs = ("ls-files", "--cached", "-z", "--", ".")- expected = b"README.md\0"+ marker = Path("..", "..", ".git") if scoped else Path(".git")+ if case == "deep-gitfile":+ marker = worktree / ".git"expected_status = 0input_data = Noneoverrides = {"GIT_TRACE_SETUP": "1"}@@ -1626,21 +1645,19 @@ def test_windows_root_inventory_retries_the_selected_git_directory(overrides["GIT_NOGLOB_PATHSPECS"] = "0"expected_status = 1expected = b""- if case == "conditional":+ if case in {"conditional", "scoped-conditional"}:excludes = tmp_path / "ignore"excludes.write_text("visible.fixture\n", encoding="utf-8")include = tmp_path / "included"include.write_text(f'[core]\n excludesFile = "{excludes.as_posix()}"\n', encoding="utf-8")config = tmp_path / "gitconfig"- config.write_text(- '[includeIf "gitdir:./checkout/**"]\n path = included\n',- encoding="utf-8",- )+ pattern = "./source/.git" if scoped else "./checkout/**"+ config.write_text(f'[includeIf "gitdir:{pattern}"]\n path = included\n', encoding="utf-8")monkeypatch.setenv("GIT_CONFIG_GLOBAL", str(config))monkeypatch.setenv("GIT_CONFIG_NOSYSTEM", "1")- (worktree / "visible.fixture").write_text("selected\n", encoding="utf-8")+ (cwd / "visible.fixture").write_text("selected\n", encoding="utf-8")args = ("ls-files", "--others", "--exclude-standard", "-z", "--", "visible.fixture")- expected = b"visible.fixture\0"+ expected = b"" if scoped else b"visible.fixture\0"english = subprocess.CompletedProcess([], 128, b"", b"fatal: '$GIT_DIR' too big\n")first = (subprocess.CompletedProcess([], 128, b"", b"fatal: '$GIT_DIR' zu gro\xc3\x9f\n")@@ -1657,16 +1674,22 @@ def test_windows_root_inventory_retries_the_selected_git_directory(return (english if (kwargs["environment_overrides"] or {}).get("LC_ALL") == "C" else first)- assert directory == Path(".git")+ assert directory == markerassert kwargs["environment_overrides"] == overridesassert kwargs["input_data"] == input_datawith pytest.raises(PermissionError):metadata.rename(source / "moved")+ if scoped:+ for pinned in (cwd, cwd.parent):+ with pytest.raises(PermissionError):+ pinned.rename(pinned.with_name("moved"))if worktree != source:with pytest.raises(PermissionError), (worktree / ".git").open("r+b"):passif case == "failed-retry":return subprocess.CompletedProcess([], 1, b"", b"fixture read failed")+ if case == "deep-gitfile":+ return subprocess.CompletedProcess([], 0, expected, b"trace verified gitfile")return native(target, *args, **kwargs)monkeypatch.setattr(workbench_target, "git_command", reject_long_argument)@@ -1678,11 +1701,11 @@ def test_windows_root_inventory_retries_the_selected_git_directory(input_data=input_data,environment_overrides=overrides.copy(),)- if case in {"scoped", "different-pointer", "relative-pointer", "case-marker"}:+ if case in {"different-pointer", "relative-pointer", "case-marker"}:assert result is first and calls == [metadata]else:initial = [metadata, metadata] if case == "localized" else [metadata]- assert calls == [*initial, Path(".git")]+ assert calls == [*initial, marker]if case == "failed-retry":assert result is firstelse:@@ -1712,12 +1735,17 @@ def test_root_git_marker_declines_unsupported_paths_without_opening_directories():with windows_files.pinned_root_git_marker(Path(target), Path(metadata)) as selected:assert selected is None+ root = Path(r"C:\repo")+ for cwd in (Path(r"C:\repo-other\scope"), root / ".." / "scope", Path(r"D:\repo\scope")):+ with windows_files.pinned_root_git_marker(cwd, root / ".git", work_tree=root) as selected:+ assert selected is None@pytest.mark.native_windows@pytest.mark.skipif(os.name != "nt", reason="requires Windows directory junctions")[email protected]("inside_scope", [False, True])def test_root_git_marker_refuses_a_reparse_ancestor_before_descending(- tmp_path: Path, monkeypatch: pytest.MonkeyPatch+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, inside_scope: bool) -> None:import _winapiimport ctypes@@ -1730,8 +1758,12 @@ def test_root_git_marker_refuses_a_reparse_ancestor_before_descending(cwd.mkdir()ordinary = tmp_path / "ordinary"(ordinary / "metadata").mkdir(parents=True)- link = tmp_path / "link"+ link = (cwd if inside_scope else tmp_path) / "link"_winapi.CreateJunction(str(ordinary), str(link))+ metadata = cwd / ".git" if inside_scope else link / "metadata"+ scope = link / "metadata" if inside_scope else cwd+ if inside_scope:+ metadata.mkdir()visited = []open_directory = windows_files._open_directoryresolve = Path.resolve@@ -1747,11 +1779,45 @@ def test_root_git_marker_refuses_a_reparse_ancestor_before_descending(monkeypatch.setattr(windows_files, "_open_directory", observe)monkeypatch.setattr(Path, "resolve", no_link_resolution)- with windows_files.pinned_root_git_marker(cwd, link / "metadata") as selected:+ with windows_files.pinned_root_git_marker(scope, metadata, work_tree=cwd) as selected:assert selected is Noneassert link in visited[email protected]_windows[email protected](os.name != "nt", reason="requires Windows Git directory inventory")+def test_long_git_metadata_keeps_nested_directory_inventory_scoped(tmp_path: Path) -> None:+ import _winapi+ import ctypes++ import workbench_target++ if len(tmp_path.drive) != 2 or ctypes.windll.kernel32.GetDriveTypeW(tmp_path.anchor) != 3:+ pytest.skip("requires an ordinary fixed local drive")+ seed = tmp_path / "seed"+ initialize_git_repository(seed)+ subprocess.run(["git", "config", "core.longpaths", "true"], cwd=seed, check=True)+ # More than 220 UTF-8 bytes in --git-dir without requiring a long Win32 component.+ checkout = tmp_path / ("界" * 75)+ shutil.copytree(seed, checkout)+ scope = checkout / "selected[ab]" / "component"+ scope.mkdir(parents=True)+ (scope / "source.txt").write_text("selected\n")+ (checkout / "selecteda").mkdir()+ (checkout / "selecteda" / "outside.txt").write_text("sibling\n")+ (checkout / ".gitignore").write_text("ignored/\n")+ (scope / "ignored").mkdir()+ (scope / "ignored" / "excluded.txt").write_text("ignored\n")+ external = tmp_path / "external"+ external.mkdir()+ _winapi.CreateJunction(str(external), str(scope / "empty-junction"))++ assert set(workbench_target.git_directory_snapshot_paths(scope)) == {+ scope / "source.txt",+ scope / "empty-junction",+ }++def test_copy_skips_index_paths_under_a_directory_replaced_by_a_file(tmp_path: Path) -> None:target = tmp_path / "target"initialize_git_repository(target)