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): read diff blobs with Git before 2.38 (#1715)

wj5351b715c057
diff --git a/plugins/codex-security/scripts/workbench_target.py b/plugins/codex-security/scripts/workbench_target.py
index 919f1e674..8d1c53c46 100644
--- a/plugins/codex-security/scripts/workbench_target.py
+++ b/plugins/codex-security/scripts/workbench_target.py
@@ -87,7 +87,10 @@ def git_blob_samples(
if not object_names:
return []
- request = b"\0".join(os.fsencode(name) for name in object_names) + b"\0"
+ # Git before 2.38 rejects -z, so only names with line breaks need it.
+ nul_input = any("\n" in name or "\r" in name for name in object_names)
+ separator = b"\0" if nul_input else b"\n"
+ request = separator.join(os.fsencode(name) for name in object_names) + separator
read_fd, write_fd = os.pipe()
with (
ThreadPoolExecutor(max_workers=1) as reader,
@@ -106,7 +109,7 @@ def git_blob_samples(
target,
"cat-file",
"--batch",
- "-z",
+ *(["-z"] if nul_input else []),
text=False,
input_data=request,
git_dir=git_dir,
diff --git a/plugins/codex-security/tests/test_workbench_target.py b/plugins/codex-security/tests/test_workbench_target.py
index b163f6997..f4f1c32bd 100644
--- a/plugins/codex-security/tests/test_workbench_target.py
+++ b/plugins/codex-security/tests/test_workbench_target.py
@@ -810,6 +810,28 @@ def test_git_blob_samples_reject_git_failure(
assert git_blob_samples(tmp_path, ["HEAD:source"]) == [None]
+def test_git_blob_samples_work_with_git_before_2_38(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ target = tmp_path / "target"
+ initialize_git_repository(target)
+ git_blob_samples = WORKBENCH_TARGET["git_blob_samples"]
+ git_command = git_blob_samples.__globals__["git_command"]
+
+ def git_before_2_38(*args: Any, **kwargs: Any):
+ # Git before 2.38 rejects cat-file -z with a usage error.
+ if "-z" in args:
+ return subprocess.CompletedProcess(args, 129)
+ return git_command(*args, **kwargs)
+
+ monkeypatch.setitem(git_blob_samples.__globals__, "git_command", git_before_2_38)
+
+ assert git_blob_samples(target, ["HEAD:README.md", "HEAD:missing"]) == [
+ (b"fixture\n", False),
+ None,
+ ]
+
+
@pytest.mark.parametrize(
("log_encoding", "subject"),
[