GitHub advisory · PyPI
GitPython: Repository content can impersonate the git directory, leading to arbitrary code execution
Description
Repo.__init__ decides which directory is the git directory by testing candidate paths in an order that
considers the real .git last. Two earlier tests can be satisfied by ordinary tracked files. Git
reserves only the literal name .git, so HEAD, objects/, refs/, config, gitdir, commondir
and hooks/ at a repository root are all legal tracked content.
Consequently, after a victim opens or clones an attacker's repository, GitPython resolves git_dir to
the working-tree root while real git correctly resolves <root>/.git. Everything GitPython then
treats as "inside the git directory" is attacker-authored content — including hooks/, which it
executes.
The discovery loop in git/repo/base.py tests, in order:
git/repo/base.py:299 — isfile(curpath/gitdir) and isfile(curpath/commondir) and isfile(curpath/HEAD)git/repo/base.py:320 — is_git_dir(curpath)git/repo/base.py:341 — dotgit = osp.join(curpath, ".git") ← the real git dir, considered lastis_git_dir (git/repo/fun.py:60) requires only that objects/ and refs/ are directories and that
HEAD is a file; HEAD's contents are never parsed. The hook path is resolved from
index.repo.git_dir (git/index/fun.py:73), i.e. the mis-resolved directory.
Requires only pip install GitPython==3.1.59. Full script attached as poc1_rce.py; it runs entirely
in a temp directory and the payload only writes a marker file.
Attacker repository — four ordinary tracked files at the root:
| path | mode | content |
|---|---|---|
| gitdir | 100644 | .git\n |
| commondir | 100644 | .git\n |
| HEAD | 100644 | ref: refs/heads/master\n |
| hooks/pre-commit | 100755 | #!/bin/sh + payload |
Victim — two ordinary calls:
repo = git.Repo.clone_from(url, dst) # or git.Repo(dst)
repo.index.commit("automated commit") # code execution happens here
```…
Manual fix · PyPI
pip install --upgrade "gitpython>=3.1.60"Sources