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): preserve Windows directory symlinks in snapshot copies (#2391)

Michael D'Angelo8b8200682a21
diff --git a/plugins/codex-security/scripts/workbench_target.py b/plugins/codex-security/scripts/workbench_target.py
index 15cfc6791..f2d399015 100644
--- a/plugins/codex-security/scripts/workbench_target.py
+++ b/plugins/codex-security/scripts/workbench_target.py
@@ -1231,7 +1231,12 @@ def copy_git_worktree_files(
destination_path = destination / relative
destination_path.parent.mkdir(parents=True, exist_ok=True)
if stat.S_ISLNK(metadata.st_mode):
- destination_path.symlink_to(os.readlink(source_path))
+ directory_link = bool(
+ getattr(metadata, "st_file_attributes", 0) & stat.FILE_ATTRIBUTE_DIRECTORY
+ )
+ destination_path.symlink_to(
+ os.readlink(source_path), target_is_directory=directory_link
+ )
elif stat.S_ISREG(metadata.st_mode):
shutil.copy2(source_path, destination_path, follow_symlinks=False)
elif stat.S_ISDIR(metadata.st_mode):
diff --git a/plugins/codex-security/tests/test_workbench_target.py b/plugins/codex-security/tests/test_workbench_target.py
index 1c4b4dad7..f741150f8 100644
--- a/plugins/codex-security/tests/test_workbench_target.py
+++ b/plugins/codex-security/tests/test_workbench_target.py
@@ -1531,6 +1531,22 @@ def test_git_context_preserves_trailing_root_whitespace(
assert (destination / "synthetic.txt").read_text() == "selected target content\n"
[email protected](os.name != "nt", reason="Windows symlinks record the target type")
+def test_copy_git_worktree_preserves_dangling_windows_directory_symlink(tmp_path: Path) -> None:
+ source = tmp_path / "source"
+ initialize_unborn_git_repository(source)
+ link = source / "directory-link"
+ link.symlink_to("missing", target_is_directory=True)
+
+ destination = tmp_path / "copy"
+ WORKBENCH_TARGET["copy_git_worktree_files"](source, destination, ())
+
+ copied = destination / link.name
+ assert os.readlink(copied) == os.readlink(link)
+ assert copied.lstat().st_file_attributes & stat.FILE_ATTRIBUTE_DIRECTORY
+
+
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)