Summary
Dulwich's submodule handling trusted attacker-controlled paths from .gitmodules without validating that they stayed within safe repository locations.
A malicious upstream repository can drop executable files into a victim's .git/hooks directory during submodule operations, leading to arbitrary code execution when hooks run.
Affected context
This vulnerability affects environments where Dulwich is used to clone or update Git repositories with submodules, especially via dulwich.porcelain.submodule_update or porcelain.clone(..., recurse_submodules=True).
Risk arises when the upstream repository is not fully trusted, or when repositories can be influenced by attackers (e.g., user-submitted or mirrored repositories).
The issue manifests when submodule paths from .gitmodules are accepted as-is and materialized into the filesystem, including within the parent repository's .git directory.
Exploit preconditions
- Attack surface: Git repositories processed by Dulwich that contain submodules.
- Required access: Ability to control or modify the upstream repository contents, including
.gitmodulesand the tree entries that correspond to submodules. - Trigger point: Execution of
dulwich.porcelain.submodule_updateorporcelain.clone(..., recurse_submodules=True)against the crafted repository. - Why exploitation is possible: Dulwich's submodule porcelain accepts submodule
pathvalues from.gitmoduleswithout enforcing that they remain outside the.gitdirectory or other sensitive internal paths, allowing attacker-controlled content to be written into.git/hooksand similar locations.
Why this happens
The core issue is a lack of path validation and normalization when materializing submodule paths specified in the .gitmodules file.
Dulwich's implementation of submodule update logic treats these paths as trustworthy and directly maps them to filesystem locations within the parent repository, including its .git directory.
This behavior aligns with CWE-22: Improper Limitation of a Pathname to a Restricted Directory, where user-controlled or remote-controlled path components are not constrained to a safe directory, enabling writes into sensitive internal paths such as .git/hooks.
Potential impact
- Attacker-controlled executables can be written into
.git/hooks/, preserving executable permissions. - Arbitrary code can be executed when Git or Dulwich commands trigger the compromised hooks.
- Local developer or CI environment compromise through execution of malicious hooks during clone, fetch, commit, or other hook-invoking operations.
- Potential pivot into broader build or deployment infrastructure if compromised repositories are used in automated pipelines.
General mitigation guidance
Mitigation typically focuses on constraining where submodule paths are allowed to resolve and treating repository metadata as untrusted input.
Useful defensive patterns include:
- Enforcing path normalization and explicit checks that submodule paths cannot point into
.gitor other internal control directories. - Rejecting any submodule configuration whose paths are absolute, contain
..segments, or otherwise escape the repository working tree. - Treating upstream repositories, especially from external or user-controlled sources, as untrusted and applying additional validation or policy checks before materializing submodules.
- Running repository operations under least-privilege accounts and separating build/CI agents from sensitive host-level privileges to limit the blast radius of malicious hooks.
- Where feasible, disabling automatic hook execution in high-risk automation contexts or enforcing a whitelist of allowed hooks.
How teams detect issues like this
Detection efforts can focus on both source-level patterns and repository hygiene:
- Static analysis or code review to identify filesystem write operations that rely on unvalidated paths taken from
.gitmodulesor other repository metadata. - Repository scanning to flag
.gitmodulesentries whosepathpoints into.git, contains traversal sequences, or otherwise targets internal directories. - CI policies that reject or quarantine repositories where submodule paths do not remain within the main working tree.
- Monitoring of hook directories for unexpected or newly introduced executable files, especially following clone or submodule update operations.
- Integrating automated review tools into development workflows to highlight missing path normalization and checks around submodule handling code.

