http: remove 'data' and 'end' listener if client parser error

There might be the case of some more data coming through after
the parser has returned an error and we have destroyed the socket.
We should also be removing the 'data' event handler.

Fixes: https://github.com/nodejs/node/issues/40242

PR-URL: https://github.com/nodejs/node/pull/40244
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Matteo Collina authored and Michaël Zasso committed 2021-10-04 20:32:16 +02:00
1 parent 383dbe940d
commit 555af5b808
2 files changed
+6 -1

No files matched your search

+2
View File
@@ -489,6 +489,8 @@ function socketOnData(d) {
prepareError(ret, parser, d);
debug('parse error', ret);
freeParser(parser, req, socket);
socket.removeListener('data', socketOnData);
socket.removeListener('end', socketOnEnd);
socket.destroy();
req.socket._hadError = true;
req.emit('error', ret);
@@ -23,6 +23,7 @@
const common = require('../common');
const http = require('http');
const net = require('net');
const assert = require('assert');
const Countdown = require('../common/countdown');
const countdown = new Countdown(2, () => server.close());
@@ -38,10 +39,12 @@ const server =
server.listen(0, common.mustCall(() => {
for (let i = 0; i < 2; i++) {
http.get({
const req = http.get({
port: server.address().port,
path: '/'
}).on('error', common.mustCall((e) => {
assert.strictEqual(req.socket.listenerCount('data'), 0);
assert.strictEqual(req.socket.listenerCount('end'), 1);
common.expectsError({
code: 'HPE_INVALID_CONSTANT',
message: 'Parse Error: Expected HTTP/'