Skip to content

Commit 6a396ff

Browse files
jasnellBethGriggs
authored andcommitted
http2: throw better error when accessing unbound socket proxy
Fixes: #22268 Backport-PR-URL: #22850 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 d0be932 commit 6a396ff

File tree

4 files changed

+87
-2
lines changed

4 files changed

+87
-2
lines changed

doc/api/errors.md

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

846+
<a id="ERR_HTTP2_SOCKET_UNBOUND"></a>
847+
### ERR_HTTP2_SOCKET_UNBOUND
848+
849+
An attempt was made to use the `socket` property of an `Http2Session` that
850+
has already been closed.
851+
846852
<a id="ERR_HTTP2_STATUS_101"></a>
847853
### ERR_HTTP2_STATUS_101
848854

lib/internal/errors.js

+2
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,8 @@ E('ERR_HTTP2_SESSION_ERROR', 'Session closed with error code %s');
338338
E('ERR_HTTP2_SETTINGS_CANCEL', 'HTTP2 session settings canceled');
339339
E('ERR_HTTP2_SOCKET_BOUND',
340340
'The socket is already bound to an Http2Session');
341+
E('ERR_HTTP2_SOCKET_UNBOUND',
342+
'The socket has been disconnected from the Http2Session');
341343
E('ERR_HTTP2_STATUS_101',
342344
'HTTP status code 101 (Switching Protocols) is forbidden in HTTP/2');
343345
E('ERR_HTTP2_STATUS_INVALID', 'Invalid status code: %s');

lib/internal/http2/core.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -641,12 +641,17 @@ const proxySocketHandler = {
641641
throw new errors.Error('ERR_HTTP2_NO_SOCKET_MANIPULATION');
642642
default:
643643
const socket = session[kSocket];
644+
if (socket === undefined)
645+
throw new errors.Error('ERR_HTTP2_SOCKET_UNBOUND');
644646
const value = socket[prop];
645647
return typeof value === 'function' ? value.bind(socket) : value;
646648
}
647649
},
648650
getPrototypeOf(session) {
649-
return Reflect.getPrototypeOf(session[kSocket]);
651+
const socket = session[kSocket];
652+
if (socket === undefined)
653+
throw new errors.Error('ERR_HTTP2_SOCKET_UNBOUND');
654+
return Reflect.getPrototypeOf(socket);
650655
},
651656
set(session, prop, value) {
652657
switch (prop) {
@@ -662,7 +667,10 @@ const proxySocketHandler = {
662667
case 'write':
663668
throw new errors.Error('ERR_HTTP2_NO_SOCKET_MANIPULATION');
664669
default:
665-
session[kSocket][prop] = value;
670+
const socket = session[kSocket];
671+
if (socket === undefined)
672+
throw new errors.Error('ERR_HTTP2_SOCKET_UNBOUND');
673+
socket[prop] = value;
666674
return true;
667675
}
668676
}
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)