summaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
authorClaude <noreply@anthropic.com>2026-11-12 15:01:04 +0000
committerClaude <noreply@anthropic.com>2026-11-12 15:01:04 +0000
commit7122fc93f8f13f06e6f413ed68d0e0cd8a9541d0 (patch)
treec2adb828373cb9199e189b7ab165f4b83c271510 /plugins
parente0ab6e51219eef74e417a0e1d89e5d53e5e763d4 (diff)
security-guidance: only run git in ordinary checkouts
Treat a folder as a repository to run git in only when it has its own .git entry or the caller named the repository through GIT_DIR, and set safe.bareRepository=explicit on the hook's git commands. Linked worktrees and ordinary checkouts work as before. Co-authored-by: mhegazy <mhegazy@anthropic.com>
Diffstat (limited to 'plugins')
-rw-r--r--plugins/security-guidance/hooks/gitutil.py15
-rw-r--r--plugins/security-guidance/tests/test_bare_repo_toplevel.py48
2 files changed, 61 insertions, 2 deletions
diff --git a/plugins/security-guidance/hooks/gitutil.py b/plugins/security-guidance/hooks/gitutil.py
index 36732482..1b9e124e 100644
--- a/plugins/security-guidance/hooks/gitutil.py
+++ b/plugins/security-guidance/hooks/gitutil.py
@@ -110,6 +110,7 @@ SAFE_GIT_CONFIG = (
("diff.ignoreSubmodules", "all"),
("diff.submodule", "short"),
("log.showSignature", "false"),
+ ("safe.bareRepository", "explicit"),
)
# The hook's git runs in the background beside the session's own git. Without
@@ -963,6 +964,13 @@ def _drop_gitlinks(top, env, tmp_index):
return False
+def _has_git_entry(top):
+ """True when a work tree root has its own `.git`, or the caller named the
+ repository through the environment. Git versions before 2.38 have no
+ safe.bareRepository, so this is what keeps them to ordinary checkouts."""
+ return bool(os.environ.get("GIT_DIR")) or os.path.lexists(os.path.join(top, ".git"))
+
+
def _git_toplevel(cwd):
"""Absolute repo root for `cwd`, or None if not in a work tree."""
try:
@@ -976,7 +984,7 @@ def _git_toplevel(cwd):
if r.returncode != 0:
return None
path = r.stdout.decode("utf-8", errors="replace").strip()
- return path if path else None
+ return path if path and _has_git_entry(path) else None
except (subprocess.TimeoutExpired, FileNotFoundError, OSError):
return None
@@ -995,7 +1003,10 @@ def _git_toplevel_and_head(cwd):
return None, None
# See #2099: the first line is a PATH, so decode the bytes manually.
top, _, head = r.stdout.decode("utf-8", errors="replace").strip().rpartition("\n")
- return (top.strip() or None), (head.strip() or None)
+ top = top.strip()
+ if not top or not _has_git_entry(top):
+ return None, None
+ return top, (head.strip() or None)
def _git_head_at(cwd, epoch):
diff --git a/plugins/security-guidance/tests/test_bare_repo_toplevel.py b/plugins/security-guidance/tests/test_bare_repo_toplevel.py
new file mode 100644
index 00000000..1bbcf9d2
--- /dev/null
+++ b/plugins/security-guidance/tests/test_bare_repo_toplevel.py
@@ -0,0 +1,48 @@
+"""The plugin only treats ordinary checkouts as repositories to run git in."""
+import os
+import subprocess
+
+import pytest
+
+from conftest import GIT_ENV, git, make_repo
+
+import gitutil
+
+
+def _run(cwd, *args):
+ return subprocess.run(["git", *args], cwd=cwd, capture_output=True, text=True,
+ env={**os.environ, **GIT_ENV}, check=True).stdout
+
+
+@pytest.fixture
+def carried(tmp_path):
+ d = tmp_path / "project" / "src"
+ d.mkdir(parents=True)
+ _run(d, "init", "-q", "-b", "main", "--bare", ".")
+ _run(d, "config", "core.bare", "false")
+ _run(d, "config", "core.worktree", ".")
+ return d
+
+
+def test_folder_without_its_own_git_entry_is_not_a_repo(carried):
+ assert gitutil._git_toplevel(str(carried)) is None
+ assert gitutil._git_toplevel_and_head(str(carried)) == (None, None)
+
+
+def test_ordinary_checkout(tmp_path):
+ r = make_repo(tmp_path / "repo")
+ assert os.path.samefile(gitutil._git_toplevel(str(r)), r)
+
+
+def test_linked_worktree_of_a_bare_repo(tmp_path):
+ src = make_repo(tmp_path / "repo")
+ bare = tmp_path / "store.git"
+ _run(tmp_path, "clone", "-q", "--bare", str(src), str(bare))
+ wt = tmp_path / "wt"
+ _run(bare, "worktree", "add", "-q", str(wt), "main")
+ assert os.path.samefile(gitutil._git_toplevel(str(wt)), wt)
+
+
+def test_repo_named_by_the_environment(carried, monkeypatch):
+ monkeypatch.setenv("GIT_DIR", str(carried))
+ assert gitutil._git_toplevel(str(carried)) is not None