From 09e620c401382f4b3d3147333dd64c1e09188fdb Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Wed, 11 Nov 2026 15:23:25 -0800 Subject: [PATCH] [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. --- nvm.sh | 8 +++++++- .../nvm_download_artifact only caches verified archives | 9 ++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/nvm.sh b/nvm.sh index eec36dfa..e5d6ce56 100755 --- a/nvm.sh +++ b/nvm.sh @@ -3916,6 +3916,12 @@ nvm_download_artifact() { return 4 fi + # nothing can be verified without a checksum tool, so fail before touching the cache or downloading + if ! nvm_get_checksum_binary >/dev/null; then + nvm_err 'Install one of them so that nvm can verify downloads.' + return 6 + fi + local CHECKSUM CHECKSUM="$(nvm_get_checksum "${FLAVOR}" "${TYPE}" "${VERSION}" "${SLUG}" "${COMPRESSION}")" @@ -3941,7 +3947,7 @@ nvm_download_artifact() { fi nvm_compare_checksum "${TARBALL}" "${CHECKSUM}" nvm_err "Checksum check failed!" - nvm_err "Removing the broken local cache..." + nvm_err "Removing the unverified 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} diff --git a/test/fast/Unit tests/nvm_download_artifact only caches verified archives b/test/fast/Unit tests/nvm_download_artifact only caches verified archives index 6f4f23a4..1ff2779e 100755 --- a/test/fast/Unit tests/nvm_download_artifact only caches verified archives +++ b/test/fast/Unit tests/nvm_download_artifact only caches verified archives @@ -4,7 +4,7 @@ WORK="${TMPDIR:-/tmp}/nvm_download_artifact_cache.$$" cleanup () { command rm -rf "${WORK}" - unset -f cleanup die download_offline nvm_get_mirror nvm_binary_available nvm_get_download_slug nvm_get_artifact_compression nvm_cache_dir nvm_get_checksum nvm_version_greater_than_or_equal_to nvm_download nvm_compute_checksum nvm_compare_checksum + unset -f cleanup die download_offline nvm_get_mirror nvm_binary_available nvm_get_download_slug nvm_get_artifact_compression nvm_cache_dir nvm_get_checksum nvm_version_greater_than_or_equal_to nvm_download nvm_compute_checksum nvm_compare_checksum nvm_get_checksum_binary } die () { echo "$@" ; cleanup ; exit 1; } @@ -92,4 +92,11 @@ try nvm_download_artifact node binary std v20.0.0 [ ! -e "${TEST_TARBALL}.unverified" ] || die 'verified download: expected no unverified download to remain in the cache' [ ! -e "${WORK}/downloaded-to-final-name" ] || die 'an unverified download was written to the final cache name, where an interrupted download would be left for --offline' +# without any checksum tool, nothing is downloaded and the verified cache is left alone +nvm_get_checksum_binary() { return 1; } +try nvm_download_artifact node binary std v20.0.0 +[ "${CAPTURED_EXIT_CODE}" = '6' ] || die "no checksum tool: expected exit code 6, got ${CAPTURED_EXIT_CODE}" +[ "$(command cat "${TEST_TARBALL}")" = 'downloaded' ] || die "no checksum tool: expected the cached archive to be left alone, got >$(command cat "${TEST_TARBALL}")<" +[ ! -e "${TEST_TARBALL}.unverified" ] || die 'no checksum tool: expected nothing to be downloaded' + cleanup