Skip to content
Myr

[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

authored by Jordan Harband · committed by Jordan Harband · parent cb0e0ba · Browse files · 9 files+172 added−68 removed
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

9 files+172 added−68 removed

modified:test/fast/Unit tests/nvm_curl_libz_support+18 added-18 removed
1#!/bin/sh23WORK="$PWD/nvm_curl_libz_support-work.$$"4TEST_BIN="$WORK/bin"56cleanup() {4 unset -f curl7 rm -rf "$WORK"8 export PATH="$OLDPATH"9}1011die() { cleanup; echo "$@" ; exit 1; }1213: nvm.sh14\. ../../../nvm.sh1512curl() {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.sh1718OLDPATH="$PATH"1920make_fake_curl "$TEST_BIN"2122export PATH="$TEST_BIN:$OLDPATH"2324# curl with libz feature25VERSION_MESSAGE="26curl 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.327Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp smb smbs smtp smtps telnet tftp28Features: AsynchDNS IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz TLS-SRP UnixSockets"21}2930nvm_curl_libz_support || die "nvm_curl_libz_support should return 0"3125unset -f curl2627curl() {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 feature33VERSION_MESSAGE="34curl 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.3235Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp smb smbs smtp smtps telnet tftp36Features: AsynchDNS IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL TLS-SRP UnixSockets"36}3738! nvm_curl_libz_support || die "nvm_curl_libz_support should return 1"3940unset -f curl40cleanup
modified:test/fast/Unit tests/nvm_curl_use_compression+12 added-5 removed
1#!/bin/sh23WORK="$PWD/nvm_curl_use_compression-work.$$"4TEST_BIN="$WORK/bin"56cleanup () {7 unset -f die8 rm -rf "$WORK"9 export PATH="$OLDPATH"10}1112die () { echo -e "$@" ; cleanup ; exit 1; }1314NVM_ENV=testing \. ../../../nvm.sh1511curl() {12 if [ "$1" = "-V" ]; then13 echo "${VERSION_MESSAGE}"14 fi15}16\. ../../common.sh1718OLDPATH="$PATH"1920make_fake_curl "$TEST_BIN"2122export PATH="$TEST_BIN:$OLDPATH"2324CURL_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.025Protocols: dict file ftp ftps gopher http https imap imaps pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp26Features: AsynchDNS IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz TLS-SRP HTTP2 UnixSockets HTTPS-proxy PSL"27
18 unmodified lines
modified:test/fast/Unit tests/nvm_curl_version+13 added-7 removed
1#!/bin/sh23WORK="$PWD/nvm_curl_version-work.$$"4TEST_BIN="$WORK/bin"56cleanup () {4 unset -f die5 unset -f curl7 unset -f die cleanup assert_version_is8 rm -rf "$WORK"9 export PATH="$OLDPATH"10}1112die () { echo -e "$@" ; cleanup ; exit 1; }1314NVM_ENV=testing \. ../../../nvm.sh1512curl() {13 if [ "$1" = "-V" ]; then14 echo "${VERSION_MESSAGE}"15 fi16}16\. ../../common.sh1718OLDPATH="$PATH"1920make_fake_curl "$TEST_BIN"2122export PATH="$TEST_BIN:$OLDPATH"2324assert_version_is() {25 if [ "${1}" != "${2}" ]; then26 die "Expected ${2}, got ${1}, origin version message:\n${VERSION_MESSAGE}"27 return 1
11 unmodified lines
added:test/fast/Unit tests/nvm_download and curl helpers bypass shadowed curl+48 added
1#!/bin/sh23WORK="$PWD/shadowed-curl-work.$$"4TEST_BIN="$WORK/bin"5ARGV_LOG="$WORK/argv.log"67cleanup () {8 unset -f die cleanup curl wget9 rm -rf "$WORK"10 export PATH="$OLDPATH"11}1213die () { echo "$@" ; cleanup ; exit 1; }1415NVM_ENV=testing \. ../../../nvm.sh1617\. ../../common.sh1819OLDPATH="$PATH"2021# real-looking curl on PATH; calling the shadowing shell function below instead is a failure22make_fake_curl "$TEST_BIN" ': > "$ARGV_LOG"23for a in "$@"; do printf "%s\n" "$a" >> "$ARGV_LOG"; done'2425VERSION_MESSAGE="curl 7.99.0 (fake)26Features: libz"2728export ARGV_LOG29export PATH="$TEST_BIN:$OLDPATH"3031# 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)33curl() {34 echo 'curl 0.0.0-shadowed'35 return 136}37wget() {38 return 139}4041[ "$(nvm_curl_version)" = '7.99.0' ] || die "nvm_curl_version used the shadowing curl function; got $(nvm_curl_version)"4243nvm_curl_libz_support || die 'nvm_curl_libz_support used the shadowing curl function'4445nvm_download -s 'http://example.com/' -o /dev/null || die 'nvm_download used the shadowing curl function and failed'46grep -Fxq 'http://example.com/' "$ARGV_LOG" || die "nvm_download did not invoke the curl executable; got: $(cat "$ARGV_LOG")"4748cleanup
modified:test/slow/nvm_get_latest/nvm_get_latest+30 added-26 removed
1#!/bin/sh23die () { echo "$@" ; cleanup ; exit 1; }45WORK="$PWD/nvm_get_latest-work.$$"6TEST_BIN="$WORK/bin"78cleanup() {6 unset -f curl wget nvm_has9 rm -rf "$WORK"10 export PATH="$OLDPATH"11}1213\. ../../../nvm.sh1415\. ../../common.sh1617OLDPATH="$PATH"1819EXPECTED_VERSION="v12.3.456"20URL="https://github.com/nvm-sh/nvm/releases/tag/$EXPECTED_VERSION"21EXPECTED_CURL_ARGS="--compressed -q -w %{url_effective}\n -L -s -S https://latest.nvm.sh -o /dev/null"22EXPECTED_WGET_ARGS="-q https://latest.nvm.sh --server-response -O /dev/null"23export URL EXPECTED_CURL_ARGS EXPECTED_WGET_ARGS2416curl() {17 if [ $# -eq 1 ] && [ "$1" = "-V" ]; then18 echo "25VERSION_MESSAGE="26curl 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.327Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp smb smbs smtp smtps telnet tftp28Features: 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}29wget() {30 if [ "_$*" != "_$EXPECTED_WGET_ARGS" ]; then31 echo >&2 "expected args ($EXPECTED_WGET_ARGS), got ($*)"32 return 133 else34 local WGET_CONTENTS35 WGET_CONTENTS="2930# fake curl/wget: `command curl` bypasses shell functions, so the mocks must be executables on PATH31make_fake_curl "$TEST_BIN" 'if [ "_$*" != "_$EXPECTED_CURL_ARGS" ]; then32 echo >&2 "expected args ($EXPECTED_CURL_ARGS), got ($*)"33 exit 134fi35echo "$URL"'3637cat > "$TEST_BIN/wget" <<'OUTER'38#!/bin/sh39if [ "_$*" != "_$EXPECTED_WGET_ARGS" ]; then40 echo >&2 "expected args ($EXPECTED_WGET_ARGS), got ($*)"41 exit 142fi43cat >&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: 84
41 unmodified lines
89 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}93CONTENTS94OUTER95chmod +x "$TEST_BIN/wget"9693\. ../../common.sh97export PATH="$TEST_BIN:$OLDPATH"9899try nvm_get_latest100[ "_$CAPTURED_STDOUT" = "_$EXPECTED_VERSION" ] \101 || die "success path did not return version '$EXPECTED_VERSION', got '$CAPTURED_STDOUT'"
5 unmodified lines
modified:test/slow/nvm_get_latest/nvm_get_latest failed redirect+13 added-7 removed
1#!/bin/sh23die () { echo "$@" ; cleanup ; exit 1; }45WORK="$PWD/nvm_get_latest_failed_redirect-work.$$"6TEST_BIN="$WORK/bin"78cleanup() {6 unset -f curl wget9 rm -rf "$WORK"10 export PATH="$OLDPATH"11}1213\. ../../../nvm.sh1415\. ../../common.sh1613curl() {14 return 115}16wget() {17 return 118}17OLDPATH="$PATH"1819# fake curl/wget: `command curl` bypasses shell functions, so the mocks must be executables on PATH20make_fake_curl "$TEST_BIN" 'exit 1'21printf '#!/bin/sh\nexit 1\n' > "$TEST_BIN/wget"22chmod +x "$TEST_BIN/wget"2324export PATH="$TEST_BIN:$OLDPATH"2526try_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 lines
modified:test/common.sh+33 added
48 unmodified lines
49 echo "echo \"${2}\"" >> "$1"50 chmod a+x "$1"51}5253# `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.58make_fake_curl() {59 local FAKE_BIN_DIR60 FAKE_BIN_DIR="${1-}"61 [ -n "${FAKE_BIN_DIR}" ] || return 16263 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"67exit 1'68 fi6970 mkdir -p "${FAKE_BIN_DIR}" || return 27172 {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"8182 # the fake curl reads VERSION_MESSAGE from the environment83 export VERSION_MESSAGE84}8586make_fake_node() {87 local VERSION88 VERSION="${1-}"89 [ -n "${VERSION}" ] || return 1
217 unmodified lines
modified:install.sh+1 added-1 removed
117 unmodified lines
118 -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}125126install_nvm_from_git() {
381 unmodified lines
modified:nvm.sh+4 added-4 removed
85 unmodified lines
86 [ -t 1 ] && [ "${NVM_NUM_COLORS:--1}" -ge 8 ] && [ "${NVM_NO_COLORS-}" != '--no-colors' ]87}8889nvm_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}9293nvm_curl_use_compression() {94 nvm_curl_libz_support && nvm_version_greater_than_or_equal_to "$(nvm_curl_version)" 7.21.0
7 unmodified lines
102 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 fi
58 unmodified lines
169 if [ -n "${NVM_AUTH_HEADER:-}" ]; then170 set -- "$@" --header "Authorization: ${sanitized_header}"171 fi172173 "${NVM_DOWNLOADER}" "$@"173 command "${NVM_DOWNLOADER}" "$@"174}175176nvm_sanitize_auth_header() {177 # Remove potentially dangerous characters
470 unmodified lines
648 clang --version | command awk '{ if ($2 == "version") print $3; else if ($3 == "version") print $4 }' | command sed 's/-.*$//g'649}650651nvm_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}654655nvm_version_greater() {656 command awk 'BEGIN {
4226 unmodified lines