CVE-2026-87817 — gitpython
Fix: gitpython-developers/GitPython#2218CVE-2026-87817 is a Code Injection vulnerability in gitpython. A fix is available for gitpython — see the affected versions and patch details below.
GitPython before 3.1.60 Remote Code Execution via Git Directory Impersonation
Exploitation Status
No confirmed exploitation observed yet
- CISA assesses this as automatable — exploitation doesn’t require manual, per-target effort, which raises the odds of mass scanning and opportunistic attacks.
- A successful exploit gives an attacker total control of the affected component, not partial access.
- CISA’s own triage has not observed active exploitation or public proof-of-concept code for this CVE as of its last assessment.
Exploitation and automatability from CISA’s SSVC triage for CVE-2026-87817.
EPSS Exploitation Probability
Probability of exploitation in the next 30 days, from FIRST.org EPSS.
Real-World Exposure
gitpythonReal-time download stats are indexed for npm and PyPI packages. This vulnerability affects PyPI packages — download data is not available via public APIs for these ecosystems.
Description
Summary
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 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.
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.
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/credentialsdiscloses 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 with
transfer.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 tracked
commondir of .git pins common_dir to the real .git.
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.
Remediation
- 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).
Note for the maintainers
Commit 406b98e1 (2026-05-31, "respect core.hooksPath for commit hooks") resolved the hook path via
git 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
Affected Packages
| Ecosystem | Package | Vulnerable range | Fix |
|---|---|---|---|
| 🐍PyPI | gitpython | all versions | 3.1.60pip install --upgrade 'gitpython==3.1.60' |
Affected Products
gitpythongitpython_projectDetection & mitigation playbook
Open-source dependencyDetect
Scan your dependency tree (package-lock.json, pnpm-lock.yaml, requirements.txt, go.sum, etc.) for gitpython, including transitive dependencies — a direct dependency you never call can still pull in a vulnerable version.
Fix
Update gitpython to 3.1.60 or later, then make sure no transitive (indirect) dependency still pins the vulnerable range — O3 confirms CVE-2026-87817 is resolved across your whole dependency graph.
Workarounds
Stop passing untrusted input into the interpreter or shell: call the affected binary with an argument array rather than a composed command string, reject anything outside a strict allowlist of expected values, and run the component under an account that cannot reach beyond the work it legitimately does.
Fixing This On Your OS
If you run this on a Linux distribution, patch through your package manager against the distro's own security advisory below — it tracks the exact backported fix for your release, which can ship on a different timeline (and sometimes a different severity) than the upstream project.
This vulnerability is rated as Important because it allows remote code execution in Red Hat products utilizing GitPython. Exploitation requires user interaction, specifically a victim calling index.commit() on a repository where an attacker has previously placed a malicious pre-commit hook by impersonating the git…
| Product | Fixed in | Advisory |
|---|---|---|
| Red Hat Ansible Automation Platform 2.5 for RHEL 8 | automation-controller-0:4.6.33-1.el8ap | RHSA-2026:71114 |
| Red Hat Ansible Automation Platform 2.6 for RHEL 9 | automation-controller-0:4.7.17-1.el9ap | RHSA-2026:71113 |
| Red Hat Satellite 6.16 for RHEL 8 | python-gitpython-0:3.1.62-1.el8pc | RHSA-2026:74506 |
| Red Hat Satellite 6.17 for RHEL 9 | python-gitpython-0:3.1.62-1.el9pc | RHSA-2026:74505 |
| Red Hat Satellite 6.18 for RHEL 9 | python3.12-gitpython-0:3.1.62-1.el9pc | RHSA-2026:74504 |
| Red Hat Satellite 6.19 for RHEL 9 | python3.12-gitpython-0:3.1.62-1.el9pc | RHSA-2026:74503 |
| Red Hat Ansible Automation Platform 2.5 | ansible-automation-platform-25/controller-rhel8:1789607021 | RHSA-2026:71210 |
| Red Hat Ansible Automation Platform 2.6 | ansible-automation-platform-26/controller-rhel9:1789673739 | RHSA-2026:71179 |
Frequently Asked Questions
Is CVE-2026-87817 in your dependencies?
Find it across PyPI, including transitive dependencies.