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 <ruben@bridgewater.de> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent d0be932 commit 6a396ff

4 files changed

Lines changed: 87 additions & 2 deletions

File tree

‎doc/api/errors.md‎

Lines changed: 6 additions & 0 deletions
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+
<aid="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
<aid="ERR_HTTP2_STATUS_101"></a>
847853
### ERR_HTTP2_STATUS_101
848854

‎lib/internal/errors.js‎

Lines changed: 2 additions & 0 deletions
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‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,12 +641,17 @@ const proxySocketHandler = {
641641
thrownewerrors.Error('ERR_HTTP2_NO_SOCKET_MANIPULATION');
642642
default:
643643
constsocket=session[kSocket];
644+
if(socket===undefined)
645+
thrownewerrors.Error('ERR_HTTP2_SOCKET_UNBOUND');
644646
constvalue=socket[prop];
645647
returntypeofvalue==='function' ? value.bind(socket) : value;
646648
}
647649
},
648650
getPrototypeOf(session){
649-
returnReflect.getPrototypeOf(session[kSocket]);
651+
constsocket=session[kSocket];
652+
if(socket===undefined)
653+
thrownewerrors.Error('ERR_HTTP2_SOCKET_UNBOUND');
654+
returnReflect.getPrototypeOf(socket);
650655
},
651656
set(session,prop,value){
652657
switch(prop){
@@ -662,7 +667,10 @@ const proxySocketHandler = {
662667
case'write':
663668
thrownewerrors.Error('ERR_HTTP2_NO_SOCKET_MANIPULATION');
664669
default:
665-
session[kSocket][prop]=value;
670+
constsocket=session[kSocket];
671+
if(socket===undefined)
672+
thrownewerrors.Error('ERR_HTTP2_SOCKET_UNBOUND');
673+
socket[prop]=value;
666674
returntrue;
667675
}
668676
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
'use strict';
2+
3+
constcommon=require('../common');
4+
if(!common.hasCrypto)
5+
common.skip('missing crypto');
6+
consthttp2=require('http2');
7+
constnet=require('net');
8+
9+
constserver=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+
constclient=http2.connect(`http://localhost:${server.address().port}`);
17+
constsocket=client.socket;
18+
constreq=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+
socketinstanceofnet.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)