Files
nvm/test/fast/Unit tests/nvm_process_nvmrc_content rejects duplicate keys
T
Jordan Harband e4fcf988c5 [Fix] nvm_process_nvmrc_content, nvm_is_valid_version: keep .nvmrc content out of commands' arguments
A symlinked `.nvmrc`'s content is never printed (67268ebc),
but it was still parsed with external commands that took part of it as arguments,
which every local user can read (`ps`, `/proc/<pid>/cmdline`):
the duplicate-key check passed each key (the text before a line's first `=`) to `grep -E`,
and `nvm_is_valid_version` passed prerelease-shaped text to `awk` before checking that its core is numeric.
So another local user who can place a `.nvmrc` symlink where the victim runs nvm
(or merely sources `nvm.sh`, with no default alias)
could read, for example, a WireGuard private key the victim's symlink target holds.

Compare keys with `case`, literally rather than as a regex,
and let the existing shell check validate a prerelease's core,
which makes `0.0.N-<prerelease>` valid.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-GG4I2LOU.
2026-11-16 11:34:02 -08:00

44 lines
1.4 KiB
Bash
Executable File

#!/bin/sh
die () { echo "$@" ; exit 1; }
: nvm.sh
\. ../../../nvm.sh
\. ../../common.sh
for KEY in a a.b 'a*' '(' '[' 'a\' 'a b'; do
CONTENT="20
${KEY}=1
other=2
${KEY} = 3"
try_err nvm_process_nvmrc_content "${CONTENT}"
[ "${CAPTURED_EXIT_CODE}" = 1 ] || die "expected a duplicate key \`${KEY}\` to be invalid; got exit code ${CAPTURED_EXIT_CODE}"
case "${CAPTURED_STDERR}" in
*'invalid .nvmrc!'*) ;;
*) die "expected the invalid .nvmrc message for a duplicate key \`${KEY}\`; got >${CAPTURED_STDERR}<" ;;
esac
try nvm_process_nvmrc_content "${CONTENT}"
[ -z "${CAPTURED_STDOUT}" ] || die "expected no version for a duplicate key \`${KEY}\`; got \`${CAPTURED_STDOUT}\`"
done
# keys are compared as they are written, not as patterns
for KEY in b ab A a. '*' '.*' '?' 'a|b' '[a]'; do
for CONTENT in "20
a=1
${KEY}=2" "20
${KEY}=1
a=2"; do
try_err nvm_process_nvmrc_content "${CONTENT}"
[ "${CAPTURED_EXIT_CODE}" = 0 ] || die "expected the keys \`a\` and \`${KEY}\` to be distinct; got exit code ${CAPTURED_EXIT_CODE}"
case "${CAPTURED_STDERR}" in
*'invalid .nvmrc!'*) die "expected no error for the keys \`a\` and \`${KEY}\`; got >${CAPTURED_STDERR}<" ;;
esac
try nvm_process_nvmrc_content "${CONTENT}"
[ "${CAPTURED_STDOUT}" = '20' ] || die "expected \`20\` with the keys \`a\` and \`${KEY}\`; got \`${CAPTURED_STDOUT}\`"
done
done