[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.
This commit is contained in:
Jordan Harband committed 2026-11-02 11:27:17 -06:00
1 parent 03f74a8666
commit 3f8411e58d
2 files changed
+55

No files matched your search

+7
View File
@@ -4376,6 +4376,13 @@ nvm_write_nvmrc() {
if [ "${VERSION_STRING}" = '∞' ] || [ "${VERSION_STRING}" = 'N/A' ]; then
return 1
fi
# writing through a symlink would overwrite its target, which can be any file
if [ -L "$PWD"/.nvmrc ]; then
if [ "${NVM_SILENT:-0}" -ne 1 ]; then
nvm_err "Warning: Not writing version number ($VERSION_STRING) to .nvmrc, because it is a symbolic link"
fi
return 3
fi
echo "${VERSION_STRING}" | tee "$PWD"/.nvmrc > /dev/null || {
if [ "${NVM_SILENT:-0}" -ne 1 ]; then
nvm_err "Warning: Unable to write version number ($VERSION_STRING) to .nvmrc"