fix: alerts numeric flags silently produce wrong results on invalid input - #579
fix: alerts numeric flags silently produce wrong results on invalid input#579teyrebaz33 wants to merge 2 commits into
Conversation
…nput `nansen alerts list --limit abc` returned an empty list (slice(0, NaN) -> 0) instead of erroring, and `--offset -2` silently returned the last 2 alerts (negative slice counts from the end) instead of rejecting an invalid offset. The same unvalidated `Number()` coercion pattern was in `buildRange()`, used by 7 numeric range flags on `alerts create`/`update` (--market-cap-min/max, --usd-min/max, --token-amount-min/max, --fdv-min/max, --inflow/outflow/netflow-*-min/max) plus two standalone --token-age-min/max blocks: a non-numeric value produced NaN, which passed the `!= null` check as "set", but JSON.stringify(NaN) serializes to null, so the alert was silently created with that threshold disabled. All of these now validate with Number.isFinite/Number.isInteger and throw a clear NansenError, matching the existing buildPagination idiom in query-options.js/research.js. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0141eH4NcQy4GXgusCGbG1pm
pr-reviewer Summary for #45f5895📝 1 finding Review completed. Please address the findings below. Findings by Severity
Review effort: 2/5 (Simple) SummaryThis is a solid, well-scoped bug fix. The validation logic is correct, consistently uses One medium finding worth addressing before merge: Findings
|
There was a problem hiding this comment.
Auto-approved
This PR was automatically approved because:
- Claude recommends approval
- Claude assessed this as a moderate effort change
- The effort level is within the auto-approval threshold of 2
- No high or critical issues were detected
- Review comment contains non-blocking feedback
If you have any concerns, please request a manual review.
Shorten the pagination guard comment in alerts.js and drop two redundant "Before the fix" comments in the regression tests (the `it()` descriptions and error assertions already say what they said). Also fix indentation on the --token-age-min/--token-age-max test body, which had lost its indentation in transit and read as a copy-paste artifact. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0141eH4NcQy4GXgusCGbG1pm
There was a problem hiding this comment.
Auto-approved
This PR was automatically approved because:
- Claude recommends approval
- Claude assessed this as a moderate effort change
- The effort level is within the auto-approval threshold of 2
- No high or critical issues were detected
- Review comment contains non-blocking feedback
If you have any concerns, please request a manual review.
Summary
Fixes #578.
src/commands/alerts.jscalledNumber(optionValue)in several places without checking whether the result was actually a valid number: the sharedbuildRange()helper (used by 7 numeric range flags onalerts create/update), two standalone--token-age-min/maxblocks, and thelisthandler's--limit/--offsetpagination.A non-numeric value produced
NaN, which is neithernullnorundefined, so it silently passed the existing "is this flag set" checks. For the range flags,JSON.stringify({min: NaN})serializes to{"min":null}, so the alert was created with that threshold silently disabled instead of erroring. For--limit,Array.prototype.slice(0, NaN)evaluates the end index as0, so a typo'd--limitsilently returned an empty list. For--offset, a negative value madeArray.prototype.slice()count from the end of the array, so--offset -2silently returned the last 2 alerts.Fix
Validate with
Number.isFinite()(range flags, which can be decimals) /Number.isInteger()(--limit/--offset, which must be whole numbers) and throw aNansenErroron failure -- matching the existingbuildPaginationidiom already used insrc/query-options.jsandsrc/commands/research.js. I checked for the same unvalidated-Number()pattern elsewhere insrc/commands/and confirmedresearch.js's pagination already validates correctly, so this PR's scope (alerts.js only) is complete.Test plan
--limitand--offset), plus a test confirming numeric strings (how the real CLI parser passes flag values) still work.npx eslint src/commands/alerts.js src/__tests__/cli.internal.test.js-- clean.npx vitest run src/__tests__/cli.internal.test.js-- 484/484 passed.npm test): 2639 passed, 2 skipped, 0 failed.npm installproduced only unrelatedpackage-lock.jsonmetadata noise, reverted before committing;package.jsonitself was not touched.🤖 Generated with Claude Code
https://claude.ai/code/session_0141eH4NcQy4GXgusCGbG1pm