Skip to content

Commit 1f8787c

Browse files
lpincaBridgeAR
authored andcommitted
http: destroy the socket on parse error
Destroy the socket if the `'clientError'` event is emitted and there is no listener for it. Fixes: #24586 PR-URL: #24757 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 997c0e0 commit 1f8787c

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

lib/_http_server.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -509,8 +509,7 @@ function socketOnError(e) {
509509

510510
if (!this.server.emit('clientError', e, this)) {
511511
if (this.writable) {
512-
this.end(badRequestResponse);
513-
return;
512+
this.write(badRequestResponse);
514513
}
515514
this.destroy(e);
516515
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict';
2+
3+
const { expectsError, mustCall } = require('../common');
4+
5+
// Test that the request socket is destroyed if the `'clientError'` event is
6+
// emitted and there is no listener for it.
7+
8+
const assert = require('assert');
9+
const { createServer } = require('http');
10+
const { createConnection } = require('net');
11+
12+
const server = createServer();
13+
14+
server.on('connection', mustCall((socket) => {
15+
socket.on('error', expectsError({
16+
type: Error,
17+
message: 'Parse Error',
18+
code: 'HPE_INVALID_METHOD',
19+
bytesParsed: 0,
20+
rawPacket: Buffer.from('FOO /\r\n')
21+
}));
22+
}));
23+
24+
server.listen(0, () => {
25+
const chunks = [];
26+
const socket = createConnection({
27+
allowHalfOpen: true,
28+
port: server.address().port
29+
});
30+
31+
socket.on('connect', mustCall(() => {
32+
socket.write('FOO /\r\n');
33+
}));
34+
35+
socket.on('data', (chunk) => {
36+
chunks.push(chunk);
37+
});
38+
39+
socket.on('end', mustCall(() => {
40+
const expected = Buffer.from('HTTP/1.1 400 Bad Request\r\n\r\n');
41+
assert(Buffer.concat(chunks).equals(expected));
42+
43+
server.close();
44+
}));
45+
});

0 commit comments

Comments
 (0)