From 391a1ccde917f0bf2a0419b787f71c10467f5b6a Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Mon, 16 Nov 2026 15:12:18 -0800 Subject: [PATCH] [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. --- nvm.sh | 38 ++++++--- ... trust an .nvmrc replaced while being read | 78 +++++++++++++++++++ 2 files changed, 106 insertions(+), 10 deletions(-) create mode 100755 test/fast/Unit tests/nvm_process_nvmrc does not trust an .nvmrc replaced while being read diff --git a/nvm.sh b/nvm.sh index a1c1b47d..18c4d774 100755 --- a/nvm.sh +++ b/nvm.sh @@ -1041,21 +1041,39 @@ nvm_is_version_or_alias() { nvm_process_nvmrc() { local NVMRC_PATH 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 - nvm_process_nvmrc_content "$(command cat "${NVMRC_PATH}")" - return - 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 + # 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. + # 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. + local NVMRC_CONTENT local NVMRC_VERSION - if ! NVMRC_VERSION="$(nvm_process_nvmrc_content "$(command cat "${NVMRC_PATH}")" 2>/dev/null)" \ - || ! nvm_is_version_or_alias "${NVMRC_VERSION}"; then + local NVMRC_INODE + { + 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." return 1 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() { diff --git a/test/fast/Unit tests/nvm_process_nvmrc does not trust an .nvmrc replaced while being read b/test/fast/Unit tests/nvm_process_nvmrc does not trust an .nvmrc replaced while being read new file mode 100755 index 00000000..742ca6f6 --- /dev/null +++ b/test/fast/Unit tests/nvm_process_nvmrc does not trust an .nvmrc replaced while being read @@ -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