Skip to content
Myr

[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

2 files+67 added−1 removed

added:test/fast/Unit tests/security_wget_injection+65 added
1#!/bin/sh23# Security test to verify that NVM_AUTH_HEADER is sanitized in wget path4# This test ensures that command injection attacks are prevented56cleanup () {7 unset -f die cleanup8 rm -f /tmp/nvm_security_test_file 2>/dev/null || true9}10die () { echo "$@" ; cleanup ; exit 1; }1112\. ../../../nvm.sh1314set -ex1516# Skip test if wget is not available17if ! nvm_has "wget"; then18 echo "wget not available, skipping security test"19 exit 020fi2122# Test 1: Verify that malicious command injection in NVM_AUTH_HEADER is sanitized23# This should not execute the command, but should sanitize it24MALICIOUS_HEADER="Bearer test-token; touch /tmp/nvm_security_test_file; echo malicious"25NVM_AUTH_HEADER="${MALICIOUS_HEADER}" nvm_download "https://raw.githubusercontent.com/nvm-sh/nvm/HEAD/install.sh" >/dev/null 2>&1 || true2627# Verify that the malicious file was NOT created (sanitization worked)28if [ -f /tmp/nvm_security_test_file ]; then29 die "SECURITY FAILURE: Command injection succeeded! Malicious file was created."30fi3132# Test 2: Verify that sanitized header still works for legitimate requests33# The sanitized header should only contain safe characters34SANITIZED=$(nvm_sanitize_auth_header "${MALICIOUS_HEADER}")35# Verify that dangerous characters were removed36case "${SANITIZED}" in37 *";"*|*"touch"*|*"/tmp"*)38 die "SECURITY FAILURE: Sanitization did not remove dangerous characters properly"39 ;;40esac4142# Test 3: Verify that legitimate header with safe characters still works43LEGITIMATE_HEADER="Bearer test-token-123"44NVM_AUTH_HEADER="${LEGITIMATE_HEADER}" nvm_download "https://raw.githubusercontent.com/nvm-sh/nvm/HEAD/install.sh" >/dev/null 2>&1 || true4546# Test 4: Test with backticks (command substitution)47MALICIOUS_HEADER2="Bearer \`touch /tmp/nvm_security_test_file\`"48NVM_AUTH_HEADER="${MALICIOUS_HEADER2}" nvm_download "https://raw.githubusercontent.com/nvm-sh/nvm/HEAD/install.sh" >/dev/null 2>&1 || true4950# Verify that the malicious file was NOT created51if [ -f /tmp/nvm_security_test_file ]; then52 die "SECURITY FAILURE: Command injection with backticks succeeded! Malicious file was created."53fi5455# Test 5: Test with $(command substitution)56MALICIOUS_HEADER3="Bearer \$(touch /tmp/nvm_security_test_file)"57NVM_AUTH_HEADER="${MALICIOUS_HEADER3}" nvm_download "https://raw.githubusercontent.com/nvm-sh/nvm/HEAD/install.sh" >/dev/null 2>&1 || true5859# Verify that the malicious file was NOT created60if [ -f /tmp/nvm_security_test_file ]; then61 die "SECURITY FAILURE: Command injection with \$() succeeded! Malicious file was created."62fi6364cleanup65echo "All security tests passed: Command injection attacks are properly sanitized"
modified:nvm.sh+2 added-1 removed
147 unmodified lines
148 s/-C - /-c /149 ")150151 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 fi
4563 unmodified lines