Summary
Node Version Manager can interpret attacker-influenced version patterns as commands when constructing a sed filtering expression. If a malicious version value reaches affected nvm commands or automatic version-switching hooks while a Node.js version is installed, an attacker may achieve command execution or file access with the privileges of the invoking user.
Node Version Manager can interpret attacker-influenced version patterns as commands when constructing a sed filtering expression. If a malicious version value reaches affected nvm commands or automatic version-switching hooks while a Node.js version is installed, an attacker may achieve command execution or file access with the privileges of the invoking user.
Affected context
The vulnerability occurs in nvm versions 0.31.2 through 0.40.5 when version-selection functions pass insufficiently escaped input into a sed address. It may affect commands such as nvm use, nvm which, nvm ls, nvm version, nvm run, and nvm exec, as well as nvm-exec when its version value is attacker controlled. Automatic directory-switching hooks and scripts that pass raw .nvmrc content can increase exposure.
The vulnerability occurs in nvm versions 0.31.2 through 0.40.5 when version-selection functions pass insufficiently escaped input into a sed address. It may affect commands such as nvm use, nvm which, nvm ls, nvm version, nvm run, and nvm exec, as well as nvm-exec when its version value is attacker controlled. Automatic directory-switching hooks and scripts that pass raw .nvmrc content can increase exposure.
Exploit preconditions
- Attack surface: Local shell execution through nvm commands, scripts, or directory-change hooks
- Required access: Ability to provide or influence a version string,
.nvmrccontent, orNODE_VERSION - Trigger point: nvm constructs and executes the sed-based installed-version filter
- Environmental condition: At least one Node.js version must be installed
- Why exploitation is possible: The supplied pattern is embedded in a sed address without fully escaping syntax-significant characters, allowing input to alter the interpreted sed program
- Attack surface: Local shell execution through nvm commands, scripts, or directory-change hooks
- Required access: Ability to provide or influence a version string,
.nvmrccontent, orNODE_VERSION - Trigger point: nvm constructs and executes the sed-based installed-version filter
- Environmental condition: At least one Node.js version must be installed
- Why exploitation is possible: The supplied pattern is embedded in a sed address without fully escaping syntax-significant characters, allowing input to alter the interpreted sed program
Why this happens
The underlying bug is command injection caused by embedding insufficiently sanitized user-controlled text into a sed program. A version pattern containing sed syntax can terminate the intended address and cause the remaining text to be interpreted as sed commands rather than data. On implementations supporting command execution, this can invoke a shell command; other sed implementations may permit file reads or writes. This primarily maps to CWE-78: Improper Neutralization of Special Elements used in an OS Command, with a related CWE-94: Improper Control of Generation of Code concern because data changes the behavior of an interpreted program.
The underlying bug is command injection caused by embedding insufficiently sanitized user-controlled text into a sed program. A version pattern containing sed syntax can terminate the intended address and cause the remaining text to be interpreted as sed commands rather than data. On implementations supporting command execution, this can invoke a shell command; other sed implementations may permit file reads or writes. This primarily maps to CWE-78: Improper Neutralization of Special Elements used in an OS Command, with a related CWE-94: Improper Control of Generation of Code concern because data changes the behavior of an interpreted program.
Potential impact
- An attacker may execute commands as the user running nvm
- Local files may be read, modified, created, or overwritten
- Shell credentials, source code, and environment secrets could be exposed
- Developer workstations or build environments could be compromised
- Malicious changes could affect downstream development or release workflows
- An attacker may execute commands as the user running nvm
- Local files may be read, modified, created, or overwritten
- Shell credentials, source code, and environment secrets could be exposed
- Developer workstations or build environments could be compromised
- Malicious changes could affect downstream development or release workflows
General mitigation guidance
Mitigation generally focuses on keeping version values as data and preventing them from becoming executable sed syntax.
Defensive patterns include:
- Use a parser or filtering approach that does not construct commands from untrusted version patterns
- Apply complete, context-aware escaping or strict allowlist validation before invoking text-processing tools
- Treat
.nvmrc,NODE_VERSION, and environment-derived version values as untrusted input - Ensure automatic version-switching hooks validate file content before passing it to nvm
- Prefer safe-by-default parsing that rejects unexpected characters and formats
- Limit shell, filesystem, and build permissions available to developer automation
- Keep sensitive credentials and writable production paths outside processes that handle untrusted project files
Mitigation generally focuses on keeping version values as data and preventing them from becoming executable sed syntax.
Defensive patterns include:
- Use a parser or filtering approach that does not construct commands from untrusted version patterns
- Apply complete, context-aware escaping or strict allowlist validation before invoking text-processing tools
- Treat
.nvmrc,NODE_VERSION, and environment-derived version values as untrusted input - Ensure automatic version-switching hooks validate file content before passing it to nvm
- Prefer safe-by-default parsing that rejects unexpected characters and formats
- Limit shell, filesystem, and build permissions available to developer automation
- Keep sensitive credentials and writable production paths outside processes that handle untrusted project files
How teams detect issues like this
Detection can include static analysis for shell commands that interpolate variables into sed expressions, especially address ranges or scripts assembled through string concatenation. Code review should examine nvm wrappers, .nvmrc loaders, environment-variable handling, and directory-change hooks for raw version values reaching shell or text-processing utilities. Policy checks can require allowlisted version formats, safe argument construction, and review of shell code that invokes interpreters or file-access commands. Automated shell security scanners and repository review tools may help identify these patterns.
Detection can include static analysis for shell commands that interpolate variables into sed expressions, especially address ranges or scripts assembled through string concatenation. Code review should examine nvm wrappers, .nvmrc loaders, environment-variable handling, and directory-change hooks for raw version values reaching shell or text-processing utilities. Policy checks can require allowlisted version formats, safe argument construction, and review of shell code that invokes interpreters or file-access commands. Automated shell security scanners and repository review tools may help identify these patterns.

