GHSA-83vg-56qc-22m7: High severity pip/Gitpython vulnerability
Summary
createsubmodule(name, path, url) clones a submodule into the repo's own .git directory before it rejects the path. A path of ".git/hooks" is accepted by the pre-clone check, so the submodule is cloned straight into the live hooks directory, and a file named post-checkout in the submodule becomes an executable .git/hooks/post-checkout. On 3.2.0+ the later index check does raise ValueError, but only after the clone already wrote to disk, and it does not remove the planted hook; on 3.1.x there is no such check and the call succeeds silently. Either way the hook runs on the next git operation (e.g. checkout), giving code execution.
Details
The checkout path is validated too late, after the clone has already mutated disk. This breaks the "validate before mutation" invariant that commit 43a43cd8 ("Validate submodule checkout and metadata paths before mutation") set out to enforce.
Submodule.add() calls the permissive path check before cloning, and the strict check only reaches the ".git" alias at index time, after clonerepo has written to disk:
git/objects/submodule/base.py (Submodule.add) 599: path = cls.torelativepath(repo, path) # accepts ".git/hooks" 673: mrepo = cls.clonerepo(repo, url, path, name, ...) # clones into <worktree>/.git/hooks 714: index.add([sm], write=True) # strict check only triggers here
git/index/base.py (IndexFile.preprocessadditems) 732: validaterepopath(entry.path) # rejects ".git", too late
torelativepath only blocks ../absolute/out-of-tree paths; .git aliases are rejected only by validaterepopath, which was added in 3.2.0 (commit a75aedf3) and runs at the index boundary. The ValueError it raises at line 732 does not roll back what clonerepo already wrote.
Version-dependent behavior: - 3.2.0 .. tip bda18e08: validaterepopath raises ValueError at index.add, but the hook is already on disk and is not removed (incorrect order of validation vs. clone). - 3.1.x (e.g. 3.1.59): validaterepopath does not exist and torelativepath does not reject .git; createsubmodule(path=".git/hooks", ...) returns with no error at all and the hook is planted silently.
Proof of concept
Setup: evilsub/ : a git repo whose ROOT contains an executable file named post-checkout (the payload) victim/ : a git repo with at least 1 commit and a second branch, whose .git/hooks has NO sample hooks (the state of a minimal/CI checkout, or git init with an empty template dir, or GITTEMPLATEDIR/init.templateDir empty)
import git, os.path as osp try: git.Repo("victim").createsubmodule(name="x", path=".git/hooks", url=osp.abspath("evilsub")) except ValueError: pass # 3.2.0+: ".git" is rejected, but only AFTER the clone, so the plant already happened. # 3.1.x: no exception is raised at all; the call succeeds and still plants the hook.
victim/.git/hooks/post-checkout now exists, mode 0755, attacker-controlled, on every affected version. Any later checkout runs it: git -C victim checkout <other-branch> -> the hook runs -> code execution
Verified live: - tip bda18e08 (3.2.0 + 3 commits): createsubmodule raised ValueError from git/index/base.py:732, yet .git/hooks/post-checkout was present and executable, and git checkout executed it. - 3.1.59: createsubmodule returned with no error; .git/hooks/post-checkout was planted and executed by git checkout.
Impact
Arbitrary code execution. A library/app that creates a submodule from an attacker-influenced path ends up with an attacker-controlled executable in .git/hooks, which git runs on ordinary later operations. This affects any service that derives a submodule checkout path from untrusted data, e.g. one that ingests a submodule spec (name/path/url) from a user-supplied manifest or API request and feeds path into createsubmodule. On 3.2.0+ the library's own validation is the thing that fails: it correctly rejects the .git path, but only after the submodule has already been cloned into place, so an app that traps the ValueError reasonably believes the unsafe path was blocked.
Preconditions (hence AC:H): (1) the app passes an attacker-influenced submodule path; (2) the target hook name is free in .git/hooks, true for minimal/CI/container checkouts and whenever init.templateDir/GITTEMPLATEDIR is empty. With the default sample hooks present, git clone refuses the non-empty destination and no plant occurs.
Affected: all tested versions from 3.1.x (3.1.59) through the current tip bda18e08 (3.2.0 + 3 commits). 3.1.x plants silently; 3.2.0+ plants despite raising ValueError.
Affected Software
Remediation
Recommended actions to resolve this vulnerability, in priority order.
- Upgrade
Upgrade
pip/Gitpythonto a version that resolves this vulnerability.Fixed in 3.2.1
Event History
Frequently Asked Questions
Does the ValueError raised in GitPython 3.2.0 and later prevent code execution?
No. In 3.2.0 and later, the error occurs only after the submodule clone has written into .git/hooks, and the planted post-checkout hook is not removed. The hook can execute during a later Git operation such as checkout.
Which applications are exposed to this issue?
Applications are exposed when they invoke create_submodule or Submodule.add with a path that an attacker can influence. The demonstrated dangerous path is .git/hooks, which allows a cloned submodule to place a post-checkout hook in the repository’s live hooks directory.
How can I check whether a repository may already have been affected?
Inspect the repository’s .git/hooks directory for an unexpected post-checkout hook, particularly after failed or rejected submodule-add operations. On 3.2.0 and later, a ValueError does not establish that no hook was written.
What can be done if patching is not immediately possible?
Prevent untrusted input from being used as the submodule checkout path, and reject paths targeting .git/hooks before calling the affected submodule creation functionality. Avoid relying on the later index-time validation, because it occurs after disk mutation.