diff options
| author | Mohamed Hegazy <mhegazy@anthropic.com> | 2026-11-04 19:42:34 +0000 |
|---|---|---|
| committer | Claude <noreply@anthropic.com> | 2026-11-04 20:06:26 +0000 |
| commit | 923976d3de1b57b9cd738a3a2ff4fe3669acc472 (patch) | |
| tree | e814c6ca92c490d5dc3670e8e224e437c854bbef | |
| parent | fa4eb54397ee2faccb6940512a19c017cb1337f3 (diff) | |
security-guidance: start git by its full path on Windowsmhegazy/sg-git-program-from-path
A bare program name on Windows is looked up in the working directory before
PATH, and the hooks run in the project. Name the first git.exe on PATH, leaving
out relative entries, the working directory and folders inside the project, as
the claude lookup already avoids that search.
Co-authored-by: mhegazy <mhegazy@anthropic.com>
| -rw-r--r-- | plugins/security-guidance/hooks/gitutil.py | 33 | ||||
| -rw-r--r-- | plugins/security-guidance/tests/test_git_program.py | 115 |
2 files changed, 147 insertions, 1 deletions
diff --git a/plugins/security-guidance/hooks/gitutil.py b/plugins/security-guidance/hooks/gitutil.py index ec51e3c6..7ab40403 100644 --- a/plugins/security-guidance/hooks/gitutil.py +++ b/plugins/security-guidance/hooks/gitutil.py @@ -27,8 +27,39 @@ import time from _base import debug_log +def _git_program(windows=os.name == "nt"): + """How to start git. On Windows a bare name is looked up in the process's + working directory before PATH, and the hooks run in the project, so a + `git.exe` there would start in place of the installed one. Name the first + git.exe on PATH instead, leaving out relative entries, the working + directory, and folders inside the project (unless the project holds the + home folder, and with it the user's own tools). With none, a path that + does not exist: git is reported missing, as it would be anywhere else.""" + if not windows: + return "git" + def norm(p): + return os.path.normcase(os.path.normpath(p)) + try: + cwd = norm(os.getcwd()) + except OSError: + cwd = "" + home = norm(os.path.expanduser("~")) + os.sep + roots = [norm(r) for r in (cwd, os.environ.get("CLAUDE_PROJECT_DIR")) if r] + roots = [r.rstrip(os.sep) + os.sep for r in roots if not home.startswith(r.rstrip(os.sep) + os.sep)] + for d in os.environ.get("PATH", "").split(os.pathsep): + d = d.strip().strip('"') + if not d or not os.path.isabs(d) or norm(d) == cwd: + continue + if any((norm(d) + os.sep).startswith(r) for r in roots): + continue + path = os.path.join(d, "git.exe") + if os.path.isfile(path): + return path + return os.path.join(os.path.dirname(os.path.abspath(__file__)), "git.exe") + + GIT_CMD = [ - "git", + _git_program(), "-c", "core.fsmonitor=false", "-c", "core.hooksPath=/dev/null", # core.quotePath=false: emit raw UTF-8 in path-emitting commands instead diff --git a/plugins/security-guidance/tests/test_git_program.py b/plugins/security-guidance/tests/test_git_program.py new file mode 100644 index 00000000..9249c6e4 --- /dev/null +++ b/plugins/security-guidance/tests/test_git_program.py @@ -0,0 +1,115 @@ +"""On Windows the hooks name git by its full path, found on PATH, so a git.exe +in the project is not the one that starts.""" +import json +import os +import subprocess +import sys + +import pytest + +from conftest import HOOK_SCRIPT, make_repo, ups_payload, write_cli + +import gitutil + +windows_only = pytest.mark.skipif(os.name != "nt", reason="Windows program lookup") + + +def _git_in(d): + d.mkdir(parents=True, exist_ok=True) + (d / "git.exe").write_bytes(b"") + return str(d / "git.exe") + + +def test_bare_name_outside_windows(): + assert gitutil._git_program(windows=False) == "git" + + +def test_first_git_on_path(tmp_path, monkeypatch): + first, second = tmp_path / "a", tmp_path / "b" + first.mkdir() + expected = _git_in(second) + _git_in(tmp_path / "c") + monkeypatch.setenv("PATH", os.pathsep.join([str(first), str(second), str(tmp_path / "c")])) + assert gitutil._git_program(windows=True) == expected + + +def test_working_directory_and_relative_entries_are_left_out(tmp_path, monkeypatch): + project = tmp_path / "project" + _git_in(project) + _git_in(project / "bin") + expected = _git_in(tmp_path / "installed") + monkeypatch.chdir(project) + monkeypatch.delenv("CLAUDE_PROJECT_DIR", raising=False) + monkeypatch.setenv("PATH", os.pathsep.join( + ["", ".", "bin", str(project), str(project) + os.sep, str(tmp_path / "installed")])) + assert gitutil._git_program(windows=True) == expected + + +def test_folder_inside_the_project_is_left_out(tmp_path, monkeypatch): + project = tmp_path / "project" + _git_in(project) + _git_in(project / ".venv" / "Scripts") + (project / "pkg").mkdir() + expected = _git_in(tmp_path / "installed") + monkeypatch.chdir(project / "pkg") + monkeypatch.setenv("CLAUDE_PROJECT_DIR", str(project)) + monkeypatch.setenv("PATH", os.pathsep.join( + [str(project), str(project / ".venv" / "Scripts"), str(tmp_path / "installed")])) + assert gitutil._git_program(windows=True) == expected + + +def test_project_above_the_home_folder_keeps_the_users_tools(tmp_path, monkeypatch): + home = tmp_path / "me" + expected = _git_in(home / "scoop" / "shims") + monkeypatch.setenv("HOME", str(home)) + monkeypatch.setenv("USERPROFILE", str(home)) + monkeypatch.chdir(tmp_path) + monkeypatch.delenv("CLAUDE_PROJECT_DIR", raising=False) + monkeypatch.setenv("PATH", str(home / "scoop" / "shims")) + assert gitutil._git_program(windows=True) == expected + + +def test_similar_folder_name_stays(tmp_path, monkeypatch): + project = tmp_path / "project" + project.mkdir() + expected = _git_in(tmp_path / "project-tools") + monkeypatch.chdir(project) + monkeypatch.delenv("CLAUDE_PROJECT_DIR", raising=False) + monkeypatch.setenv("PATH", str(tmp_path / "project-tools")) + assert gitutil._git_program(windows=True) == expected + + +def test_quoted_entry(tmp_path, monkeypatch): + expected = _git_in(tmp_path / "installed") + monkeypatch.setenv("PATH", f'"{tmp_path / "installed"}"') + assert gitutil._git_program(windows=True) == expected + + +def test_no_git_on_path_names_nothing_that_exists(tmp_path, monkeypatch): + project = tmp_path / "project" + _git_in(project) + monkeypatch.chdir(project) + monkeypatch.setenv("PATH", str(project)) + program = gitutil._git_program(windows=True) + assert os.path.isabs(program) and not os.path.exists(program) + + +def test_working_directory_that_is_gone(tmp_path, monkeypatch): + expected = _git_in(tmp_path / "installed") + monkeypatch.setattr(gitutil.os, "getcwd", lambda: (_ for _ in ()).throw(OSError())) + monkeypatch.setenv("PATH", str(tmp_path / "installed")) + assert gitutil._git_program(windows=True) == expected + + +@windows_only +def test_git_exe_in_the_project_does_not_start(tmp_path, env): + marker = tmp_path / "started" + repo = make_repo(tmp_path / "repo", {"app.py": "print('hi')\n"}) + write_cli(repo, "git", f"open({str(marker)!r}, 'a').write('x')\n") + + r = subprocess.run( + [sys.executable, str(HOOK_SCRIPT)], input=json.dumps(ups_payload(repo)), + capture_output=True, text=True, env=env, cwd=repo, timeout=120) + + assert r.returncode == 0, r.stderr + assert not marker.exists() |
