diff --git a/lib/response.js b/lib/response.js index 7a2f0ecce56..e8e6e01c0ff 100644 --- a/lib/response.js +++ b/lib/response.js @@ -326,6 +326,18 @@ res.jsonp = function jsonp(obj) { */ res.sendStatus = function sendStatus(statusCode) { + // Prevent BigInt status codes from causing obscure errors + if (typeof statusCode === 'bigint') { + throw new TypeError('Status code must be an integer number'); + } + if (typeof statusCode !== 'number' || !Number.isInteger(statusCode) || statusCode < 100 || statusCode > 999) { + if (!Number.isInteger(statusCode)) { + throw new TypeError(`Invalid status code: ${JSON.stringify(statusCode)}. Status code must be an integer.`); + } else { + throw new RangeError(`Invalid status code: ${JSON.stringify(statusCode)}. Must be between 100 and 999.`); + } + } + var body = statuses.message[statusCode] || String(statusCode) this.status(statusCode); diff --git a/test/res.sendStatus.js b/test/res.sendStatus.js index b244cf9d173..7010fb6b2f2 100644 --- a/test/res.sendStatus.js +++ b/test/res.sendStatus.js @@ -40,5 +40,18 @@ describe('res', function () { .get('/') .expect(500, /TypeError: Invalid status code/, done) }) + + it('should raise error for BigInt(200) status code', function (done) { + var app = express(); + + app.use(function (req, res) { + res.sendStatus(BigInt(200)); + }); + + request(app) + .get('/') + .expect(500) + .expect(/Status code must be an integer/, done); + }); }) })