[Fix] nvm debug: do not let a project .npmrc configure npm

`nvm debug` ran `npm config get prefix` in `$PWD`, so npm applied the untrusted project `.npmrc`.
Its `logs-dir` chose where npm wrote its debug log,
and npm 11 copies unknown keys into that log verbatim,
so `logs-dir=~/.bashrc.d` plus a `$(...)` key runs a command in the next shell on Fedora-style setups.
Its `userconfig` makes npm 11 echo the lines of any file into the log and into the `nvm debug` output.

`-g` is not enough: npm v7 through v8.2 load the project `.npmrc` even in global mode.
Add `nvm_npm`, which runs npm from `/`, and use it for both npm commands.
`$NVM_CD_FLAGS` is not used, since a zsh parent exports `-q` to bash and sh children.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-ZF4UWQNS.
This commit is contained in:
Jordan Harband committed 2026-11-16 15:21:25 -08:00
1 parent 2abfc380ad
commit f07f4ed8f3
2 files changed
+62 -2

No files matched your search

+23 -2
View File
@@ -574,6 +574,22 @@ nvm_validate_install() {
return 0
}
# npm applies the npmrc of the project that `$PWD` is in, which is untrusted:
# eg, its `logs-dir` picks where npm writes its debug log.
# run npm from `/` instead, since `--global` alone does not avoid it on npm v7 - v8.2.
nvm_cd_root() {
# `-q` skips zsh's `chpwd` hooks; `$NVM_CD_FLAGS` is not used, since a zsh parent exports it to other shells
if nvm_is_zsh; then
nvm_cd -q /
else
nvm_cd /
fi
}
nvm_npm() {
(nvm_cd_root && npm "$@")
}
nvm_print_npm_version() {
if nvm_has "npm"; then
local NPM_VERSION
@@ -5234,10 +5250,15 @@ nvm() {
unset TEST_TOOLS ADD_TEST_TOOLS
local NVM_DEBUG_OUTPUT
for NVM_DEBUG_COMMAND in 'nvm current' 'which node' 'which iojs' 'which npm' 'npm config get prefix' 'npm root -g'; do
for NVM_DEBUG_COMMAND in 'nvm current' 'which node' 'which iojs' 'which npm'; do
NVM_DEBUG_OUTPUT="$(${NVM_DEBUG_COMMAND} 2>&1)"
nvm_err "${NVM_DEBUG_COMMAND}: $(nvm_sanitize_path "${NVM_DEBUG_OUTPUT}")"
done
for NVM_DEBUG_COMMAND in 'config get prefix' 'root -g'; do
# shellcheck disable=SC2086
NVM_DEBUG_OUTPUT="$(nvm_npm ${NVM_DEBUG_COMMAND} 2>&1)"
nvm_err "npm ${NVM_DEBUG_COMMAND}: $(nvm_sanitize_path "${NVM_DEBUG_OUTPUT}")"
done
return 42
;;
@@ -7052,7 +7073,7 @@ nvm() {
nvm_download nvm_get_latest nvm_has nvm_has_executable nvm_install_default_packages nvm_get_default_packages \
nvm_curl_use_compression nvm_curl_version \
nvm_auto nvm_supports_xz \
nvm_echo nvm_err nvm_grep nvm_cd \
nvm_echo nvm_err nvm_grep nvm_cd nvm_cd_root nvm_npm \
nvm_die_on_prefix nvm_get_make_jobs nvm_get_minor_version \
nvm_has_solaris_binary nvm_is_merged_node_version \
nvm_is_natural_num nvm_is_installed_exact_version nvm_is_version_installed nvm_validate_install \