[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.
This commit is contained in:
2 files changed
+101
-2
No files matched your search
@@ -4643,12 +4643,25 @@ nvm_write_nvmrc() {
|
||||
fi
|
||||
return 3
|
||||
fi
|
||||
echo "${VERSION_STRING}" | tee "$PWD"/.nvmrc > /dev/null || {
|
||||
# a symlink can be swapped in after that check, so never open "$PWD"/.nvmrc: rename a private file into "$PWD"/,
|
||||
# which replaces a symlink instead of following it. A rename would also replace a read-only .nvmrc, hence that check.
|
||||
local NVMRC_TMPDIR
|
||||
NVMRC_TMPDIR=''
|
||||
if { [ ! -e "$PWD"/.nvmrc ] || { [ -f "$PWD"/.nvmrc ] && [ -w "$PWD"/.nvmrc ]; }; } \
|
||||
&& NVMRC_TMPDIR="$(command mktemp -d "${NVM_DIR}/.nvmrc.XXXXXX" 2>/dev/null || command mktemp -d "${TMPDIR:-/tmp}/nvm-nvmrc.XXXXXX" 2>/dev/null)" \
|
||||
&& nvm_echo "${VERSION_STRING}" > "${NVMRC_TMPDIR}/.nvmrc" \
|
||||
&& command mv -f "${NVMRC_TMPDIR}/.nvmrc" "$PWD"/ 2>/dev/null; then
|
||||
command rmdir "${NVMRC_TMPDIR}"
|
||||
else
|
||||
if [ -n "${NVMRC_TMPDIR}" ]; then
|
||||
command rm -f "${NVMRC_TMPDIR}/.nvmrc"
|
||||
command rmdir "${NVMRC_TMPDIR}"
|
||||
fi
|
||||
if [ "${NVM_SILENT:-0}" -ne 1 ]; then
|
||||
nvm_err "Warning: Unable to write version number ($VERSION_STRING) to .nvmrc"
|
||||
fi
|
||||
return 3
|
||||
}
|
||||
fi
|
||||
if [ "${NVM_SILENT:-0}" -ne 1 ]; then
|
||||
nvm_echo "Wrote version number ($VERSION_STRING) to .nvmrc"
|
||||
fi
|
||||
|
||||
Reference in new issue
Block a user