diff --git a/README.md b/README.md index ed486057..7aa2fd22 100644 --- a/README.md +++ b/README.md @@ -734,7 +734,7 @@ Now using node v5.9.1 (npm v3.7.3) `nvm use` et. al. will traverse directory structure upwards from the current directory looking for the `.nvmrc` file. In other words, running `nvm use` et. al. in any subdirectory of a directory with an `.nvmrc` will result in that `.nvmrc` being utilized. -The contents of a `.nvmrc` file **must** contain precisely one `` (as described by `nvm --help`) followed by a newline. `.nvmrc` files may also have comments. The comment delimiter is `#`, and it and any text after it, as well as blank lines, and leading and trailing white space, will be ignored when parsing. +The contents of a `.nvmrc` file **must** contain precisely one `` (as described by `nvm --help`) followed by a newline. `.nvmrc` files may also have comments. The comment delimiter is `#`, and it and any text after it, as well as blank lines, and leading and trailing white space, will be ignored when parsing. Any other control character (besides a tab) makes the file invalid. Key/value pairs using `=` are also allowed and ignored, but are reserved for future use, and may cause validation errors in the future. diff --git a/nvm.sh b/nvm.sh index 71f5661a..0e5c841a 100755 --- a/nvm.sh +++ b/nvm.sh @@ -688,9 +688,10 @@ all non-commented content (anything after # is a comment) must be either: additionally, a single bare nvm-recognized version-ish must be present (after stripping comments)." + # show control characters (other than tabs and newlines) as `?`, so the parsed content is printed as plain text local warn_text warn_text="non-commented content parsed: -${1}" +$(nvm_echo "${1}" | command tr '\001-\010\013-\037\177' '??????????????????????????????')" nvm_err "$(nvm_wrap_with_color_code 'r' "${error_text}") @@ -709,6 +710,12 @@ nvm_process_nvmrc_content() { return 1 fi + # control characters, other than tabs, are not valid in any line + if [ -n "$(nvm_echo "${lines}" | command tr -d '\t\n' | command tr -cd '[:cntrl:]')" ]; then + nvm_nvmrc_invalid_msg "${lines}" + return 1 + fi + # Initialize key-value storage local keys keys='' diff --git a/test/fast/Unit tests/nvm_process_nvmrc_content rejects control characters b/test/fast/Unit tests/nvm_process_nvmrc_content rejects control characters new file mode 100755 index 00000000..4ef7db1d --- /dev/null +++ b/test/fast/Unit tests/nvm_process_nvmrc_content rejects control characters @@ -0,0 +1,52 @@ +#!/bin/sh + +die () { echo "$@" ; exit 1; } + +: nvm.sh +\. ../../../nvm.sh + +\. ../../common.sh + +has_control_characters() { + [ -n "$(nvm_echo "${1-}" | command tr -d '\t\n' | command tr -cd '[:cntrl:]')" ] +} + +ESC="$(printf '\033')" +BEL="$(printf '\007')" +CR="$(printf '\r')" +DEL="$(printf '\177')" +TAB="$(printf '\t')" + +for CHAR in "${ESC}" "${BEL}" "${CR}" "${DEL}"; do + try_err nvm_process_nvmrc_content "2${CHAR}0" + [ "${CAPTURED_EXIT_CODE}" != 0 ] || die "expected a control character inside a version to be invalid; got exit code ${CAPTURED_EXIT_CODE}" + ! has_control_characters "$(nvm_echo "${CAPTURED_STDERR}" | strip_colors)" || die "expected no control characters in the error; got \`${CAPTURED_STDERR}\`" + nvm_echo "${CAPTURED_STDERR}" | strip_colors | nvm_grep -q '^2?0$' || die "expected the control character to be shown as \`?\`; got \`${CAPTURED_STDERR}\`" + + try nvm_process_nvmrc_content "2${CHAR}0" + [ -z "${CAPTURED_STDOUT}" ] || die "expected no version for a control character inside a version; got \`${CAPTURED_STDOUT}\`" + + try_err nvm_process_nvmrc_content "20 +foo=b${CHAR}r" + [ "${CAPTURED_EXIT_CODE}" != 0 ] || die "expected a control character in a key=value line to be invalid; got exit code ${CAPTURED_EXIT_CODE}" + ! has_control_characters "$(nvm_echo "${CAPTURED_STDERR}" | strip_colors)" || die "expected no control characters in the error; got \`${CAPTURED_STDERR}\`" +done + +try_err nvm_process_nvmrc_content "20${ESC}]0;title${BEL} +21${ESC}[2K" +[ "${CAPTURED_EXIT_CODE}" != 0 ] || die "expected two versions with control characters to be invalid; got exit code ${CAPTURED_EXIT_CODE}" +! has_control_characters "$(nvm_echo "${CAPTURED_STDERR}" | strip_colors)" || die "expected no control characters in the error; got \`${CAPTURED_STDERR}\`" + +try nvm_process_nvmrc_content "20${CR} +" +[ "${CAPTURED_EXIT_CODE}" = 0 ] || die "expected a CRLF line ending to be valid; got exit code ${CAPTURED_EXIT_CODE}" +[ "${CAPTURED_STDOUT}" = '20' ] || die "expected \`20\` from a CRLF line ending; got \`${CAPTURED_STDOUT}\`" + +try nvm_process_nvmrc_content "${TAB}lts/*${TAB} +foo${TAB}=${TAB}bar" +[ "${CAPTURED_EXIT_CODE}" = 0 ] || die "expected tabs to be valid; got exit code ${CAPTURED_EXIT_CODE}" +[ "${CAPTURED_STDOUT}" = 'lts/*' ] || die "expected \`lts/*\` with tabs; got \`${CAPTURED_STDOUT}\`" + +try nvm_process_nvmrc_content 'v20.0.0-rc.1 # comment' +[ "${CAPTURED_EXIT_CODE}" = 0 ] || die "expected a prerelease to be valid; got exit code ${CAPTURED_EXIT_CODE}" +[ "${CAPTURED_STDOUT}" = 'v20.0.0-rc.1' ] || die "expected \`v20.0.0-rc.1\`; got \`${CAPTURED_STDOUT}\`"