[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:
Jordan Harband committed 2026-10-23 08:53:59 -07:00
1 parent 4a1f7d2875
commit 8610e1578d
3 files changed
+61 -2

No files matched your search

+8 -1
View File
@@ -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=''