Skip to content
Myr

[Fix] `nvm_download`: avoid `eval` so mirror-supplied version strings can't inject commands

Before adoption: this commit came in with the history the project adopted, so no landing records it.

`nvm_download` built a curl/wget command string and ran it with `eval`. The download URLs embed the version string taken from the mirror's `index.tab`, which is untrusted. Wrapping each argument in double quotes inside the `eval` does not prevent command substitution, so a version field such as `v1$(touch /tmp/proof)` was executed by the shell. This bypassed the earlier quoting hardening in 0ce8f5a. Pass every argument as a literal argv element instead of constructing a string for `eval`, on both the curl and wget paths, so URL arguments are never re-parsed by the shell. The wget flag translation is now done per-argument with a POSIX `set --` loop rather than `sed` over the joined string. The auth header is sanitized and added once, before invoking the downloader.

authored by Jordan Harband · committed by Jordan Harband · parent d264b79 · Browse files · 2 files+134 added−33 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+134 added−33 removed

  • tests:+86 added−0 removed
  • other:+48 added−33 removed

2 files+134 added−33 removed

added:test/fast/Unit tests/nvm_download no eval injection+86 added
1#!/bin/sh23OLDPATH="$PATH"4WORK="$PWD/nvm_download-noeval-work.$$"5TEST_BIN="$WORK/bin"6ARGV_LOG="$WORK/argv.log"7PROOF="$WORK/nvm_injection_proof"89cleanup() {10 unset -f die cleanup11 rm -rf "$WORK"12 export PATH="$OLDPATH"13}14die () { echo "$@" ; cleanup ; exit 1; }1516\. ../../../nvm.sh1718OLDPATH="$PATH"1920mkdir -p "$TEST_BIN"2122# fake curl/wget: record each received argument verbatim, then succeed23{24 echo '#!/bin/sh'25 echo ': > "$ARGV_LOG"'26 echo 'for a in "$@"; do printf "%s\n" "$a" >> "$ARGV_LOG"; done'27 echo 'exit 0'28} > "$TEST_BIN/curl"29chmod +x "$TEST_BIN/curl"30cp "$TEST_BIN/curl" "$TEST_BIN/wget"3132export ARGV_LOG33export PATH="$TEST_BIN:$OLDPATH"3435# given a url containing command-substitution syntax (mirror-supplied)36INJECT_URL="http://example.test/v1\$(touch ${PROOF})/x.tar.gz"3738# when nvm_download is invoked (curl path)39rm -f "$PROOF"40nvm_download "$INJECT_URL" -o - || die 'nvm_download (curl) returned nonzero on injection url'41# then the substitution must not have executed42[ ! -e "$PROOF" ] || die "command injection fired via curl path: proof file was created"43# and curl must have received the url as one literal argument44grep -Fxq "$INJECT_URL" "$ARGV_LOG" || die "curl did not receive the url as a single literal argument; got: $(cat "$ARGV_LOG")"4546# given a normal download (curl path)47URL="https://nodejs.org/dist/index.tab"48FILE="$WORK/target"49# when invoked with -L -s URL -o FILE50nvm_download -L -s "$URL" -o "$FILE" || die 'nvm_download (curl) returned nonzero on normal url'51# then the url, -o, and output file are passed through verbatim52grep -Fxq "$URL" "$ARGV_LOG" || die "curl did not receive the url; got: $(cat "$ARGV_LOG")"53grep -Fxqe "-o" "$ARGV_LOG" || die "curl did not receive -o; got: $(cat "$ARGV_LOG")"54grep -Fxq "$FILE" "$ARGV_LOG" || die "curl did not receive the output file; got: $(cat "$ARGV_LOG")"55# and curl-style flags are passed through verbatim (curl keeps -s; it is not translated to wget's -q)56grep -Fxqe "-s" "$ARGV_LOG" || die "curl did not receive -s verbatim; got: $(cat "$ARGV_LOG")"57rm -f "$FILE"5859# given curl is unavailable (the wget-path calls run with PATH limited to our60# fake wget, so neither the fake nor the system curl is found)61rm -f "$TEST_BIN/curl"6263# when nvm_download is invoked with the injection url (wget path)64rm -f "$PROOF"65( PATH="$TEST_BIN"; export PATH; nvm_download "$INJECT_URL" -o - ) || die 'nvm_download (wget) returned nonzero on injection url'66# then the substitution must not have executed67[ ! -e "$PROOF" ] || die "command injection fired via wget path: proof file was created"68grep -Fxq "$INJECT_URL" "$ARGV_LOG" || die "wget did not receive the url as a single literal argument; got: $(cat "$ARGV_LOG")"6970# when invoked with -L -C - --progress-bar URL -o FILE (wget path)71( PATH="$TEST_BIN"; export PATH; nvm_download -L -C - --progress-bar "$URL" -o "$FILE" ) || die 'nvm_download (wget) returned nonzero on normal url'72# then flags are translated to wget equivalents73grep -Fxqe "-c" "$ARGV_LOG" || die "wget did not translate -C - to -c; got: $(cat "$ARGV_LOG")"74grep -Fxqe "--progress=bar" "$ARGV_LOG" || die "wget did not translate --progress-bar; got: $(cat "$ARGV_LOG")"75grep -Fxqe "-O" "$ARGV_LOG" || die "wget did not translate -o to -O; got: $(cat "$ARGV_LOG")"76grep -Fxqe "-L" "$ARGV_LOG" && die "wget should drop -L; got: $(cat "$ARGV_LOG")"77grep -Fxqe "-C" "$ARGV_LOG" && die "wget should not pass -C through; got: $(cat "$ARGV_LOG")"78grep -Fxqe "-" "$ARGV_LOG" && die "wget should drop the lone - after -C; got: $(cat "$ARGV_LOG")"7980# when invoked with -s (wget path)81( PATH="$TEST_BIN"; export PATH; nvm_download -L -s "$URL" -o "$FILE" ) || die 'nvm_download (wget) returned nonzero on -s url'82# then -s becomes -q83grep -Fxqe "-q" "$ARGV_LOG" || die "wget did not translate -s to -q; got: $(cat "$ARGV_LOG")"8485cleanup86echo "nvm_download no eval injection: passed"
modified:nvm.sh+48 added-33 removed
113 unmodified lines
114 fi115 nvm_echo "${NVM_LATEST_URL##*/}"116}117118# Every argument is passed through as a literal argv element so that untrusted,119# mirror-supplied version strings in the URLs are never re-parsed by the shell120# (which would allow command substitution / OS command injection).121nvm_download() {119 if nvm_has "curl"; then120 local CURL_COMPRESSED_FLAG=""121 local CURL_HEADER_FLAG=""122 local sanitized_header122 local sanitized_header123 sanitized_header=''124 if [ -n "${NVM_AUTH_HEADER:-}" ]; then125 sanitized_header="$(nvm_sanitize_auth_header "${NVM_AUTH_HEADER}")"126 fi127124 if [ -n "${NVM_AUTH_HEADER:-}" ]; then125 sanitized_header=$(nvm_sanitize_auth_header "${NVM_AUTH_HEADER}")126 CURL_HEADER_FLAG="--header \"Authorization: ${sanitized_header}\""127 fi128128 local NVM_DOWNLOADER129 NVM_DOWNLOADER=''130 if nvm_has "curl"; then131 NVM_DOWNLOADER='curl'132 set -- -q --fail "$@"133 if nvm_curl_use_compression; then130 CURL_COMPRESSED_FLAG="--compressed"134 set -- --compressed "$@"135 fi132 local NVM_DOWNLOAD_ARGS133 NVM_DOWNLOAD_ARGS=''134 for arg in "$@"; do135 NVM_DOWNLOAD_ARGS="${NVM_DOWNLOAD_ARGS} \"$arg\""136 done137 eval "curl -q --fail ${CURL_COMPRESSED_FLAG:-} ${CURL_HEADER_FLAG:-} ${NVM_DOWNLOAD_ARGS}"136 elif nvm_has "wget"; then137 NVM_DOWNLOADER='wget'138 # Emulate curl with wget140 ARGS=$(nvm_echo "$@" | command sed "141 s/--progress-bar /--progress=bar /142 s/--compressed //143 s/--fail //144 s/-L //145 s/-I /--server-response /146 s/-s /-q /147 s/-sS /-nv /148 s/-o /-O /149 s/-C - /-c /150 ")139 local NVM_DOWNLOAD_WGET_COUNT140 NVM_DOWNLOAD_WGET_COUNT=$#141 local NVM_DOWNLOAD_WGET_SKIP142 NVM_DOWNLOAD_WGET_SKIP=0143 local NVM_DOWNLOAD_WGET_ARG144 for NVM_DOWNLOAD_WGET_ARG in "$@"; do145 if [ "${NVM_DOWNLOAD_WGET_SKIP}" = '1' ]; then146 NVM_DOWNLOAD_WGET_SKIP=0147 continue148 fi149 case "${NVM_DOWNLOAD_WGET_ARG}" in150 '--progress-bar') set -- "$@" '--progress=bar' ;;151 '--compressed') : ;;152 '--fail') : ;;153 '-L') : ;;154 '-I') set -- "$@" '--server-response' ;;155 '-s') set -- "$@" '-q' ;;156 '-sS') set -- "$@" '-nv' ;;157 '-o') set -- "$@" '-O' ;;158 '-C') NVM_DOWNLOAD_WGET_SKIP=1; set -- "$@" '-c' ;;159 *) set -- "$@" "${NVM_DOWNLOAD_WGET_ARG}" ;;160 esac161 done162 shift "${NVM_DOWNLOAD_WGET_COUNT}"163 fi164152 if [ -n "${NVM_AUTH_HEADER:-}" ]; then153 sanitized_header=$(nvm_sanitize_auth_header "${NVM_AUTH_HEADER}")154 ARGS="${ARGS} --header \"Authorization: ${sanitized_header}\""155 fi156 # shellcheck disable=SC2086157 eval wget $ARGS165 if [ -z "${NVM_DOWNLOADER}" ]; then166 return 0167 fi168169 if [ -n "${NVM_AUTH_HEADER:-}" ]; then170 set -- "$@" --header "Authorization: ${sanitized_header}"171 fi172173 "${NVM_DOWNLOADER}" "$@"174}175176nvm_sanitize_auth_header() {177 # Remove potentially dangerous characters
4663 unmodified lines