diff --git a/nvm.sh b/nvm.sh index 7417db2b..7aaed0ed 100755 --- a/nvm.sh +++ b/nvm.sh @@ -2822,9 +2822,14 @@ nvm_ls_remote_index_tab() { nvm_is_zsh && setopt local_options shwordsplit local VERSION_LIST + VERSION_LIST="$(nvm_download -L -s "${MIRROR}/index.tab" -o -)" || { + nvm_err "Failed to download version list from $(nvm_sanitize_url "${MIRROR}/index.tab")" + nvm_echo 'N/A' + return 3 + } # the mirror's index.tab is untrusted: skip the header, and any row whose version # is malformed, before it is printed, stored as an LTS alias, or matched against a pattern - VERSION_LIST="$(nvm_download -L -s "${MIRROR}/index.tab" -o - \ + VERSION_LIST="$(nvm_echo "${VERSION_LIST}" \ | command awk -v prefix="${PREFIX}" 'NR > 1 && $1 ~ /^v[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$/ { print prefix $0 }' \ )" local LTS_ALIAS diff --git a/test/fast/Unit tests/nvm_ls_remote reports download failures b/test/fast/Unit tests/nvm_ls_remote reports download failures new file mode 100755 index 00000000..85f7fb82 --- /dev/null +++ b/test/fast/Unit tests/nvm_ls_remote reports download failures @@ -0,0 +1,60 @@ +#!/bin/sh +# Mocks are called indirectly by the sourced nvm functions. +# shellcheck disable=SC2329 + +die () { echo "$@"; exit 1; } + +: nvm.sh +\. ../../../nvm.sh --no-use +\. ../../common.sh + +nvm_record_lts_status() { :; } +export NVM_NODEJS_ORG_MIRROR='https://nodejs.org/dist' +export NVM_IOJS_ORG_MIRROR='https://iojs.org/dist' + +# A failed request may have already written part of index.tab to stdout. +# Neither an empty response nor a partial one should look like a successful list. +for FLAVOR in node iojs; do + for PARTIAL in no yes; do + nvm_download() { + if [ "${PARTIAL}" = yes ]; then + cat "${PWD}/mocks/nodejs.org-dist-index.tab" + fi + return 60 + } + + try nvm_ls_remote_index_tab "${FLAVOR}" std '' + [ "${CAPTURED_EXIT_CODE}" = 3 ] || die "${FLAVOR}: failed download should return 3" + [ "${CAPTURED_STDOUT}" = 'N/A' ] || die "${FLAVOR}: failed download must not print partial versions" + + MIRROR="$(nvm_get_mirror "${FLAVOR}" std)" + try_err nvm_ls_remote_index_tab "${FLAVOR}" std '' + [ "${CAPTURED_STDERR}" = "Failed to download version list from ${MIRROR}/index.tab" ] \ + || die "${FLAVOR}: missing download error: ${CAPTURED_STDERR}" + done +done + +# A working mirror should still provide its versions when the other one fails, +# but the failure must remain visible on stderr. +nvm_download() { + case "$*" in + *https://nodejs.org/dist/index.tab*) return 60 ;; + *) cat "${PWD}/mocks/iojs.org-dist-index.tab" ;; + esac +} +try nvm_remote_versions +[ "${CAPTURED_EXIT_CODE}" = 3 ] || die 'partial list should preserve the failure status' +[ "${CAPTURED_STDOUT}" = "$(cat "${PWD}/mocks/nvm_ls_remote_iojs.txt")" ] \ + || die 'the working io.js mirror should still provide its versions' +try_err nvm_remote_versions +[ "${CAPTURED_STDERR}" = 'Failed to download version list from https://nodejs.org/dist/index.tab' ] \ + || die 'partial list should report the failed mirror' + +# Custom mirrors may contain credentials; the new diagnostic must redact them. +export NVM_NODEJS_ORG_MIRROR='https://user:password@example.com/dist' +nvm_download() { return 60; } +try_err nvm_ls_remote +[ "${CAPTURED_STDERR}" = 'Failed to download version list from https://***@example.com/dist/index.tab' ] \ + || die 'the download error should redact mirror credentials' + +unset -f nvm_download nvm_record_lts_status