Command Injection via `child_process.spawn` with `shell: true` on Windows in Angular CLI
Command
add, new
Is this a regression?
- [ ] Yes, this behavior used to work in the previous version
The previous version in which this bug was not present was
No response
Description
Two files in Angular CLI use a deprecated spawn() pattern on Windows — arguments are concatenated into a single string instead of being passed as an array. This causes shell metacharacters (&, |, >) to be interpreted by cmd.exe, leading to unintended OS command injection.
Node.js has explicitly deprecated this pattern as DEP0190. The Linux/macOS code path already does it correctly.
File 1: packages/angular/cli/src/package-managers/host.ts (Lines 135-136)
const childProcess = isWin32
? spawn(`${command} ${args.join(' ')}`, spawnOptions) // Windows: concatenated string
: spawn(command, args, spawnOptions); // Linux: proper array
When ng add receives a package specifier with shell metacharacters (e.g., [email protected] & echo injected), npm-package-arg parses it as a valid range specifier. The & echo injected portion flows unsanitized into args, gets concatenated, and cmd.exe executes it as a separate command.
File 2: packages/angular_devkit/schematics/tasks/repo-init/executor.ts (Line 44)
spawn(`git ${args.join(' ')}`, spawnOptions); // Same deprecated pattern
The git commit message passed via ng new --commit.message="..." flows into this spawn() call. A crafted message like " & echo PWNED > pwned.txt & rem " breaks out of the git command and executes arbitrary commands.
The commit message is injected at line 85 without sanitization:
await execute(['commit', `-m "${message}"`]);
Impact: On Windows, both code paths allow OS command injection — arbitrary commands execute with the privileges of the user running Angular CLI. This is a behavioral inconsistency with Linux/macOS, where args are passed as an array and shell metacharacters are treated as literal characters.
Minimal Reproduction
Reproduction 1: ng add (host.ts)
ng new test-app
cd test-app
ng add "[email protected] & echo INJECTED" --skip-confirmation
Expected: The entire string [email protected] & echo INJECTED is treated as a single argument (same as Linux).
Actual (Windows): cmd.exe splits at & and runs echo INJECTED as a separate command.
Reproduction 2: ng new (executor.ts)
ng new malicious-app --skip-install --commit.message="\" & echo PWNED > pwned.txt & rem \""
Expected: The message is passed as a literal string to git commit -m.
Actual (Windows): cmd.exe executes echo PWNED > pwned.txt — a file pwned.txt is created in the working directory, confirming OS command injection.
Exception or Error
No exception. The injected commands execute silently alongside the intended operation. This is what makes the behavior dangerous — there is no visible error to alert the user.
Example for `ng add`:
✔ Determining Package Manager
› Using package manager: npm
✔ Loading package information ← injected command runs here silently
✔ Installing package
Your Environment
Angular CLI: 21.1.4
Node: 25.6.0
Package Manager: npm 11.3.0
OS: Windows 11
Angular: 21.1.4
Package Version
------------------------------------------------------
@angular-devkit/architect 0.2101.4
@angular-devkit/build-angular 21.1.4
@angular-devkit/core 21.1.4
@angular-devkit/schematics 21.1.4
@angular/cli 21.1.4
@schematics/angular 21.1.4
Anything else relevant?
Suggested fix for both files — use the same spawn pattern as Linux/macOS:
File 1 (host.ts):
- const childProcess = isWin32
- ? spawn(`${command} ${args.join(' ')}`, spawnOptions)
- : spawn(command, args, spawnOptions);
+ const childProcess = spawn(command, args, spawnOptions);
File 2 (executor.ts):
- spawn(`git ${args.join(' ')}`, spawnOptions)
+ spawn('git', args, spawnOptions)
And for the commit message argument:
- await execute(['commit', `-m "${message}"`]);
+ await execute(['commit', '-m', message]);
Real-World Attack Scenarios
1. Malicious npm package README / Installation instructions`
2. Open-source project contributor exploitation
3. CI/CD pipeline poisoning
References:
- Node.js DEP0190: https://nodejs.org/api/deprecations.html#DEP0190
- CWE-78 (OS Command Injection): https://cwe.mitre.org/data/definitions/78.html
- CVE-2024-27980 (related Node.js spawn vulnerability): https://nodejs.org/en/blog/vulnerability/april-2024-security-releases
The suggested changes actually use deprecated syntax. Node.js contributors recommend the concatenated string format over the array-based version, as discussed in https://github.com/nodejs/node/issues/58763#issuecomment-3005576831
Thanks for the pointer to nodejs/node#58763 — I appreciate the context.
I agree that DEP0190 deprecates passing an args array with shell: true, since Node.js just concatenates them without escaping. You're right that the single-string form is what Node.js recommends if shell: true is required.
However, the security issue still stands — the current code concatenates user-controlled input into a shell command string without any sanitization. Whether it's spawn(cmd + ' ' + args.join(' ')) or spawn(cmd, args, { shell: true }), both are equally vulnerable to injection via &, |, > and other shell metacharacters on Windows.
I tested on Windows (Node v25.6.0) and confirmed:
spawn('npm', ['--version'], { shell: false })→ fails with ENOENT (npm isnpm.cmd, needs shell)spawn('git', ['--version'], { shell: false })→ works fine (git is a native .exe)
So the fix should be different for each file:
For executor.ts (git) — shell: true is unnecessary. Git is a native executable on Windows. The fix is straightforward:
spawn('git', args, { ...spawnOptions, shell: false });
And separate the commit message argument properly:
await execute(['commit', '-m', message]); // instead of `-m "${message}"`
For host.ts (npm/yarn/pnpm) — shell: true is needed on Windows because package managers are .cmd scripts. But the arguments must be sanitized before concatenation. At minimum, arguments containing shell metacharacters should be quoted/escaped, or a library like cross-spawn could be used, which handles Windows shell escaping correctly.
Either way, the current code allows unsanitized user input to flow into a shell command string — that's the core vulnerability, regardless of which spawn() form is used.
Happy to submit a PR addressing both files if the team agrees with this direction.
I agree that we should sanitize the arguments and remove shell: true where it isn't necessary. If you're open to submitting a PR, please go ahead. However, we should avoid introducing any additional dependencies to resolve this.
https://github.com/user-attachments/assets/080d4fea-04d5-49a8-8f97-2253bfa8bb4e
https://github.com/user-attachments/assets/1ad5ca72-3eab-4e45-84a3-a64149ff34a8
Thanks @alan-agius4 for the review and for checking with @clydin. I appreciate the time you both took.
You're correct about executor.ts — I verified that --commit.message is rejected by the CLI's yargs parser (Unknown argument: commit.message), and --commit is only registered as a boolean. The commit message in executor.ts is not user-controlled via the CLI. I'm happy to keep that as a defense-in-depth cleanup only.
However, I respectfully disagree about host.ts. The host.ts vulnerability is real and exploitable. I have two confirmed PoCs where calc.exe opens on Windows:
PoC 1: Package specifier injection via ng add
ng add "[email protected] & calc" --skip-confirmation
Result: calc.exe opens. The injected command executes silently alongside npm install.
Why it works — full stack trace:
IMPORTANT NOTE: STACK TRACE MIGHT BE WRONG BUT ABLE TO REPRODUCE THE VULN
1. User runs: ng add "[email protected] & calc" --skip-confirmation
2. add/cli.ts:164 → npa("[email protected] & calc")
└─ npa splits at '@' → name="lodash", spec="1.0.0 & calc"
└─ npa.js:462 → semver.validRange("1.0.0 & calc") returns truthy
└─ npa classifies as type: "range"
(BYPASSES the encodeURIComponent check on line 468,
which only runs for type "tag")
└─ Returns: { name: "lodash", type: "range", fetchSpec: "1.0.0 & calc" }
3. add/cli.ts → loadPackageInfoTask()
└─ packageManager.getManifest(packageIdentifier)
└─ package-manager.ts:494 → case 'range': getRegistryMetadata("lodash")
└─ Fetches metadata, finds version "1.0.0" satisfies range
└─ Returns manifest for [email protected] ✓
4. add/cli.ts → installPackageTask()
└─ packageManager.add("[email protected] & calc", ...)
└─ package-manager.ts:326 → args = ["add", "[email protected] & calc", "--save-exact"]
└─ package-manager.ts:327 → this.#run(args, { registry })
5. package-manager.ts:196 → host.runCommand("npm", ["add", "[email protected] & calc", ...])
The key bypass is in npm-package-arg (npa). When a user provides [email protected] & calc, npa splits at @ and passes 1.0.0 & calc to semver.validRange(). Semver is permissive — it ignores the trailing garbage & calc and returns a valid range. Because npa classifies this as type "range" (not "tag"), the encodeURIComponent validation on npa.js line 468 is never executed. The unsanitized & calc flows all the way to host.ts.
PoC 2: Registry flag injection via ng add --registry
ng add @angular/localize --registry "http://registry.npmjs.org & calc" --skip-confirmation
Result: calc.exe opens 3 times (once per npm command invoked during install).
Why it works — full stack trace:
1. User runs: ng add @angular/localize --registry "http://registry.npmjs.org & calc"
2. add/cli.ts:118 → yargs parses --registry as a string option (no validation)
└─ options.registry = "http://registry.npmjs.org & calc"
3. add/cli.ts:555 → installPackageTask() destructures { registry } from options
└─ add/cli.ts:583-590 → packageManager.add(packageIdentifier, ..., { registry })
└─ package-manager.ts:327 → this.#run(args, options) (options includes { registry })
└─ package-manager.ts:161 → #run(args, { registry: "http://registry.npmjs.org & calc" })
4. package-manager.ts:172 → descriptor.getRegistryOptions(registry)
└─ Returns: { args: ["--registry", "http://registry.npmjs.org & calc"] }
└─ package-manager.ts:180 → finalArgs.push("--registry", "http://registry.npmjs.org & calc")
5. package-manager.ts:196 → host.runCommand("npm", [..., "--registry", "http://registry.npmjs.org & calc"])
6. host.ts:182 → shellCommand = "npm view ... --registry http://registry.npmjs.org & calc"
The host.ts fix is a critical security fix, not just a cleanup. I agree that executor.ts is defense-in-depth only.
Tested on: Angular CLI 21.1.4, Node.js v25.6.0, Windows 11, npm 11.3.0
Happy to adjust the PR scope based on this. Let me know how you'd like to proceed.
After reviewing the provided PoCs and internal discussion, we have evaluated the risk profile for this issue. We have decided not to move forward with the suggested changes to host.ts for the following reasons:
- Trust Boundary & Threat Model: The Angular CLI is a developer-facing tool. Our threat model assumes that the environment where the CLI is executed (local terminal or CI/CD) is already a trusted context. If an adversary can influence the arguments passed to an
ngcommand, the execution environment is already effectively compromised. - Maintenance & Complexity: Properly sanitizing shell arguments on Windows without introducing external dependencies (like
cross-spawn) introduces significant maintenance overhead and potential for regression. We do not believe the complexity of maintaining custom sanitization logic is justified given the existing trust requirements of the CLI. - Scope of Fix: We remain open to a PR for the defense-in-depth cleanup in
executor.tsto removeshell: truewhere it is strictly unnecessary. However, forhost.ts, we are prioritizing platform alignment with Node.js over custom mitigation.
I Agree
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.