nvm_download_no_eval_injection
This file
Path
test/install_script/nvm_download_no_eval_injection
99 lines · 4.4 KiB
Last changed by
[Robustness] `install.sh`: `nvm_download`: avoid `eval` so arguments aren't re-parsed by the shellJordan Harbandbefore adoptionb17550a
History of this fileTake it
[Robustness] `install.sh`: `nvm_download`: avoid `eval` so arguments aren't re-parsed by the shellJordan Harbandbefore adoptionb17550a
lines 1–99why, one step back1#!/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; }1516NVM_ENV=testing \. ../../install.sh1718mkdir -p "$TEST_BIN"1920# fake curl/wget: record each received argument verbatim, then succeed21{22 echo '#!/bin/sh'23 echo ': > "$ARGV_LOG"'24 echo 'for a in "$@"; do printf "%s\n" "$a" >> "$ARGV_LOG"; done'25 echo 'exit 0'26} > "$TEST_BIN/curl"27chmod +x "$TEST_BIN/curl"28cp "$TEST_BIN/curl" "$TEST_BIN/wget"2930# the wget-path calls below restrict PATH to $TEST_BIN so that no real curl is31# found; link sed and touch in so that a regression to the old sed|eval32# implementation fails this test by actually injecting, rather than by failing33# to find the binaries the injection needs34ln -s "$(command -v sed)" "$TEST_BIN/sed"35ln -s "$(command -v touch)" "$TEST_BIN/touch"3637export ARGV_LOG38export PATH="$TEST_BIN:$OLDPATH"3940URL="https://raw.githubusercontent.com/nvm-sh/nvm/HEAD/nvm.sh"4142# given a url containing command-substitution syntax43INJECT_URL="http://example.test/v1\$(touch ${PROOF})/x"4445# when nvm_download is invoked (curl path)46rm -f "$PROOF"47nvm_download "$INJECT_URL" -o - || die 'nvm_download (curl) returned nonzero on injection url'48# then the substitution must not have executed49[ ! -e "$PROOF" ] || die "command injection fired via curl path: proof file was created"50# and curl must have received the url as one literal argument51grep -Fxq "$INJECT_URL" "$ARGV_LOG" || die "curl did not receive the url as a single literal argument; got: $(cat "$ARGV_LOG")"5253# given curl is unavailable (the wget-path calls run with PATH limited to our54# fake wget, so neither the fake nor the system curl is found)55rm -f "$TEST_BIN/curl"5657# when nvm_download is invoked with the injection url (wget path)58rm -f "$PROOF"59( PATH="$TEST_BIN"; export PATH; nvm_download "$INJECT_URL" -o - ) || die 'nvm_download (wget) returned nonzero on injection url'60# then the substitution must not have executed61[ ! -e "$PROOF" ] || die "command injection fired via wget path: proof file was created"62grep -Fxq "$INJECT_URL" "$ARGV_LOG" || die "wget did not receive the url as a single literal argument; got: $(cat "$ARGV_LOG")"6364# given an output path containing a space, as a real $NVM_DIR may65SPACED_DIR="$WORK/dir with space"66SPACED="$SPACED_DIR/nvm.sh"67mkdir -p "$SPACED_DIR"6869# when nvm_download is invoked with it (wget path)70( PATH="$TEST_BIN"; export PATH; nvm_download -s "$URL" -o "$SPACED" ) || die 'nvm_download (wget) returned nonzero on spaced output path'71# then wget receives it as one argument rather than word-split72grep -Fxq "$SPACED" "$ARGV_LOG" || die "wget did not receive the spaced output path as a single argument; got: $(cat "$ARGV_LOG")"7374# given a url with an ampersand in its query string, as a real mirror may75AMP_URL="http://example.test/nvm.sh?a=1&b=2"7677# when nvm_download is invoked with it (wget path)78( PATH="$TEST_BIN"; export PATH; nvm_download -s "$AMP_URL" -o - ) || die 'nvm_download (wget) returned nonzero on ampersand url'79# then the url is passed intact rather than backgrounding the command80grep -Fxq "$AMP_URL" "$ARGV_LOG" || die "wget did not receive the ampersand url intact; got: $(cat "$ARGV_LOG")"8182# when invoked with -L -C - --progress-bar URL -o FILE (wget path)83FILE="$WORK/target"84( PATH="$TEST_BIN"; export PATH; nvm_download -L -C - --progress-bar "$URL" -o "$FILE" ) || die 'nvm_download (wget) returned nonzero on normal url'85# then flags are translated to wget equivalents86grep -Fxqe "-c" "$ARGV_LOG" || die "wget did not translate -C - to -c; got: $(cat "$ARGV_LOG")"87grep -Fxqe "--progress=bar" "$ARGV_LOG" || die "wget did not translate --progress-bar; got: $(cat "$ARGV_LOG")"88grep -Fxqe "-O" "$ARGV_LOG" || die "wget did not translate -o to -O; got: $(cat "$ARGV_LOG")"89grep -Fxqe "-L" "$ARGV_LOG" && die "wget should drop -L; got: $(cat "$ARGV_LOG")"90grep -Fxqe "-C" "$ARGV_LOG" && die "wget should not pass -C through; got: $(cat "$ARGV_LOG")"91grep -Fxqe "-" "$ARGV_LOG" && die "wget should drop the lone - after -C; got: $(cat "$ARGV_LOG")"9293# when invoked with -s (wget path)94( PATH="$TEST_BIN"; export PATH; nvm_download -s "$URL" -o "$FILE" ) || die 'nvm_download (wget) returned nonzero on -s url'95# then -s becomes -q96grep -Fxqe "-q" "$ARGV_LOG" || die "wget did not translate -s to -q; got: $(cat "$ARGV_LOG")"9798cleanup99echo "nvm_download no eval injection: passed"