[readme] zsh and fish auto-switch hooks: let nvm parse .nvmrc

The zsh `load-nvmrc` and fish `load_nvm` examples passed `$(cat .nvmrc)` to `nvm version` and `nvm install`,
so a `.nvmrc` symlinked to any readable file put that file's content,
a whole one-line file with no `=` needed,
in the arguments of `find`, `sed`, and (for fish) `bass` and `bash`,
which every local user can read,
and tried to install it as a version.
Get the version from `nvm_process_nvmrc` instead, as the bash `cdnvm` example has since c314f1cb.

bass runs its arguments as bash code,
so the fish example also ran `.nvmrc` content like `v20$(touch x)`;
pass the path and version to bass in the environment instead.

Reported by Anthropic's OSS Scanner, alongside vulnerability ANT-2026-GG4I2LOU.
This commit is contained in:
Jordan Harband committed 2026-11-16 11:35:44 -08:00
1 parent e4fcf988c5
commit 5671e688d2
1 file changed
+14 -3
+14 -3
View File
@@ -866,8 +866,13 @@ load-nvmrc() {
nvmrc_path="$(nvm_find_nvmrc)"
if [ -n "$nvmrc_path" ]; then
local nvmrc_version
# Let nvm parse `.nvmrc`, so that comments are ignored and the content of an invalid
# or symlinked `.nvmrc` is not passed along to other commands
nvmrc_version="$(nvm_process_nvmrc "${nvmrc_path}")" || return
local nvmrc_node_version
nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")
nvmrc_node_version=$(nvm version "${nvmrc_version}")
if [ "$nvmrc_node_version" = "N/A" ]; then
nvm install
@@ -906,9 +911,15 @@ function load_nvm --on-variable="PWD"
set -l node_version (nvm version)
set -l nvmrc_path (nvm_find_nvmrc)
if test -n "$nvmrc_path"
set -l nvmrc_node_version (nvm version (cat $nvmrc_path))
# Let nvm parse `.nvmrc`, so that comments are ignored and the content of an invalid
# or symlinked `.nvmrc` is not passed along to other commands.
# bass runs its arguments as bash code, so pass the path and version in the environment.
set -lx NVMRC_PATH $nvmrc_path
set -lx NVMRC_VERSION (bass source ~/.nvm/nvm.sh --no-use ';' nvm_process_nvmrc '"$NVMRC_PATH"')
or return
set -l nvmrc_node_version (nvm version '"$NVMRC_VERSION"')
if test "$nvmrc_node_version" = "N/A"
nvm install (cat $nvmrc_path)
nvm install '"$NVMRC_VERSION"'
else if test "$nvmrc_node_version" != "$node_version"
nvm use $nvmrc_node_version
end