Summary
GitPython versions 3.1.30 through 3.1.46 contain a command injection vulnerability where certain Git transport options are blocked in the CLI flags, but their Python keyword argument equivalents are not.
If an application forwards attacker-controlled keyword arguments like upload_pack or receive_pack into GitPython's clone or remote APIs, an attacker can trigger arbitrary command execution even when unsafe options are ostensibly disabled.
Affected context
This vulnerability appears in Python applications that embed GitPython and:
- Use
Repo.clone_from(),Remote.fetch(),Remote.pull(), orRemote.push() - Accept or propagate user-controlled or otherwise untrusted data into GitPython keyword arguments
- Rely on GitPython's
allow_unsafe_options=Falsedefault to block dangerous Git options
The risk is most pronounced in services like source code management frontends, CI/CD orchestrators, or web APIs that perform Git operations on behalf of remote users.
Exploit preconditions
- Attack surface: Any code path that calls:
git.Repo.clone_from(...)remote.fetch(...)remote.pull(...)remote.push(...)
with keyword arguments that influence Git transport options.
- Required access: Ability to control or significantly influence function kwargs such as
upload_packorreceive_pack(directly via an API parameter, indirectly via configuration, job specs, or serialized tasks). - Trigger point: Invocation of Git operations where dangerous options are passed via kwargs rather than the corresponding CLI flags, causing Git to execute attacker-supplied commands.
- Why exploitation is possible: The library's safeguard that blocks dangerous Git options examines CLI-style arguments but does not equivalently enforce restrictions on the parallel Python keyword params (
upload_pack,receive_pack). This mismatch leaves a command execution vector open even whenallow_unsafe_optionsis set to its safe default.
Why this happens
The underlying bug belongs to CWE-78: Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection').
GitPython attempts to limit exposure to dangerous Git transport options by filtering certain command-line flags (e.g., --upload-pack, --receive-pack) when allow_unsafe_options is False. However, the API exposes Python keyword arguments (upload_pack, receive_pack) that semantically map to the same Git capabilities but are not subjected to the same safety checks. When untrusted data reaches these kwargs, it can be incorporated into the underlying Git command line without proper neutralization or validation. This design gap between option filtering and the higher-level API leaves a command injection channel that bypasses the intended security control.
Potential impact
- Execution of arbitrary OS commands with the privileges of the application process.
- Compromise of the host running the vulnerable service or CI/CD pipeline.
- Unauthorized access, modification, or exfiltration of source code repositories.
- Lateral movement within build or deployment infrastructure using the compromised process.
- Tampering with build artifacts, leading to supply-chain compromise for downstream consumers.
General mitigation guidance
Effective mitigation centers on not trusting caller-provided Git options and ensuring invariant-safe Git operations:
- Avoid passing user-controlled or loosely validated values into GitPython keyword arguments that influence Git transport or helper binaries, especially
upload_packandreceive_pack. - Introduce strict allowlists for permitted Git options, transports, and remote URLs; reject or ignore anything outside a well-defined, minimal set.
- Encapsulate Git operations behind a narrow internal API that only accepts high-level parameters (e.g., repository URL, branch, reference) and never raw Git options or arbitrary kwargs.
- Drop privileges and isolate Git operations using least-privilege OS accounts, reduced filesystem access, and, where feasible, container or sandbox boundaries to constrain the blast radius of command execution.
- Treat any configuration, job specification, or pipeline definition that can influence Git options as untrusted input and validate it accordingly.
Architectural patterns that keep Git invocation logic centralized and declarative generally reduce the opportunity for option-level injections.
How teams detect issues like this
Detection typically involves code-centric and process-centric approaches:
- Static analysis and code search:
- Scan for calls to
Repo.clone_from,Remote.fetch,Remote.pull, andRemote.pushthat pass dynamic keyword arguments, particularlyupload_packandreceive_pack. - Flag patterns where kwargs are built from HTTP request data, environment variables, YAML/JSON configs, or job descriptors without sanitization or allowlisting.
- Code review signals:
- Question any use of GitPython kwargs that manipulate transport helpers, custom binaries, or arbitrary Git command-line fragments.
- Investigate utility wrappers that accept
**kwargsand forward them directly into GitPython APIs. - Policy and tooling:
- Enforce secure coding guidelines that prohibit passing untrusted data into Git options across the codebase.
- Use automated review tools or linters that can identify dangerous GitPython usage patterns and surface them during CI.
- Runtime monitoring (defense in depth):
- Monitor for unusual Git sub-process invocations or unexpected binaries launched under Git-related service accounts.
- Apply command-logging and anomaly detection on Git service hosts to catch suspicious option usage indicative of injection attempts.

