[Fix] nvm_download: pass a mirror URL with credentials to curl and GNU Wget on stdin, not as an argument

903e3493 moved `$NVM_AUTH_HEADER` out of the downloader's arguments,
but a mirror URL with `user:password@` in it was still one,
so any local user could read the password in `ps` or /proc/<pid>/cmdline
for every index.tab, SHASUMS, and tarball request.
curl now gets such a URL as `url = "..."` in the config it already reads with `--config -`,
with `"` and `\` escaped;
GNU Wget reads it with `--input-file=-`,
including in the same-origin redirect loop.
A URL with whitespace or a control character cannot be one config line, and stays an argument.
BusyBox wget has no way to take a URL other than as an argument; the README says so.

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

No files matched your search

@@ -0,0 +1,149 @@
#!/bin/sh
WORK="$PWD/nvm_download-url-credentials-work.$$"
TEST_BIN="$WORK/bin"
ARGV_LOG="$WORK/argv.log"
STDIN_LOG="$WORK/stdin.log"
OLDHOME="$HOME"
cleanup() {
unset -f die cleanup nvm_has_executable
unset NVM_AUTH_HEADER NVM_NODEJS_ORG_MIRROR WGET_IS_GNU FAKE_EXIT
rm -rf "$WORK"
export PATH="$OLDPATH"
HOME="$OLDHOME"
export HOME
}
die () { echo "$@" ; cleanup ; exit 1; }
\. ../../../nvm.sh
\. ../../common.sh
OLDPATH="$PATH"
# fake curl: record every argument, and the config it reads from stdin
make_fake_curl "$TEST_BIN" 'for a in "$@"; do printf "%s\n" "$a"; done > "$ARGV_LOG"
: > "$STDIN_LOG"
for a in "$@"; do if [ "$a" = --config ]; then cat > "$STDIN_LOG"; fi; done'
VERSION_MESSAGE="curl 7.99.0 (fake)"
# fake wget: GNU Wget when $WGET_IS_GNU is set, and like BusyBox's otherwise;
# record every argument, and the URLs it reads from stdin
{
echo '#!/bin/sh'
echo 'if [ "$1" = --version ]; then'
echo ' [ -n "${WGET_IS_GNU-}" ] || exit 1'
echo ' echo "GNU Wget 1.21.2 built on linux-gnu."; exit 0'
echo 'fi'
echo 'for a in "$@"; do printf "%s\n" "$a"; done > "$ARGV_LOG"'
echo ': > "$STDIN_LOG"'
echo 'for a in "$@"; do if [ "$a" = --input-file=- ]; then cat > "$STDIN_LOG"; fi; done'
echo 'exit "${FAKE_EXIT:-0}"'
} > "$TEST_BIN/wget"
chmod +x "$TEST_BIN/wget"
mkdir -p "$WORK/home"
HOME="$WORK/home"
export HOME ARGV_LOG STDIN_LOG
export PATH="$TEST_BIN:$OLDPATH"
NVM_NODEJS_ORG_MIRROR='https://ci-bot:s3cr3t@mirror.example/dist'
export NVM_NODEJS_ORG_MIRROR
URL="${NVM_NODEJS_ORG_MIRROR}/index.tab"
assert_not_an_argument() {
! grep -q 's3cr3t' "$ARGV_LOG" || die "${1}: the mirror password is an argument; got: $(cat "$ARGV_LOG")"
}
# given curl
nvm_download -L -s "$URL" -o - || die 'curl: nvm_download returned nonzero'
assert_not_an_argument 'curl'
EXPECTED='-q
--fail
-L
-s
-o
-
--config
-'
[ "$(cat "$ARGV_LOG")" = "$EXPECTED" ] || die "curl: expected args >$EXPECTED<, got >$(cat "$ARGV_LOG")<"
[ "$(cat "$STDIN_LOG")" = "url = \"${URL}\"" ] || die "curl: expected the URL in its config, got >$(cat "$STDIN_LOG")<"
# given a header as well, both go in the one config
NVM_AUTH_HEADER='Bearer test-token'
export NVM_AUTH_HEADER
nvm_download -L -s "$URL" -o - || die 'curl with header: nvm_download returned nonzero'
assert_not_an_argument 'curl with header'
[ "$(grep -c -Fxe '--config' "$ARGV_LOG")" = 1 ] || die "curl with header: expected one --config, got >$(cat "$ARGV_LOG")<"
EXPECTED="header = \"Authorization: Bearer test-token\"
url = \"${URL}\""
[ "$(cat "$STDIN_LOG")" = "$EXPECTED" ] || die "curl with header: expected config >$EXPECTED<, got >$(cat "$STDIN_LOG")<"
unset NVM_AUTH_HEADER
# given a quote or a backslash, they are escaped within curl's quoted string
nvm_download -L -s "${NVM_NODEJS_ORG_MIRROR}/a\"b\\c" -o - || die 'curl, quote: nvm_download returned nonzero'
assert_not_an_argument 'curl, quote'
[ "$(cat "$STDIN_LOG")" = "url = \"${NVM_NODEJS_ORG_MIRROR}/a\\\"b\\\\c\"" ] || die "curl, quote: got config >$(cat "$STDIN_LOG")<"
# given a line break, the URL cannot add a line to the config
nvm_download -L -s "$(printf '%s\noutput = "/tmp/nvm-pwned' "$URL")" -o - || die 'curl, line break: nvm_download returned nonzero'
! grep -Fxqe '--config' "$ARGV_LOG" || die "curl, line break: got config >$(cat "$STDIN_LOG")<"
# given a URL without credentials, it stays an argument
nvm_download -L -s 'https://nodejs.org/dist/index.tab' -o - || die 'curl, no credentials: nvm_download returned nonzero'
EXPECTED='-q
--fail
-L
-s
https://nodejs.org/dist/index.tab
-o
-'
[ "$(cat "$ARGV_LOG")" = "$EXPECTED" ] || die "curl, no credentials: expected args >$EXPECTED<, got >$(cat "$ARGV_LOG")<"
# given only GNU Wget
nvm_has_executable() { [ "$1" != curl ] && command -v "$1" >/dev/null 2>&1; }
WGET_IS_GNU=1
export WGET_IS_GNU
nvm_download -L -C - --progress-bar "$URL" -o "$WORK/out" || die 'GNU Wget: nvm_download returned nonzero'
assert_not_an_argument 'GNU Wget'
EXPECTED="-c
--progress=bar
-O
$WORK/out
--input-file=-"
[ "$(cat "$ARGV_LOG")" = "$EXPECTED" ] || die "GNU Wget: expected args >$EXPECTED<, got >$(cat "$ARGV_LOG")<"
[ "$(cat "$STDIN_LOG")" = "$URL" ] || die "GNU Wget: expected the URL on stdin, got >$(cat "$STDIN_LOG")<"
# and its exit code is kept
FAKE_EXIT=8
export FAKE_EXIT
nvm_download -L -s "$URL" -o -
EXIT_CODE=$?
unset FAKE_EXIT
[ "$EXIT_CODE" = 8 ] || die "GNU Wget: expected exit code 8, got $EXIT_CODE"
# given a header as well, which goes through the same-origin redirect check
NVM_AUTH_HEADER='Bearer test-token'
export NVM_AUTH_HEADER
nvm_download -L -s "$URL" -o - || die 'GNU Wget with header: nvm_download returned nonzero'
assert_not_an_argument 'GNU Wget with header'
EXPECTED='--max-redirect=0
-q
-O
-
--input-file=-'
[ "$(cat "$ARGV_LOG")" = "$EXPECTED" ] || die "GNU Wget with header: expected args >$EXPECTED<, got >$(cat "$ARGV_LOG")<"
[ "$(cat "$STDIN_LOG")" = "$URL" ] || die "GNU Wget with header: expected the URL on stdin, got >$(cat "$STDIN_LOG")<"
unset NVM_AUTH_HEADER
# given a wget that reads no URL from stdin, like BusyBox's, the URL stays an argument
unset WGET_IS_GNU
nvm_download -L -s "$URL" -o - || die 'other wget: nvm_download returned nonzero'
EXPECTED="-q
${URL}
-O
-"
[ "$(cat "$ARGV_LOG")" = "$EXPECTED" ] || die "other wget: expected args >$EXPECTED<, got >$(cat "$ARGV_LOG")<"
cleanup
+33
View File
@@ -0,0 +1,33 @@
#!/bin/sh
die () { echo "$@" ; exit 1; }
\. ../../../nvm.sh
yes_creds() {
nvm_url_has_credentials "${1}" || die "nvm_url_has_credentials '${1}': expected success"
}
no_creds() {
! nvm_url_has_credentials "${1}" || die "nvm_url_has_credentials '${1}': expected failure"
}
yes_creds 'https://user:pw@mirror.example/dist'
yes_creds 'http://user:pw@127.0.0.1:8080/dist/index.tab'
yes_creds 'https://user@mirror.example'
yes_creds 'https://user:pw@[::1]:8080/dist'
yes_creds 'https://user:p@ss@mirror.example/dist'
yes_creds 'https://user:pw@mirror.example/a"b\c'
no_creds 'https://nodejs.org/dist/index.tab'
no_creds 'https://mirror.example/dist/@scope/index.tab'
no_creds 'https://mirror.example/user:pw@elsewhere.example/dist'
no_creds ''
no_creds '-o'
no_creds 'user:pw@mirror.example/dist'
no_creds 'ftp://user:pw@mirror.example/dist'
no_creds '/tmp/user:pw@mirror.example/node.tar.xz'
no_creds 'https://user:pw@mirror.example/a b'
no_creds "$(printf 'https://user:pw@mirror.example/a\nb')"
no_creds "$(printf 'https://user:pw@mirror.example/a\rb')"
no_creds "$(printf 'https://user:pw@mirror.example/a\033b')"