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): show Git errors for unreadable tracked files (#2058)

Michael D'Angelobe158fa8e04f
diff --git a/plugins/codex-security/scripts/workbench_target.py b/plugins/codex-security/scripts/workbench_target.py
index d262b439c..4785d9ac9 100644
--- a/plugins/codex-security/scripts/workbench_target.py
+++ b/plugins/codex-security/scripts/workbench_target.py
@@ -12,6 +12,7 @@ import stat
import subprocess
import sys
import tempfile
+import unicodedata
from pathlib import Path, PurePosixPath
from typing import Any, BinaryIO, Callable
@@ -356,6 +357,14 @@ def update_digest_field(digest: Any, label: bytes, value: bytes) -> None:
digest.update(value)
+def _visible_git_character(char: str) -> str:
+ if char in "\n\t" or unicodedata.category(char) not in {"Cc", "Cf"}:
+ return char
+ if ord(char) <= 0xFF:
+ return f"\\x{ord(char):02x}"
+ return ascii(char)[1:-1]
+
+
def _update_digest_field_from_git(
digest: Any,
label: bytes,
@@ -363,7 +372,7 @@ def _update_digest_field_from_git(
*args: str,
git_dir: Path | None = None,
work_tree: Path | None = None,
-) -> bool:
+) -> None:
with tempfile.TemporaryFile() as output:
completed = git_command(
target,
@@ -374,14 +383,19 @@ def _update_digest_field_from_git(
stdout_file=output,
)
if completed.returncode != 0:
- return False
+ detail = completed.stderr.decode("utf-8", errors="backslashreplace").replace(
+ "\r\n", "\n"
+ )
+ detail = "".join(map(_visible_git_character, detail))
+ 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()
_update_digest_field_header(digest, label, value_size)
output.seek(0)
for chunk in iter(lambda: output.read(1024 * 1024), b""):
digest.update(chunk)
- return True
def worktree_content_digest(target: Path) -> str:
@@ -463,7 +477,7 @@ def worktree_content_digest_for_context(
":(top,glob,exclude)**/.svn",
":(top,glob,exclude)**/.svn/**",
]
- tracked_ok = _update_digest_field_from_git(
+ _update_digest_field_from_git(
digest,
b"tracked-diff",
repository,
@@ -492,7 +506,7 @@ def worktree_content_digest_for_context(
git_dir=git_dir,
work_tree=work_tree,
)
- if not tracked_ok or untracked is None:
+ if untracked is None:
raise SystemExit("Could not snapshot the selected working-tree changes.")
for raw_path in sorted(path for path in untracked.split(b"\0") if path):
relative_path = os.fsdecode(raw_path)
diff --git a/plugins/codex-security/tests/test_workbench_scan_validation.py b/plugins/codex-security/tests/test_workbench_scan_validation.py
index 8c819f907..aadf0152e 100644
--- a/plugins/codex-security/tests/test_workbench_scan_validation.py
+++ b/plugins/codex-security/tests/test_workbench_scan_validation.py
@@ -21,6 +21,34 @@ def start_validation_scan(
return state_dir, scan["scanId"]
+ os.name == "nt" or getattr(os, "geteuid", lambda: 0)() == 0,
+ reason="Mode permissions require a non-root Unix user",
+)
+def test_snapshot_reports_unreadable_tracked_file(
+ tmp_path: Path, workbench_api, monkeypatch
+) -> None:
+ repository = tmp_path / "repository"
+ initialize_git_repository(repository)
+ (repository / ".gitattributes").write_bytes(
+ b"* invalid\x1b[2J\xff" + "é\u202e\U000e0001\n".encode()
+ )
+ source = repository / "README.md"
+ source.write_text("Changed contents.\n")
+ mode = source.stat().st_mode
+ monkeypatch.setenv("LC_ALL", "C")
+ source.chmod(0)
+ try:
+ with pytest.raises(SystemExit) as error:
+ workbench_api["worktree_content_digest"](repository)
+ finally:
+ source.chmod(mode)
+ diagnostic = str(error.value)
+ assert "README.md" in diagnostic
+ assert "Permission denied" in diagnostic
+ assert r"invalid\x1b[2J\xffé\u202e\U000e0001" in diagnostic
+
+
@pytest.mark.parametrize("kind", ["directory", "git", "dirty", "working_tree", "refs"])
def test_check_scan_target_matches_recorded_contents(tmp_path: Path, kind: str) -> None:
repository = tmp_path / "repository"