Skip to content

Commit a740145

Browse files
jasnelltargos
authored andcommitted
http2: throw better error when accessing unbound socket proxy
Fixes: #22268 PR-URL: #22486 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 397235e commit a740145

File tree

4 files changed

+88
-2
lines changed

4 files changed

+88
-2
lines changed

doc/api/errors.md

+6
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,12 @@ The `Http2Session` settings canceled.
10331033
An attempt was made to connect a `Http2Session` object to a `net.Socket` or
10341034
`tls.TLSSocket` that had already been bound to another `Http2Session` object.
10351035

1036+
<a id="ERR_HTTP2_SOCKET_UNBOUND"></a>
1037+
### ERR_HTTP2_SOCKET_UNBOUND
1038+
1039+
An attempt was made to use the `socket` property of an `Http2Session` that
1040+
has already been closed.
1041+
10361042
<a id="ERR_HTTP2_STATUS_101"></a>
10371043
### ERR_HTTP2_STATUS_101
10381044

lib/internal/errors.js

+2
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,8 @@ E('ERR_HTTP2_SESSION_ERROR', 'Session closed with error code %s', Error);
590590
E('ERR_HTTP2_SETTINGS_CANCEL', 'HTTP2 session settings canceled', Error);
591591
E('ERR_HTTP2_SOCKET_BOUND',
592592
'The socket is already bound to an Http2Session', Error);
593+
E('ERR_HTTP2_SOCKET_UNBOUND',
594+
'The socket has been disconnected from the Http2Session', Error);
593595
E('ERR_HTTP2_STATUS_101',
594596
'HTTP status code 101 (Switching Protocols) is forbidden in HTTP/2', Error);
595597
E('ERR_HTTP2_STATUS_INVALID', 'Invalid status code: %s', RangeError);

lib/internal/http2/core.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ const {
5959
ERR_HTTP2_SESSION_ERROR,
6060
ERR_HTTP2_SETTINGS_CANCEL,
6161
ERR_HTTP2_SOCKET_BOUND,
62+
ERR_HTTP2_SOCKET_UNBOUND,
6263
ERR_HTTP2_STATUS_101,
6364
ERR_HTTP2_STATUS_INVALID,
6465
ERR_HTTP2_STREAM_CANCEL,
@@ -684,12 +685,17 @@ const proxySocketHandler = {
684685
throw new ERR_HTTP2_NO_SOCKET_MANIPULATION();
685686
default:
686687
const socket = session[kSocket];
688+
if (socket === undefined)
689+
throw new ERR_HTTP2_SOCKET_UNBOUND();
687690
const value = socket[prop];
688691
return typeof value === 'function' ? value.bind(socket) : value;
689692
}
690693
},
691694
getPrototypeOf(session) {
692-
return Reflect.getPrototypeOf(session[kSocket]);
695+
const socket = session[kSocket];
696+
if (socket === undefined)
697+
throw new ERR_HTTP2_SOCKET_UNBOUND();
698+
return Reflect.getPrototypeOf(socket);
693699
},
694700
set(session, prop, value) {
695701
switch (prop) {
@@ -705,7 +711,10 @@ const proxySocketHandler = {
705711
case 'write':
706712
throw new ERR_HTTP2_NO_SOCKET_MANIPULATION();
707713
default:
708-
session[kSocket][prop] = value;
714+
const socket = session[kSocket];
715+
if (socket === undefined)
716+
throw new ERR_HTTP2_SOCKET_UNBOUND();
717+
socket[prop] = value;
709718
return true;
710719
}
711720
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
if (!common.hasCrypto)
5+
common.skip('missing crypto');
6+
const http2 = require('http2');
7+
const net = require('net');
8+
9+
const server = http2.createServer();
10+
server.on('stream', common.mustCall((stream) => {
11+
stream.respond();
12+
stream.end('ok');
13+
}));
14+
15+
server.listen(0, common.mustCall(() => {
16+
const client = http2.connect(`http://localhost:${server.address().port}`);
17+
const socket = client.socket;
18+
const req = client.request();
19+
req.resume();
20+
req.on('close', common.mustCall(() => {
21+
client.close();
22+
server.close();
23+
24+
// Tests to make sure accessing the socket proxy fails with an
25+
// informative error.
26+
setImmediate(common.mustCall(() => {
27+
common.expectsError(() => {
28+
socket.example;
29+
}, {
30+
code: 'ERR_HTTP2_SOCKET_UNBOUND'
31+
});
32+
common.expectsError(() => {
33+
socket.example = 1;
34+
}, {
35+
code: 'ERR_HTTP2_SOCKET_UNBOUND'
36+
});
37+
common.expectsError(() => {
38+
socket instanceof net.Socket;
39+
}, {
40+
code: 'ERR_HTTP2_SOCKET_UNBOUND'
41+
});
42+
common.expectsError(() => {
43+
socket.ref();
44+
}, {
45+
code: 'ERR_HTTP2_SOCKET_UNBOUND'
46+
});
47+
common.expectsError(() => {
48+
socket.unref();
49+
}, {
50+
code: 'ERR_HTTP2_SOCKET_UNBOUND'
51+
});
52+
common.expectsError(() => {
53+
socket.setEncoding();
54+
}, {
55+
code: 'ERR_HTTP2_SOCKET_UNBOUND'
56+
});
57+
common.expectsError(() => {
58+
socket.setKeepAlive();
59+
}, {
60+
code: 'ERR_HTTP2_SOCKET_UNBOUND'
61+
});
62+
common.expectsError(() => {
63+
socket.setNoDelay();
64+
}, {
65+
code: 'ERR_HTTP2_SOCKET_UNBOUND'
66+
});
67+
}));
68+
}));
69+
}));

0 commit comments

Comments
 (0)