From c966193b981f84d4d6894936dcbb47b363d59456 Mon Sep 17 00:00:00 2001 From: Srinjoy Dev Date: Tue, 21 Oct 2025 16:49:31 +0000 Subject: [PATCH] fix: prevent crash when BigInt passed to res.status() Replace JSON.stringify() with String() in error messages to prevent uncaught TypeError when non-JSON-serializable values (like BigInt) are passed to res.status() or res.sendStatus(). The error messages now include both the stringified value and its type for better debugging, e.g., 'Invalid status code: 200 (bigint)'. This change maintains backward compatibility while making the library more robust against edge cases that could crash the server. Fixes #6756 --- lib/response.js | 4 ++-- test/res.sendStatus.js | 36 ++++++++++++++++++++++++++++++++++++ test/res.status.js | 12 ++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/lib/response.js b/lib/response.js index 7a2f0ecce56..1095ce4e864 100644 --- a/lib/response.js +++ b/lib/response.js @@ -64,11 +64,11 @@ module.exports = res res.status = function status(code) { // Check if the status code is not an integer if (!Number.isInteger(code)) { - throw new TypeError(`Invalid status code: ${JSON.stringify(code)}. Status code must be an integer.`); + throw new TypeError(`Invalid status code: ${String(code)} (${typeof code}). Status code must be an integer.`); } // Check if the status code is outside of Node's valid range if (code < 100 || code > 999) { - throw new RangeError(`Invalid status code: ${JSON.stringify(code)}. Status code must be greater than 99 and less than 1000.`); + throw new RangeError(`Invalid status code: ${String(code)} (${typeof code}). Status code must be greater than 99 and less than 1000.`); } this.statusCode = code; diff --git a/test/res.sendStatus.js b/test/res.sendStatus.js index b244cf9d173..e710dafb0aa 100644 --- a/test/res.sendStatus.js +++ b/test/res.sendStatus.js @@ -40,5 +40,41 @@ describe('res', function () { .get('/') .expect(500, /TypeError: Invalid status code/, done) }) + + it('should raise error for BigInt status code', function (done) { + var app = express() + + app.use(function (req, res) { + res.sendStatus(200n) + }) + + request(app) + .get('/') + .expect(500, /TypeError: Invalid status code: 200 \(bigint\)/, done) + }) + + it('should raise error for string status code', function (done) { + var app = express() + + app.use(function (req, res) { + res.sendStatus('200') + }) + + request(app) + .get('/') + .expect(500, /TypeError: Invalid status code: 200 \(string\)/, done) + }) + + it('should raise error for object status code', function (done) { + var app = express() + + app.use(function (req, res) { + res.sendStatus({ status: 200 }) + }) + + request(app) + .get('/') + .expect(500, /TypeError: Invalid status code.*\(object\)/, done) + }) }) }) diff --git a/test/res.status.js b/test/res.status.js index 59c8a57e702..8c50f894612 100644 --- a/test/res.status.js +++ b/test/res.status.js @@ -200,6 +200,18 @@ describe('res', function () { .get('/') .expect(500, /Invalid status code/, done); }); + + it('should raise error for BigInt status code', function (done) { + var app = express(); + + app.use(function (req, res) { + res.status(200n).end(); + }); + + request(app) + .get('/') + .expect(500, /TypeError: Invalid status code: 200 \(bigint\)/, done); + }); }); }); });