[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:
Jordan Harband committed 2026-11-16 11:34:02 -08:00
1 parent ceef142f8a
commit e4fcf988c5
4 files changed
+101 -11

No files matched your search

+9 -8
View File
@@ -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%.}"