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 2021-09-28 16:55:10 +02:00 committed by Node.js GitHub Bot
parent 4f3eda66cc
commit 140b25a35f
2 changed files with 6 additions and 1 deletions

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);

View File

@ -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/'