[Fix] nvm.sh: detect NVM_DIR without $0/$_; fall back to the default install location instead of the working directory
When `nvm.sh` is sourced with `NVM_DIR` unset,
it derived `NVM_DIR` from the sourced file in bash
(via `BASH_SOURCE`),
and otherwise from `dirname "${_:-$0}"`.
In shells that do not set `BASH_SOURCE`
(dash, sh, ksh, and zsh under `emulate sh` or without `FUNCTION_ARGZERO`),
`$0` is the shell's or the calling script's name and `$_` is whatever was inherited,
so `NVM_DIR` became an unrelated directory:
for `sh -c '. ~/.nvm/nvm.sh'`,
as run by `system(3)` or `popen(3)`,
that is the working directory,
whose `alias/default` and `versions/` nvm then used,
and from which `nvm cache clear` and friends would operate.
Detection now uses only reliable sources:
`BASH_SOURCE` in bash,
`${(%):-%x}` in zsh
(which names the sourced file regardless of options),
and `${.sh.file}` in ksh93
(probed in a subshell first, since other shells with `KSH_VERSION` reject the syntax)
A detected source that is not a regular file
(eg, `/dev/fd/63` from `. <(…)`)
is ignored.
Otherwise, nvm uses the default install location that `install.sh` uses
(`$XDG_CONFIG_HOME/nvm`, else `~/.nvm`)
when it contains `nvm.sh`, and if not,
it prints an error asking for `NVM_DIR` to be set and returns 1,
instead of guessing.
Setups that export `NVM_DIR` before sourcing `nvm.sh`,
as `install.sh` and the README do, are unaffected
Discovered by Anthropic's OSS Scanner, as ANT-2026-8RIT0MGO; the scanner reported it as a vulnerability, but it is not one.
This commit is contained in:
3 files changed
+97
-6
No files matched your search
@@ -10,9 +10,6 @@
|
||||
# shellcheck disable=SC2039,SC2016,SC2001,SC3043
|
||||
{ # this ensures the entire script is downloaded #
|
||||
|
||||
# shellcheck disable=SC3028
|
||||
NVM_SCRIPT_SOURCE="${_:-}"
|
||||
|
||||
nvm_is_zsh() {
|
||||
[ -n "${ZSH_VERSION-}" ]
|
||||
}
|
||||
@@ -589,12 +586,40 @@ fi
|
||||
|
||||
# Auto detect the NVM_DIR when not set
|
||||
if [ -z "${NVM_DIR-}" ]; then
|
||||
NVM_SCRIPT_SOURCE=''
|
||||
# shellcheck disable=SC2128
|
||||
if [ -n "${BASH_SOURCE-}" ]; then
|
||||
NVM_SCRIPT_SOURCE="${BASH_SOURCE}"
|
||||
elif nvm_is_zsh; then
|
||||
# `%x` names the file being sourced even when `$0` does not, eg under `emulate sh`
|
||||
eval 'NVM_SCRIPT_SOURCE="${(%):-%x}"'
|
||||
elif [ -n "${KSH_VERSION-}" ] && (eval ': "${.sh.file}"') 2>/dev/null; then
|
||||
# only ksh93 has `.sh.file`; other shells abort on the syntax, so it is probed in a subshell first
|
||||
eval 'NVM_SCRIPT_SOURCE="${.sh.file}"'
|
||||
fi
|
||||
if [ -n "${NVM_SCRIPT_SOURCE}" ] && [ -f "${NVM_SCRIPT_SOURCE}" ]; then
|
||||
# shellcheck disable=SC2086
|
||||
NVM_DIR="$(nvm_cd ${NVM_CD_FLAGS} "$(dirname "${NVM_SCRIPT_SOURCE}")" >/dev/null && \pwd)"
|
||||
else
|
||||
# other shells (eg, dash) cannot tell where a sourced file lives (`$0` names the shell or the calling script),
|
||||
# and a file sourced from a pipe (eg, `/dev/fd/63`) has no directory, so use the default install location, as `install.sh` does
|
||||
if [ -n "${XDG_CONFIG_HOME-}" ]; then
|
||||
NVM_DIR="${XDG_CONFIG_HOME}/nvm"
|
||||
else
|
||||
NVM_DIR="${HOME-}/.nvm"
|
||||
fi
|
||||
if [ -f "${NVM_DIR}/nvm.sh" ]; then
|
||||
# shellcheck disable=SC2086
|
||||
NVM_DIR="$(nvm_cd ${NVM_CD_FLAGS} "${NVM_DIR}" >/dev/null && \pwd)"
|
||||
else
|
||||
NVM_DIR=''
|
||||
fi
|
||||
fi
|
||||
if [ -z "${NVM_DIR}" ]; then
|
||||
unset NVM_DIR NVM_SCRIPT_SOURCE 2>/dev/null
|
||||
nvm_err 'nvm: unable to detect $NVM_DIR; set it to the directory containing nvm.sh before sourcing it'
|
||||
return 1
|
||||
fi
|
||||
# shellcheck disable=SC2086
|
||||
NVM_DIR="$(nvm_cd ${NVM_CD_FLAGS} "$(dirname "${NVM_SCRIPT_SOURCE:-$0}")" >/dev/null && \pwd)"
|
||||
export NVM_DIR
|
||||
else
|
||||
# https://unix.stackexchange.com/a/198289
|
||||
|
||||
Reference in new issue
Block a user