Summary
GitPython before 3.1.60 may fail to properly validate a repository's Git-directory location, allowing tracked files to impersonate Git metadata such as gitdir, commondir, and HEAD. If a victim opens or clones a crafted repository and calls index.commit(), a malicious pre-commit hook may execute arbitrary code in the victim's environment.
Affected context
The issue can occur when GitPython processes a cloned or opened repository whose tracked files influence the location or interpretation of Git metadata. Risk is higher when repository contents are obtained from an untrusted source and application code invokes commit operations without independently validating repository structure or hook behavior.
Exploit preconditions
- Attack surface: GitPython repository opening, cloning, and commit-handling paths
- Required access: The attacker must be able to supply or influence a repository and its tracked files
- Trigger point: A victim or service calls
index.commit()on the affected repository - Why exploitation is possible: Insufficient validation may allow tracked files to impersonate Git-directory metadata and expose a malicious pre-commit hook to the commit workflow
Why this happens
The underlying bug is inadequate validation of the location and identity of the Git directory when repository metadata is resolved. This can cause attacker-controlled tracked files to be treated as trusted Git control data, including hook locations. When a commit operation invokes the resulting hook, attacker-supplied code may run in the process context. This behavior maps to CWE-94, Improper Control of Generation of Code, because untrusted repository content can influence executable hook processing.
Potential impact
- Arbitrary code could execute with the privileges of the GitPython process
- Application credentials, tokens, or repository data may be exposed
- A compromised developer or service environment could be used for lateral movement
- Build, automation, or source-control workflows may be tampered with
General mitigation guidance
Mitigation generally focuses on reducing trust in repository-provided metadata
and preventing untrusted hooks from executing in automated workflows.
Common defensive patterns include:
- Use a maintained GitPython release that contains the relevant security correction
- Treat repositories from untrusted sources as hostile input
- Validate repository paths and Git-directory relationships independently of tracked files
- Disable or restrict hook execution for automation and sandbox repository operations where feasible
- Prefer isolated, least-privileged workers for repository processing
- Avoid exposing credentials, write access, or sensitive host resources to repository-handling processes
- Apply allowlists and policy checks before invoking commit operations on externally supplied repositories
How teams detect issues like this
Detection can include:
- Static analysis and dependency review for repository APIs that resolve Git metadata or invoke hooks
- Code review checks for commit operations performed on untrusted or insufficiently validated repositories
- Repository policy enforcement that blocks unexpected
gitdir,commondir,HEAD, or hook-related structures - Runtime monitoring for child processes launched during repository processing
- Alerts for unusual process execution, file changes, or network access originating from Git automation workers
- Automated security review tools that identify unsafe hook execution and untrusted repository handling

