[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.
This commit is contained in:
Jordan Harband committed 2026-10-16 22:02:45 -07:00
1 parent aa0572b7c4
commit 1f99e4c20b
3 files changed
+98 -6

No files matched your search

+17 -6
View File
@@ -3868,25 +3868,36 @@ nvm_download_artifact() {
nvm_err "Removing the broken local cache..."
command rm -rf "${TARBALL}"
fi
# `--offline` uses the cached archive without a checksum, so only a verified archive may ever sit at ${TARBALL}
local UNVERIFIED_TARBALL
UNVERIFIED_TARBALL="${TARBALL}.unverified"
command rm -f "${UNVERIFIED_TARBALL}"
nvm_err "Downloading $(nvm_sanitize_url "${TARBALL_URL}")..."
# stdout is reserved for the tarball path, so nothing the downloader prints may reach it
nvm_download -L -C - "${PROGRESS_BAR}" "${TARBALL_URL}" -o "${TARBALL}" >&2 || {
command rm -rf "${TARBALL}" "${tmpdir}"
nvm_download -L -C - "${PROGRESS_BAR}" "${TARBALL_URL}" -o "${UNVERIFIED_TARBALL}" >&2 || {
command rm -rf "${UNVERIFIED_TARBALL}" "${tmpdir}"
nvm_err "download from $(nvm_sanitize_url "${TARBALL_URL}") failed"
return 4
}
if nvm_grep '404 Not Found' "${TARBALL}" >/dev/null; then
command rm -rf "${TARBALL}" "${tmpdir}"
if nvm_grep '404 Not Found' "${UNVERIFIED_TARBALL}" >/dev/null; then
command rm -rf "${UNVERIFIED_TARBALL}" "${tmpdir}"
nvm_err "HTTP 404 at URL $(nvm_sanitize_url "${TARBALL_URL}")"
return 5
fi
nvm_compare_checksum "${TARBALL}" "${CHECKSUM}" || {
command rm -rf "${tmpdir}/files"
nvm_compare_checksum "${UNVERIFIED_TARBALL}" "${CHECKSUM}" || {
command rm -rf "${UNVERIFIED_TARBALL}" "${tmpdir}"
return 6
}
command mv -f "${UNVERIFIED_TARBALL}" "${TARBALL}" || {
command rm -rf "${UNVERIFIED_TARBALL}" "${tmpdir}"
nvm_err "moving the downloaded archive into the cache at $(nvm_sanitize_path "${TARBALL}") failed"
return 7
}
nvm_echo "${TARBALL}"
}