fix(sanitizeStatusCode): return default for non-numeric input instead of NaN#1420
Conversation
… of NaN `+statusCode` coerces non-numeric strings to `NaN`, and every comparison with `NaN` is `false`, so `NaN` slipped past the `< 100 || > 599` range check and was returned as-is. A `NaN` status then throws `RangeError: init["status"] must be in the range of 200 to 599` when used to build a `Response`, defeating the purpose of the sanitizer. Guard against `NaN` so an invalid code always falls back to the default.
📝 WalkthroughWalkthrough
ChangesNaN guard in sanitizeStatusCode
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes 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)
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 |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
test/unit/sanitize.test.ts (1)
4-34: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueConsider using
describeMatrixfor cross-runtime test coverage (optional).The coding guidelines specify: "Use
describeMatrixfor writing cross-runtime tests that execute in bothwebandnodemodes" for files intest/**/*.test.ts. These utility functions have no runtime-specific behavior (no DOM APIs, no Node-specific features), so they behave identically in web and node environments. UsingdescribeMatrixwould run the same tests twice without discovering new behavior differences. However, if the project's convention is to applydescribeMatrixuniformly across all test files for consistency, consider wrapping the test suites indescribeMatrix("sanitizeStatusCode", ({ it }) => { ... })and adapting assertions to use the matrixitfunction. Given that this is a utility test with pure functions, this refactor is optional and low-priority.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@test/unit/sanitize.test.ts` around lines 4 - 34, The test file for the sanitizeStatusCode utility is not using the describeMatrix function for cross-runtime test coverage. Wrap the entire describe block for sanitizeStatusCode in describeMatrix("sanitizeStatusCode", ({ it }) => { ... }) and replace all instances of the describe it function with the matrix it function provided by describeMatrix to enable the tests to run in both web and node runtime environments. Note that this refactor is optional and low-priority since sanitizeStatusCode has no runtime-specific dependencies.Source: Coding guidelines
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@test/unit/sanitize.test.ts`:
- Around line 4-34: The test file for the sanitizeStatusCode utility is not
using the describeMatrix function for cross-runtime test coverage. Wrap the
entire describe block for sanitizeStatusCode in
describeMatrix("sanitizeStatusCode", ({ it }) => { ... }) and replace all
instances of the describe it function with the matrix it function provided by
describeMatrix to enable the tests to run in both web and node runtime
environments. Note that this refactor is optional and low-priority since
sanitizeStatusCode has no runtime-specific dependencies.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 47febfb2-019d-460e-9c9a-09ab09355c48
📒 Files selected for processing (2)
src/utils/sanitize.tstest/unit/sanitize.test.ts
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Coercing a non-numeric string status to a number yields
NaN, andNaNsilently passes the range check, so the sanitizer returnsNaN— defeating its own purpose ("Make sure the status code is a valid HTTP status code").Bug
Inside
sanitizeStatusCode,+statusCodeisNaNfor non-numeric strings. Because every comparison withNaNisfalse, thestatusCode < 100 || statusCode > 599guard does not catch it andNaNis returned unchanged.This is reachable through
HTTPError(src/error.tsreadsstatus/statusCodefrom arbitrary error/cause objects) and the deprecatedsetResponseStatus(event, code)(which accepts a string). ANaNstatus then throws when the response is built:So a value meant to be sanitized into a safe code instead crashes response construction.
Fix
Reject
NaNalongside the existing out-of-range check so an invalid code always falls back todefaultStatusCode:Valid numbers and numeric strings are unaffected. The bug has been present since the original implementation (2023).
Tests
Added
test/unit/sanitize.test.ts. Thenon-numeric strings (never NaN)case fails onmain(expected NaN to be 200) and passes with the fix; the other cases pin existing behavior (valid codes, out-of-range, falsy input).vitest run test/unit/sanitize.test.ts→ 5 passedtest/bench/bundle.test.ts, which needs the nativeesbuildbinary and fails identically on a clean checkout — unrelated to this change)oxfmt --checkclean on both filesSummary by CodeRabbit
Bug Fixes
Tests