Skip to content

Commit afea10b

Browse files
danbevBethGriggs
authored andcommittedFeb 22, 2021
http2: add unknownProtocol timeout
This commit add a configuration options named unknownProtocolTimeout which can be specified to set a value for the timeout in milliseconds that a server should wait when an unknowProtocol is sent to it. When this happens a timer will be started and the if the socket has not been destroyed during that time the timer callback will destoy it. Refs: https://hackerone.com/reports/1043360 CVE-ID: CVE-2021-22883 PR-URL: nodejs-private/node-private#246 Backport-PR-URL: nodejs-private/node-private#250 Reviewed-By: Beth Griggs <bgriggs@redhat.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Michael Dawson <midawson@redhat.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent 1ca3f5a commit afea10b

File tree

3 files changed

+84
-5
lines changed

3 files changed

+84
-5
lines changed
 

‎doc/api/http2.md

+24-1
Original file line numberDiff line numberDiff line change
@@ -1993,7 +1993,9 @@ added: v8.4.0
19931993
The `'unknownProtocol'` event is emitted when a connecting client fails to
19941994
negotiate an allowed protocol (i.e. HTTP/2 or HTTP/1.1). The event handler
19951995
receives the socket for handling. If no listener is registered for this event,
1996-
the connection is terminated. See the [Compatibility API][].
1996+
the connection is terminated. A timeout may be specified using the
1997+
`'unknownProtocolTimeout'` option passed to [`http2.createSecureServer()`][].
1998+
See the [Compatibility API][].
19971999

19982000
#### `server.close([callback])`
19992001
<!-- YAML
@@ -2052,6 +2054,9 @@ value only affects new connections to the server, not any existing connections.
20522054
<!-- YAML
20532055
added: v8.4.0
20542056
changes:
2057+
- version: REPLACEME
2058+
pr-url: https://github.com/nodejs-private/node-private/pull/250
2059+
description: Added `unknownProtocolTimeout` option with a default of 10000.
20552060
- version:
20562061
- v14.4.0
20572062
- v12.18.0
@@ -2158,6 +2163,10 @@ changes:
21582163
`Http2ServerResponse` class to use.
21592164
Useful for extending the original `Http2ServerResponse`.
21602165
**Default:** `Http2ServerResponse`.
2166+
* `unknownProtocolTimeout` {number} Specifies a timeout in milliseconds that
2167+
a server should wait when an [`'unknownProtocol'`][] is emitted. If the
2168+
socket has not been destroyed by that time the server will destroy it.
2169+
**Default:** `10000`.
21612170
* ...: Any [`net.createServer()`][] option can be provided.
21622171
* `onRequestHandler` {Function} See [Compatibility API][]
21632172
* Returns: {Http2Server}
@@ -2194,6 +2203,9 @@ server.listen(80);
21942203
<!-- YAML
21952204
added: v8.4.0
21962205
changes:
2206+
- version: REPLACEME
2207+
pr-url: https://github.com/nodejs-private/node-private/pull/250
2208+
description: Added `unknownProtocolTimeout` option with a default of 10000.
21972209
- version:
21982210
- v14.4.0
21992211
- v12.18.0
@@ -2290,6 +2302,10 @@ changes:
22902302
servers, the identity options (`pfx` or `key`/`cert`) are usually required.
22912303
* `origins` {string[]} An array of origin strings to send within an `ORIGIN`
22922304
frame immediately following creation of a new server `Http2Session`.
2305+
* `unknownProtocolTimeout` {number} Specifies a timeout in milliseconds that
2306+
a server should wait when an [`'unknownProtocol'`][] event is emitted. If
2307+
the socket has not been destroyed by that time the server will destroy it.
2308+
**Default:** `10000`.
22932309
* `onRequestHandler` {Function} See [Compatibility API][]
22942310
* Returns: {Http2SecureServer}
22952311

@@ -2323,6 +2339,9 @@ server.listen(80);
23232339
<!-- YAML
23242340
added: v8.4.0
23252341
changes:
2342+
- version: REPLACEME
2343+
pr-url: https://github.com/nodejs-private/node-private/pull/250
2344+
description: Added `unknownProtocolTimeout` option with a default of 10000.
23262345
- version:
23272346
- v14.4.0
23282347
- v12.18.0
@@ -2406,6 +2425,10 @@ changes:
24062425
instance passed to `connect` and the `options` object, and returns any
24072426
[`Duplex`][] stream that is to be used as the connection for this session.
24082427
* ...: Any [`net.connect()`][] or [`tls.connect()`][] options can be provided.
2428+
* `unknownProtocolTimeout` {number} Specifies a timeout in milliseconds that
2429+
a server should wait when an [`'unknownProtocol'`][] event is emitted. If
2430+
the socket has not been destroyed by that time the server will destroy it.
2431+
**Default:** `10000`.
24092432
* `listener` {Function} Will be registered as a one-time listener of the
24102433
[`'connect'`][] event.
24112434
* Returns: {ClientHttp2Session}

