Skip to content

Commit

Permalink
http: disallow sending obviously invalid status codes
Browse files Browse the repository at this point in the history
PR-URL: #6291
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
mscdex authored and Myles Borins committed May 18, 2016
1 parent f60ba54 commit 620a261
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ ServerResponse.prototype.writeHead = function(statusCode, reason, obj) {
headers = obj;
}

statusCode |= 0;
if (statusCode < 100 || statusCode > 999)
throw new RangeError(`Invalid status code: ${statusCode}`);

var statusLine = 'HTTP/1.1 ' + statusCode.toString() + ' ' +
this.statusMessage + CRLF;

Expand Down
91 changes: 91 additions & 0 deletions test/parallel/test-http-response-statuscode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');

const MAX_REQUESTS = 12;
var reqNum = 0;

const server = http.Server(common.mustCall(function(req, res) {
switch (reqNum) {
case 0:
assert.throws(common.mustCall(() => {
res.writeHead(-1);
}, /invalid status code/i));
break;
case 1:
assert.throws(common.mustCall(() => {
res.writeHead(Infinity);
}, /invalid status code/i));
break;
case 2:
assert.throws(common.mustCall(() => {
res.writeHead(NaN);
}, /invalid status code/i));
break;
case 3:
assert.throws(common.mustCall(() => {
res.writeHead({});
}, /invalid status code/i));
break;
case 4:
assert.throws(common.mustCall(() => {
res.writeHead(99);
}, /invalid status code/i));
break;
case 5:
assert.throws(common.mustCall(() => {
res.writeHead(1000);
}, /invalid status code/i));
break;
case 6:
assert.throws(common.mustCall(() => {
res.writeHead('1000');
}, /invalid status code/i));
break;
case 7:
assert.throws(common.mustCall(() => {
res.writeHead(null);
}, /invalid status code/i));
break;
case 8:
assert.throws(common.mustCall(() => {
res.writeHead(true);
}, /invalid status code/i));
break;
case 9:
assert.throws(common.mustCall(() => {
res.writeHead([]);
}, /invalid status code/i));
break;
case 10:
assert.throws(common.mustCall(() => {
res.writeHead('this is not valid');
}, /invalid status code/i));
break;
case 11:
assert.throws(common.mustCall(() => {
res.writeHead('404 this is not valid either');
}, /invalid status code/i));
this.close();
break;
default:
throw new Error('Unexpected request');
}
res.statusCode = 200;
res.end();
}, MAX_REQUESTS));
server.listen();

server.on('listening', function makeRequest() {
http.get({
port: this.address().port
}, (res) => {
assert.strictEqual(res.statusCode, 200);
res.on('end', () => {
if (++reqNum < MAX_REQUESTS)
makeRequest.call(this);
});
res.resume();
});
});

0 comments on commit 620a261

Please sign in to comment.