[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() {
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() {