Skip to content
Myr

[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.

authored by Jordan Harband · committed by Jordan Harband · parent 72a8784 · Browse files · 3 files+177 added−2 removed
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

3 files+177 added−2 removed

added:test/fast/Unit tests/nvm install verifies the installed version+71 added
1#!/bin/sh23cleanup() {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}1112die() { echo "$@" ; cleanup ; exit 1; }1314: nvm.sh15\. ../../../nvm.sh1617unset npm_config_prefix NPM_CONFIG_PREFIX1819NVM_DIR="$(mktemp -d)"20[ -n "${NVM_DIR}" ] || die 'unable to create temp NVM_DIR'2122version='v20.0.0'23version_path="${NVM_DIR}/versions/node/${version}"24REINSTALL_MARKER="${NVM_DIR}/reinstalled"2526# Stay offline and keep the post-install activation path inert; only the27# verification behaviour is under test here.28nvm_remote_version() { nvm_echo "${version}"; }29nvm_has_executable() { return 0; }30nvm_binary_available() { return 0; }31nvm_install_source() { return 1; }32nvm_use_if_needed() { return 0; }33nvm_install_npm_if_needed() { return 0; }34nvm_install_default_packages() { return 0; }35nvm_ensure_default_set() { return 0; }3637make_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}42make_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}4748# 1) An install that reports success but leaves a broken node must fail loudly49# and must not be activated.50rm -rf "${version_path}"51nvm_install_binary() { make_broken; return 0; }52OUTPUT="$(nvm install -b "${version}" 2>&1)"; EXIT_CODE=$?53[ "${EXIT_CODE}" != "0" ] || die "a broken install should fail; got exit 0, output >${OUTPUT}<"54case "${OUTPUT}" in55 *'failed verification'*) ;;56 *) die "expected a verification-failure message; got >${OUTPUT}<" ;;57esac5859# 2) An already-present but broken version must not short-circuit as "already60# installed"; it must be reinstalled.61rm -rf "${version_path}"62make_broken63nvm_install_binary() { command touch "${REINSTALL_MARKER}"; make_healthy; return 0; }64OUTPUT="$(nvm install -b "${version}" 2>&1)"; EXIT_CODE=$?65case "${OUTPUT}" in66 *'is already installed'*) die "a broken version should not be treated as already installed: >${OUTPUT}<" ;;67esac68[ -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}<"7071cleanup
added:test/fast/Unit tests/nvm_validate_install+53 added
1#!/bin/sh23cleanup() {4 [ -n "${NVM_DIR}" ] && [ -d "${NVM_DIR}" ] && rm -rf "${NVM_DIR}"5 unset -f die cleanup6 unset NVM_DIR version version_path7}89die() { echo "$@" ; cleanup ; exit 1; }1011: nvm.sh12\. ../../../nvm.sh1314type nvm_validate_install > /dev/null 2>&1 || die 'nvm_validate_install is not available'1516NVM_DIR="$(mktemp -d)"17[ -n "${NVM_DIR}" ] || die 'Unable to create temporary folder'1819version='v20.0.0'20version_path="${NVM_DIR}/versions/node/${version}"2122# No version directory at all: not valid.23nvm_validate_install "${version}" 2>/dev/null && die 'a missing version dir should not validate'2425# 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.)28mkdir -p "${version_path}/bin" "${version_path}/lib/node_modules/npm/bin" || die 'setup mkdir failed'29printf '#!/bin/sh\necho "%s"\n' "${version}" > "${version_path}/bin/node"30chmod +x "${version_path}/bin/node"31echo '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 )33nvm_validate_install "${version}" 2>/dev/null || die 'a healthy install should validate'3435# 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.38printf '#!/bin/sh\nexit 1\n' > "${version_path}/bin/node"39chmod +x "${version_path}/bin/node"40nvm_validate_install "${version}" 2>/dev/null || die 'a non-runnable but present binary should still validate'4142# A zero-byte but executable node: passes `[ -x ]` but must not validate.43: > "${version_path}/bin/node"44chmod +x "${version_path}/bin/node"45nvm_validate_install "${version}" 2>/dev/null && die 'a zero-byte node should not validate'4647# A non-empty node again, but with a dangling npm symlink: must not validate.48printf '#!/bin/sh\necho "%s"\n' "${version}" > "${version_path}/bin/node"49chmod +x "${version_path}/bin/node"50rm -f "${version_path}/lib/node_modules/npm/bin/npm-cli.js"51nvm_validate_install "${version}" 2>/dev/null && die 'a dangling npm symlink should not validate'5253cleanup
modified:nvm.sh+53 added-2 removed
216 unmodified lines
217 fi218 return 1219}220221# 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.229nvm_validate_install() {230 local VERSION231 VERSION="${1-}"232 if [ -z "${VERSION}" ]; then233 return 1234 fi235236 local VERSION_PATH237 VERSION_PATH="$(nvm_version_path "${VERSION}" 2>/dev/null)"238 if [ -z "${VERSION_PATH}" ] || [ ! -d "${VERSION_PATH}" ]; then239 return 1240 fi241242 local NVM_NODE_BINARY243 NVM_NODE_BINARY='node'244 if [ "_$(nvm_get_os)" = '_win' ]; then245 NVM_NODE_BINARY='node.exe'246 fi247248 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 fi256257 # 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 fi263264 return 0265}266267nvm_print_npm_version() {268 if nvm_has "npm"; then269 local NPM_VERSION270 NPM_VERSION="$(npm --version 2>/dev/null)"
3591 unmodified lines
3862 fi38633864 EXIT_CODE=038653820 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 ]; then
93 unmodified lines
3964 fi3965 fi3966 fi39673968 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 fi39723973 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 lines
4839 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