Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v12.x] http: fix crash for sync write errors during header parsing #34251

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,12 @@ function socketErrorListener(err) {

const parser = socket.parser;
if (parser) {
parser.finish();
freeParser(parser, req, socket);
// Use process.nextTick() on v12.x since 'error' events might be
// emitted synchronously from e.g. a failed write() call on the socket.
process.nextTick(() => {
parser.finish();
freeParser(parser, req, socket);
});
}

// Ensure that no further data will come out of the socket
Expand Down
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;
}));
}