516 Commits
Author SHA1 Message Date
Jordan HarbandandDivya Chukkapalli 62adf80079 [Fix] --lts=<name>: match codenames containing the name, not as a regex
`nvm_ls_remote_index_tab` compared the codename with awk's `!~`,
so the name was a regex: `--lts=.` matched every LTS line,
and `--lts=i.on` or `--lts=(` didn't mean what they say.
Use `index()` so a name still matches every codename that contains it,
but literally, and do the same in `nvm_filter_lts_versions`,
for `--lts=<name>` in an alias.

Locally, `--lts=<name>` resolved the `lts/<name>` alias,
which only exists for full codenames,
so `nvm use --lts=ir` failed where `nvm install --lts=ir` installed Iron.
Add `nvm_resolve_lts_name`,
which picks the newest codename in `$NVM_DIR/.lts-status` that contains the name,
and resolve `lts/<codename>` with it in `nvm use`, `uninstall`, `exec`, `run`,
and in `nvm install` for its offline lookup and the default and `--alias=` it writes.
Without a recorded status, or when no codename contains the name,
the name is used as given.

Signed-off-by: Divya Chukkapalli <12871391+divya0795@users.noreply.github.com>
Co-authored-by: Divya Chukkapalli <12871391+divya0795@users.noreply.github.com>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
2026-11-20 10:36:47 -08:00
Jordan Harbandanddajiaohuang dbda4b9387 [Fix] install.sh: update from nvm's remote; nvm_ls: list versions without globs; nvm_ensure_version_installed: do not leak VERSION
`install.sh`: an existing install now fetches from the remote whose URL is nvm's source,
adding one when none matches,
instead of always fetching `origin`;
fresh installs still name it `origin`.
The remote's URL is read with `git config`,
since `git remote get-url` needs git 2.7.

`nvm_ls`: `find` lists each version directory's non-hidden subdirectories itself,
so it works under `set -f`,
which disables the globs it was given;
the directories only need to exist.

`nvm_ensure_version_installed`:
the resolved alias was assigned to `VERSION` without declaring it local.

Co-authored-by: dajiaohuang <mikewushuwen@outlook.com>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
2026-11-19 22:51:28 -08:00
Jordan Harband b0a377a524 [Perf] nvm_ls: check whether a version directory has entries without forking
Each check ran `ls` piped into `grep` in a subshell,
which cost more than the `find` it guards;
a glob finds the same entries in the shell.
2026-11-19 16:05:28 -08:00
Hridhin Varkey Joseph 887c2969b7 [Perf] nvm_version: list installed versions once for node/stable
Once the newest installed version is 1.0 or later, `stable` is that version, so list the installed versions once and take the newest, instead of `nvm_ls stable` listing them three times.

Only 0.x installs (where `stable` is the newest even minor), a user alias named `stable`, and `--lts` filters keep the existing path. A system node, which `nvm_ls` lists last, is skipped.

Part of #3416.
2026-11-19 15:39:47 -08:00
hqzingandJordan Harband e8144d4194 [New] Add support for OpenHarmony platform
also: `nvm_get_os`: set `NVM_DIR` in the OpenHarmony chroots, and skip them without a glibc x86-64 layout

`sudo` resets the environment, and `nvm.sh` returns early when it cannot detect `NVM_DIR`,
so in the chroots `nvm_get_os` was undefined and every check got an empty result.
The chroot setup needs `ldd` and `/lib64/ld-linux-x86-64.so.2`, which macOS does not have.

Co-authored-by: hqzing <hqzing@outlook.com>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
2026-08-18 17:23:11 +08:00
Jordan Harband 944232943b [Fix] nvm_process_nvmrc_content: a key containing a space is not a duplicate of each word in it
The duplicate-key check kept keys in a space-separated list,
so a key such as `a b` followed by a key `a` or `b` was reported as a duplicate,
unlike in the `nvmrc` package.
Keep the keys newline-separated, since a key cannot contain a newline.
2026-11-18 16:20:53 -08:00
Divya Chukkapalli e8cfc3975d [Tests] nvm_process_nvmrc_content: compare .nvmrc keys literally
The duplicate-key check used to splice each key into an extended regex,
so distinct keys whose characters are regex syntax collided
(`node.dist` matched `node-dist`, `a+b` matched `ab`),
a key with `(` or `[` printed a grep error,
and a repeated `[x` key was not caught.
e4fcf988 now compares keys literally with a `case` pattern;
cover those cases.

