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(workbench): recover long Windows root inventories with an exact Git marker (#2404)

Michael D'Angelob676bee6d5a0
diff --git a/plugins/codex-security/scripts/windows_scan_local_files.py b/plugins/codex-security/scripts/windows_scan_local_files.py
index 864e0a8fa..cb12fed53 100644
--- a/plugins/codex-security/scripts/windows_scan_local_files.py
+++ b/plugins/codex-security/scripts/windows_scan_local_files.py
@@ -45,7 +45,9 @@ class WindowsScanLocalPathError(WindowsScanLocalFileError):
# CreateFile access and sharing flags.
+_DRIVE_FIXED = 3
_DELETE = 0x00010000
+_FILE_TRAVERSE = 0x00000020
_FILE_READ_ATTRIBUTES = 0x00000080
_GENERIC_READ = 0x80000000
_GENERIC_WRITE = 0x40000000
@@ -181,6 +183,10 @@ if os.name == "nt":
]
_GetFileSecurityW.restype = wintypes.BOOL
+ _GetDriveTypeW = _kernel32.GetDriveTypeW
+ _GetDriveTypeW.argtypes = [wintypes.LPCWSTR]
+ _GetDriveTypeW.restype = wintypes.UINT
+
_CreateFileW = _kernel32.CreateFileW
_CreateFileW.argtypes = [
wintypes.LPCWSTR,
@@ -463,11 +469,12 @@ def _open_directory(
missing_ok: bool = False,
missing_is_path_error: bool = True,
movable: bool = False,
+ require_traverse: bool = False,
) -> _OwnedHandle | None:
try:
handle = _create_file(
path,
- access=_FILE_READ_ATTRIBUTES,
+ access=_FILE_READ_ATTRIBUTES | (_FILE_TRAVERSE if require_traverse else 0),
share=_FILE_SHARE_READ | _FILE_SHARE_WRITE | (_FILE_SHARE_DELETE if movable else 0),
disposition=_OPEN_EXISTING,
flags=_FILE_FLAG_BACKUP_SEMANTICS | _FILE_FLAG_OPEN_REPARSE_POINT,
@@ -615,6 +622,50 @@ def scan_root_identity(scan_dir: Path) -> tuple[Path, tuple[int, int]]:
return root_path, (metadata.st_dev, metadata.st_ino)
+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."""
+ if (
+ not cwd.is_absolute()
+ or not metadata.is_absolute()
+ or len(cwd.drive) != 2
+ or cwd.drive != metadata.drive
+ or len(cwd.parts) < 2
+ or len(metadata.parts) < 2
+ or _GetDriveTypeW(cwd.anchor) != _DRIVE_FIXED
+ ):
+ yield None
+ return
+ marker = None
+ with contextlib.ExitStack() as pins:
+ try:
+ for path in (cwd, metadata):
+ _validated_parts(PurePosixPath(*path.parts[1:]).as_posix())
+ # Pin each literal ancestor before opening the next component. Never resolve
+ # a repository-controlled reparse point to find the directory to be pinned.
+ for directory in (*reversed(path.parents), path):
+ handle = _open_directory(directory, require_traverse=True)
+ assert handle is not None
+ pins.enter_context(handle)
+ assert handle.value is not None
+ if _final_path(handle.value) != _extended_path(path):
+ raise ValueError("the selected Git directory spelling is not canonical")
+ if str(cwd / ".git") != 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")
+ pointer = pins.enter_context(os.fdopen(fd, "rb"))
+ if leaf != ".git":
+ raise ValueError("the root marker name does not match .git")
+ expected = f"gitdir: {metadata}".encode("utf-8")
+ 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")
+ except (OSError, ValueError):
+ pins.close()
+ yield marker
+
+
def open_read_fd(scan_dir: Path, relative_path: str, context: str) -> int:
"""Open a verified regular file and return an owned binary read descriptor."""
diff --git a/plugins/codex-security/scripts/workbench_target.py b/plugins/codex-security/scripts/workbench_target.py
index f2d399015..18eee2a92 100644
--- a/plugins/codex-security/scripts/workbench_target.py
+++ b/plugins/codex-security/scripts/workbench_target.py
@@ -16,12 +16,12 @@ import tempfile
import unicodedata
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path, PurePosixPath
-from typing import Any, BinaryIO, Callable
+from typing import Any, BinaryIO, Callable, cast
# Some plugin hosts launch Python with safe-path isolation enabled.
sys.path.insert(0, str(Path(__file__).resolve().parent))
from filesystem_identity import stored_filesystem_identity_matches
-from windows_scan_local_files import copy_directory_junction
+from windows_scan_local_files import copy_directory_junction, pinned_root_git_marker
from workbench_constants import GIT_REPOSITORY_ENVIRONMENT
SOURCE_METADATA_DIRECTORIES = {".git", ".svn"}
@@ -861,6 +861,54 @@ def _directory_link_boundary(target: Path, path: Path, linked_prefixes: dict[str
return path
+def _windows_inventory_query(
+ target: Path,
+ *args: str,
+ git_dir: Path,
+ work_tree: Path,
+ 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."""
+
+ def query(
+ directory: Path, environment: dict[str, str] | None
+ ) -> subprocess.CompletedProcess[bytes]:
+ return cast(
+ subprocess.CompletedProcess[bytes],
+ git_command(
+ target,
+ *args,
+ text=False,
+ git_dir=directory,
+ work_tree=work_tree,
+ input_data=input_data,
+ environment_overrides=environment,
+ ),
+ )
+
+ result = query(git_dir, environment_overrides)
+ if (
+ result.returncode != 128
+ or result.stdout
+ or str(target) != str(work_tree)
+ or b"GIT_DIR" not in result.stderr
+ ):
+ return result
+ diagnostic = b"fatal: '$GIT_DIR' too big"
+ if diagnostic not in result.stderr.splitlines():
+ # Confirm translated diagnostics without changing the actual inventory's environment.
+ 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:
+ if marker is not None:
+ retry = query(marker, environment_overrides)
+ if retry.returncode == 0 or (retry.returncode == 1 and "check-ignore" in args):
+ return retry
+ return result
+
+
def git_directory_snapshot_paths(target: Path) -> list[Path] | None:
repository_root = git_output(target, "rev-parse", "--show-toplevel")
if repository_root is None:
@@ -954,7 +1002,7 @@ def git_directory_snapshot_paths(target: Path) -> list[Path] | None:
git_directory = git_output(target, "rev-parse", "--absolute-git-dir")
if git_directory is None:
raise SystemExit("Could not inspect the selected Git working tree.")
- directories = git_bytes(
+ directories = _windows_inventory_query(
target,
"--no-literal-pathspecs",
"ls-files",
@@ -968,9 +1016,9 @@ def git_directory_snapshot_paths(target: Path) -> list[Path] | None:
git_dir=Path(git_directory),
work_tree=repository,
)
- if directories is None:
+ if directories.returncode != 0:
raise SystemExit("Could not inspect directories in the selected Git working tree.")
- pending = [target / os.fsdecode(path) for path in directories.split(b"\0") if path]
+ pending = [target / os.fsdecode(path) for path in directories.stdout.split(b"\0") if path]
while pending:
children: list[Path] = []
for path in pending:
@@ -1009,13 +1057,12 @@ def git_directory_snapshot_paths(target: Path) -> list[Path] | None:
if not candidates:
break
# Filter a directory level at once, retaining the outer repository context.
- ignored = git_command(
+ ignored = _windows_inventory_query(
repository,
"--no-literal-pathspecs",
"check-ignore",
"-z",
"--stdin",
- text=False,
input_data=b"\0".join(candidates) + b"\0",
git_dir=Path(git_directory),
work_tree=repository,
diff --git a/plugins/codex-security/tests/test_workbench_target.py b/plugins/codex-security/tests/test_workbench_target.py
index f741150f8..c09c12ea0 100644
--- a/plugins/codex-security/tests/test_workbench_target.py
+++ b/plugins/codex-security/tests/test_workbench_target.py
@@ -1547,6 +1547,191 @@ def test_copy_git_worktree_preserves_dangling_windows_directory_symlink(tmp_path
assert copied.lstat().st_file_attributes & stat.FILE_ATTRIBUTE_DIRECTORY
[email protected](os.name != "nt", reason="requires Windows directory sharing")
+ "case",
+ [
+ "in-tree",
+ "separate",
+ "localized",
+ "scoped",
+ "failed-retry",
+ "check-ignore-unmatched",
+ "different-pointer",
+ "case-marker",
+ "relative-pointer",
+ "conditional",
+ ],
+)
+def test_windows_root_inventory_retries_the_selected_git_directory(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, case: str
+) -> None:
+ 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")
+ source = tmp_path / "source"
+ initialize_git_repository(source)
+ metadata = source / ".git"
+ worktree = source
+ if case != "in-tree":
+ worktree = tmp_path / "checkout"
+ worktree.mkdir()
+ spelling = metadata.as_posix()
+ if case == "different-pointer":
+ spelling = (tmp_path / "other-metadata").as_posix()
+ if case == "relative-pointer":
+ spelling = os.path.relpath(metadata, worktree)
+ 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()
+ native = workbench_target.git_command
+ args = ("ls-files", "--cached", "-z", "--", ".")
+ expected = b"README.md\0"
+ expected_status = 0
+ input_data = None
+ overrides = {"GIT_TRACE_SETUP": "1"}
+ if case == "check-ignore-unmatched":
+ for name in ("visible one", "visible two"):
+ (worktree / name).mkdir()
+ (worktree / ".gitignore").write_text("!visible one/\n!visible two/\n", encoding="utf-8")
+ args = ("--no-literal-pathspecs", "check-ignore", "-z", "--stdin")
+ input_data = b"visible one\0visible two\0"
+ overrides["GIT_NOGLOB_PATHSPECS"] = "0"
+ expected_status = 1
+ expected = b""
+ if case == "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",
+ )
+ monkeypatch.setenv("GIT_CONFIG_GLOBAL", str(config))
+ monkeypatch.setenv("GIT_CONFIG_NOSYSTEM", "1")
+ (worktree / "visible.fixture").write_text("selected\n", encoding="utf-8")
+ args = ("ls-files", "--others", "--exclude-standard", "-z", "--", "visible.fixture")
+ expected = 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")
+ if case == "localized"
+ else english
+ )
+ calls = []
+
+ def reject_long_argument(target, *args, **kwargs):
+ directory = kwargs["git_dir"]
+ calls.append(directory)
+ assert str(target) == str(cwd) and str(kwargs["work_tree"]) == str(worktree)
+ if directory == metadata:
+ return (
+ english if (kwargs["environment_overrides"] or {}).get("LC_ALL") == "C" else first
+ )
+ assert directory == Path(".git")
+ assert kwargs["environment_overrides"] == overrides
+ assert kwargs["input_data"] == input_data
+ with pytest.raises(PermissionError):
+ metadata.rename(source / "moved")
+ if worktree != source:
+ with pytest.raises(PermissionError), (worktree / ".git").open("r+b"):
+ pass
+ if case == "failed-retry":
+ return subprocess.CompletedProcess([], 1, b"", b"fixture read failed")
+ return native(target, *args, **kwargs)
+
+ monkeypatch.setattr(workbench_target, "git_command", reject_long_argument)
+ result = workbench_target._windows_inventory_query(
+ cwd,
+ *args,
+ git_dir=metadata,
+ work_tree=worktree,
+ input_data=input_data,
+ environment_overrides=overrides.copy(),
+ )
+ if case in {"scoped", "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")]
+ if case == "failed-retry":
+ assert result is first
+ else:
+ assert result.returncode == expected_status and result.stdout == expected
+ assert b"trace" in result.stderr
+
+
[email protected](os.name != "nt", reason="requires Windows path semantics")
+def test_root_git_marker_declines_unsupported_paths_without_opening_directories(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ import windows_scan_local_files as windows_files
+
+ monkeypatch.setattr(windows_files, "_GetDriveTypeW", lambda _anchor: 3)
+
+ def unexpected_open(*_args, **_kwargs):
+ pytest.fail("unsupported paths must be declined before opening directories")
+
+ monkeypatch.setattr(windows_files, "_open_directory", unexpected_open)
+ for target, metadata in (
+ (r"C:\repo", r"D:\metadata"),
+ (r"\\?\C:\repo", r"\\?\C:\metadata"),
+ (r"\\server.invalid\share\repo", r"\\server.invalid\share\metadata"),
+ (r"C:checkout", r"C:\metadata"),
+ ("C:\\", r"C:\metadata"),
+ ):
+ with windows_files.pinned_root_git_marker(Path(target), Path(metadata)) as selected:
+ assert selected is None
+
+
[email protected](os.name != "nt", reason="requires Windows directory junctions")
+def test_root_git_marker_refuses_a_reparse_ancestor_before_descending(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ import _winapi
+ import ctypes
+
+ import windows_scan_local_files as windows_files
+
+ if len(tmp_path.drive) != 2 or ctypes.windll.kernel32.GetDriveTypeW(tmp_path.anchor) != 3:
+ pytest.skip("requires an ordinary fixed local drive")
+ cwd = tmp_path / "checkout"
+ cwd.mkdir()
+ ordinary = tmp_path / "ordinary"
+ (ordinary / "metadata").mkdir(parents=True)
+ link = tmp_path / "link"
+ _winapi.CreateJunction(str(ordinary), str(link))
+ visited = []
+ open_directory = windows_files._open_directory
+ resolve = Path.resolve
+
+ def observe(path, **kwargs):
+ assert link not in path.parents
+ visited.append(path)
+ return open_directory(path, **kwargs)
+
+ def no_link_resolution(path, *args, **kwargs):
+ assert path != link and link not in path.parents
+ return resolve(path, *args, **kwargs)
+
+ 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:
+ assert selected is None
+ assert link in visited
+
+
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)