[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

@@ -0,0 +1,39 @@
#!/bin/sh
TMP_DIR=''
cleanup () {
case "${TMP_DIR}" in
/*) rm -rf "${TMP_DIR}" ;;
esac
}
die () { echo "$@" ; cleanup ; exit 1; }
: nvm.sh
\. ../../../nvm.sh
TMP_DIR="$(mktemp -d)"
mkdir -p "${TMP_DIR}/project" || die 'could not make the project dir'
echo '{}' > "${TMP_DIR}/project/package.json"
echo "logs-dir=${TMP_DIR}/logs" > "${TMP_DIR}/project/.npmrc"
NPM_CALLS="${TMP_DIR}/npm-calls"
npm() {
echo "$(pwd): $*" >> "${NPM_CALLS}"
echo "npm $*"
}
OUTPUT="$(cd "${TMP_DIR}/project" && nvm debug 2>&1)"
EXPECTED_CALLS="/: config get prefix
/: root -g"
CALLS="$(cat "${NPM_CALLS}")"
[ "_${CALLS}" = "_${EXPECTED_CALLS}" ] || die "nvm debug should run npm outside the project: expected >${EXPECTED_CALLS}<; got >${CALLS}<"
case "${OUTPUT}" in
*"npm config get prefix: npm config get prefix"*"npm root -g: npm root -g"*) ;;
*) die "nvm debug should print npm's prefix and global root; got >${OUTPUT}<" ;;
esac
cleanup