Skip to content

Commit 44e2590

Browse files
Sy2n0ljharb
authored andcommitted
[Fix] sanitize NVM_AUTH_HEADER in wget path
1 parent 242d997 commit 44e2590

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

nvm.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ nvm_download() {
149149
")
150150

151151
if [ -n "${NVM_AUTH_HEADER:-}" ]; then
152-
ARGS="${ARGS} --header \"${NVM_AUTH_HEADER}\""
152+
sanitized_header=$(nvm_sanitize_auth_header "${NVM_AUTH_HEADER}")
153+
ARGS="${ARGS} --header \"${sanitized_header}\""
153154
fi
154155
# shellcheck disable=SC2086
155156
eval wget $ARGS
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/bin/sh
2+
3+
# Security test to verify that NVM_AUTH_HEADER is sanitized in wget path
4+
# This test ensures that command injection attacks are prevented
5+
6+
cleanup () {
7+
unset -f die cleanup
8+
rm -f /tmp/nvm_security_test_file 2>/dev/null || true
9+
}
10+
die () { echo "$@" ; cleanup ; exit 1; }
11+
12+
\. ../../../nvm.sh
13+
14+
set -ex
15+
16+
# Skip test if wget is not available
17+
if ! nvm_has "wget"; then
18+
echo "wget not available, skipping security test"
19+
exit 0
20+
fi
21+
22+
# Test 1: Verify that malicious command injection in NVM_AUTH_HEADER is sanitized
23+
# This should not execute the command, but should sanitize it
24+
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 || true
26+
27+
# Verify that the malicious file was NOT created (sanitization worked)
28+
if [ -f /tmp/nvm_security_test_file ]; then
29+
die "SECURITY FAILURE: Command injection succeeded! Malicious file was created."
30+
fi
31+
32+
# Test 2: Verify that sanitized header still works for legitimate requests
33+
# The sanitized header should only contain safe characters
34+
SANITIZED=$(nvm_sanitize_auth_header "${MALICIOUS_HEADER}")
35+
# Verify that dangerous characters were removed
36+
case "${SANITIZED}" in
37+
*";"*|*"touch"*|*"/tmp"*)
38+
die "SECURITY FAILURE: Sanitization did not remove dangerous characters properly"
39+
;;
40+
esac
41+
42+
# Test 3: Verify that legitimate header with safe characters still works
43+
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 || true
45+
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 || true
49+
50+
# Verify that the malicious file was NOT created
51+
if [ -f /tmp/nvm_security_test_file ]; then
52+
die "SECURITY FAILURE: Command injection with backticks succeeded! Malicious file was created."
53+
fi
54+
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 || true
58+
59+
# Verify that the malicious file was NOT created
60+
if [ -f /tmp/nvm_security_test_file ]; then
61+
die "SECURITY FAILURE: Command injection with \$() succeeded! Malicious file was created."
62+
fi
63+
64+
cleanup
65+
echo "All security tests passed: Command injection attacks are properly sanitized"

0 commit comments

Comments
 (0)