Signed-off-by: Divya Chukkapalli <12871391+divya0795@users.noreply.github.com>
2026-11-18 11:59:49 -08:00
Jordan Harband ad42d90860 [Fix] nvm_ls_remote_index_tab, nvm_compare_checksum: also keep non-ASCII controls from the mirror off the terminal
Rejecting only ASCII control characters in the LTS codename still lets through a UTF-8 C1 control
(like U+009B, a CSI) or a bidi override (like U+202E),
which some terminals act on.
A row is now kept only if its codename is `-`, empty, or alias-safe,
which is the same shape the LTS alias step already requires,
and every real codename has.

BusyBox `tr` mishandles `-c` with a character class,
so on Alpine the expected checksum printed as almost all `?`;
an octal range in the C locale works in BusyBox, GNU, and BSD `tr`,
and does not make BSD `tr` abort on invalid UTF-8.
2026-11-16 15:13:13 -08:00
kasparovabi f3ad1d23b7 [Fix] nvm_ls_remote_index_tab, nvm_compare_checksum: keep control characters from the mirror off the terminal
`index.tab` and `SHASUMS256.txt` are untrusted mirror metadata.
An LTS codename (`$10`) carrying ESC/BEL bytes was printed by `nvm ls-remote`,
and a mismatched expected checksum was echoed as received.

Skip `index.tab` rows whose codename has a control character,
next to the existing version check,
and show non-printable bytes of the expected checksum as `?`.
2026-11-12 14:28:16 +03:00
Jordan Harband e837e16067 [Fix] run npm outside the project for nvm use, install-latest-npm, and global package installs
These also ran npm in `$PWD`, applying the untrusted project `.npmrc`.
npm v7 through v8.2 apply it even with `-g`,
so its `registry` chose where `nvm install-latest-npm`, `--latest-npm`, `default-packages`, and `reinstall-packages` downloaded globally-installed packages from, and their install scripts then ran.
npm v8.11 through v10.5 honor its `logs-dir` and `cache` for `npm --version`,
so every non-silent `nvm use` created those directories and wrote a debug log there.

Use `nvm_npm`, or `nvm_cd_root` where `xargs` runs npm.
`npm link` still runs in each linked package's own directory.
2026-11-16 15:31:01 -08:00
Jordan Harband f07f4ed8f3 [Fix] nvm debug: do not let a project .npmrc configure npm
`nvm debug` ran `npm config get prefix` in `$PWD`, so npm applied the untrusted project `.npmrc`.
Its `logs-dir` chose where npm wrote its debug log,
and npm 11 copies unknown keys into that log verbatim,
so `logs-dir=~/.bashrc.d` plus a `$(...)` key runs a command in the next shell on Fedora-style setups.
Its `userconfig` makes npm 11 echo the lines of any file into the log and into the `nvm debug` output.

