Skip to content

Commit 867c451

Browse files
committedSep 22, 2020
test: add regression tests for HTTP parser crash
Since the tests only crash on v12.x, this commit adds separate regression tests. PR-URL: #34251 Refs: #15102 Refs: #34016 Reviewed-By: Fedor Indutny <[email protected]> Reviewed-By: Gerhard Stöbich <[email protected]>
1 parent 756ac65 commit 867c451

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const http = require('http');
5+
const MakeDuplexPair = require('../common/duplexpair');
6+
7+
// Regression test for the crash reported in
8+
// https://github.com/nodejs/node/issues/15102 (httpParser.finish() is called
9+
// during httpParser.execute()):
10+
11+
{
12+
const { clientSide, serverSide } = MakeDuplexPair();
13+
14+
serverSide.on('data', common.mustCall((data) => {
15+
assert.strictEqual(data.toString('utf8'), `\
16+
GET / HTTP/1.1
17+
Expect: 100-continue
18+
Host: localhost:80
19+
Connection: close
20+
21+
`.replace(/\n/g, '\r\n'));
22+
23+
setImmediate(() => {
24+
serverSide.write('HTTP/1.1 100 Continue\r\n\r\n');
25+
});
26+
}));
27+
28+
const req = http.request({
29+
createConnection: common.mustCall(() => clientSide),
30+
headers: {
31+
'Expect': '100-continue'
32+
}
33+
});
34+
req.on('continue', common.mustCall((res) => {
35+
let sync = true;
36+
37+
clientSide._writev = null;
38+
clientSide._write = common.mustCall((chunk, enc, cb) => {
39+
assert(sync);
40+
// On affected versions of Node.js, the error would be emitted on `req`
41+
// synchronously (i.e. before commit f663b31cc2aec), which would cause
42+
// parser.finish() to be called while we are here in the 'continue'
43+
// callback, which is inside a parser.execute() call.
44+
45+
assert.strictEqual(chunk.length, 0);
46+
clientSide.destroy(new Error('sometimes the code just doesn’t work'), cb);
47+
});
48+
req.on('error', common.mustCall());
49+
req.end();
50+
51+
sync = false;
52+
}));
53+
}

0 commit comments

Comments
 (0)