Summary
GitPython: Repository content can impersonate the git directory, leading to arbitrary code execution
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.
CVE-2026-87817
Affected code (3.1.59)
The discovery loop in git/repo/base.py tests, in order:
git/repo/base.py:299,isfile(curpath/gitdir)andisfile(curpath/commondir)andisfile(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 last
is_git_dir (git/repo/fun.py:60) requires only that objects/ and refs/ are directories and thatHEAD is a file; HEAD's contents are never parsed. The hook path is resolved fromindex.repo.git_dir (git/index/fun.py:73), i.e. the mis-resolved directory.
Proof of concept
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
Observed on the PyPI release 3.1.59 (Linux and Windows):
real git says the git dir is : /tmp/.../victim/.git
GitPython says it is : /tmp/.../victim <- shadowed
attacker's hook executed : True
git fsck : (clean)
The pre-commit hook runs at git/index/base.py:1201, before write_tree(), so it fires even though
the commit later fails.
Threat model / preconditions
- Attacker controls the content of a repository the victim opens or clones with GitPython (public repo,
fork PR, mirrored dependency). - For code execution, the victim performs a commit via GitPython's native
index.commit(). For the
file-read impact, opening the repo and reading config is enough. GIT_WORK_TREEis not a mitigation,git_diris assigned and the discovery loop breaks before
the environment is consulted.- Real git is unaffected; only GitPython mis-resolves the directory.
Note for the maintainers
Commit 406b98e1 (2026-05-31, "respect core.hooksPath for commit hooks") resolved the hook path viagit rev-parse --git-path, which is immune because git rediscovers the true .git. Commit 9bc287a2
(2026-07-20) reverted it to avoid an unconditional rev-parse dependency, reintroducing the exec step.
Measured at each commit with the healthy-looking layout: 406b98e1 → hook did not fire; 9bc287a2 …3.1.59 → hook fired.
Scope note
Only the repository-root case is reported: where a real .git exists, git prefers it, and GitPython
does not. A fake git dir in a subdirectory fools real git too, so that variant is out of scope as a
general ecosystem hazard rather than a GitPython defect.
###POC Files :
poc1_rce.py
poc2_file_read.py
Impact
A service that opens or clones an untrusted repository with GitPython, a CI runner building a fork
pull request, a code-scanning/SBOM service, a mirror, a dependency bot, an AI code-review/agent tool ,
can be made to:
- Execute arbitrary commands via the tracked
<root>/hooks/pre-commitwhen the victim callsindex.commit(). - Read files outside the repository: the tracked
<root>/configbecomes the repository config and
is parsed withmerge_includes=True(git/repo/base.py:765), so[include] path = ~/.aws/credentials
discloses the file. (Repo._config_readerstill defaultsmerge_includes=True, so the hardening
added in 3.1.59 for.gitmodules/GHSA-7833 does not cover this path.) - Write a config file to an attacker-chosen directory via an absolute tracked
commondir.
Delivery is silent: git clone exits 0, git fsck (including --strict, and withtransfer.fsckObjects/fetch.fsckObjects=true) reports nothing, and the clone passes every read-only
probe (head.commit, branches, is_dirty(), untracked_files, iter_commits) because a trackedcommondir of .git pins common_dir to the real .git.
Untrusted input is evaluated as executable code within the application's runtime environment. Typical impact: arbitrary code execution within the application's privilege context.
CVE-2026-87817 has a CVSS score of 8.8 (High). The vector is network-reachable, no privileges required, and user interaction required. A CVSS score reflects the worst-case severity of the vulnerability, not your specific exposure. Whether this affects your application depends on whether the vulnerable code is present and reachable in your environment. A fixed version is available (3.1.60); upgrading removes the vulnerable code path.
Affected versions
Security releases
Kodem intelligence
Severity tells you how bad this could be in the worst case. It does not tell you whether you are exposed. Exploitability and impact are functions of runtime truth: whether the vulnerable code is present, reachable, and actually executes in your application. A vulnerable package can sit in your dependency tree and never run.
Kodem, an Intelligent Application Security platform, uses runtime intelligence to reveal which vulnerabilities actually execute in production, so teams prioritize the ones that genuinely matter. Kodem's runtime-powered SCA identifies whether this CVE is reachable in your applications.
Already deployed Kodem?
See it in your environmentNew to Kodem? Get a demo →Remediation advice
- Test
curpath/.gitbefore thegitdir/commondir/HEADtriple and beforeis_git_dir(curpath). - Resolve
hook_path/_commit_hook_path/_get_validated_reflog_paththroughrepo.common_dir. - Containment-check
commondir/gitdircontents before joining (reuseSymbolicReference._get_validated_path). - Validate
HEADinis_git_dir(requireref: refs/...or a 40-hex sha). - Pass
merge_includes=FalseinRepo._config_reader(git/repo/base.py:765).
Frequently Asked Questions
- What is CVE-2026-87817? CVE-2026-87817 is a high-severity code injection vulnerability in gitpython (pip), affecting versions <= 3.1.59. It is fixed in 3.1.60. Untrusted input is evaluated as executable code within the application's runtime environment.
- How severe is CVE-2026-87817? CVE-2026-87817 has a CVSS score of 8.8 (High). This score reflects the worst-case severity of the vulnerability, not your specific exposure. Whether it represents real risk in your environment depends on whether the vulnerable code is present and reachable.
- Which versions of gitpython are affected by CVE-2026-87817? gitpython (pip) versions <= 3.1.59 is affected.
- Is there a fix for CVE-2026-87817? Yes. CVE-2026-87817 is fixed in 3.1.60. Upgrade to this version or later.
- Is CVE-2026-87817 exploitable, and should I be worried? Whether CVE-2026-87817 is exploitable in your environment depends on whether the vulnerable code is present and reachable. A CVSS score is a worst-case rating; it does not account for your specific deployment, configuration, or usage patterns. Kodem, an Intelligent Application Security platform, uses runtime intelligence to show which vulnerabilities actually execute in production, so you can focus on the ones that represent real risk. Get a demo
- What actually determines whether CVE-2026-87817 is exploitable, and how bad it is? Exploitability and impact are not fixed properties of a CVE. They depend on runtime truth: whether the vulnerable code is present, reachable, and actually executes in your application. A high CVSS score on a dependency that never runs is not the same as real risk. Kodem, an Intelligent Application Security platform, uses runtime intelligence to reveal which vulnerabilities actually execute in production, so teams prioritize the ones that genuinely matter.
- How do I fix CVE-2026-87817? Upgrade
gitpythonto 3.1.60 or later.