`-g` is not enough: npm v7 through v8.2 load the project `.npmrc` even in global mode.
Add `nvm_npm`, which runs npm from `/`, and use it for both npm commands.
`$NVM_CD_FLAGS` is not used, since a zsh parent exports `-q` to bash and sh children.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-ZF4UWQNS.
2026-11-16 15:21:25 -08:00
Jordan Harband 2abfc380ad [Fix] nvm_download: pass a mirror URL with credentials to curl and GNU Wget on stdin, not as an argument
903e3493 moved `$NVM_AUTH_HEADER` out of the downloader's arguments,
but a mirror URL with `user:password@` in it was still one,
so any local user could read the password in `ps` or /proc/<pid>/cmdline
for every index.tab, SHASUMS, and tarball request.
curl now gets such a URL as `url = "..."` in the config it already reads with `--config -`,
with `"` and `\` escaped;
GNU Wget reads it with `--input-file=-`,
including in the same-origin redirect loop.
A URL with whitespace or a control character cannot be one config line, and stays an argument.
BusyBox wget has no way to take a URL other than as an argument; the README says so.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-MNDN98RD.
2026-11-16 15:15:25 -08:00
Jordan Harband cb7b66c729 [Fix] nvm_download_artifact: give each download a path no other process knows
Every download went to the fixed `<archive>.unverified`,
and curl opens its `-o` file only on the first body byte,
so a curl left running by an earlier install of the same version
(past the install lock via `nvm cache clear`, a stale-lock takeover, or a removed lock)
survived the `rm -f`, then opened the same file
and could rewrite it after the checksum passed, while `tar` read it,
and leave it in the cache that `--offline` trusts.

Downloading into a `mktemp -d` directory in the same cache dir
keeps the rename atomic and the file's mode following the umask.
Leftover `.unverified*` paths from interrupted installs are still removed first.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-XUXR5LQ4.
2026-11-16 15:09:20 -08:00
Jordan Harband f39a6cbc2d [Fix] nvm_write_nvmrc: don't let a symlink swapped in after the check be written through
The symlink check and the `tee` were two steps,
so anyone who can write `$PWD` could rename a symlink over `.nvmrc` in between,
and `nvm use --save`/`nvm install --save` would truncate any file the user can write.

Write the version in a private `mktemp -d` dir instead, and rename it into `$PWD/`:
a rename replaces a symlink rather than following it,
and moving into the directory (not onto `.nvmrc`) avoids landing inside a symlinked directory.
An existing `.nvmrc` must be a writable regular file, since a rename would also replace a read-only one.
`.nvmrc` is now a new file whose mode comes from the umask.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-EXZRTATX.
2026-11-16 15:08:32 -08:00
Jordan Harband 391a1ccde9 [Fix] nvm_process_nvmrc: check that the file read is the regular file at the path
`nvm_process_nvmrc` tested `[ ! -L ]` and then opened the path again with `cat`,
so another local user who can write the directory
could rename a symlink over `.nvmrc` in between (timed by `cat`'s exec),
and the target's content was printed and passed to `find` and `sed` as a version,
where that user could read it from the process list (67268ebc's check, bypassed).

sh can't open without following symlinks,
so read the file once, from one open,
and when its content is not a version or an existing alias,
show it or pass it on only if one `lstat` (`find -prune -type f -links 1 -inum`)
finds a regular file at the path with the open file's inode and no other links.
Anything else is handled as a symlink.
A hard link to another user's file, where the OS allows one, is refused the same way.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-WJUJDPM2.
2026-11-16 15:12:18 -08:00
Jordan Harband e4fcf988c5 [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.
2026-11-16 11:34:02 -08:00
ryenusandJordan Harband ceef142f8a [New] ls-remote, install, version-remote: add --min=
add CLI option --min=<version>, fallback to env var "NVM_MIN" if set.

The latter two pick the newest match,
which is below the minimum only when every match is,
so they check the resolved version.
`ls-remote` prints its notice on stderr, so the list stays parseable.

Other minor changes:
 - fix: disable color in nvm_print_versions tests
 - trim leading space in mock output due to eslint errors
 - ensure prefixed versions like v18 also work
 - remove function ref duplication
 - avoid inline initialization for ksh compatibility
 - apply `--min=` before checking for an empty list; require a version

Co-authored-by: ryenus <ryenus@gmail.com>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
2026-10-29 20:23:46 +08:00
Wei-Lun Li a2c0ebaec1 [Perf] nvm_ls: only look for a system node when the pattern can list it
`nvm_ls` lists the system version only for an empty pattern, `v`, or
`system`, but it checked for a system node/io.js for every pattern.
Each check runs `nvm deactivate` in a subshell, and a found system node
also runs `node -v`. So every partial-version lookup, like resolving a
`default` alias of `24` on shell startup, did that work and discarded it.

Refs #3425, #3416.

Signed-off-by: Wei-Lun Li <b84330808@gmail.com>
2026-11-14 22:28:35 +09:00
Amar 6294413afa [Fix] Report remote version index download failures
Check the downloader exit status before parsing index.tab, and report the failed mirror without exposing its credentials. Discard incomplete responses instead of treating them as successful version lists.

Fixes #673.
2026-11-13 13:15:55 +05:30
Jordan Harband 6f5767a530 [Fix] nvm_extract_tarball: don't let tar rewrite member names
`--strip-components 1` (and OpenBSD's `-s` equivalent)
rewrites each name after the extracting tar has checked it,
so with tars that have no `openat2` jail
(upstream GNU tar, busybox tar without its traversal protection)
a member named `node-v…//home/user/.bashrc` was written to `/home/user/.bashrc`,
and a hard link to `node-v…//abs/path` linked that file into the install tree,
both silently and with a zero exit.

