Summary
simple-git, a JavaScript library that wraps native Git commands, contains an OS command injection flaw in versions up to and including 3.31.1 due to incomplete blocking of dangerous Git options.
By crafting Git options that bypass the regular-expression-based blocklist, an attacker can cause execution of arbitrary commands through the underlying Git binary when simple-git is used on untrusted input.
Affected context
This vulnerability affects applications that use simple-git (≤3.31.1) to invoke Git operations, particularly when Git options or arguments are influenced by user-controlled or otherwise untrusted input.
The issue manifests in code paths that rely on the unsafe operations plugin's regex blocklist to prevent dangerous options (such as -u or --upload-pack). Because Git supports flexible and compact option combinations (e.g., -vu, -4u, -nu), this blocklist can be bypassed, allowing attackers to inject effective dangerous options through variants that still parse as valid flags to Git.
Exploit preconditions
- Attack surface: Any API in an application that uses simple-git to run Git commands where command-line options or arguments are derived from external input (HTTP parameters, webhook payloads, configuration files, CI pipeline variables, etc.).
- Required access: Ability to control or significantly influence Git options or parts of the Git command line passed through simple-git. This can be direct (user-provided CLI flags) or indirect (repository URLs, environment-derived options, or dynamic configuration).
- Trigger point: Execution of a simple-git operation that engages the unsafe operations plugin's protection logic but still accepts crafted option strings containing dangerous variants (e.g., concatenated short options including
u). - Why exploitation is possible: The protection relies on a regex-based blocklist of specific string forms (e.g.,
-u,--upload-pack) and does not replicate Git's full option-parsing semantics. Git interprets many different character combinations as equivalent to the blocked options, allowing bypass of the blocklist and enabling arbitrary command execution through the invoked Git process.
Why this happens
The root cause lies in attempting to secure command execution using a blocklist-based filter against a highly flexible command-line interface without modeling its full parsing rules.
simple-git introduces an "unsafe operations" plugin that scans option strings and rejects known-dangerous options like -u and --upload-pack using regular expressions. However, Git's option parsing allows dense packing and reordering of short options and recognizes numerous syntactic variants that map back to the same semantic behavior.
This creates a mismatch between the filter's simplistic string-level understanding and Git's rich parsing behavior: the filter inspects only literal substrings, while Git reconstructs options from character sequences. As a result, attackers can craft alternative forms that Git still interprets as dangerous options but that do not match the regex, exemplifying CWE-78: Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection').
Potential impact
- Execution of arbitrary OS commands in the context of the process using simple-git.
- Compromise of the host running the Node.js application (including reading or modifying local files accessible to the process).
- Abuse of CI/CD agents, build servers, or deployment pipelines that rely on simple-git for repository operations.
- Lateral movement within development or production infrastructure by leveraging the compromised process's network and system privileges.
- Integrity and confidentiality breaches of source code repositories or associated build artifacts.
General mitigation guidance
Effective mitigation focuses on eliminating blocklist-based command filtering and minimizing the command surface exposed to untrusted input:
- Prefer allowlist-based argument construction: restrict the Git commands and options to a fixed, hard-coded set and never pass arbitrary option strings directly derived from user input.
- Use structured parameters instead of raw option strings: map high-level operations (e.g., "clone this repo at branch X") to predefined argument lists, validating each parameter (such as repository URL or branch name) against strict rules (e.g., safe character sets, length bounds, and expected formats).
- Avoid dynamically building command lines: rely on APIs that accept arrays of arguments, and ensure those arrays are assembled only from validated, normalized values rather than concatenated strings.
- Apply principle of least privilege to the environment running simple-git: run under non-privileged accounts, avoid unnecessary file system and network permissions, and use containerization or sandboxing where feasible to reduce blast radius.
- Where dynamic options are truly necessary, perform semantic validation that mirrors Git expectations (e.g., explicit parsing of recognized options and rejection of any option packing or unknown flags), instead of regex-based substring matching.
How teams detect issues like this
The vulnerability can be surfaced and monitored through a combination of static and dynamic techniques:
- Static analysis and code review:
- Flag any call sites where simple-git APIs receive user-influenced strings as options, arguments, or repository URLs.
- Identify patterns of dynamic command construction (string concatenation, template literals, passing through unvalidated query parameters or request bodies).
- Review uses of the unsafe operations plugin for reliance on regex-based blocklists and absence of allowlisted command/option sets.
- Policy and configuration enforcement:
- Establish internal guidelines forbidding raw user input from being passed as CLI options to Git or other system binaries.
- Introduce pre-merge checks or linters that detect risky patterns like
simpleGit(...).raw(userInput)or similarly unguarded APIs. - Runtime and monitoring approaches:
- Instrument logging of Git commands executed via simple-git, capturing arguments in a sanitized form, and alert on suspicious patterns (packed short options, unexpected flags, or out-of-policy parameters).
- Use host or container-level security tooling (e.g., process monitoring, EDR) to detect anomalous child processes spawned from the Node.js runtime or unusual Git invocation patterns.

