[Fix] use `command` to bypass `curl`/`wget` shell functions and aliases
Before adoption: this commit came in with the history the project adopted, so no landing records it.
zsh (and interactive bash with `expand_aliases`) bakes a preexisting `curl` alias into nvm's function bodies at source time, and shell functions named `curl`/`wget` shadow the binaries at call time - either one breaks downloads. Prefixing invocations with `command` bypasses both: here, `nvm_download`'s dispatch, `nvm_curl_version`, `nvm_curl_libz_support`, and the wget branch of `nvm_get_latest`; the remaining bare `curl` invocations in `nvm_get_latest` and the install script are prefixed in a followup commit. The tests that previously mocked curl/wget as shell functions now install fake executables on PATH instead, via a shared `make_fake_curl` helper in `test/common.sh`, and a new test asserts the bypass. Refs #2923
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
9 files+172 added−68 removed
- tests:+167 added−63 removed
- other:+5 added−5 removed
Provenance
- Commit
04fef13- Tree
8e59be7- Author
Jordan Harband <ljharb@gmail.com>- Committer
Jordan Harband <ljharb@gmail.com>- Parent
cb0e0ba
9 files changed+172 added−68 removed
modified:test/fast/Unit tests/nvm_curl_libz_support+18 added-18 removed
1 #!/bin/sh2 3+WORK="$PWD/nvm_curl_libz_support-work.$$"4+TEST_BIN="$WORK/bin"5+6 cleanup() {4- unset -f curl7+ rm -rf "$WORK"8+ export PATH="$OLDPATH"9 }10 11 die() { cleanup; echo "$@" ; exit 1; }12 13 : nvm.sh14 \. ../../../nvm.sh15 12-curl() {13- # curl with libz feature14- if [ $# -ne 1 ] || [ "$1" != "-V" ]; then15- die "This fake curl only takes one parameter -V"16- fi17- echo "16+\. ../../common.sh17+18+OLDPATH="$PATH"19+20+make_fake_curl "$TEST_BIN"21+22+export PATH="$TEST_BIN:$OLDPATH"23+24+# curl with libz feature25+VERSION_MESSAGE="26 curl 7.47.0 (x86_64-pc-linux-gnu) libcurl/7.47.0 GnuTLS/3.4.10 zlib/1.2.8 libidn/1.32 librtmp/2.327 Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp smb smbs smtp smtps telnet tftp28 Features: AsynchDNS IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz TLS-SRP UnixSockets"21-}29 30 nvm_curl_libz_support || die "nvm_curl_libz_support should return 0"31 25-unset -f curl26-27-curl() {28- # curl without libz feature29- if [ "$#" -ne 1 ] || [ "$1" != "-V" ]; then30- die "This fake curl only takes one parameter -V"31- fi32- echo "32+# curl without libz feature33+VERSION_MESSAGE="34 curl 7.47.0 (x86_64-pc-linux-gnu) libcurl/7.47.0 GnuTLS/3.4.10 zlib/1.2.8 libidn/1.32 librtmp/2.3235 Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp smb smbs smtp smtps telnet tftp36 Features: AsynchDNS IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL TLS-SRP UnixSockets"36-}37 38 ! nvm_curl_libz_support || die "nvm_curl_libz_support should return 1"39 40-unset -f curl40+cleanupmodified:test/fast/Unit tests/nvm_curl_use_compression+12 added-5 removed
1 #!/bin/sh2 3+WORK="$PWD/nvm_curl_use_compression-work.$$"4+TEST_BIN="$WORK/bin"5+6 cleanup () {7 unset -f die8+ rm -rf "$WORK"9+ export PATH="$OLDPATH"10 }11 12 die () { echo -e "$@" ; cleanup ; exit 1; }13 14 NVM_ENV=testing \. ../../../nvm.sh15 11-curl() {12- if [ "$1" = "-V" ]; then13- echo "${VERSION_MESSAGE}"14- fi15-}16+\. ../../common.sh17 18+OLDPATH="$PATH"19+20+make_fake_curl "$TEST_BIN"21+22+export PATH="$TEST_BIN:$OLDPATH"23+24 CURL_VERSION_ON_ARCHLINUX_WITH_LIBZ="curl 7.54.0 (x86_64-pc-linux-gnu) libcurl/7.54.0 OpenSSL/1.1.0f zlib/1.2.11 libpsl/0.17.0 (+libicu/59.1) libssh2/1.8.0 nghttp2/1.22.025 Protocols: dict file ftp ftps gopher http https imap imaps pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp26 Features: AsynchDNS IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz TLS-SRP HTTP2 UnixSockets HTTPS-proxy PSL"27 18 unmodified linesmodified:test/fast/Unit tests/nvm_curl_version+13 added-7 removed
1 #!/bin/sh2 3+WORK="$PWD/nvm_curl_version-work.$$"4+TEST_BIN="$WORK/bin"5+6 cleanup () {4- unset -f die5- unset -f curl7+ unset -f die cleanup assert_version_is8+ rm -rf "$WORK"9+ export PATH="$OLDPATH"10 }11 12 die () { echo -e "$@" ; cleanup ; exit 1; }13 14 NVM_ENV=testing \. ../../../nvm.sh15 12-curl() {13- if [ "$1" = "-V" ]; then14- echo "${VERSION_MESSAGE}"15- fi16-}16+\. ../../common.sh17 18+OLDPATH="$PATH"19+20+make_fake_curl "$TEST_BIN"21+22+export PATH="$TEST_BIN:$OLDPATH"23+24 assert_version_is() {25 if [ "${1}" != "${2}" ]; then26 die "Expected ${2}, got ${1}, origin version message:\n${VERSION_MESSAGE}"27 return 111 unmodified linesadded:test/fast/Unit tests/nvm_download and curl helpers bypass shadowed curl+48 added
1+#!/bin/sh2+3+WORK="$PWD/shadowed-curl-work.$$"4+TEST_BIN="$WORK/bin"5+ARGV_LOG="$WORK/argv.log"6+7+cleanup () {8+ unset -f die cleanup curl wget9+ rm -rf "$WORK"10+ export PATH="$OLDPATH"11+}12+13+die () { echo "$@" ; cleanup ; exit 1; }14+15+NVM_ENV=testing \. ../../../nvm.sh16+17+\. ../../common.sh18+19+OLDPATH="$PATH"20+21+# real-looking curl on PATH; calling the shadowing shell function below instead is a failure22+make_fake_curl "$TEST_BIN" ': > "$ARGV_LOG"23+for a in "$@"; do printf "%s\n" "$a" >> "$ARGV_LOG"; done'24+25+VERSION_MESSAGE="curl 7.99.0 (fake)26+Features: libz"27+28+export ARGV_LOG29+export PATH="$TEST_BIN:$OLDPATH"30+31+# shadowing shell functions, like aliases baked into a user's shell, must be32+# bypassed in favor of the executables on PATH (https://github.com/nvm-sh/nvm/issues/2923)33+curl() {34+ echo 'curl 0.0.0-shadowed'35+ return 136+}37+wget() {38+ return 139+}40+41+[ "$(nvm_curl_version)" = '7.99.0' ] || die "nvm_curl_version used the shadowing curl function; got $(nvm_curl_version)"42+43+nvm_curl_libz_support || die 'nvm_curl_libz_support used the shadowing curl function'44+45+nvm_download -s 'http://example.com/' -o /dev/null || die 'nvm_download used the shadowing curl function and failed'46+grep -Fxq 'http://example.com/' "$ARGV_LOG" || die "nvm_download did not invoke the curl executable; got: $(cat "$ARGV_LOG")"47+48+cleanupmodified:test/slow/nvm_get_latest/nvm_get_latest+30 added-26 removed
1 #!/bin/sh2 3 die () { echo "$@" ; cleanup ; exit 1; }4 5+WORK="$PWD/nvm_get_latest-work.$$"6+TEST_BIN="$WORK/bin"7+8 cleanup() {6- unset -f curl wget nvm_has9+ rm -rf "$WORK"10+ export PATH="$OLDPATH"11 }12 13 \. ../../../nvm.sh14 15+\. ../../common.sh16+17+OLDPATH="$PATH"18+19 EXPECTED_VERSION="v12.3.456"20 URL="https://github.com/nvm-sh/nvm/releases/tag/$EXPECTED_VERSION"21 EXPECTED_CURL_ARGS="--compressed -q -w %{url_effective}\n -L -s -S https://latest.nvm.sh -o /dev/null"22 EXPECTED_WGET_ARGS="-q https://latest.nvm.sh --server-response -O /dev/null"23+export URL EXPECTED_CURL_ARGS EXPECTED_WGET_ARGS24 16-curl() {17- if [ $# -eq 1 ] && [ "$1" = "-V" ]; then18- echo "25+VERSION_MESSAGE="26 curl 7.47.0 (x86_64-pc-linux-gnu) libcurl/7.47.0 GnuTLS/3.4.10 zlib/1.2.8 libidn/1.32 librtmp/2.327 Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp smb smbs smtp smtps telnet tftp28 Features: AsynchDNS IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz TLS-SRP UnixSockets"22- elif [ "_$*" != "_$EXPECTED_CURL_ARGS" ]; then23- echo >&2 "expected args ($EXPECTED_CURL_ARGS), got ($*)"24- return 125- else26- echo $URL27- fi28-}29-wget() {30- if [ "_$*" != "_$EXPECTED_WGET_ARGS" ]; then31- echo >&2 "expected args ($EXPECTED_WGET_ARGS), got ($*)"32- return 133- else34- local WGET_CONTENTS35- WGET_CONTENTS="29+30+# fake curl/wget: `command curl` bypasses shell functions, so the mocks must be executables on PATH31+make_fake_curl "$TEST_BIN" 'if [ "_$*" != "_$EXPECTED_CURL_ARGS" ]; then32+ echo >&2 "expected args ($EXPECTED_CURL_ARGS), got ($*)"33+ exit 134+fi35+echo "$URL"'36+37+cat > "$TEST_BIN/wget" <<'OUTER'38+#!/bin/sh39+if [ "_$*" != "_$EXPECTED_WGET_ARGS" ]; then40+ echo >&2 "expected args ($EXPECTED_WGET_ARGS), got ($*)"41+ exit 142+fi43+cat >&2 <<CONTENTS44 HTTP/1.1 301 Moved Permanently45 Location: https://github.com/nvm-sh/nvm/releases/latest46 Content-Type: text/html; charset=utf-847 Content-Length: 8441 unmodified lines89 Strict-Transport-Security: max-age=31536000; includeSubdomains; preload90 X-Content-Type-Options: nosniff91 Vary: Accept-Encoding92 X-Served-By: 926b734ea1992f8ee1f88ab967a93dac85-"86- "$WGET_CONTENTS" | while read line87- do88- >&2 echo "$line"89- done90- fi91-}93+CONTENTS94+OUTER95+chmod +x "$TEST_BIN/wget"96 93-\. ../../common.sh97+export PATH="$TEST_BIN:$OLDPATH"98 99 try nvm_get_latest100 [ "_$CAPTURED_STDOUT" = "_$EXPECTED_VERSION" ] \101 || die "success path did not return version '$EXPECTED_VERSION', got '$CAPTURED_STDOUT'"5 unmodified linesmodified:test/slow/nvm_get_latest/nvm_get_latest failed redirect+13 added-7 removed
1 #!/bin/sh2 3 die () { echo "$@" ; cleanup ; exit 1; }4 5+WORK="$PWD/nvm_get_latest_failed_redirect-work.$$"6+TEST_BIN="$WORK/bin"7+8 cleanup() {6- unset -f curl wget9+ rm -rf "$WORK"10+ export PATH="$OLDPATH"11 }12 13 \. ../../../nvm.sh14 15 \. ../../common.sh16 13-curl() {14- return 115-}16-wget() {17- return 118-}17+OLDPATH="$PATH"18 19+# fake curl/wget: `command curl` bypasses shell functions, so the mocks must be executables on PATH20+make_fake_curl "$TEST_BIN" 'exit 1'21+printf '#!/bin/sh\nexit 1\n' > "$TEST_BIN/wget"22+chmod +x "$TEST_BIN/wget"23+24+export PATH="$TEST_BIN:$OLDPATH"25+26 try_err nvm_get_latest27 [ "_$CAPTURED_STDERR" = "_https://latest.nvm.sh did not redirect to the latest release on GitHub" ] \28 || die "failed redirect did not report correct error message, got '$CAPTURED_STDERR'"29 [ "_$CAPTURED_EXIT_CODE" = "_2" ] \3 unmodified linesmodified:test/common.sh+33 added
48 unmodified lines49 echo "echo \"${2}\"" >> "$1"50 chmod a+x "$1"51 }52 53+# `command curl` bypasses shell functions and aliases, so tests that mock curl54+# must provide an actual executable on the PATH: this creates one in the given55+# directory. Invoked as `curl -V`, it prints ${VERSION_MESSAGE} (exported here56+# on the mock's behalf); any other invocation runs the body given as the57+# second argument, or fails.58+make_fake_curl() {59+ local FAKE_BIN_DIR60+ FAKE_BIN_DIR="${1-}"61+ [ -n "${FAKE_BIN_DIR}" ] || return 162+63+ local FAKE_CURL_BODY64+ FAKE_CURL_BODY="${2-}"65+ if [ -z "${FAKE_CURL_BODY}" ]; then66+ FAKE_CURL_BODY='echo >&2 "This fake curl only takes one parameter, -V"67+exit 1'68+ fi69+70+ mkdir -p "${FAKE_BIN_DIR}" || return 271+72+ {73+ echo '#!/bin/sh'74+ echo 'if [ "$#" -eq 1 ] && [ "$1" = "-V" ]; then'75+ echo ' echo "${VERSION_MESSAGE}"'76+ echo ' exit 0'77+ echo 'fi'78+ printf '%s\n' "${FAKE_CURL_BODY}"79+ } > "${FAKE_BIN_DIR}/curl"80+ chmod +x "${FAKE_BIN_DIR}/curl"81+82+ # the fake curl reads VERSION_MESSAGE from the environment83+ export VERSION_MESSAGE84+}85+86 make_fake_node() {87 local VERSION88 VERSION="${1-}"89 [ -n "${VERSION}" ] || return 1217 unmodified linesmodified:install.sh+1 added-1 removed
117 unmodified lines118 -e 's/-sS /-nv /' \119 -e 's/-o /-O /' \120 -e 's/-C - /-c /')121 # shellcheck disable=SC2086122- eval wget $ARGS122+ eval command wget $ARGS123 fi124 }125 126 install_nvm_from_git() {381 unmodified linesmodified:nvm.sh+4 added-4 removed
85 unmodified lines86 [ -t 1 ] && [ "${NVM_NUM_COLORS:--1}" -ge 8 ] && [ "${NVM_NO_COLORS-}" != '--no-colors' ]87 }88 89 nvm_curl_libz_support() {90- curl -V 2>/dev/null | nvm_grep "^Features:" | nvm_grep -q "libz"90+ command curl -V 2>/dev/null | nvm_grep "^Features:" | nvm_grep -q "libz"91 }92 93 nvm_curl_use_compression() {94 nvm_curl_libz_support && nvm_version_greater_than_or_equal_to "$(nvm_curl_version)" 7.21.07 unmodified lines102 CURL_COMPRESSED_FLAG="--compressed"103 fi104 NVM_LATEST_URL="$(curl ${CURL_COMPRESSED_FLAG:-} -q -w "%{url_effective}\\n" -L -s -S https://latest.nvm.sh -o /dev/null)"105 elif nvm_has "wget"; then106- NVM_LATEST_URL="$(wget -q https://latest.nvm.sh --server-response -O /dev/null 2>&1 | command awk '/^ Location: /{DEST=$2} END{ print DEST }')"106+ NVM_LATEST_URL="$(command wget -q https://latest.nvm.sh --server-response -O /dev/null 2>&1 | command awk '/^ Location: /{DEST=$2} END{ print DEST }')"107 else108 nvm_err 'nvm needs curl or wget to proceed.'109 return 1110 fi58 unmodified lines169 if [ -n "${NVM_AUTH_HEADER:-}" ]; then170 set -- "$@" --header "Authorization: ${sanitized_header}"171 fi172 173- "${NVM_DOWNLOADER}" "$@"173+ command "${NVM_DOWNLOADER}" "$@"174 }175 176 nvm_sanitize_auth_header() {177 # Remove potentially dangerous characters470 unmodified lines648 clang --version | command awk '{ if ($2 == "version") print $3; else if ($3 == "version") print $4 }' | command sed 's/-.*$//g'649 }650 651 nvm_curl_version() {652- curl -V | command awk '{ if ($1 == "curl") print $2 }' | command sed 's/-.*$//g'652+ command curl -V | command awk '{ if ($1 == "curl") print $2 }' | command sed 's/-.*$//g'653 }654 655 nvm_version_greater() {656 command awk 'BEGIN {4226 unmodified lines