[Fix] nvm_normalize_lts, nvm_ls_remote_index_tab: keep untrusted text out of shell arithmetic and LTS aliases
`nvm_normalize_lts` matched `lts/-N` with a glob that only pins the first digit after `lts/-`, then evaluated the rest inside `$((N+1))`. bash and zsh evaluate a variable's contents as an arithmetic expression there, including command substitutions in array subscripts, so a name like `lts/-10*path[$(cmd)1]` ran `cmd`. Such a name reaches this function from `nvm alias`, `nvm use`, `nvm install`, and `.nvmrc`, and also from LTS alias files written from a mirror's `index.tab`, which are resolved whenever `lts/*` or that codename is used. The version column of a mirror's `index.tab` was used verbatim: printed by `nvm ls-remote` and `nvm version-remote`, matched against patterns, and stored as the target of `lts/*` codename aliases. A hostile or compromised mirror could thus persist such a payload into `$NVM_DIR/alias/lts/`, where it is replayed later by `nvm alias` and alias resolution without the mirror being involved, and could put terminal escape sequences on the user's screen. `nvm_normalize_lts` now takes everything after `lts/-` with parameter expansion and passes the name through unchanged unless it is all digits, before any arithmetic happens. `lts/-1` through `lts/-N`, and the error when there are not that many LTS lines, behave as before; malformed offsets like `lts/-1x` or `lts/-10+1` are now returned unchanged in every shell, instead of erroring in dash or being evaluated in bash and zsh. This also drops the `$?` check after the arithmetic, which could never fire. `nvm_ls_remote_index_tab` now skips the header and keeps only rows whose version is `vX.Y.Z`, optionally followed by a `-` prerelease suffix of letters, digits, `.` and `-`, before anything is printed, stored, or matched. The check runs on the raw column, before the `iojs-` prefix is added. Every row of the current nodejs.org, nodejs.org nightly, and iojs.org index files passes, including `rc`, `nightly`, and `v8-canary` versions. Discovered by Anthropic's OSS Scanner, as vulnerabilities ANT-2026-0GXXE91V and ANT-2026-31ZIEY85.
This commit is contained in:
3 files changed
+122
-10
No files matched your search
@@ -1483,13 +1483,17 @@ nvm_normalize_lts() {
|
||||
case "${LTS}" in
|
||||
lts/-[123456789] | lts/-[123456789][0123456789]*)
|
||||
local N
|
||||
N="$(echo "${LTS}" | cut -d '-' -f 2)"
|
||||
N="${LTS#lts/-}"
|
||||
# the glob above only pins the first digit; bash and zsh evaluate a variable's
|
||||
# contents as an expression inside `$(( ))`, including command substitutions,
|
||||
# so only a plain number may get there
|
||||
case "${N}" in
|
||||
*[!0123456789]*)
|
||||
nvm_echo "${LTS}"
|
||||
return 0
|
||||
;;
|
||||
esac
|
||||
N=$((N+1))
|
||||
# shellcheck disable=SC2181
|
||||
if [ $? -ne 0 ]; then
|
||||
nvm_echo "${LTS}"
|
||||
return 0
|
||||
fi
|
||||
local NVM_ALIAS_DIR
|
||||
NVM_ALIAS_DIR="$(nvm_alias_path)"
|
||||
local RESULT
|
||||
@@ -2742,11 +2746,10 @@ nvm_ls_remote_index_tab() {
|
||||
|
||||
nvm_is_zsh && setopt local_options shwordsplit
|
||||
local VERSION_LIST
|
||||
# the mirror's index.tab is untrusted: skip the header, and any row whose version
|
||||
# is malformed, before it is printed, stored as an LTS alias, or matched against a pattern
|
||||
VERSION_LIST="$(nvm_download -L -s "${MIRROR}/index.tab" -o - \
|
||||
| command sed "
|
||||
1d;
|
||||
s/^/${PREFIX}/;
|
||||
" \
|
||||
| command awk -v prefix="${PREFIX}" 'NR > 1 && $1 ~ /^v[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$/ { print prefix $0 }' \
|
||||
)"
|
||||
local LTS_ALIAS
|
||||
local LTS_VERSION
|
||||
|
||||
Reference in new issue
Block a user