[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>
This commit is contained in:
NUAAwqyandJordan Harband authored and Jordan Harband committed 2026-11-06 03:13:03 +00:00
1 parent 015c4f1e3b
commit 362f5e2c71
4 files changed
+22 -6

No files matched your search

+1 -1
View File
@@ -540,7 +540,7 @@ nvm install --offline 14.7.0
This resolves versions using only locally installed versions and cached downloads. It will not attempt to download anything. This is useful in air-gapped environments, on planes, or when you want to avoid network latency. This resolves versions using only locally installed versions and cached downloads. It will not attempt to download anything. This is useful in air-gapped environments, on planes, or when you want to avoid network latency.
A download is only added to the cache (`$NVM_DIR/.cache`) once its checksum has been verified (unless no checksum tool is available, in which case `nvm` warns that it is continuing without verification). `--offline` does not verify cached archives again, so anything placed in the cache by other means is trusted as-is. A download is only added to the cache (`$NVM_DIR/.cache`) once its checksum has been verified. `--offline` does not verify cached archives again, so anything placed in the cache by other means is trusted as-is.
You can combine `--offline` with `--lts` to install the latest cached LTS version (as long as LTS aliases have been populated by a prior `nvm ls-remote --lts`): You can combine `--offline` with `--lts` to install the latest cached LTS version (as long as LTS aliases have been populated by a prior `nvm ls-remote --lts`):
+2 -2
View File
@@ -3003,8 +3003,8 @@ nvm_compare_checksum() {
if [ -z "${COMPUTED_SUM}" ]; then if [ -z "${COMPUTED_SUM}" ]; then
nvm_err "Computed checksum of '${FILE}' is empty." # missing in raspberry pi binary nvm_err "Computed checksum of '${FILE}' is empty." # missing in raspberry pi binary
nvm_err 'WARNING: Continuing *without checksum verification*' nvm_err 'Refusing to continue *without checksum verification*'
return return 1
elif [ "${COMPUTED_SUM}" != "${CHECKSUM}" ] && [ "${COMPUTED_SUM}" != "\\${CHECKSUM}" ]; then elif [ "${COMPUTED_SUM}" != "${CHECKSUM}" ] && [ "${COMPUTED_SUM}" != "\\${CHECKSUM}" ]; then
nvm_err "Checksums do not match: '${COMPUTED_SUM}' found, '${CHECKSUM}' expected." nvm_err "Checksums do not match: '${COMPUTED_SUM}' found, '${CHECKSUM}' expected."
return 1 return 1
+2 -2
View File
@@ -33,9 +33,9 @@ EXPECTED_OUTPUT='Provided checksum to compare to is empty.'
try_err nvm_compare_checksum ../../../nvm.sh checksum try_err nvm_compare_checksum ../../../nvm.sh checksum
EXPECTED_OUTPUT="Computed checksum of '../../../nvm.sh' is empty. EXPECTED_OUTPUT="Computed checksum of '../../../nvm.sh' is empty.
WARNING: Continuing *without checksum verification*" Refusing to continue *without checksum verification*"
[ "${CAPTURED_STDERR}" = "${EXPECTED_OUTPUT}" ] || die "expected >${EXPECTED_OUTPUT}<, got >${CAPTURED_STDERR}<" [ "${CAPTURED_STDERR}" = "${EXPECTED_OUTPUT}" ] || die "expected >${EXPECTED_OUTPUT}<, got >${CAPTURED_STDERR}<"
[ "${CAPTURED_EXIT_CODE}" = 0 ] || die "expected to exit with code 0, got ${CAPTURED_EXIT_CODE}" [ "${CAPTURED_EXIT_CODE}" = 1 ] || die "expected to exit with code 1, got ${CAPTURED_EXIT_CODE}"
nvm_compute_checksum() { nvm_compute_checksum() {
echo "not checksum: ${1}" echo "not checksum: ${1}"
@@ -4,7 +4,7 @@ WORK="${TMPDIR:-/tmp}/nvm_download_artifact_cache.$$"
cleanup () { cleanup () {
command rm -rf "${WORK}" 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_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
} }
die () { echo "$@" ; cleanup ; exit 1; } die () { echo "$@" ; cleanup ; exit 1; }
@@ -57,6 +57,22 @@ try download_offline
[ "${CAPTURED_EXIT_CODE}" = '4' ] || die "empty checksum: expected --offline to find no cached archive (exit code 4), got ${CAPTURED_EXIT_CODE}" [ "${CAPTURED_EXIT_CODE}" = '4' ] || die "empty checksum: expected --offline to find no cached archive (exit code 4), got ${CAPTURED_EXIT_CODE}"
nvm_get_checksum() { nvm_echo 'fake_checksum'; } nvm_get_checksum() { nvm_echo 'fake_checksum'; }
# a download whose checksum cannot be computed has not been verified, so it must not be accepted either
# (this still uses the real `nvm_compare_checksum`, so it runs before that is mocked)
nvm_compute_checksum() { nvm_echo ''; }
try nvm_download_artifact node binary std v20.0.0
[ "${CAPTURED_EXIT_CODE}" = '6' ] || die "unverifiable checksum: expected exit code 6, got ${CAPTURED_EXIT_CODE}"
[ ! -e "${TEST_CACHE_DIR}" ] || die "unverifiable checksum: expected ${TEST_CACHE_DIR} to be removed"
try download_offline
[ "${CAPTURED_EXIT_CODE}" = '4' ] || die "unverifiable checksum: expected --offline to find no cached archive (exit code 4), got ${CAPTURED_EXIT_CODE}"
# nor may a cached archive whose checksum cannot be computed be used
command mkdir -p "${TEST_CACHE_DIR}"
nvm_echo 'cached' > "${TEST_TARBALL}"
try nvm_download_artifact node binary std v20.0.0
[ "${CAPTURED_EXIT_CODE}" = '6' ] || die "unverifiable cached archive: expected exit code 6, got ${CAPTURED_EXIT_CODE}"
[ ! -e "${TEST_CACHE_DIR}" ] || die "unverifiable cached archive: expected ${TEST_CACHE_DIR} to be removed"
# a download that fails its checksum must not be left in the cache either # a download that fails its checksum must not be left in the cache either
nvm_compare_checksum() { return 1; } nvm_compare_checksum() { return 1; }
try nvm_download_artifact node binary std v20.0.0 try nvm_download_artifact node binary std v20.0.0