-
fix(core): prevent command injection in getNpmPackageVersion (#34309)
发布于
2026-02-03 21:37:59 +00:00 Current Behavior
The
getNpmPackageVersionfunction in
packages/workspace/src/generators/utils/get-npm-package-version.ts
usesexecSyncwith direct string interpolation of thepackageName
parameter. When a user runscreate-nx-workspacewith a custom
--presetvalue that doesn't match a known preset, the value flows
unsanitized into a shell command:execSync(`npm view ${packageName}... version --json`)This allows arbitrary command execution via shell metacharacters (e.g.,
--preset='pkg$(malicious command)').Expected Behavior
User-supplied package names are validated against a strict npm package
name regex before being passed to any shell command. The function now
usesexecFileSyncwith an args array instead ofexecSyncwith string
interpolation, providing defense in depth:- Input validation — rejects anything that isn't a valid npm
package name - Safe execution — arguments are passed as an array so Node.js
handles escaping, rather than concatenating into a raw shell string
(cherry picked from commit
79d878f240)下载附件
- Input validation — rejects anything that isn't a valid npm