fix(sanitize): reject NaN status code from non-numeric strings#1397
fix(sanitize): reject NaN status code from non-numeric strings#1397francisjohnjohnston-web wants to merge 1 commit into
Conversation
sanitizeStatusCode("abc") returned NaN: `+"abc"` is NaN, and both
`NaN < 100` and `NaN > 599` are false, so the range guard let it
through. This is reachable via HTTPError, whose status is derived from
an arbitrary `cause.status`/`cause.statusCode` — a non-numeric value
there produced an HTTPError with `status: NaN` (an invalid response).
Rewrite the guard as a positive range check: `NaN >= 100` is false, so
NaN now falls back to the default. The ternary form is also smaller than
the previous two-statement if/return, keeping the bundle-size budget.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughThis PR fixes HTTP status code validation in ChangesHTTP Status Code Validation
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
ESLint skipped: no ESLint configuration detected in root package.json. To enable, add Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
AI Disclosure: This contribution was proposed by Prodia, an autonomous TypeScript evolution system (https://prodia.dev/). Human review was applied before submission. See our full disclosure policy: https://github.com/francisjohnjohnston-web/prodia-dev-33ff1150/blob/main/docs/AI-CONTRIBUTION-POLICY.md |
Problem
sanitizeStatusCodecan returnNaN, defeating its own documented purpose ("Make sure the status code is a valid HTTP status code"):+"abc"isNaN, and bothNaN < 100andNaN > 599evaluate tofalse, so the range guard letsNaNthrough.This is reachable through
HTTPError. Its status is derived from an arbitrarycause:causeis any caught error, so a non-numericstatusCode/statuson it (e.g. a wrapped library error) yields anHTTPErrorwithstatus: NaN— an invalid HTTP response.Fix
Rewrite the range guard as a positive check.
NaN >= 100isfalse, soNaNnow correctly falls back to the default:Strictly behaviour-preserving for every numeric input in/out of range; only the previously-undefined
NaNpath changes (now returns the default). The ternary is also smaller than the prior two-statementif/return, so thedefineHandlerbundle-size budget test stays green.Tests
Added
test/unit/sanitize.test.tscovering valid pass-through, out-of-range fallback, non-numeric-string fallback, and theHTTPError-from-non-numeric-causeregression. Full suite green (1154 passed),tsc0 errors, oxlint + oxfmt clean.Summary by CodeRabbit
Bug Fixes
Tests