[Fix] sanitize `NVM_AUTH_HEADER` in `wget` path
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sy2n0 · committed by Jordan Harband · parent 242d997 · Browse files · 2 files·+67 added−1 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+67 added−1 removed
- tests:+65 added−0 removed
- other:+2 added−1 removed
Provenance
- Commit
44e2590- Tree
ab8b0ee- Author
Sy2n0 <sy2n0@naver.com>- Committer
Jordan Harband <ljharb@gmail.com>- Parent
242d997
2 files changed+67 added−1 removed
added:test/fast/Unit tests/security_wget_injection+65 added
1+#!/bin/sh2+3+# Security test to verify that NVM_AUTH_HEADER is sanitized in wget path4+# This test ensures that command injection attacks are prevented5+6+cleanup () {7+ unset -f die cleanup8+ rm -f /tmp/nvm_security_test_file 2>/dev/null || true9+}10+die () { echo "$@" ; cleanup ; exit 1; }11+12+\. ../../../nvm.sh13+14+set -ex15+16+# Skip test if wget is not available17+if ! nvm_has "wget"; then18+ echo "wget not available, skipping security test"19+ exit 020+fi21+22+# Test 1: Verify that malicious command injection in NVM_AUTH_HEADER is sanitized23+# This should not execute the command, but should sanitize it24+MALICIOUS_HEADER="Bearer test-token; touch /tmp/nvm_security_test_file; echo malicious"25+NVM_AUTH_HEADER="${MALICIOUS_HEADER}" nvm_download "https://raw.githubusercontent.com/nvm-sh/nvm/HEAD/install.sh" >/dev/null 2>&1 || true26+27+# Verify that the malicious file was NOT created (sanitization worked)28+if [ -f /tmp/nvm_security_test_file ]; then29+ die "SECURITY FAILURE: Command injection succeeded! Malicious file was created."30+fi31+32+# Test 2: Verify that sanitized header still works for legitimate requests33+# The sanitized header should only contain safe characters34+SANITIZED=$(nvm_sanitize_auth_header "${MALICIOUS_HEADER}")35+# Verify that dangerous characters were removed36+case "${SANITIZED}" in37+ *";"*|*"touch"*|*"/tmp"*)38+ die "SECURITY FAILURE: Sanitization did not remove dangerous characters properly"39+ ;;40+esac41+42+# Test 3: Verify that legitimate header with safe characters still works43+LEGITIMATE_HEADER="Bearer test-token-123"44+NVM_AUTH_HEADER="${LEGITIMATE_HEADER}" nvm_download "https://raw.githubusercontent.com/nvm-sh/nvm/HEAD/install.sh" >/dev/null 2>&1 || true45+46+# Test 4: Test with backticks (command substitution)47+MALICIOUS_HEADER2="Bearer \`touch /tmp/nvm_security_test_file\`"48+NVM_AUTH_HEADER="${MALICIOUS_HEADER2}" nvm_download "https://raw.githubusercontent.com/nvm-sh/nvm/HEAD/install.sh" >/dev/null 2>&1 || true49+50+# Verify that the malicious file was NOT created51+if [ -f /tmp/nvm_security_test_file ]; then52+ die "SECURITY FAILURE: Command injection with backticks succeeded! Malicious file was created."53+fi54+55+# Test 5: Test with $(command substitution)56+MALICIOUS_HEADER3="Bearer \$(touch /tmp/nvm_security_test_file)"57+NVM_AUTH_HEADER="${MALICIOUS_HEADER3}" nvm_download "https://raw.githubusercontent.com/nvm-sh/nvm/HEAD/install.sh" >/dev/null 2>&1 || true58+59+# Verify that the malicious file was NOT created60+if [ -f /tmp/nvm_security_test_file ]; then61+ die "SECURITY FAILURE: Command injection with \$() succeeded! Malicious file was created."62+fi63+64+cleanup65+echo "All security tests passed: Command injection attacks are properly sanitized"modified:nvm.sh+2 added-1 removed
147 unmodified lines148 s/-C - /-c /149 ")150 151 if [ -n "${NVM_AUTH_HEADER:-}" ]; then152+ sanitized_header=$(nvm_sanitize_auth_header "${NVM_AUTH_HEADER}")152- ARGS="${ARGS} --header \"${NVM_AUTH_HEADER}\""153+ ARGS="${ARGS} --header \"${sanitized_header}\""154 fi155 # shellcheck disable=SC2086156 eval wget $ARGS157 fi4563 unmodified lines