mirror of
https://github.com/nvm-sh/nvm.git
synced 2026-11-20 16:22:52 +03:00
[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.
This commit is contained in:
4 files changed
+101
-11
No files matched your search
@@ -984,11 +984,14 @@ nvm_process_nvmrc_content() {
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Check for duplicate keys
|
||||
if nvm_echo "${keys}" | nvm_grep -q -E "(^| )${key}( |$)"; then
|
||||
nvm_nvmrc_invalid_msg "${lines}"
|
||||
return 1
|
||||
fi
|
||||
# Check for duplicate keys, in the shell: a symlinked `.nvmrc` can hold any file's content,
|
||||
# and every local user can read a process's arguments
|
||||
case "${keys} " in
|
||||
*" ${key} "*)
|
||||
nvm_nvmrc_invalid_msg "${lines}"
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
keys="${keys} ${key}"
|
||||
values="${values} ${value}"
|
||||
else
|
||||
@@ -1523,9 +1526,7 @@ nvm_is_valid_version() {
|
||||
*.*.*) ;;
|
||||
*) return 1 ;;
|
||||
esac
|
||||
# a prerelease's `-` makes it unreachable for the pure-shell checks
|
||||
# below, so do the full check now.
|
||||
nvm_version_greater_than_or_equal_to "${VERSION}" 0 || return 1
|
||||
# the core is checked below, in the shell, so it never reaches `awk`'s arguments
|
||||
;;
|
||||
*.)
|
||||
NVM_VERSION_CORE="${NVM_VERSION_CORE%.}"
|
||||
|
||||
@@ -42,9 +42,8 @@ nvm_is_valid_version v9.0.0-v8-canary201710301f8f3dcf36 || die "nvm_is_valid_ver
|
||||
nvm_is_valid_version v8.16.2-test201910293843c56739f || die "nvm_is_valid_version v8.16.2-test201910293843c56739f did not return 0"
|
||||
nvm_is_valid_version iojs-v3.2.1-nightly201508291a531b4e44 || die "nvm_is_valid_version iojs-v3.2.1-nightly201508291a531b4e44 did not return 0"
|
||||
|
||||
# `nvm_is_valid_version` must not fork a command for a plain version: mock
|
||||
# `awk`, which the version comparisons used, and fail if it runs. (Prereleases
|
||||
# still reach the comparison, which is why this checks a release version.)
|
||||
# `nvm_is_valid_version` must not fork a command, since it checks what a symlinked `.nvmrc` points at:
|
||||
# mock `awk`, which the version comparisons used, and fail if it runs.
|
||||
TEST_DIR="$(pwd)/nvm_is_valid_version_tmp"
|
||||
[ ! -e "${TEST_DIR}" ] && mkdir "${TEST_DIR}"
|
||||
MOCK_BIN="${TEST_DIR}/bin"
|
||||
@@ -58,5 +57,14 @@ chmod +x "${MOCK_BIN}/awk"
|
||||
|
||||
PATH="${MOCK_BIN}:${PATH}" nvm_is_valid_version v18.20.0 \
|
||||
|| die 'nvm_is_valid_version v18.20.0 forked a command'
|
||||
PATH="${MOCK_BIN}:${PATH}" nvm_is_valid_version v9.4.0-rc.1 \
|
||||
|| die 'nvm_is_valid_version v9.4.0-rc.1 forked a command'
|
||||
for VERSION in a.b.c-rc.1 1.2.3.4-rc.1 1..2-rc.1; do
|
||||
OUTPUT="$(PATH="${MOCK_BIN}:${PATH}" nvm_is_valid_version "${VERSION}" 2>&1)" \
|
||||
&& die "nvm_is_valid_version ${VERSION} did not return 1"
|
||||
case "${OUTPUT}" in
|
||||
*'awk was called'*) die "nvm_is_valid_version ${VERSION} forked a command" ;;
|
||||
esac
|
||||
done
|
||||
|
||||
rm -rf "${TEST_DIR}"
|
||||
@@ -35,6 +35,44 @@ for TARGET in .npmrc .git-credentials; do
|
||||
[ -z "${CAPTURED_STDOUT}" ] || die "expected no version from a symlink to ${TARGET}; got \`${CAPTURED_STDOUT}\`"
|
||||
done
|
||||
|
||||
# every local user can read a command's arguments, so the content is not passed in them either
|
||||
MOCK_BIN="${TEST_DIR}/bin"
|
||||
mkdir -p "${MOCK_BIN}" || die 'failed to create the mock bin dir'
|
||||
for COMMAND in grep sed awk tr; do
|
||||
{
|
||||
echo '#!/bin/sh'
|
||||
printf '%s\n' 'printf "%s\n" "$*" >> "${0%/*}/../arguments"'
|
||||
echo 'PATH="${PATH#*:}"'
|
||||
echo 'exec "${0##*/}" "$@"'
|
||||
} > "${MOCK_BIN}/${COMMAND}"
|
||||
chmod +x "${MOCK_BIN}/${COMMAND}"
|
||||
done
|
||||
printf 'SECRET=\n' > "${TEST_DIR}/home/wg.key"
|
||||
printf 'a=b\nSECRET=c\nSECRET=d\n' > "${TEST_DIR}/home/.env"
|
||||
printf 'a.b.c-SECRET\n' > "${TEST_DIR}/home/token"
|
||||
printf '1.2.3.4-SECRET\n' > "${TEST_DIR}/home/token2"
|
||||
|
||||
for TARGET in wg.key .env token token2; do
|
||||
rm -f "${NVMRC}"
|
||||
ln -s "../home/${TARGET}" "${NVMRC}" || die 'failed to create the symlink'
|
||||
|
||||
: > "${TEST_DIR}/arguments"
|
||||
CAPTURED_STDERR="$(set +x; PATH="${MOCK_BIN}:${PATH}" nvm_process_nvmrc "${NVMRC}" 2>&1 >/dev/null)" \
|
||||
&& die "expected a symlink to ${TARGET} to be invalid"
|
||||
[ "${CAPTURED_STDERR}" = "invalid .nvmrc! '${NVMRC}' is a symbolic link, so it must contain a version or an alias name; its content is not shown." ] || die "expected the symlink error for ${TARGET}; got \`${CAPTURED_STDERR}\`"
|
||||
[ -s "${TEST_DIR}/arguments" ] || die 'expected the mocked commands to record their arguments'
|
||||
! nvm_grep -q SECRET "${TEST_DIR}/arguments" || die "expected a symlink to ${TARGET} to not put its content in a command's arguments; got \`$(nvm_grep SECRET "${TEST_DIR}/arguments")\`"
|
||||
done
|
||||
|
||||
# nor when the content is valid
|
||||
printf 'SECRET=\n20\n' > "${TEST_DIR}/home/accepted"
|
||||
rm -f "${NVMRC}"
|
||||
ln -s ../home/accepted "${NVMRC}" || die 'failed to create the symlink'
|
||||
: > "${TEST_DIR}/arguments"
|
||||
CAPTURED_STDOUT="$(set +x; PATH="${MOCK_BIN}:${PATH}" nvm_process_nvmrc "${NVMRC}" 2>/dev/null)" || die 'expected a symlink to a valid version file to be valid'
|
||||
[ "${CAPTURED_STDOUT}" = '20' ] || die "expected \`20\` from a symlink to a valid version file; got \`${CAPTURED_STDOUT}\`"
|
||||
! nvm_grep -q SECRET "${TEST_DIR}/arguments" || die "expected a symlink to a valid version file to not put its content in a command's arguments; got \`$(nvm_grep SECRET "${TEST_DIR}/arguments")\`"
|
||||
|
||||
nvm alias nvmrc_symlink_alias v0.0.1 > /dev/null 2>&1 || die 'failed to create an alias'
|
||||
|
||||
# a deliberate symlink to another version file, in or above the project, still works
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
#!/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
|
||||
Reference in new issue
Block a user