[Fix] nvm_is_natural_num: reject anything but digits

`[ "$1" -eq "$1" ]` accepts more than a natural number:
a leading `+`, and leading or trailing whitespace,
and in ksh it evaluates its operands as arithmetic expressions,
so words like `install` and expressions like `1+1` or `V=1` pass too.
Values with any non-digit character are now rejected before that test.

The sunos and aix branches of `nvm_get_make_jobs` count cores with `wc -l`,
which on those platforms pads the count with whitespace,
so that count is now stripped of whitespace before it is checked

Discovered by Anthropic's OSS Scanner, as bug ANT-2026-SYLQD6PZ; the scanner correctly classified it as not a vulnerability.
This commit is contained in:
Jordan Harband committed 2026-10-22 20:10:42 -07:00
1 parent 8df635740e
commit 813b5f6dcc
3 files changed
+22 -2

No files matched your search

+4 -2
View File
@@ -3247,10 +3247,11 @@ nvm_get_make_jobs() {
NVM_CPU_CORES="$(sysctl -n hw.ncpu)"
;;
"_sunos")
NVM_CPU_CORES="$(psrinfo | wc -l)"
# some `wc`s pad the count with whitespace
NVM_CPU_CORES="$(psrinfo | wc -l | command tr -d '[:space:]')"
;;
"_aix")
NVM_CPU_CORES="$(pmcycles -m | wc -l)"
NVM_CPU_CORES="$(pmcycles -m | wc -l | command tr -d '[:space:]')"
;;
*)
# `_NPROCESSORS_ONLN` is how glibc, FreeBSD, macOS, and Cygwin spell it;
@@ -3690,6 +3691,7 @@ nvm_is_natural_num() {
case "$1" in
0) return 1 ;;
-*) return 3 ;; # some BSDs return false positives for double-negated args
*[!0-9]*) return 2 ;; # `-eq` also accepts signs and whitespace, and in ksh, any arithmetic expression
*)
[ "$1" -eq "$1" ] 2>/dev/null # returns 2 if it doesn't match
;;