Skip to content
Myr

[Fix] `nvm_get_mirror`: allow ports, userinfo, IPv6 hosts, and percent-encoding

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

3c480159 correctly made this validator actually enforce, instead of the no-op `awk '{ $0 ~ "..." }'` it had been, but its character class had never been audited against real mirror URLs: it has no `:`, so enabling enforcement silently rejected every mirror URL bearing a port. $ NVM_NODEJS_ORG_MIRROR=https://mirror.internal:8443/dist nvm_ls_remote $NVM_NODEJS_ORG_MIRROR and $NVM_IOJS_ORG_MIRROR may only contain a URL No request is made at all, so an internal mirror on a non-default port has been unusable since v0.40.5. Allow RFC 3986 `unreserved` plus the authority and path punctuation a mirror URL legitimately needs: `:` for a port, `@` for userinfo, `[`/`]` for an IPv6 literal host, and `%` for percent-encoding. `sub-delims` and the query/fragment delimiters stay rejected: nvm appends a path to this value, so a query string could never have worked, and several of those characters are shell metacharacters.

authored by Jordan Harband · committed by Jordan Harband · parent dd0f702 · Browse files · 2 files+31 added−1 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+31 added−1 removed

  • tests:+23 added−0 removed
  • other:+8 added−1 removed

2 files+31 added−1 removed

modified:test/fast/Unit tests/nvm_get_mirror+23 added
50 unmodified lines
51testMirrors 'not a url'52testMirrors 'ftp://wrong-scheme'53testMirrors 'http://'54testMirrors 'javascript:alert(1)'5556# a query string can never work, since nvm appends a path to the mirror57testMirrors 'https://nodejs.org/dist?token=abc'58testMirrors 'https://nodejs.org/dist#frag'5960testAcceptedMirrors() {61 NVM_NODEJS_ORG_MIRROR="${1-}"62 [ "$(nvm_get_mirror node std)" = "${1-}" ] || die "NVM_NODEJS_ORG_MIRROR should accept ${1-}"63 unset NVM_NODEJS_ORG_MIRROR6465 NVM_IOJS_ORG_MIRROR="${1-}"66 [ "$(nvm_get_mirror iojs std)" = "${1-}" ] || die "NVM_IOJS_ORG_MIRROR should accept ${1-}"67 unset NVM_IOJS_ORG_MIRROR68}6970# a private mirror on a non-default port is a URL, and must not be rejected71testAcceptedMirrors 'https://mirror.internal:8443/dist'72testAcceptedMirrors 'http://localhost:8080/dist'73# an IPv6 literal host, userinfo, percent-encoding, and `~` are all legal too74testAcceptedMirrors 'http://[::1]:8080/dist'75testAcceptedMirrors 'https://user:pw@nexus.corp.example.com/repository/nodejs-dist'76testAcceptedMirrors 'https://mirror.internal/a%20b/dist'77testAcceptedMirrors 'https://mirror.internal/~dist'
modified:nvm.sh+8 added-1 removed
2386 unmodified lines
2387 ;;2388 esac238923902391 # Allow RFC 3986 `unreserved` (A-Za-z0-9-._~) plus the authority and path2392 # punctuation a mirror URL legitimately needs: `:` for a port, `@` for2393 # userinfo, `[`/`]` for an IPv6 literal host, `%` for percent-encoding.2394 # `sub-delims` (!$&'()*+,;=) and the query/fragment delimiters (?#) stay out:2395 # nvm appends a path to this value, so a query string could never work anyway,2396 # and several of those characters are shell metacharacters.2397 # Note: in a bracket expression `]` must come first and `-` last to be literal.2391 if ! nvm_echo "${NVM_MIRROR}" | command awk '{ if ($0 !~ /^https?:\/\/[a-zA-Z0-9.\/_-]+$/) exit 1 }'; then2398 if ! nvm_echo "${NVM_MIRROR}" | command awk '{ if ($0 !~ /^https?:\/\/[]a-zA-Z0-9._~:\/@%[-]+$/) exit 1 }'; then2399 nvm_err '$NVM_NODEJS_ORG_MIRROR and $NVM_IOJS_ORG_MIRROR may only contain a URL'2400 return 22401 fi2402
2728 unmodified lines