[Fix] nvm_process_nvmrc_content: reject control characters
An `.nvmrc` line containing a control character (eg, an escape, a bell, or a carriage return in the middle of the version) was accepted as-is, so the raw bytes were passed along as the version and printed verbatim in the "Found '…' with version <…>" and "N/A: version … is not yet installed" messages, garbling the terminal output. Any line that still contains a control character other than a tab, after comments and leading and trailing white space are removed, now makes the file invalid. CRLF line endings keep working, since the trailing carriage return is trimmed as white space before the check. The "non-commented content parsed" part of the invalid `.nvmrc` message now shows each control character (other than tabs and newlines) as `?`, so it prints as plain text Discovered by Anthropic's OSS Scanner, as ANT-2026-R3FR49Z7; the scanner reported it as a vulnerability, but it is not one.
This commit is contained in:
3 files changed
+61
-2
No files matched your search
@@ -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 `<version>` (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 `<version>` (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.
|
||||
|
||||
|
||||
@@ -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=''
|
||||
|
||||
@@ -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}\`"
|
||||
Reference in new issue
Block a user