Commit Graph
543 Commits
Author SHA1 Message Date
Jordan Harband 08763e43ef [Tests] nvm_list_aliases: add unit tests 2026-11-30 18:07:24 -08:00
Jordan Harband aa20d996b6 [Tests] nvm_is_zsh: add unit tests 2026-11-30 18:07:24 -08:00
Jordan Harband 4b12bf4a90 [Tests] nvm_is_version_selected: add unit tests 2026-11-30 18:07:24 -08:00
Jordan Harband 866a9d64c3 [Tests] nvm_is_version_or_alias: add unit tests 2026-11-30 18:07:23 -08:00
Jordan Harband fb17d36f31 [Tests] nvm_is_below_min_version: add unit tests 2026-11-30 18:07:23 -08:00
Jordan Harband 43a4ea2a12 [Tests] nvm_install_npm_if_needed: add unit tests 2026-11-30 18:07:23 -08:00
Jordan Harband e27de13dfa [Tests] nvm_grep: add unit tests 2026-11-30 18:07:23 -08:00
Jordan Harband e3cc267387 [Tests] nvm_get_remote_aliases: add unit tests 2026-11-30 18:07:22 -08:00
Jordan Harband 10dada17da [Tests] nvm_find_nvmrc: add unit tests 2026-11-30 18:07:22 -08:00
Jordan Harband 0527e34f99 [Tests] nvm_filter_min_versions: add unit tests 2026-11-30 18:07:22 -08:00
Jordan Harband b2ad63095f [Tests] nvm_filter_lts_versions: add unit tests 2026-11-30 18:07:21 -08:00
Jordan Harband 6bdf17fb4d [Tests] nvm_explain_excluded_version: add unit tests 2026-11-30 18:07:21 -08:00
Jordan Harband 300ab22dfb [Tests] nvm_err_with_colors: add unit tests 2026-11-30 18:07:21 -08:00
Jordan Harband 4c227362a4 [Tests] nvm_ensure_lts_status: add unit tests 2026-11-30 18:07:20 -08:00
Jordan Harband a5eef5568e [Tests] nvm_echo_with_colors: add unit tests 2026-11-30 18:07:20 -08:00
Jordan Harband 8baffe92e0 [Tests] nvm_cd_root: add unit tests 2026-11-30 18:07:20 -08:00
Jordan Harband 7773328bb5 [Tests] nvm_cache_dir: add unit tests 2026-11-30 18:07:19 -08:00
Jordan Harband c4bb986185 [Tests] nvm_binary_available: add unit tests 2026-11-30 18:07:19 -08:00
Oikantik Basu 74544c12f4 [Fix] nvm install: explain native Windows curl write failures
Refs #3391.
2026-11-24 19:09:58 +05:30
Hridhin Varkey Joseph 41041a99ca [Tests] nvm_check_file_permissions: symlinked directories and cycles are not traversed 2026-11-22 22:41:58 +05:30
thejesh23 35519ec047 [Fix] nvm reinstall-packages: fail when the version is not installed
`nvm reinstall-packages 99` resolved the version to `N/A` and kept going:
it told the user to run `nvm install N/A`,
printed "Reinstalling global packages from N/A..." and exited 0.
Only the case of an installed version excluded by `--only` returned early.

Now any unresolved version prints "Version '99' is not installed." and returns 3,
like `nvm install --reinstall-packages-from=`,
which already requires an installed version.

Assisted-by: Claude Code <noreply@anthropic.com>
Signed-off-by: thejesh23 <35212698+thejesh23@users.noreply.github.com>
2026-11-22 08:26:23 -08:00
NUAAwqyandJordan Harband a66a5ae5d2 [Fix] nvm.sh: use NVM_-prefixed locals in the sourcing path
Sourcing nvm.sh fails when the caller has made readonly a variable that nvm.sh declares as a local,
such as `readonly VERSION` in a CI build script:
bash rejects the `local`, and dash exits.

Prefix the locals that sourcing and its `nvm use` reach.
The test derives the unprefixed local names from nvm.sh,
so a new collision fails it.

`nvm_install_default_packages` reported an invalid default-packages file by assigning `nvm`'s `EXIT_CODE` without declaring it.
It now declares its own, and `nvm install` uses its return status instead,
except 3, which means npm failed to install the packages and,
as before, does not fail the install.

Fixes #3584

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-28 06:30:00 +00:00
585b6ec915 [New] add prune command
`nvm prune` uninstalls every version except the newest install per major
(per minor with `--minor`, and always per minor for `v0.x`),
the current version, the `$NVM_DIR/current` symlink target,
and anything an alias resolves to.
It lists what it will remove and asks first;
`--dry-run` only lists, and `--force` skips the prompt.

Co-authored-by: Guillermo Esguerra <gesguerrab@gmail.com>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
2026-02-23 13:21:38 -05:00
Peter Dave Hello eb0c8b95c8 [Tests] Verify explicit make jobs without redundant source builds
Exercise both argument orders and multiple job counts against the existing fake source fixture,
checking the actual build and install make arguments.
Use an isolated offline cache and a conflicting inherited job count to catch regressions that a successful real compilation alone cannot detect.

Keep a real source build with explicit jobs in each installation suite,
but remove the redundant serial compilation and make installation failures and version mismatches fail the test immediately.
2026-11-26 03:13:13 +08:00
Peter Dave HelloandJordan Harband bea27fbe0f [New] bash_completion: complete options and arguments in context
Parse words before the cursor once to offer flags, versions, and aliases
in the positions that accept them. Distinguish install options before
and after the version, and leave run and exec arguments to the shell.

Match local names literally. Account for Readline splitting at `=` and
quoting common prefixes. Give zsh the same candidates as raw strings so
its line editor can match, quote, and close arguments in its own syntax.

Check the offered flags against nvm help and cover argument boundaries,
including the actual arguments submitted by zsh after Tab completion.

Co-authored-by: Peter Dave Hello <hsu@peterdavehello.org>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
2026-11-25 13:23:26 -08:00
Jordan Harband 3bf6686090 [Robustness] handle an overridden type
A `type` function or alias defined before nvm is sourced
hijacked `nvm_has` and `nvm_command_info`.
`nvm_has` now uses `command -v`;
`nvm_command_info` needs `type`'s output,
so it uses `builtin type` in zsh,
whose `command type` runs an external `type` that can't see shell functions.

Since `command -v` makes dash hash the command,
`nvm_command_info` also parses dash's and ksh's "tracked alias" output.

Fixes #2352.
2020-11-21 13:33:12 -08:00
thejesh23 adf35d7697 [Fix] nvm_get_default_packages: ignore leading and trailing blanks
A trailing space (or tab) left by an editor, or an indented line,
in `$NVM_DIR/default-packages` tripped the "multiple space-separated values" check,
so the whole file was rejected and no default packages were installed after `nvm install`,
with an error naming the wrong problem.
Trim blanks around each line before checking it,
as is already done for a trailing `\r`;
a line with two values is still rejected.

Assisted-by: Claude Code <noreply@anthropic.com>
Signed-off-by: thejesh23 <35212698+thejesh23@users.noreply.github.com>
2026-11-22 08:26:23 -08:00
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