Skip to content
Myr

[Fix] `nvm_download_artifact`: reject version strings with disallowed characters

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

The mirror-supplied (untrusted) version flows into download URLs, filesystem paths, and the checksum awk match. Reject any version outside the node/io.js grammar (`[0-9A-Za-z._+-]`) before it is used. A blocklist of metacharacters is used rather than a strict semver allowlist so RCs, nightlies, v8-canary, and io.js versions still install. Completes the remediation of GHSA-3c52-35h2-gfmm.

authored by Jordan Harband · committed by Jordan Harband · parent 90bb887 · Browse files · 2 files+56 added−4 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+56 added−4 removed

  • tests:+46 added−0 removed
  • other:+10 added−4 removed

2 files+56 added−4 removed

added:test/fast/Unit tests/nvm_download_artifact version injection+46 added
1#!/bin/sh23WORK="${TMPDIR:-/tmp}/nvm_version_injection.$$"4PROOF="${WORK}/PWNED"56cleanup () {7 unset -f die cleanup nvm_download8 rm -rf "${WORK}"9}10die () { echo "$@" ; cleanup ; exit 1; }1112\. ../../../nvm.sh1314mkdir -p "${WORK}"15export NVM_DIR="${WORK}"1617# GHSA-3c52-35h2-gfmm: a mirror-supplied version with shell/awk metacharacters18# must be rejected before it is used in URLs, paths, or awk. Neutralize network.19nvm_download () { return 0; }2021# given a version containing command-substitution syntax22# when nvm_download_artifact is asked to download it23# then it is rejected for disallowed characters and nothing is executed24rm -f "${PROOF}"25out="$(nvm_download_artifact node source std 'v1$(touch '"${PROOF}"')' 2>&1 </dev/null)"26case "${out}" in27 *'disallowed characters'*) : ;;28 *) die "command-substitution version was not rejected; got: ${out}" ;;29esac30[ ! -e "${PROOF}" ] || die 'command-substitution payload executed via nvm_download_artifact'3132# and an awk-breakout version is likewise rejected33out="$(nvm_download_artifact node source std 'v1"==$2){system("x")}#' 2>&1 </dev/null)"34case "${out}" in35 *'disallowed characters'*) : ;;36 *) die "awk-style version was not rejected; got: ${out}" ;;37esac3839# and a legitimate nightly version must NOT be rejected by the character guard40out="$(nvm_download_artifact node source std 'v22.0.0-nightly20240101abcdef' 2>&1 </dev/null)"41case "${out}" in42 *'disallowed characters'*) die 'a legitimate nightly version was wrongly rejected by the guard' ;;43esac4445cleanup46echo 'nvm_download_artifact version injection: passed'
modified:nvm.sh+10 added-4 removed
2504 unmodified lines
25052506 local VERSION2507 VERSION="${4}"25082509 if [ -z "${VERSION}" ]; then2510 nvm_err 'A version number is required.'2511 return 32512 fi2509 case "${VERSION}" in2510 '')2511 nvm_err 'A version number is required.'2512 return 32513 ;;2514 *[!0-9A-Za-z._+-]*)2515 nvm_err 'Invalid version: contains disallowed characters'2516 return 32517 ;;2518 esac25192520 if [ "${KIND}" = 'binary' ] && ! nvm_binary_available "${VERSION}"; then2521 nvm_err "No precompiled binary available for ${VERSION}."2522 return
2324 unmodified lines