Skip to content

fix: normalize NaN status to 500 in createError#159

Open
greymoth-jp wants to merge 1 commit into
jshttp:masterfrom
greymoth-jp:fix-nan-status-500
Open

fix: normalize NaN status to 500 in createError#159
greymoth-jp wants to merge 1 commit into
jshttp:masterfrom
greymoth-jp:fix-nan-status-500

Conversation

@greymoth-jp

Copy link
Copy Markdown

Behavior

createError(NaN) returns an error whose status and statusCode are NaN, with an empty message:

const createError = require('http-errors')
createError(NaN).status   // => NaN
createError(NaN).message  // => ''

Every other invalid status is normalized to 500:

createError(700).status   // => 500
createError(-5).status    // => 500

The normalization step is supposed to catch this, but NaN < 400 and NaN >= 600 are both false, so NaN is the one number that slips past:

if (typeof status !== 'number' ||
  (!statuses.message[status] && (status < 400 || status >= 600))) {
  status = 500
}

This is reachable whenever a status is derived from input, e.g. createError(Number(value)) where value is not numeric. The README documents statusCode as defaulting to 500, so an invalid status like NaN should fall back to 500 the same way 700 does, instead of producing an error with status: NaN.

Fix

Add an isNaN(status) check to the existing normalization guard so NaN is handled like any other invalid status. isNaN keeps this ES5-friendly, since the package still targets node >= 0.8. It only runs after the typeof status !== 'number' check short-circuits, so no coercion happens for non-number values.

Tests

Added a "when status is NaN" group asserting createError(NaN) yields the 500 InternalServerError (message, name, status, statusCode), mirroring the existing unknown 4xx/5xx tests. The full suite and npm run lint pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant