Skip to content

Commit 3614a17

Browse files
evanlucasrvagg
authored andcommitted
http: check reason chars in writeHead
Previously, the reason argument passed to ServerResponse#writeHead was not being properly validated. One could pass CRLFs which could lead to http response splitting. This commit changes the behavior to throw an error in the event any invalid characters are included in the reason. CVE-2016-5325 PR-URL: nodejs-private/node-private#48 Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com> Reviewed-By: Rod Vagg <rod@vagg.org>
1 parent f243343 commit 3614a17

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

lib/http.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,9 @@ ServerResponse.prototype.writeHead = function(statusCode) {
12331233
if (statusCode < 100 || statusCode > 999)
12341234
throw new RangeError('Invalid status code: ' + statusCode);
12351235

1236+
if (_checkInvalidHeaderChar(reasonPhrase))
1237+
throw new Error('Invalid character in statusMessage.');
1238+
12361239
var statusLine = 'HTTP/1.1 ' + statusCode.toString() + ' ' +
12371240
reasonPhrase + CRLF;
12381241

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
var common = require('../common');
4+
var assert = require('assert');
5+
var http = require('http');
6+
7+
var server = http.createServer(function(req, res) {
8+
assert.throws(function() {
9+
res.writeHead(200, 'OK\r\nContent-Type: text/html\r\n');
10+
}, /Invalid character in statusMessage/);
11+
12+
assert.throws(function() {
13+
res.writeHead(200, 'OK\u010D\u010AContent-Type: gotcha\r\n');
14+
}, /Invalid character in statusMessage/);
15+
res.end();
16+
}).listen(common.PORT, common.mustCall(function() {
17+
var url = 'http://localhost:' + common.PORT;
18+
var left = 1;
19+
var check = common.mustCall(function(res) {
20+
res.resume();
21+
left--;
22+
assert.notEqual(res.headers['content-type'], 'text/html');
23+
assert.notEqual(res.headers['content-type'], 'gotcha');
24+
if (left === 0) server.close();
25+
}, 1);
26+
http.get(url + '/explicit', check);
27+
}));

0 commit comments

Comments
 (0)