[Robustness] `nvm install`: reject a structurally broken installed version
Before adoption: this commit came in with the history the project adopted, so no landing records it.
`nvm_is_version_installed` only checks that `bin/node` has the execute bit, which a zero-byte binary and a dangling `npm` symlink both pass, so a partial install could be reported as a success — and a broken existing version could short-circuit `nvm install` as "already installed". Add `nvm_validate_install`, which requires a non-empty `bin/node` and an `npm` entry that resolves, and use it in two places: gate the "already installed" shortcut on it, so a broken version is reinstalled rather than reused, and re-check it after an install reports success, so a broken result fails loudly instead of being activated. It checks layout, not execution: a correctly installed binary can still fail to run on an incompatible host (e.g. a newer node on an older glibc), which is not a broken install, and a corrupt download is already rejected by the checksum check before extraction.
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+177 added−2 removed
- tests:+124 added−0 removed
- other:+53 added−2 removed
Provenance
- Commit
aee1f83- Tree
6c5f39e- Author
Jordan Harband <ljharb@gmail.com>- Committer
Jordan Harband <ljharb@gmail.com>- Parent
72a8784
3 files changed+177 added−2 removed
added:test/fast/Unit tests/nvm install verifies the installed version+71 added
1+#!/bin/sh2+3+cleanup() {4+ [ -n "${NVM_DIR}" ] && [ -d "${NVM_DIR}" ] && rm -rf "${NVM_DIR}"5+ unset -f die cleanup make_healthy make_broken \6+ nvm_remote_version nvm_has_executable nvm_binary_available \7+ nvm_install_binary nvm_install_source nvm_use_if_needed \8+ nvm_install_npm_if_needed nvm_install_default_packages nvm_ensure_default_set9+ unset NVM_DIR version version_path REINSTALL_MARKER OUTPUT EXIT_CODE10+}11+12+die() { echo "$@" ; cleanup ; exit 1; }13+14+: nvm.sh15+\. ../../../nvm.sh16+17+unset npm_config_prefix NPM_CONFIG_PREFIX18+19+NVM_DIR="$(mktemp -d)"20+[ -n "${NVM_DIR}" ] || die 'unable to create temp NVM_DIR'21+22+version='v20.0.0'23+version_path="${NVM_DIR}/versions/node/${version}"24+REINSTALL_MARKER="${NVM_DIR}/reinstalled"25+26+# Stay offline and keep the post-install activation path inert; only the27+# verification behaviour is under test here.28+nvm_remote_version() { nvm_echo "${version}"; }29+nvm_has_executable() { return 0; }30+nvm_binary_available() { return 0; }31+nvm_install_source() { return 1; }32+nvm_use_if_needed() { return 0; }33+nvm_install_npm_if_needed() { return 0; }34+nvm_install_default_packages() { return 0; }35+nvm_ensure_default_set() { return 0; }36+37+make_healthy() {38+ mkdir -p "${version_path}/bin"39+ printf '#!/bin/sh\necho "%s"\n' "${version}" > "${version_path}/bin/node"40+ chmod +x "${version_path}/bin/node"41+}42+make_broken() {43+ mkdir -p "${version_path}/bin"44+ : > "${version_path}/bin/node" # zero-byte but +x: passes nvm_is_version_installed45+ chmod +x "${version_path}/bin/node"46+}47+48+# 1) An install that reports success but leaves a broken node must fail loudly49+# and must not be activated.50+rm -rf "${version_path}"51+nvm_install_binary() { make_broken; return 0; }52+OUTPUT="$(nvm install -b "${version}" 2>&1)"; EXIT_CODE=$?53+[ "${EXIT_CODE}" != "0" ] || die "a broken install should fail; got exit 0, output >${OUTPUT}<"54+case "${OUTPUT}" in55+ *'failed verification'*) ;;56+ *) die "expected a verification-failure message; got >${OUTPUT}<" ;;57+esac58+59+# 2) An already-present but broken version must not short-circuit as "already60+# installed"; it must be reinstalled.61+rm -rf "${version_path}"62+make_broken63+nvm_install_binary() { command touch "${REINSTALL_MARKER}"; make_healthy; return 0; }64+OUTPUT="$(nvm install -b "${version}" 2>&1)"; EXIT_CODE=$?65+case "${OUTPUT}" in66+ *'is already installed'*) die "a broken version should not be treated as already installed: >${OUTPUT}<" ;;67+esac68+[ -f "${REINSTALL_MARKER}" ] || die 'a broken already-present version was not reinstalled'69+[ "${EXIT_CODE}" = "0" ] || die "the healing reinstall should succeed; got ${EXIT_CODE}, output >${OUTPUT}<"70+71+cleanupadded:test/fast/Unit tests/nvm_validate_install+53 added
1+#!/bin/sh2+3+cleanup() {4+ [ -n "${NVM_DIR}" ] && [ -d "${NVM_DIR}" ] && rm -rf "${NVM_DIR}"5+ unset -f die cleanup6+ unset NVM_DIR version version_path7+}8+9+die() { echo "$@" ; cleanup ; exit 1; }10+11+: nvm.sh12+\. ../../../nvm.sh13+14+type nvm_validate_install > /dev/null 2>&1 || die 'nvm_validate_install is not available'15+16+NVM_DIR="$(mktemp -d)"17+[ -n "${NVM_DIR}" ] || die 'Unable to create temporary folder'18+19+version='v20.0.0'20+version_path="${NVM_DIR}/versions/node/${version}"21+22+# No version directory at all: not valid.23+nvm_validate_install "${version}" 2>/dev/null && die 'a missing version dir should not validate'24+25+# A healthy install: a non-empty node plus an npm symlink that resolves.26+# (validation checks layout, not execution, so bin/node need not be a real27+# runnable binary here - just non-empty and executable.)28+mkdir -p "${version_path}/bin" "${version_path}/lib/node_modules/npm/bin" || die 'setup mkdir failed'29+printf '#!/bin/sh\necho "%s"\n' "${version}" > "${version_path}/bin/node"30+chmod +x "${version_path}/bin/node"31+echo 'npm-cli' > "${version_path}/lib/node_modules/npm/bin/npm-cli.js"32+( cd "${version_path}/bin" && ln -s ../lib/node_modules/npm/bin/npm-cli.js npm )33+nvm_validate_install "${version}" 2>/dev/null || die 'a healthy install should validate'34+35+# A binary that is correctly installed but cannot run on this host (e.g. a36+# newer node on an older glibc) must STILL validate: whether it runs is the37+# host's concern, not a broken install.38+printf '#!/bin/sh\nexit 1\n' > "${version_path}/bin/node"39+chmod +x "${version_path}/bin/node"40+nvm_validate_install "${version}" 2>/dev/null || die 'a non-runnable but present binary should still validate'41+42+# A zero-byte but executable node: passes `[ -x ]` but must not validate.43+: > "${version_path}/bin/node"44+chmod +x "${version_path}/bin/node"45+nvm_validate_install "${version}" 2>/dev/null && die 'a zero-byte node should not validate'46+47+# A non-empty node again, but with a dangling npm symlink: must not validate.48+printf '#!/bin/sh\necho "%s"\n' "${version}" > "${version_path}/bin/node"49+chmod +x "${version_path}/bin/node"50+rm -f "${version_path}/lib/node_modules/npm/bin/npm-cli.js"51+nvm_validate_install "${version}" 2>/dev/null && die 'a dangling npm symlink should not validate'52+53+cleanupmodified:nvm.sh+53 added-2 removed
216 unmodified lines217 fi218 return 1219 }220 221+# Sanity-check an installed version's layout: a non-empty, executable bin/node222+# and, if present, an npm entry that resolves. nvm_is_version_installed only223+# tests the bin/node exec bit, which a zero-byte binary and a dangling npm224+# symlink both pass; this catches those, so a partial install is not reported225+# as a success. It deliberately does NOT execute node: a correctly installed226+# binary can still fail to run on an incompatible host (e.g. a newer node on an227+# older glibc), which is not a broken install - and a corrupt download is228+# already rejected by the checksum check before extraction.229+nvm_validate_install() {230+ local VERSION231+ VERSION="${1-}"232+ if [ -z "${VERSION}" ]; then233+ return 1234+ fi235+236+ local VERSION_PATH237+ VERSION_PATH="$(nvm_version_path "${VERSION}" 2>/dev/null)"238+ if [ -z "${VERSION_PATH}" ] || [ ! -d "${VERSION_PATH}" ]; then239+ return 1240+ fi241+242+ local NVM_NODE_BINARY243+ NVM_NODE_BINARY='node'244+ if [ "_$(nvm_get_os)" = '_win' ]; then245+ NVM_NODE_BINARY='node.exe'246+ fi247+248+ local NVM_NODE_PATH249+ NVM_NODE_PATH="${VERSION_PATH}/bin/${NVM_NODE_BINARY}"250+ # A zero-byte file with the exec bit set still passes `[ -x ]` (the shell251+ # would run it as an empty script), so require a non-empty executable.252+ if [ ! -s "${NVM_NODE_PATH}" ] || [ ! -x "${NVM_NODE_PATH}" ]; then253+ nvm_err "The installed node binary at ${NVM_NODE_PATH} is missing or empty."254+ return 1255+ fi256+257+ # npm ships with every supported node/io.js version; if its entry is a258+ # symlink it must resolve (one pointing at a removed target counts as broken).259+ if [ -h "${VERSION_PATH}/bin/npm" ] && [ ! -e "${VERSION_PATH}/bin/npm" ]; then260+ nvm_err "npm for ${VERSION} is a dangling symlink."261+ return 1262+ fi263+264+ return 0265+}266+267 nvm_print_npm_version() {268 if nvm_has "npm"; then269 local NPM_VERSION270 NPM_VERSION="$(npm --version 2>/dev/null)"3591 unmodified lines3862 fi3863 3864 EXIT_CODE=03865 3820- if nvm_is_version_installed "${VERSION}"; then3866+ if nvm_is_version_installed "${VERSION}" && nvm_validate_install "${VERSION}"; then3867 nvm_err "${VERSION} is already installed."3868 nvm use "${VERSION}"3869 EXIT_CODE=$?3870 if [ $EXIT_CODE -eq 0 ]; then93 unmodified lines3964 fi3965 fi3966 fi3967 3968+ if [ $EXIT_CODE -eq 0 ] && ! nvm_validate_install "${VERSION}"; then3969+ nvm_err "The install of ${VERSION} reported success but failed verification; not activating it."3970+ EXIT_CODE=13971+ fi3972+3973 if [ $EXIT_CODE -eq 0 ]; then3974 if nvm_use_if_needed "${VERSION}" && nvm_install_npm_if_needed "${VERSION}"; then3975 if [ -n "${LTS-}" ]; then3976 nvm_ensure_default_set "lts/${LTS}"862 unmodified lines4839 nvm_auto nvm_supports_xz \4840 nvm_echo nvm_err nvm_grep nvm_cd \4841 nvm_die_on_prefix nvm_get_make_jobs nvm_get_minor_version \4842 nvm_has_solaris_binary nvm_is_merged_node_version \4792- nvm_is_natural_num nvm_is_version_installed \4843+ nvm_is_natural_num nvm_is_version_installed nvm_validate_install \4844 nvm_list_aliases nvm_make_alias nvm_print_alias_path \4845 nvm_print_default_alias nvm_print_formatted_alias nvm_resolve_local_alias \4846 nvm_sanitize_path nvm_has_colors nvm_process_parameters \4847 nvm_node_version_has_solaris_binary nvm_iojs_version_has_solaris_binary \181 unmodified lines