diff --git a/nvm.sh b/nvm.sh index c27934e8..da5d02ca 100755 --- a/nvm.sh +++ b/nvm.sh @@ -2950,9 +2950,12 @@ nvm_ls_remote_index_tab() { 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 + # is malformed or whose LTS codename carries control characters (which would reach + # the terminal as escape sequences), before it is printed, stored as an LTS alias, + # or matched against a pattern. The control characters are an octal range because + # mawk 1.3.3 has no `[[:cntrl:]]`. 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 }' \ + | command awk -v prefix="${PREFIX}" 'NR > 1 && $1 ~ /^v[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$/ && $10 !~ /[\001-\037\177]/ { print prefix $0 }' \ )" local LTS_ALIAS local LTS_VERSION @@ -3133,7 +3136,8 @@ nvm_compare_checksum() { nvm_err 'Refusing to continue *without checksum verification*' return 1 elif [ "${COMPUTED_SUM}" != "${CHECKSUM}" ] && [ "${COMPUTED_SUM}" != "\\${CHECKSUM}" ]; then - nvm_err "Checksums do not match: '${COMPUTED_SUM}' found, '${CHECKSUM}' expected." + # the expected checksum comes from the mirror, so show its control characters as `?` + nvm_err "Checksums do not match: '${COMPUTED_SUM}' found, '$(command printf '%s' "${CHECKSUM}" | command tr -c '[:print:]' '?')' expected." return 1 fi nvm_err 'Checksums matched!' diff --git a/test/fast/Unit tests/nvm_ls_remote LTS codename control characters b/test/fast/Unit tests/nvm_ls_remote LTS codename control characters new file mode 100755 index 00000000..fad2ff8b --- /dev/null +++ b/test/fast/Unit tests/nvm_ls_remote LTS codename control characters @@ -0,0 +1,52 @@ +#!/bin/sh + +WORK="$PWD/ls_remote-cntrl-work.$$" +export HOME="$WORK/home" +export NVM_DIR="$HOME/.nvm" + +cleanup() { + unset -f die cleanup nvm_download + rm -rf "$WORK" +} +die () { echo "$@" ; cleanup ; exit 1; } + +mkdir -p "$NVM_DIR/alias" + +: nvm.sh +\. ../../../nvm.sh + +ESC="$(printf '\033')" +BEL="$(printf '\007')" + +# a malicious/compromised mirror: the LTS codename field ($10) carries terminal +# escape sequences (an OSC window-title write and an SGR color change); +# a valid codename (Iron) is included to prove real rows are still listed +nvm_download() { + printf 'version\tdate\tfiles\tnpm\tv8\tuv\tzlib\topenssl\tmodules\tlts\tsecurity\n' + printf 'v22.0.0\t2026-01-01\tlinux-x64\t-\t-\t-\t-\t-\t-\tJod\033]0;PWNED\007\033[31m\t-\n' + printf 'v20.0.0\t2026-01-01\tlinux-x64\t-\t-\t-\t-\t-\t-\tIron\t-\n' +} + +OUTPUT="$(nvm_ls_remote 2>&1)" || true +case "${OUTPUT}" in + *"${ESC}"* | *"${BEL}"*) die "nvm_ls_remote printed control characters from the mirror's LTS codename: $(printf '%s' "${OUTPUT}" | od -c | head -5)" ;; +esac +case "${OUTPUT}" in + *'v20.0.0'*'Iron'*) ;; + *) die "nvm_ls_remote did not list the valid row: ${OUTPUT}" ;; +esac +if [ -f "$NVM_DIR/.lts-status" ] && nvm_grep -q "${ESC}" "$NVM_DIR/.lts-status"; then + die 'control characters from the mirror were recorded in .lts-status' +fi + +# a checksum from the mirror is printed when it does not match; its control characters must not be +printf 'tarball' > "$WORK/tarball" +OUTPUT="$(nvm_compare_checksum "$WORK/tarball" "${ESC}]0;PWNED${BEL}${ESC}[31m" 2>&1)" && die 'nvm_compare_checksum accepted a mismatched checksum' +case "${OUTPUT}" in + *"${ESC}"* | *"${BEL}"*) die "nvm_compare_checksum printed control characters from the mirror's checksum: $(printf '%s' "${OUTPUT}" | od -c | head -5)" ;; + *"Checksums do not match"*"?]0;PWNED??[31m' expected."*) ;; + *) die "unexpected nvm_compare_checksum output: ${OUTPUT}" ;; +esac + +cleanup +echo "nvm_ls_remote LTS codename control characters: passed"