Extract the archive as-is into a sibling staging directory,
require exactly one top-level entry that is a real directory,
and rename it into place;
dotfiles in the archive are kept.

busybox tar does not contain hard link targets at all
(absolute, `..`, or through an earlier symlink member),
so refuse any extracted file with more than one link.
No node.js or io.js archive contains a hard link.
2026-11-16 10:43:29 -08:00
Jordan Harband 09e620c401 [Fix] nvm_download_artifact: without a checksum tool, fail before touching the cache or downloading
Previously, every download was refused only after it was fetched,
a verified cached archive was deleted as "broken",
and the source fallback then downloaded a second archive that was refused the same way,
with no message saying what to install.
2026-11-11 15:23:25 -08:00
NUAAwqyandJordan Harband 362f5e2c71 [Breaking] nvm_compare_checksum: refuse a file whose checksum cannot be verified
The empty-computed-sum branch ended with a bare `return`,
whose status is that of the preceding `nvm_err`, which is 0.
Callers treat 0 as "verified",
so an artifact that could not be verified was installed anyway,
and was even moved into the verified cache slot,
violating nvm.sh's own invariant that only a verified archive may sit at ${TARBALL}
(a later `--offline` install would then use it without any verification).

Return 1 so the artifact is rejected, as intended by #1858.

Fixes #1858

Signed-off-by: NUAAwqy <146524024+NUAAwqy@users.noreply.github.com>
Co-authored-by: NUAAwqy <146524024+NUAAwqy@users.noreply.github.com>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
2026-11-06 03:13:03 +00:00
Iain 015c4f1e3b [Fix] install-latest-npm: npm v12 is out 2026-11-11 12:55:38 +00:00
Jordan Harband e1927f5096 [Tests] make fast tests pass as root, without sudo, and without a terminal
Six fast tests failed outside of CI's setup,
which runs them as a non-root user with `sudo` and a terminal.
`nvm_download_artifact error propagation` expected `mkdir -p` under `/nonexistent` to fail,
which root can create; it now uses a path under a regular file, which nobody can create.
`nvm_get_arch_unofficial` now only uses `sudo` when it is not already root,
and skips the chroot checks when it can do neither.
`nvm_write_nvmrc`'s permissions check and the `nvm uninstall` permissions test are skipped as root,
since root ignores file permissions.
`nvm_stdout_is_terminal`'s `/dev/tty` checks are skipped when there is no controlling terminal,
as in `docker run` without `-t`.
`nvm_process_nvmrc` now fails with a clear message when the `test/fixtures/nvmrc` submodule is not checked out,
instead of on an unexpanded glob.
This is not a vulnerability fix.
2026-11-04 14:12:38 -06:00
Jordan Harband d0ebdd3709 [Fix] nvm: treat a bare --only like an empty --only=
Every command that accepts `--only=<categories>` only recognized it with an `=`, so a bare `--only` got whatever an unknown argument gets, and no command said what was wrong with it.
`nvm ls`, `nvm ls-remote`, `nvm exec`, `nvm alias`, and `nvm version-remote` rejected it as an unsupported option; `nvm uninstall` and `nvm reinstall-packages` printed their usage; `nvm use`, `nvm which`, `nvm run`, and `nvm version` took it, or the version after it, as the version to look for; and `nvm install --only 20` printed grep's usage, while `nvm install 20 --only` ignored it.
A bare `--only` is now parsed as `--only=` with an empty value, so every command gives the same error for both: `--only` needs a comma-separated list of categories.
Aliases already reject such a value when they are created, so stored alias targets need no change.
2026-11-02 13:44:48 -06:00
Soujas K ed2de88826 [Fix] nvm_ls_remote, nvm_remote_version: match dots between version components literally
The version pattern went to grep with its dots unescaped,
so each one matched any character:
`20.1` also matched `v2041.0.0`,
and `20.1.1` matched `v20.111.1`,
so `nvm_remote_version 20.1.1` resolved to `v20.111.1`.

