Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 13 additions & 0 deletions test/res.sendStatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
})
})