Skip to content

Commit

Permalink
test: add regression tests for HTTP parser crash
Browse files Browse the repository at this point in the history
Since the tests only crash on v12.x, this commit adds separate
regression tests.

Refs: nodejs#15102
  • Loading branch information
addaleax committed Jul 7, 2020
1 parent d9220ae commit 11406b9
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
34 changes: 34 additions & 0 deletions test/parallel/test-http-fake-socket-error-during-headers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');
const MakeDuplexPair = require('../common/duplexpair');

// Regression test for the crash reported in
// https://github.com/nodejs/node/issues/15102 (httpParser.finish() is called
// during httpParser.execute()):

{
const { clientSide, serverSide } = MakeDuplexPair();

serverSide.on('data', common.mustCall((data) => {
assert.strictEqual(data.toString('utf8'), `\
GET / HTTP/1.1
Host: localhost:80
Connection: close
`.replace(/\n/g, '\r\n'));

setImmediate(() => {
serverSide.write('HTTP/1.1 200 OK\r\n\r\n');
});
}));

const req = http.get({
createConnection: common.mustCall(() => clientSide)
}, common.mustCall((res) => {
req.on('error', common.mustCall());
req.socket.emit('error', new Error('very fake error'));
}));
req.end();
}
53 changes: 53 additions & 0 deletions test/parallel/test-http-sync-write-error-during-continue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');
const MakeDuplexPair = require('../common/duplexpair');

// Regression test for the crash reported in
// https://github.com/nodejs/node/issues/15102 (httpParser.finish() is called
// during httpParser.execute()):

{
const { clientSide, serverSide } = MakeDuplexPair();

serverSide.on('data', common.mustCall((data) => {
assert.strictEqual(data.toString('utf8'), `\
GET / HTTP/1.1
Expect: 100-continue
Host: localhost:80
Connection: close
`.replace(/\n/g, '\r\n'));

setImmediate(() => {
serverSide.write('HTTP/1.1 100 Continue\r\n\r\n');
});
}));

const req = http.request({
createConnection: common.mustCall(() => clientSide),
headers: {
'Expect': '100-continue'
}
});
req.on('continue', common.mustCall((res) => {
let sync = true;

clientSide._writev = null;
clientSide._write = common.mustCall((chunk, enc, cb) => {
assert(sync);
// On affected versions of Node.js, the error would be emitted on `req`
// synchronously (i.e. before commit f663b31cc2aec), which would cause
// parser.finish() to be called while we are here in the 'continue'
// callback, which is inside a parser.execute() call.

assert.strictEqual(chunk.length, 0);
clientSide.destroy(new Error('sometimes the code just doesn’t work'), cb);
});
req.on('error', common.mustCall());
req.end();

sync = false;
}));
}

0 comments on commit 11406b9

Please sign in to comment.