[Fix] nvm_process_nvmrc: check that the file read is the regular file at the path

`nvm_process_nvmrc` tested `[ ! -L ]` and then opened the path again with `cat`,
so another local user who can write the directory
could rename a symlink over `.nvmrc` in between (timed by `cat`'s exec),
and the target's content was printed and passed to `find` and `sed` as a version,
where that user could read it from the process list (67268ebc's check, bypassed).

sh can't open without following symlinks,
so read the file once, from one open,
and when its content is not a version or an existing alias,
show it or pass it on only if one `lstat` (`find -prune -type f -links 1 -inum`)
finds a regular file at the path with the open file's inode and no other links.
Anything else is handled as a symlink.
A hard link to another user's file, where the OS allows one, is refused the same way.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-WJUJDPM2.
This commit is contained in:
Jordan Harband committed 2026-11-16 15:12:18 -08:00
1 parent f7280d116e
commit 391a1ccde9
2 files changed
+106 -10

No files matched your search

+28 -10
View File
@@ -1041,21 +1041,39 @@ nvm_is_version_or_alias() {
nvm_process_nvmrc() { nvm_process_nvmrc() {
local NVMRC_PATH local NVMRC_PATH
NVMRC_PATH="$1" NVMRC_PATH="$1"
local NVMRC_FIND_PATH
case "${NVMRC_PATH}" in
/*) NVMRC_FIND_PATH="${NVMRC_PATH}" ;;
*) NVMRC_FIND_PATH="./${NVMRC_PATH}" ;;
esac
if [ ! -L "${NVMRC_PATH}" ]; then # a symlink can point at any file, so only a version or an existing alias is accepted from it, and its content is never shown.
nvm_process_nvmrc_content "$(command cat "${NVMRC_PATH}")" # A symlink can be swapped in after any check, so read from one open, then check that what was read is the path's only link.
return local NVMRC_CONTENT
fi
# a symlink can point at any file, so never print its content,
# and only accept a version or an existing alias, which would be printed later
local NVMRC_VERSION local NVMRC_VERSION
if ! NVMRC_VERSION="$(nvm_process_nvmrc_content "$(command cat "${NVMRC_PATH}")" 2>/dev/null)" \ local NVMRC_INODE
|| ! nvm_is_version_or_alias "${NVMRC_VERSION}"; then {
NVMRC_CONTENT="$(command cat)"
if NVMRC_VERSION="$(nvm_process_nvmrc_content "${NVMRC_CONTENT}" 2>/dev/null)" \
&& nvm_is_version_or_alias "${NVMRC_VERSION}"; then
nvm_echo "${NVMRC_VERSION}"
return 0
fi
NVMRC_INODE="$(command ls -dLi /dev/stdin)"
} < "${NVMRC_PATH}" || return
NVMRC_INODE="${NVMRC_INODE#"${NVMRC_INODE%%[! ]*}"}"
NVMRC_INODE="${NVMRC_INODE%%[!0123456789]*}"
if [ -L "${NVMRC_PATH}" ]; then
nvm_err "invalid .nvmrc! '${NVMRC_PATH}' is a symbolic link, so it must contain a version or an alias name; its content is not shown." nvm_err "invalid .nvmrc! '${NVMRC_PATH}' is a symbolic link, so it must contain a version or an alias name; its content is not shown."
return 1 return 1
fi fi
nvm_echo "${NVMRC_VERSION}" if [ -z "${NVMRC_INODE}" ] \
|| [ -z "$(command find "${NVMRC_FIND_PATH}" -prune -type f -links 1 -inum "${NVMRC_INODE}")" ]; then
nvm_err "invalid .nvmrc! '${NVMRC_PATH}' has another link, or was replaced while being read, so it must contain a version or an alias name; its content is not shown."
return 1
fi
nvm_process_nvmrc_content "${NVMRC_CONTENT}"
} }
nvm_rc_version() { nvm_rc_version() {
@@ -0,0 +1,78 @@
#!/bin/sh
die () { echo "$@" ; cleanup ; exit 1; }
cleanup() {
rm -rf "${TEST_DIR-}"
unset TEST_DIR SWAP_FROM SWAP_TO SWAP_DONE
}
: nvm.sh
\. ../../../nvm.sh
\. ../../common.sh
TEST_DIR="${PWD}/nvm_process_nvmrc_swap_tmp"
mkdir -p "${TEST_DIR}/home" "${TEST_DIR}/project" "${TEST_DIR}/bin" || die 'failed to create test dirs'
printf 'https://user:SECRET@github.com\n' > "${TEST_DIR}/home/.git-credentials"
NVMRC="${TEST_DIR}/project/.nvmrc"
# `cat` is the first command `nvm_process_nvmrc` runs:
# the mock swaps `.nvmrc` once, as another user who can write the directory could, then records its arguments
export SWAP_FROM SWAP_TO SWAP_DONE
SWAP_TO="${NVMRC}"
SWAP_DONE="${TEST_DIR}/swapped"
{
echo '#!/bin/sh'
printf '%s\n' '[ -e "${SWAP_DONE}" ] || { : > "${SWAP_DONE}"; command mv -f "${SWAP_FROM}" "${SWAP_TO}"; }'
} > "${TEST_DIR}/bin/cat"
for COMMAND in cat grep sed awk tr find ls; do
{
[ "${COMMAND}" = cat ] || echo '#!/bin/sh'
printf '%s\n' 'printf "%s\n" "$*" >> "${0%/*}/../arguments"'
echo 'PATH="${PATH#*:}"'
echo 'exec "${0##*/}" "$@"'
} >> "${TEST_DIR}/bin/${COMMAND}"
chmod +x "${TEST_DIR}/bin/${COMMAND}"
done
check() {
: > "${TEST_DIR}/arguments"
rm -f "${SWAP_DONE}"
CAPTURED_EXIT_CODE=0
CAPTURED_STDOUT="$(set +x; PATH="${TEST_DIR}/bin:${PATH}" nvm_process_nvmrc "${NVMRC}" 2> "${TEST_DIR}/stderr")" || CAPTURED_EXIT_CODE=$?
CAPTURED_STDERR="$(command cat "${TEST_DIR}/stderr")"
[ -e "${SWAP_DONE}" ] || die "$1: expected the mocked \`cat\` to swap the file"
case "${CAPTURED_STDOUT}${CAPTURED_STDERR}" in
*SECRET*) die "$1: expected the content to not be shown; got \`${CAPTURED_STDOUT}${CAPTURED_STDERR}\`" ;;
esac
! nvm_grep -q SECRET "${TEST_DIR}/arguments" || die "$1: expected the content to not be in a command's arguments; got \`$(nvm_grep SECRET "${TEST_DIR}/arguments")\`"
}
# a regular file, swapped for a symlink before it is read
rm -f "${NVMRC}"
printf 'v20.0.0\n' > "${NVMRC}"
SWAP_FROM="${TEST_DIR}/project/link"
ln -s ../home/.git-credentials "${SWAP_FROM}" || die 'failed to create the symlink'
check 'a regular file swapped for a symlink'
[ "${CAPTURED_EXIT_CODE}" = 0 ] || die "expected the regular file's version; got exit code ${CAPTURED_EXIT_CODE}"
[ "${CAPTURED_STDOUT}" = v20.0.0 ] || die "expected the regular file's version; got \`${CAPTURED_STDOUT}\`"
# a symlink, swapped for a regular file before it is checked
rm -f "${NVMRC}"
ln -s ../home/.git-credentials "${NVMRC}" || die 'failed to create the symlink'
SWAP_FROM="${TEST_DIR}/project/regular"
printf 'v20.0.0\n' > "${SWAP_FROM}"
check 'a symlink swapped for a regular file'
[ "${CAPTURED_EXIT_CODE}" != 0 ] || die "expected a symlink swapped for a regular file to be invalid; got exit code ${CAPTURED_EXIT_CODE}"
# a hard link to another file is treated like a symlink
rm -f "${NVMRC}"
ln "${TEST_DIR}/home/.git-credentials" "${NVMRC}" || die 'failed to create the hard link'
try_err nvm_process_nvmrc "${NVMRC}"
[ "${CAPTURED_EXIT_CODE}" != 0 ] || die "expected a hard link to be invalid; got exit code ${CAPTURED_EXIT_CODE}"
case "${CAPTURED_STDERR}" in
*SECRET*) die "expected a hard link's content to not be shown; got \`${CAPTURED_STDERR}\`" ;;
esac
cleanup