Skip to content
Myr

[Fix] `nvm_alias`, `nvm_version_path`: reject `..` path components

Before adoption: this commit came in with the history the project adopted, so no landing records it.

`nvm_alias` concatenated the requested name onto `$NVM_DIR/alias` and read whatever that resolved to, so a name with a `..` component escaped the alias dir - under the default layout, `../../.npmrc` reaches `$HOME/.npmrc`. `nvm_print_alias_file` echoes every non-comment line of what it opens, and `nvm use` reports the first one in its "is not yet installed" error, so an untrusted `.nvmrc` could disclose any file the invoking user can read. The write side already rejected `..` in an alias name (9275c5ba); apply the same check on the read side, and to `nvm_version_path`, which composes a version into a path with no check of its own. Slashes stay legal, since `lts/iron` and the `lts/*` alias file depend on them.

authored by Jordan Harband · committed by Jordan Harband · parent 60a8da7 · Browse files · 2 files+82 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+82 added−0 removed

  • tests:+67 added−0 removed
  • other:+15 added−0 removed

2 files+82 added−0 removed

added:test/fast/Unit tests/nvm_alias path traversal+67 added
1#!/bin/sh23ORIG_NVM_DIR="${NVM_DIR}"4TMP_DIR=''56cleanup () {7 case "${TMP_DIR}" in8 /*) rm -rf "${TMP_DIR}" ;;9 esac10 NVM_DIR="${ORIG_NVM_DIR}"11 export NVM_DIR12}1314die () { echo "$@" ; cleanup ; exit 1; }1516: nvm.sh17\. ../../../nvm.sh1819TMP_DIR="$(mktemp -d)"20NVM_DIR="${TMP_DIR}/nvm"21export NVM_DIR22mkdir -p "${NVM_DIR}/alias/lts" || die 'could not create the scratch alias dir'2324# a file outside $NVM_DIR that alias resolution must never reach25printf '%s\n' 'SECRET_OUTSIDE_NVM_DIR' > "${TMP_DIR}/outside"2627# legitimate aliases, to prove the guard does not reject the names nvm uses28printf '%s\n' 'v1.2.3' > "${NVM_DIR}/alias/default"29printf '%s\n' 'v4.5.6' > "${NVM_DIR}/alias/lts/iron"3031for TRAVERSAL in '../../outside' 'lts/../../../outside' '..' 'lts/..' 'a/../../outside'; do32 OUTPUT="$(nvm_alias "${TRAVERSAL}" 2>/dev/null)"33 EXIT_CODE=$?34 [ "_${EXIT_CODE}" = '_3' ] || die "'nvm_alias ${TRAVERSAL}' exited with ${EXIT_CODE}, expected 3"35 [ -z "${OUTPUT}" ] || die "'nvm_alias ${TRAVERSAL}' produced output: ${OUTPUT}"36 # capture stderr through a file, and match rather than compare: ksh 93u+ has37 # no `local`, so it interleaves its own noise with anything nvm.sh writes38 nvm_alias "${TRAVERSAL}" >/dev/null 2>"${TMP_DIR}/stderr"39 grep -qF "invalid alias name: ${TRAVERSAL}" "${TMP_DIR}/stderr" \40 || die "'nvm_alias ${TRAVERSAL}' produced stderr: $(cat "${TMP_DIR}/stderr")"41done4243# the disclosure path the advisory reports: resolution must not echo the file44for TRAVERSAL in '../../outside' 'lts/../../../outside'; do45 OUTPUT="$(nvm_resolve_alias "${TRAVERSAL}" 2>&1)"46 case "${OUTPUT}" in47 *SECRET_OUTSIDE_NVM_DIR*) die "'nvm_resolve_alias ${TRAVERSAL}' leaked a file outside \$NVM_DIR" ;;48 esac49done5051OUTPUT="$(nvm_alias default)"52[ "_${OUTPUT}" = '_v1.2.3' ] || die "'nvm_alias default' produced \"${OUTPUT}\", expected v1.2.3"5354OUTPUT="$(nvm_alias lts/iron)"55[ "_${OUTPUT}" = '_v4.5.6' ] || die "'nvm_alias lts/iron' produced \"${OUTPUT}\", expected v4.5.6"5657for BAD_VERSION in '../../outside' 'v1.2.3/../../..' '..'; do58 OUTPUT="$(nvm_version_path "${BAD_VERSION}" 2>/dev/null)"59 EXIT_CODE=$?60 [ "_${EXIT_CODE}" = '_3' ] || die "'nvm_version_path ${BAD_VERSION}' exited with ${EXIT_CODE}, expected 3"61 [ -z "${OUTPUT}" ] || die "'nvm_version_path ${BAD_VERSION}' produced output: ${OUTPUT}"62done6364OUTPUT="$(nvm_version_path v18.0.0)"65[ "_${OUTPUT}" = "_${NVM_DIR}/versions/node/v18.0.0" ] || die "'nvm_version_path v18.0.0' produced \"${OUTPUT}\""6667cleanup
modified:nvm.sh+15 added
798 unmodified lines
799800nvm_version_path() {801 local VERSION802 VERSION="${1-}"803 case "/${VERSION}/" in804 */../*)805 nvm_err "invalid version: ${VERSION}"806 return 3807 ;;808 esac809 if [ -z "${VERSION}" ]; then810 nvm_err 'version is required'811 return 3812 elif nvm_is_iojs_version "${VERSION}"; then
690 unmodified lines
1503 if [ -z "${ALIAS}" ]; then1504 return 21505 fi15061507 # slashes are legal (eg `lts/iron`), but a `..` component would read outside1508 # the alias dir; `nvm_make_alias` rejects the same shape on the write side1509 case "/${ALIAS}/" in1510 */../*)1511 nvm_err "invalid alias name: ${ALIAS}"1512 return 31513 ;;1514 esac15151516 local NVM_ALIAS_PATH1517 NVM_ALIAS_PATH="$(nvm_alias_path)/${ALIAS}"1518 if [ ! -f "${NVM_ALIAS_PATH}" ]; then1519 nvm_err 'Alias does not exist.'
3707 unmodified lines