Skip to content

Commit b5c57ff

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#46 Reviewed-By: Fedor Indutny <[email protected]> Reviewed-By: Rod Vagg <[email protected]>
1 parent af9dda1 commit b5c57ff

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

lib/_http_server.js

+3
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ ServerResponse.prototype.writeHead = function(statusCode, reason, obj) {
191191
if (statusCode < 100 || statusCode > 999)
192192
throw new RangeError(`Invalid status code: ${statusCode}`);
193193

194+
if (common._checkInvalidHeaderChar(this.statusMessage))
195+
throw new Error('Invalid character in statusMessage.');
196+
194197
var statusLine = 'HTTP/1.1 ' + statusCode.toString() + ' ' +
195198
this.statusMessage + CRLF;
196199

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const http = require('http');
6+
7+
function explicit(req, res) {
8+
assert.throws(() => {
9+
res.writeHead(200, `OK\r\nContent-Type: text/html\r\n`);
10+
}, /Invalid character in statusMessage/);
11+
12+
assert.throws(() => {
13+
res.writeHead(200, 'OK\u010D\u010AContent-Type: gotcha\r\n');
14+
}, /Invalid character in statusMessage/);
15+
16+
res.statusMessage = 'OK';
17+
res.end();
18+
}
19+
20+
function implicit(req, res) {
21+
assert.throws(() => {
22+
res.statusMessage = `OK\r\nContent-Type: text/html\r\n`;
23+
res.writeHead(200);
24+
}, /Invalid character in statusMessage/);
25+
res.statusMessage = 'OK';
26+
res.end();
27+
}
28+
29+
const server = http.createServer((req, res) => {
30+
if (req.url === '/explicit') {
31+
explicit(req, res);
32+
} else {
33+
implicit(req, res);
34+
}
35+
}).listen(common.PORT, common.mustCall(() => {
36+
const url = `http://localhost:${common.PORT}`;
37+
let left = 2;
38+
const check = common.mustCall((res) => {
39+
left--;
40+
assert.notEqual(res.headers['content-type'], 'text/html');
41+
assert.notEqual(res.headers['content-type'], 'gotcha');
42+
if (left === 0) server.close();
43+
}, 2);
44+
http.get(`${url}/explicit`, check).end();
45+
http.get(`${url}/implicit`, check).end();
46+
}));

0 commit comments

Comments
 (0)