Summary
Degit contains a command injection vulnerability where unsanitized user input is passed into git shell commands that are executed via exec().
By providing a specially crafted git repository name, an attacker can cause arbitrary operating system commands to run with the privileges of the degit process, especially in workflows that clone untrusted repositories.
Affected context
This vulnerability affects environments where degit is used to clone git repositories based on user-controlled or partially untrusted input.
It is most relevant in CLI tools, scaffolding generators, CI/CD pipelines, or web services that accept repository identifiers and forward them directly to degit without strict validation.
The problem arises when the repository name is interpolated into git shell commands constructed as strings and executed via exec(), without robust escaping or restriction to a safe subset of characters and formats.
Exploit preconditions
- Attack surface: Any interface (CLI, API, web form, CI job configuration, or script) that passes user-supplied repository names into degit.
- Required access: Ability to influence or control the git repository argument consumed by degit, either directly (user input) or indirectly (config files, environment variables, templates).
- Trigger point: Invocation of
_cloneWithGit()orfetchRefs()where the crafted repository name is concatenated into a shell command that is executed usingexec(). - Why exploitation is possible: The repository name is treated as trusted data and embedded into a shell command line without proper quoting, escaping, or parameterization, allowing shell metacharacters to break out of the intended git command and inject additional commands.
Why this happens
The underlying issue is classic command injection: dynamic shell commands are constructed by concatenating untrusted input and then executed with a generic shell API.
Instead of invoking git with a parameterized, argument-based process spawn, degit uses exec() with a single command string that includes the repository name.
This pattern corresponds to CWE-78 (Improper Neutralization of Special Elements used in an OS Command) and CWE-77 (Improper Neutralization of Special Elements used in a Command), where shell metacharacters are not neutralized, allowing injected tokens to be interpreted as separate commands or arguments.
Conceptually, the code fails to enforce a strict separation between data (user repository identifiers) and control (the shell command being executed), enabling arbitrary command execution in the context of the running process.
Potential impact
- Arbitrary command execution with the privileges of the process running degit
- Unauthorized access to sensitive files and environment variables on the host system
- Compromise of CI/CD runners, build agents, or developer workstations that rely on degit
- Installation of malware, backdoors, or persistence mechanisms in development environments
- Lateral movement within the network if credentials or tokens are accessible to the compromised process
General mitigation guidance
Mitigation focuses on eliminating unsafe command construction patterns and reducing trust in repository identifiers:
- Prefer APIs that spawn subprocesses with argument arrays (e.g.,
spawn/execFileequivalents) instead of building shell command strings. - Treat repository names as opaque data and strictly validate them against a whitelist of allowed patterns (e.g., specific URL schemes, hostnames, and character sets).
- Avoid passing user input through the shell; when shell usage is unavoidable, apply rigorous escaping and avoid interpreting metacharacters.
- Run tools that interact with untrusted repositories under least-privilege service accounts with limited filesystem and network access.
- Isolate high-risk cloning workflows (e.g., in CI pipelines) inside containers or sandboxes to contain potential compromise.
How teams detect issues like this
Effective detection usually combines static analysis, code review, and runtime controls:
- Use static analysis rules to flag usages of generic shell execution functions (like
exec,execSync, or similar APIs) where user input, configuration strings, or environment variables influence the command string. - In code review, look for string concatenation or template literals that assemble git commands, and verify that any user-controllable values are either disallowed or rigorously validated.
- Enforce internal policies that prohibit shell-based command construction for VCS operations, preferring library APIs or argument-based subprocess execution.
- Integrate security-focused linters or SAST tools in CI to automatically detect OS command injection patterns across the codebase.

