[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:
4 files changed
+266
-8
No files matched your search
@@ -712,7 +712,7 @@ The header is only sent with requests for URLs under a mirror, never to any othe
|
||||
|
||||
nvm gives the header to `curl` and to GNU Wget in a config file (for GNU Wget, a private temporary file that also holds a copy of your `~/.wgetrc`), not as a command-line argument, which other users of the same machine could read. Other `wget`s, like BusyBox's, read no config file, so on a machine shared with other users, install `curl` before using `$NVM_AUTH_HEADER`.
|
||||
|
||||
`$NVM_AUTH_HEADER` is the recommended way to authenticate to a mirror. Credentials embedded in the mirror URL itself (`https://user:password@host/...`) also work, and nvm prints such URLs with the credentials masked as `***@`.
|
||||
`$NVM_AUTH_HEADER` is the recommended way to authenticate to a mirror. Credentials embedded in the mirror URL itself (`https://user:password@host/...`) also work, and nvm prints such URLs with the credentials masked as `***@`. Like the header, such a URL is given to `curl` in a config and to GNU Wget on its standard input, not as a command-line argument; other `wget`s, like BusyBox's, only take a URL as an argument, so on a machine shared with other users, install `curl` before putting credentials in a mirror URL.
|
||||
|
||||
### Platforms without official binaries
|
||||
|
||||
|
||||
@@ -226,12 +226,34 @@ nvm_download() {
|
||||
fi
|
||||
|
||||
# every local user can read a process's arguments (`ps`, /proc/<pid>/cmdline),
|
||||
# so the credential goes to the downloader in a config file, not an argument
|
||||
if [ -n "${sanitized_header}" ]; then
|
||||
if [ "${NVM_DOWNLOADER}" = 'curl' ]; then
|
||||
nvm_echo "header = \"Authorization: ${sanitized_header}\"" | command curl "$@" --config -
|
||||
# so a credential, in the header or in a URL, goes to the downloader on stdin
|
||||
if [ "${NVM_DOWNLOADER}" = 'curl' ]; then
|
||||
local NVM_DOWNLOAD_CURL_CONFIG
|
||||
NVM_DOWNLOAD_CURL_CONFIG=''
|
||||
if [ -n "${sanitized_header}" ]; then
|
||||
NVM_DOWNLOAD_CURL_CONFIG="header = \"Authorization: ${sanitized_header}\""
|
||||
fi
|
||||
local NVM_DOWNLOAD_CURL_COUNT
|
||||
NVM_DOWNLOAD_CURL_COUNT=$#
|
||||
local NVM_DOWNLOAD_CURL_ARG
|
||||
for NVM_DOWNLOAD_CURL_ARG in "$@"; do
|
||||
if nvm_url_has_credentials "${NVM_DOWNLOAD_CURL_ARG}"; then
|
||||
NVM_DOWNLOAD_CURL_CONFIG="${NVM_DOWNLOAD_CURL_CONFIG:+${NVM_DOWNLOAD_CURL_CONFIG}
|
||||
}url = \"$(nvm_echo "${NVM_DOWNLOAD_CURL_ARG}" | command sed 's/[\\"]/\\&/g')\""
|
||||
else
|
||||
set -- "$@" "${NVM_DOWNLOAD_CURL_ARG}"
|
||||
fi
|
||||
done
|
||||
shift "${NVM_DOWNLOAD_CURL_COUNT}"
|
||||
if [ -n "${NVM_DOWNLOAD_CURL_CONFIG}" ]; then
|
||||
nvm_echo "${NVM_DOWNLOAD_CURL_CONFIG}" | command curl "$@" --config -
|
||||
return $?
|
||||
fi
|
||||
command curl "$@"
|
||||
return $?
|
||||
fi
|
||||
|
||||
if [ -n "${sanitized_header}" ]; then
|
||||
if nvm_wget_is_gnu; then
|
||||
nvm_wget_with_auth_header "${sanitized_header}" "$@"
|
||||
return $?
|
||||
@@ -240,7 +262,60 @@ nvm_download() {
|
||||
set -- "$@" --header "Authorization: ${sanitized_header}"
|
||||
fi
|
||||
|
||||
command "${NVM_DOWNLOADER}" "$@"
|
||||
local NVM_DOWNLOAD_WGET_URL
|
||||
for NVM_DOWNLOAD_WGET_URL in "$@"; do
|
||||
if nvm_url_has_credentials "${NVM_DOWNLOAD_WGET_URL}"; then
|
||||
# other wgets, like BusyBox's, only take a URL as an argument
|
||||
if nvm_wget_is_gnu; then
|
||||
nvm_wget_with_url_credentials "$@"
|
||||
return $?
|
||||
fi
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
command wget "$@"
|
||||
}
|
||||
|
||||
# args: downloader argument
|
||||
# Whether it is an http(s) URL with userinfo (`user:password@`) in its authority
|
||||
# that fits on one line of a config file; no valid URL has whitespace or control characters.
|
||||
nvm_url_has_credentials() {
|
||||
case "${1-}" in
|
||||
*[[:space:][:cntrl:]]*) return 1 ;;
|
||||
http://* | https://*) ;;
|
||||
*) return 1 ;;
|
||||
esac
|
||||
local NVM_URL_AUTHORITY
|
||||
NVM_URL_AUTHORITY="${1#*://}"
|
||||
case "${NVM_URL_AUTHORITY%%/*}" in
|
||||
*@*) return 0 ;;
|
||||
esac
|
||||
return 1
|
||||
}
|
||||
|
||||
# args: GNU Wget arguments
|
||||
# URLs with credentials are read from stdin (`--input-file=-`) instead of being arguments.
|
||||
nvm_wget_with_url_credentials() {
|
||||
local NVM_WGET_STDIN_URLS
|
||||
NVM_WGET_STDIN_URLS=''
|
||||
local NVM_WGET_STDIN_COUNT
|
||||
NVM_WGET_STDIN_COUNT=$#
|
||||
local NVM_WGET_STDIN_ARG
|
||||
for NVM_WGET_STDIN_ARG in "$@"; do
|
||||
if nvm_url_has_credentials "${NVM_WGET_STDIN_ARG}"; then
|
||||
NVM_WGET_STDIN_URLS="${NVM_WGET_STDIN_URLS:+${NVM_WGET_STDIN_URLS}
|
||||
}${NVM_WGET_STDIN_ARG}"
|
||||
else
|
||||
set -- "$@" "${NVM_WGET_STDIN_ARG}"
|
||||
fi
|
||||
done
|
||||
shift "${NVM_WGET_STDIN_COUNT}"
|
||||
if [ -n "${NVM_WGET_STDIN_URLS}" ]; then
|
||||
nvm_echo "${NVM_WGET_STDIN_URLS}" | command wget "$@" --input-file=-
|
||||
return $?
|
||||
fi
|
||||
command wget "$@"
|
||||
}
|
||||
|
||||
# BusyBox wget rejects `--version`; GNU Wget2 reports itself as "GNU Wget2"
|
||||
@@ -305,13 +380,13 @@ nvm_wget_same_origin() {
|
||||
local NVM_WGET_REDIRECTS
|
||||
NVM_WGET_REDIRECTS=0
|
||||
while :; do
|
||||
command wget --max-redirect=0 "$@"
|
||||
nvm_wget_with_url_credentials --max-redirect=0 "$@"
|
||||
NVM_WGET_EXIT_CODE=$?
|
||||
# wget exits with 8 for an HTTP error status, including a redirect it did not follow
|
||||
if [ "${NVM_WGET_EXIT_CODE}" != 8 ] || [ -z "${NVM_WGET_URL}" ]; then
|
||||
return "${NVM_WGET_EXIT_CODE}"
|
||||
fi
|
||||
NVM_WGET_LOCATION="$(command wget --max-redirect=0 -q -S -O /dev/null "${NVM_WGET_URL}" 2>&1 \
|
||||
NVM_WGET_LOCATION="$(nvm_wget_with_url_credentials --max-redirect=0 -q -S -O /dev/null "${NVM_WGET_URL}" 2>&1 \
|
||||
| command awk 'tolower($1) == "location:" { print $2; exit }' | command tr -d '\r')"
|
||||
case "${NVM_WGET_LOCATION}" in
|
||||
'') return "${NVM_WGET_EXIT_CODE}" ;;
|
||||
@@ -6989,6 +7064,7 @@ nvm() {
|
||||
nvm_curl_libz_support nvm_command_info nvm_is_zsh nvm_stdout_is_terminal \
|
||||
nvm_npmrc_bad_news_bears nvm_sanitize_auth_header nvm_auth_header_applies_to \
|
||||
nvm_wget_is_gnu nvm_url_origin nvm_wget_same_origin nvm_wget_with_auth_header \
|
||||
nvm_url_has_credentials nvm_wget_with_url_credentials \
|
||||
nvm_get_colors nvm_set_colors nvm_print_color_code nvm_wrap_with_color_code \
|
||||
nvm_print_color_legend \
|
||||
nvm_echo_with_colors nvm_err_with_colors \
|
||||
|
||||
Executable
+149
@@ -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
|
||||
Executable
+33
@@ -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')"
|
||||
Reference in new issue
Block a user