Skip to content

Commit ed5b94a

Browse files
committed
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
1 parent 171b810 commit ed5b94a

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

lib/_http_server.js

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

513513
if (!this.server.emit('clientError', e, this)) {
514514
if (this.writable) {
515-
this.end(badRequestResponse);
515+
this.write(badRequestResponse);
516+
this.destroy(e);
516517
return;
517518
}
518519
this.destroy(e);
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)