From 8df635740ec4ad64319cdd3b7f180d7639c54ca6 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Sun, 18 Oct 2026 23:07:54 -0700 Subject: [PATCH] [Fix] `nvm_remote_version`: match the requested version as a whole version component The check that the resolved version actually matches the request used an unanchored substring match on the raw pattern, so `nvm install 20` could accept `v10.20.0` when a remote list's row was selected by a `v20` in its LTS column and no 20.x rows were present. Match the `v`-prefixed pattern followed by a non-digit against the version instead, the same way `nvm_ls_remote_index_tab` normalizes it, so `20` no longer matches `v10.20.0` or `v200.0.0`, while regex-style requests like `4.*` and `v[68]`, and a trailing dot like `8.`, keep working. This also fixes `iojs-1` and `iojs-3`, which previously resolved to N/A because `iojs-3` is not a substring of `iojs-v3.3.1`. Discovered by Anthropic's OSS Scanner, as bug ANT-2026-7GFFYVJP; the scanner correctly classified it as not a vulnerability. --- nvm.sh | 3 +- test/fast/Unit tests/nvm_remote_version | 55 +++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/nvm.sh b/nvm.sh index 2855bbf7..5e7211f3 100755 --- a/nvm.sh +++ b/nvm.sh @@ -1023,7 +1023,8 @@ nvm_remote_version() { if [ -n "${PATTERN}" ] && [ "_${VERSION}" != "_N/A" ] && ! nvm_validate_implicit_alias "${PATTERN}" 2>/dev/null; then local VERSION_NUM VERSION_NUM="$(nvm_echo "${VERSION}" | command awk '{print $1}')" - if ! nvm_echo "${VERSION_NUM}" | nvm_grep -q "${PATTERN}"; then + # the `v` prefix and the trailing non-digit keep `20` from matching `v10.20.0` or `v200.0.0` + if ! nvm_echo "${VERSION_NUM} " | nvm_grep -q "$(nvm_ensure_version_prefix "${PATTERN%.}")[^0-9]"; then VERSION='N/A' fi fi diff --git a/test/fast/Unit tests/nvm_remote_version b/test/fast/Unit tests/nvm_remote_version index 11ceb983..314881b3 100755 --- a/test/fast/Unit tests/nvm_remote_version +++ b/test/fast/Unit tests/nvm_remote_version @@ -88,4 +88,59 @@ try nvm_remote_version 4 [ "_$CAPTURED_STDOUT" = "_v4.9.1" ] || die "nvm_remote_version 4 should return v4.9.1, got $CAPTURED_STDOUT" [ "_$CAPTURED_EXIT_CODE" = "_0" ] || die "nvm_remote_version 4 should exit with code 0, got $CAPTURED_EXIT_CODE" +# A version-like LTS field must not let a request match a different major line +nvm_remote_versions() { + echo "v10.20.0 v20 *" +} +try nvm_remote_version 20 +[ "_$CAPTURED_STDOUT" = "_N/A" ] || die "nvm_remote_version 20 should not match v10.20.0, got $CAPTURED_STDOUT" +[ "_$CAPTURED_EXIT_CODE" = "_3" ] || die "nvm_remote_version 20 should exit with code 3, got $CAPTURED_EXIT_CODE" + +try nvm_remote_version 10 +[ "_$CAPTURED_STDOUT" = "_v10.20.0 v20 *" ] || die "nvm_remote_version 10 should match v10.20.0, got $CAPTURED_STDOUT" +[ "_$CAPTURED_EXIT_CODE" = "_0" ] || die "nvm_remote_version 10 should exit with code 0, got $CAPTURED_EXIT_CODE" + +try nvm_remote_version 10.20 +[ "_$CAPTURED_STDOUT" = "_v10.20.0 v20 *" ] || die "nvm_remote_version 10.20 should match v10.20.0, got $CAPTURED_STDOUT" +[ "_$CAPTURED_EXIT_CODE" = "_0" ] || die "nvm_remote_version 10.20 should exit with code 0, got $CAPTURED_EXIT_CODE" + +nvm_remote_versions() { + echo "v10.0.0 v1 *" +} +try nvm_remote_version 1 +[ "_$CAPTURED_STDOUT" = "_N/A" ] || die "nvm_remote_version 1 should not match v10.0.0, got $CAPTURED_STDOUT" +[ "_$CAPTURED_EXIT_CODE" = "_3" ] || die "nvm_remote_version 1 should exit with code 3, got $CAPTURED_EXIT_CODE" + +nvm_remote_versions() { + echo "iojs-v3.3.1" +} +try nvm_remote_version iojs-3 +[ "_$CAPTURED_STDOUT" = "_iojs-v3.3.1" ] || die "nvm_remote_version iojs-3 should match iojs-v3.3.1, got $CAPTURED_STDOUT" +[ "_$CAPTURED_EXIT_CODE" = "_0" ] || die "nvm_remote_version iojs-3 should exit with code 0, got $CAPTURED_EXIT_CODE" + +nvm_remote_versions() { + echo "v8.17.0 Carbon" +} +try nvm_remote_version 'v[68]' +[ "_$CAPTURED_STDOUT" = "_v8.17.0 Carbon" ] || die "nvm_remote_version v[68] should match v8.17.0, got $CAPTURED_STDOUT" +[ "_$CAPTURED_EXIT_CODE" = "_0" ] || die "nvm_remote_version v[68] should exit with code 0, got $CAPTURED_EXIT_CODE" + +try nvm_remote_version 8. +[ "_$CAPTURED_STDOUT" = "_v8.17.0 Carbon" ] || die "nvm_remote_version 8. should match v8.17.0, got $CAPTURED_STDOUT" +[ "_$CAPTURED_EXIT_CODE" = "_0" ] || die "nvm_remote_version 8. should exit with code 0, got $CAPTURED_EXIT_CODE" + +nvm_remote_versions() { + echo "v4.9.1" +} +try nvm_remote_version '4.*' +[ "_$CAPTURED_STDOUT" = "_v4.9.1" ] || die "nvm_remote_version 4.* should match v4.9.1, got $CAPTURED_STDOUT" +[ "_$CAPTURED_EXIT_CODE" = "_0" ] || die "nvm_remote_version 4.* should exit with code 0, got $CAPTURED_EXIT_CODE" + +nvm_remote_versions() { + echo "v20.0.0-rc.1" +} +try nvm_remote_version v20.0.0-rc.1 +[ "_$CAPTURED_STDOUT" = "_v20.0.0-rc.1" ] || die "nvm_remote_version v20.0.0-rc.1 should match itself, got $CAPTURED_STDOUT" +[ "_$CAPTURED_EXIT_CODE" = "_0" ] || die "nvm_remote_version v20.0.0-rc.1 should exit with code 0, got $CAPTURED_EXIT_CODE" + cleanup