‎lib/internal/http2/core.js

+27-4
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const net = require('net');
3535
const { Duplex } = require('stream');
3636
const tls = require('tls');
3737
const { URL } = require('url');
38-
const { setImmediate } = require('timers');
38+
const { setImmediate, setTimeout, clearTimeout } = require('timers');
3939

4040
const { kIncomingMessage } = require('_http_common');
4141
const { kServerResponse } = require('_http_server');
@@ -2810,14 +2810,14 @@ function handleHeaderContinue(headers) {
28102810
this.emit('continue');
28112811
}
28122812

2813-
const setTimeout = {
2813+
const setTimeoutValue = {
28142814
configurable: true,
28152815
enumerable: true,
28162816
writable: true,
28172817
value: setStreamTimeout
28182818
};
2819-
ObjectDefineProperty(Http2Stream.prototype, 'setTimeout', setTimeout);
2820-
ObjectDefineProperty(Http2Session.prototype, 'setTimeout', setTimeout);
2819+
ObjectDefineProperty(Http2Stream.prototype, 'setTimeout', setTimeoutValue);
2820+
ObjectDefineProperty(Http2Session.prototype, 'setTimeout', setTimeoutValue);
28212821

28222822

28232823
// When the socket emits an error, destroy the associated Http2Session and
@@ -2877,6 +2877,22 @@ function connectionListener(socket) {
28772877
debug('Unknown protocol from %s:%s',
28782878
socket.remoteAddress, socket.remotePort);
28792879
if (!this.emit('unknownProtocol', socket)) {
2880+
debug('Unknown protocol timeout: %s', options.unknownProtocolTimeout);
2881+
// Install a timeout if the socket was not successfully closed, then
2882+
// destroy the socket to ensure that the underlying resources are
2883+
// released.
2884+
const timer = setTimeout(() => {
2885+
if (!socket.destroyed) {
2886+
debug('UnknownProtocol socket timeout, destroy socket');
2887+
socket.destroy();
2888+
}
2889+
}, options.unknownProtocolTimeout);
2890+
// Un-reference the timer to avoid blocking of application shutdown and
2891+
// clear the timeout if the socket was successfully closed.
2892+
timer.unref();
2893+
2894+
socket.once('close', () => clearTimeout(timer));
2895+
28802896
// We don't know what to do, so let's just tell the other side what's
28812897
// going on in a format that they *might* understand.
28822898
socket.end('HTTP/1.0 403 Forbidden\r\n' +
@@ -2922,6 +2938,13 @@ function initializeOptions(options) {
29222938
);
29232939
}
29242940

2941+
if (options.unknownProtocolTimeout !== undefined)
2942+
validateUint32(options.unknownProtocolTimeout, 'unknownProtocolTimeout');
2943+
else
2944+
// TODO(danbev): is this a good default value?
2945+
options.unknownProtocolTimeout = 10000;
2946+
2947+
29252948
// Used only with allowHTTP1
29262949
options.Http1IncomingMessage = options.Http1IncomingMessage ||
29272950
http.IncomingMessage;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
const common = require('../common');
3+
const fixtures = require('../common/fixtures');
4+
5+
// This test verifies that when a server receives an unknownProtocol it will
6+
// not leave the socket open if the client does not close it.
7+
8+
if (!common.hasCrypto)
9+
common.skip('missing crypto');
10+
11+
const h2 = require('http2');
12+
const tls = require('tls');
13+
14+
const server = h2.createSecureServer({
15+
key: fixtures.readKey('rsa_private.pem'),
16+
cert: fixtures.readKey('rsa_cert.crt'),
17+
unknownProtocolTimeout: 500,
18+
allowHalfOpen: true
19+
});
20+
21+
server.on('connection', (socket) => {
22+
socket.on('close', common.mustCall(() => {
23+
server.close();
24+
}));
25+
});
26+
27+
server.listen(0, function() {
28+
tls.connect({
29+
port: server.address().port,
30+
rejectUnauthorized: false,
31+
ALPNProtocols: ['bogus']
32+
});
33+
});

0 commit comments

Comments
 (0)
Please sign in to comment.