Escape each dot that is followed by a digit,
keeping any other regex syntax in the pattern, like `v[68]`.
2026-11-04 13:32:35 +05:30
Jordan Harband 699c898671 [Robustness] nvm_nvmrc_invalid_msg: only show an excerpt of the parsed content
The invalid `.nvmrc` message printed all of the file's parsed content, so a large file that is not really an `.nvmrc` was printed in full. It now shows at most the first 10 lines, each cut to 100 characters, and marks anything left out with `[...]`; a real `.nvmrc` is well within those limits.

This is hardening, not a vulnerability fix: since 67268ebc (the fix for ANT-2026-G9REVGKT), the content of a symlinked `.nvmrc` is never shown, so this message only ever shows the content of a regular `.nvmrc`.
Found by Anthropic's OSS Scanner while analyzing ANT-2026-G9REVGKT.
2026-10-17 22:38:06 -07:00
Jordan Harband cfb33c84ed [Fix] nvm_nvmrc_invalid_msg: respect --silent
The invalid `.nvmrc` message, which includes the file's parsed content, was printed even when `NVM_SILENT` was set (eg, by `nvm use --silent`), unlike nvm's other `.nvmrc` messages. It is now suppressed there too; the exit status still reports the invalid file.

This is hardening, not a vulnerability fix: since 67268ebc (the fix for ANT-2026-G9REVGKT), the content of a symlinked `.nvmrc` is never shown, so this message only ever shows the content of a regular `.nvmrc`.
Found by Anthropic's OSS Scanner while analyzing ANT-2026-G9REVGKT.
2026-10-17 22:38:05 -07:00
Jordan Harband 903e349369 [Fix] nvm_download: pass $NVM_AUTH_HEADER to curl and GNU Wget in a config file, not as an argument
The header was passed as a `--header` argument, so it was readable by anyone who could see the downloader's command line.

