CVE-2026-87817
Improper git directory validation enables malicious pre‑commit hook execution via GitPython.
- CVSS 8.7
- CWE-94
- Input Validation and Sanitization
- Remote
GitPython before 3.1.60 fails to properly validate the git directory location, allowing attackers to impersonate the git directory using tracked files like gitdir, commondir, and HEAD. Attackers can execute arbitrary code by placing a malicious pre-commit hook in the tracked hooks directory that executes when a victim calls index.commit() on a cloned or opened repository.
- CWE
- CWE-94
- CVSS base score
- 8.7
- Published
- 2026-09-09
- OWASP
- A08 Software and Data Integrity Failures
- Orthogonal defect classification
- Checking
- Code defect classification
- Missing Check
- Category
- Input Validation and Sanitization
- Subcategory
- Insecure Direct Object References (IDOR)
- Accessibility scope
- Remote
- Impact
- Arbitrary Code Execution
- Affected component
- GitPython
- Fixed by upgrading
- Yes
Solution
Upgrade GitPython to 3.1.60 (or any later version).
Vulnerable code sample
import sys
from git import Repo
def commit_changes(repo_path, message):
# VULNERABLE: trusts unvalidated git directory location
repo = Repo(repo_path) # attacker can point to a crafted .git structure
index = repo.index
index.add(['file.txt'])
index.commit(message)
def main():
repo_path = sys.argv[1]
commit_changes(repo_path, "Update")Patched code sample
import sys
import os
from git import Repo
def commit_changes(repo_path, message):
# FIX: validate that the .git directory resides within the provided repository root
abs_repo_path = os.path.abspath(repo_path)
git_dir = os.path.abspath(os.path.join(abs_repo_path, '.git'))
if not git_dir.startswith(abs_repo_path + os.sep):
raise ValueError("Invalid repository path: .git directory outside repository")
repo = Repo(abs_repo_path)
index = repo.index
index.add(['file.txt'])
index.commit(message)
def main():
repo_path = sys.argv[1]
commit_changes(repo_path, "Update")Published fix
Public sources link this CVE to the changes below. The code samples above were generated to illustrate the defect; these links lead to the code the project actually changed.
- fix: prefer `.git` during repository discovery (gitpython-developers/GitPython)
git/repo/base.py,git/repo/base.py · Repo._get_alternates,git/repo/base.py · Repo._set_alternates,git/repo/base.py · Repo.__init__,git/repo/fun.py · find_submodule_git_dir,git/repo/fun.py · find_worktree_git_dir,git/repo/fun.py · is_git_dir
Cite this entry
@misc{vaitp:cve202687817,
title = {{Improper git directory validation enables malicious pre‑commit hook execution via GitPython.}},
author = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
year = {2026},
note = {VAITP Python Vulnerability Dataset, entry CVE-2026-87817},
howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2026-87817/}}
}
Introducing the "VAITP dataset": a specialized repository of Python vulnerabilities and patches, meticulously compiled for the use of the security research community. As Python's prominence grows, understanding and addressing potential security vulnerabilities become crucial. Crafted by and for the cybersecurity community, this dataset offers a valuable resource for researchers, analysts, and developers to analyze and mitigate the security risks associated with Python. Through the comprehensive exploration of vulnerabilities and corresponding patches, the VAITP dataset fosters a safer and more resilient Python ecosystem, encouraging collaborative advancements in programming security.
The supreme art of war is to subdue the enemy without fighting.
Sun Tzu – “The Art of War”
:: Shaping the future through research and ingenuity ::
