security_wget_injection
This file
Path
test/fast/Unit tests/security_wget_injection
65 lines · 2.6 KiB
Last changed by
[Fix] auth header: Add all valid base64 chars to sanitize functionMathew Dodgsonbefore adoption3a8b51c
History of this fileTake it
[Fix] sanitize `NVM_AUTH_HEADER` in `wget` pathSy2n0before adoption44e2590
lines 1–34why, one step back1#!/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}")[Fix] auth header: Add all valid base64 chars to sanitize functionMathew Dodgsonbefore adoption3a8b51c
line 35why, one step back35# Verify that dangerous shell metacharacters were removed[Fix] sanitize `NVM_AUTH_HEADER` in `wget` pathSy2n0before adoption44e2590
line 36why, one step back36case "${SANITIZED}" in[Fix] auth header: Add all valid base64 chars to sanitize functionMathew Dodgsonbefore adoption3a8b51c
line 37why, one step back37 *";"*|*'$'*|*"\`"*)[Fix] sanitize `NVM_AUTH_HEADER` in `wget` pathSy2n0before adoption44e2590
lines 38–65why, one step back38 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"