curl now reads it as `header = "…"` from a config on stdin (`--config -`); the sanitized value can contain neither `"` nor `\` nor a line break, so it cannot escape the quoted string or add another option.
GNU Wget only reads a config file that is a regular file, so a subshell writes the header to a `mktemp` file in `$TMPDIR` (mode 0600), after a copy of the user's `$WGETRC` or `~/.wgetrc`, which that file replaces, and points `WGETRC` at it;
traps on EXIT, HUP, INT, and TERM remove it, and `WGETRC` never reaches the user's shell.
BusyBox wget reads no config file and has no option for one, so it still gets the header as an argument; the README recommends curl on shared machines.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-540RL2U6.
2026-10-17 22:28:01 -07:00
Jordan Harband bce663e387 [Fix] nvm_ls_remote_index_tab, nvm_remote_version, nvm_ls_cached: pass the version pattern to grep with -e
The requested version pattern was passed to grep as a bare argument,
so a pattern starting with `-` was parsed as grep options instead.
That pattern can come from a `.nvmrc` in a repository the user cloned,
or, for implicit aliases like `node` and `stable`,
from the version column of the mirror's index.tab.
A pattern like `-Rohenpm_[A-Za-z0-9]*` made GNU grep ignore the version list on stdin and search the current directory recursively,
following symlinks, so a string from a local file
(such as an npm token in a `.npmrc`) was taken as the "version", printed,
and requested from the mirror in the download URLs;
since the version list was never read, this also worked with `--offline`.
Pass the pattern after `-e` in `nvm_ls_remote_index_tab`
(which also covers `nvm_ls_remote`, `nvm_ls_remote_iojs`, and `nvm_remote_versions`),
in the post-check in `nvm_remote_version`, and in `nvm_ls_cached`,
so a leading `-` is just part of a pattern that matches no version.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-EN9DM7WC.
2026-10-16 22:28:12 -07:00
Jordan Harband 4049296ca8 [Fix] nvm_die_on_prefix: never let a project .npmrc choose which npmrc file --delete-prefix edits
`nvm use --delete-prefix`, which nvm itself recommends when it finds a `prefix` or `globalconfig` setting, ran `npm config delete` from the current directory.
For a project `.npmrc`, it ran a bare `npm config delete prefix`/`globalconfig`, so npm loaded that untrusted file first, and its `userconfig`, `globalconfig`, or `location=global` keys picked which file npm rewrote, deleted, or chmodded to 0666 - eg, `~/.bashrc` or `~/.aws/credentials`.
The builtin and user branches pinned `--userconfig`, but still loaded the project file, so a project `location=global` plus an indented ` globalconfig=` (which nvm's check does not match) redirected those writes the same way.
`npm config delete` never edits the project file anyway, so that branch could not do what it claimed.

For the project npmrc, no longer run npm at all: with `--delete-prefix`, warn that project npmrc files are not modified and must be edited by hand, and continue, as before.
For the builtin, global, and user npmrc files, pass `--prefix` set to the version directory, so npm does not walk up from the current directory to load the project npmrc, and its global config resolves to that version's `etc/npmrc`.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-WYRTKBQ5.
2026-10-16 20:08:01 -07:00
Jordan Harband 1f99e4c20b [Fix] nvm_download_artifact: only put an archive in the cache once its checksum has passed
`nvm install --offline` installs whatever archive sits at the cache path without checking it again, which is only safe if everything nvm writes there has been verified.
When a download failed its checksum, `nvm_download_artifact` removed only the empty `files` directory and left the rejected archive in `$NVM_DIR/.cache`, so a later `nvm install --offline` would install the very tarball the online install had just refused.
An interrupted download was also left at the final cache name.

Download to a temporary `.unverified` name instead, run the 404 and checksum checks on that file, and move it to the cache path only once the checksum has passed.
Every failure path, including a checksum mismatch or a missing expected checksum, now removes the temporary file and the version's cache directory, so `--offline` no longer finds the version at all.
The README's offline section now says that only verified downloads are cached and that `--offline` trusts the cache as-is.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-7NCT51FE.
2026-10-16 22:02:45 -07:00
Jordan Harband aa0572b7c4 [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.
2026-10-16 21:35:44 -07:00
Yashvardhan Thanvi 6131a266a4 [Fix] nvm install: keep nvm options after the version out of ./configure
Options after the version went to `./configure` for a source install and were ignored for a binary install.

`--latest-npm`, `--default`, `--alias`, `--save` and `--no-progress` now also work after the version,
like `--reinstall-packages-from` already does.
`-s`, `-b`, `-j`, `--offline` and `--lts` are checked,
or used to find the version, before it is resolved:
after the version they now fail right away with a message to put them first,
and so do `---` typos.

Fixes #2066.
2026-11-04 00:37:28 +05:30
Yashvardhan Thanvi d190476345 [Fix] nvm install: apply --alias, --default and --save after every install
`nvm_install_binary` set the alias from its caller's variables,
so a source install or an `$NVM_INSTALL_THIRD_PARTY_HOOK` install never got it,
and no fresh install wrote `.nvmrc`.
Apply both at the end of `nvm install`, after the install is validated.

`--lts` without a version, or `lts/*`, leaves no provided version,
and `nvm alias` treats an empty target as `nvm unalias`:
`nvm install --lts --default` on an installed version removed the default alias.
In that case point the alias at `lts/<name>`, as the default alias already is.
2026-11-04 00:35:16 +05:30
Jordan Harband 00cc205d6f [Fix] nvm install: don't leave NVM_NO_PROGRESS set in the caller's shell
`nvm install` passed the progress setting to `nvm_install_binary` and `nvm_install_source` as a prefix assignment,
which bash 3.2 in POSIX mode (macOS's `sh`) keeps after a function call returns,
so one install left `NVM_NO_PROGRESS=0` set,
and every later `nvm install --no-progress` was ignored.

Set it as a local instead.
2026-11-03 16:46:55 -06:00
Jordan Harband 17270cd532 [Perf] nvm_print_implicit_alias: classify implicit aliases in one awk pass
To resolve `stable` and `unstable`,
`nvm_print_implicit_alias` started an awk process per installed minor version,
plus another per pre-1.0 one to check its parity.
Classify the whole list in a single awk pass instead,
and skip it when there is nothing to classify.
The results are unchanged.

Refs #3416. Refs #3811.
2026-11-03 14:42:50 -06:00
Wei-Lun Li 84b176d46d [Fix] nvm_get_default_packages: ignore CRLF line endings
A `default-packages` file saved with Windows line endings left a carriage return on every package name,
and turned each blank line into a lone `\r` entry, so `npm install -g` was given `rimraf\r` and failed.
Strip a trailing `\r` from each line before parsing it,
like `.nvmrc` parsing already tolerates.

Assisted-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Wei-Lun Li <b84330808@gmail.com>
2026-11-02 01:47:37 +09:00
NUAAwqyandJordan Harband 7ab48a653b [Fix] nvm_print_implicit_alias: keep STABLE bound, and STABLE and UNSTABLE independent of the caller
`STABLE` is only assigned inside the loop over `LAST_TWO`,
but it is echoed unconditionally afterwards.
With nothing installed, or with only an odd minor,
the loop never runs, so under `set -u` `nvm ls` dies on
`STABLE: unbound variable` instead of resolving `stable` to nothing.

`local STABLE` without an initializer also keeps the caller's value in sh and dash,
so `STABLE=x nvm ls stable` resolves the alias to something that isn't installed.
Initializing it where it is declared fixes both,
the way 139ed9c did for `PATTERN`, `NVM_LTS`, `LTS` and `VERSION`.
`local UNSTABLE` keeps the caller's value the same way,
which its `N/A` fallback doesn't cover, so it is initialized too.

The nounset test now covers the two states that leave `stable` unresolved,
and the caller test that `nvm ls stable` answers the same either way,
and that an outer `UNSTABLE` doesn't become the `unstable` alias.

Co-authored-by: NUAAwqy <146524024+NUAAwqy@users.noreply.github.com>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
2026-10-29 17:08:50 +00:00
NUAAwqy c8dc8c9b1c [Fix] nvm_ls: treat a version dir with only hidden entries as empty
`nvm_ls` decides whether a version dir is worth searching with `ls -1qA`, which
also counts dotfiles, but then collects versions with `find "${DIR}"/*`, which
never matches them. A dir holding nothing but hidden entries passes the guard,
so when `nullglob` is not set `find` is handed the literal `*` and `nvm ls`
prints "No such file or directory" to stderr.

Drop `-A` from the guard so both halves agree on what counts as an entry.

Fixes #3727.
2026-10-29 16:59:39 +00:00
NUAAwqy c6bf252f7d [Fix] ls: list versions whose directory is a symlink
nvm_ls collects installed versions with `find <dirs>/* -name . -o -type d -prune -o -path ...`,
and `-type d` is false for a symlink,
so a version dir that links to another volume was skipped from the listing entirely
- while `nvm version`, `nvm which` and `nvm use` all kept working on it.

Have that find follow symlinks,
which makes linked-in versions visible and still ignores a broken link.

Fixes #617.
2026-10-29 14:59:29 +00:00
Wei-Lun Li 970ed9ea18 [Fix] nvm_die_on_prefix: check a nonexistent prefix on Windows
On Windows, `npm_config_prefix` is resolved with `cd` + `pwd` so it can be compared with `$NVM_DIR`.
When the directory does not exist yet, that resolution fails,
the value becomes empty, and the check is skipped.
Keep the value as given in that case, like on other systems.

Assisted-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Wei-Lun Li <b84330808@gmail.com>
2026-10-29 09:48:45 +09:00
Jordan Harband 67268ebcb6 [Fix] nvm_process_nvmrc: don't show the content of a symlinked .nvmrc
`.nvmrc` is found and read through symlinks,
so a repository that commits `.nvmrc` as a symlink to a path outside itself
(eg, `../../.npmrc`)
made a plain `nvm use`, `nvm install`, or `nvm which`
(or `nvm-exec`, or the README's auto-switching shell hook)
print that file:
the invalid `.nvmrc` message shows everything that was parsed,
even with `--silent`,
and a one-line file was taken as the version,
which is printed in the "Found" message and in later errors.

A symlinked `.nvmrc` is still followed,
so linking to another version file
(eg, `.node-version`, or a parent directory's `.nvmrc`)
keeps working,
but its content is never shown,
and it is valid only if it names a version or an existing alias,
via a new `nvm_is_version_or_alias` helper.
2026-11-02 11:34:39 -06:00
Jordan Harband 3f8411e58d [Fix] nvm_write_nvmrc: don't write through a symlinked .nvmrc
`nvm use --save` and `nvm install --save` wrote the version to `$PWD/.nvmrc` with `tee`,
which follows a symlink,
so a repository that commits `.nvmrc` as a symlink to a path outside itself made `--save` truncate any file the user can write
(eg, a shell startup file or `~/.ssh/authorized_keys`)
and replace it with the version string,
or create the target of a dangling link.
The link itself was left in place, so `git status` stayed clean.

`nvm_write_nvmrc` now refuses, with a warning, when `.nvmrc` in the current directory is a symlink,
dangling or not.
2026-11-02 11:27:17 -06:00
Jordan Harband 03f74a8666 [Fix] nvm_ls: match NVM_DIR literally
`nvm_ls` strips `$NVM_DIR` and the io.js version dir from each installed version's path with sed,
but put both into the regex as-is,
so an `NVM_DIR` containing a backslash, `[`, or `*` listed no installed versions,
and one containing `#` made sed fail.

Escape both before use, as `SEARCH_PATTERN` already is.
2026-11-02 13:53:00 -06:00
maliv2 bfabdbc278 [Fix] nvm use: do not pile up PATH entries in nested sessions
`nvm_change_path` prepends the new version
(instead of replacing the existing nvm path)
when a system bin dir precedes an nvm path,
to handle macOS `path_helper` reordering the `PATH` of nested sessions
(#1652). However, the check matched a system dir followed by *any* nvm path.
Once a stale nvm path sits after the system dirs
(e.g. inherited by a nested shell, tmux, or an editor's terminal),
the check stays true forever,
so every `nvm use` prepends yet another entry and the previously used version is never removed from the `PATH`.

Only consider the first nvm path:
if it is not preceded by a system bin dir,
replace it in place as usual.

Fixes #3561
2026-10-14 20:53:08 +03:00
Jordan Harband 16df5aa69a [Fix] nvm_filter_installed_versions, nvm_filter_lts_versions, nvm_record_lts_status, nvm_get_default_packages: keep backslashes in NVM_DIR paths
These passed a path under `$NVM_DIR` to awk with `-v`,
which processes backslash escapes,
so with a backslash in `NVM_DIR` awk opened the wrong file:
the LTS status record read as empty,
so `nvm_filter_installed_versions` treated every version as non-LTS
and `nvm_filter_lts_versions` matched none,
`nvm_record_lts_status` could not merge an index without LTS versions into the record,
and `nvm_get_default_packages` named the wrong file in its error.

The LTS functions now pass the path through the environment,
like `nvm_strip_path` and `nvm_replace_literal`;
`nvm_get_default_packages` uses awk's `FILENAME`, which is taken literally.
2026-11-02 12:30:15 -06:00
Mola-maker 8486d07a5e [Fix] nvm_strip_path: preserve literal backslashes in NVM_DIR 2026-11-02 12:16:01 -06:00
Daniel OriyanandClaude Opus 5.5 ea84cb311e [Fix] nvm_find_up, nvm_find_project_dir: stop at a Windows drive root instead of looping forever
With a Windows-style `$PWD` (e.g. `D:/Work`, as Git Bash inherits when launched
by a native Windows program), `${path_%/*}` stops changing at `D:`, so the walk
never ends and sourcing `nvm.sh` hangs. Stop when going up doesn't change the
path, which also covers the existing `.` case.

Fixes #3967.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RBGzjdkmgq6LSA7SFtjRsY
2026-10-23 13:54:04 +03:00
Shuyun Yang 13eee46e7d [Fix] nvm_ls_cached: match dots in the version literally, and ignore a trailing dot
The pattern reached `grep` as a regex, so its dots matched any character: `20.1.1` also matched a cached `v20.111.1`, which `nvm install --offline 20.1.1` could then pick as the newest match.
A trailing dot, as in `20.1.`, likewise matched `v20.10.0` but not `v20.1.x`.
A bare `v`, `iojs-`, or `iojs-v` now matches every cached node or io.js version respectively.
2026-10-27 20:08:55 -07:00