Skip to content

Commit 4c24ef8

Browse files
islandryumarco-ippolito
authored andcommitted
http2: omit server name when HTTP2 host is IP address
Fixes: #56189 PR-URL: #56530 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Yongsheng Zhang <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent ca79914 commit 4c24ef8

File tree

2 files changed

+68
-9
lines changed

2 files changed

+68
-9
lines changed

‎lib/internal/http2/core.js‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -645,15 +645,21 @@ function initOriginSet(session){
645645
if(originSet===undefined){
646646
constsocket=session[kSocket];
647647
session[kState].originSet=originSet=newSafeSet();
648-
if(socket.servername!=null){
649-
letoriginString=`https://${socket.servername}`;
650-
if(socket.remotePort!=null)
651-
originString+=`:${socket.remotePort}`;
652-
// We have to ensure that it is a properly serialized
653-
// ASCII origin string. The socket.servername might not
654-
// be properly ASCII encoded.
655-
originSet.add(getURLOrigin(originString));
648+
lethostName=socket.servername;
649+
if(hostName===null||hostName===false){
650+
if(socket.remoteFamily==='IPv6'){
651+
hostName=`[${socket.remoteAddress}]`;
652+
}else{
653+
hostName=socket.remoteAddress;
654+
}
656655
}
656+
letoriginString=`https://${hostName}`;
657+
if(socket.remotePort!=null)
658+
originString+=`:${socket.remotePort}`;
659+
// We have to ensure that it is a properly serialized
660+
// ASCII origin string. The socket.servername might not
661+
// be properly ASCII encoded.
662+
originSet.add(getURLOrigin(originString));
657663
}
658664
returnoriginSet;
659665
}
@@ -3352,7 +3358,7 @@ function connect(authority, options, listener){
33523358
socket=net.connect({ port, host, ...options});
33533359
break;
33543360
case'https:':
3355-
socket=tls.connect(port,host,initializeTLSOptions(options,host));
3361+
socket=tls.connect(port,host,initializeTLSOptions(options,net.isIP(host) ? undefined : host));
33563362
break;
33573363
default:
33583364
thrownewERR_HTTP2_UNSUPPORTED_PROTOCOL(protocol);
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
3+
constcommon=require('../common');
4+
if(!common.hasCrypto){common.skip('missing crypto');}
5+
constassert=require('assert');
6+
constfixtures=require('../common/fixtures');
7+
consth2=require('http2');
8+
9+
functionloadKey(keyname){
10+
returnfixtures.readKey(keyname,'binary');
11+
}
12+
13+
constkey=loadKey('agent8-key.pem');
14+
constcert=fixtures.readKey('agent8-cert.pem');
15+
16+
constserver=h2.createSecureServer({ key, cert });
17+
consthasIPv6=common.hasIPv6;
18+
consttestCount=hasIPv6 ? 2 : 1;
19+
20+
server.on('stream',common.mustCall((stream)=>{
21+
constsession=stream.session;
22+
assert.strictEqual(session.servername,undefined);
23+
stream.respond({'content-type': 'application/json'});
24+
stream.end(JSON.stringify({
25+
servername: session.servername,
26+
originSet: session.originSet
27+
})
28+
);
29+
},testCount));
30+
31+
letdone=0;
32+
33+
server.listen(0,common.mustCall(()=>{
34+
functionhandleRequest(url){
35+
constclient=h2.connect(url,
36+
{rejectUnauthorized: false});
37+
constreq=client.request();
38+
letdata='';
39+
req.setEncoding('utf8');
40+
req.on('data',(d)=>data+=d);
41+
req.on('end',common.mustCall(()=>{
42+
constoriginSet=req.session.originSet;
43+
assert.strictEqual(originSet[0],url);
44+
client.close();
45+
if(++done===testCount)server.close();
46+
}));
47+
}
48+
49+
constipv4Url=`https://127.0.0.1:${server.address().port}`;
50+
constipv6Url=`https://[::1]:${server.address().port}`;
51+
handleRequest(ipv4Url);
52+
if(hasIPv6)handleRequest(ipv6Url);
53+
}));

0 commit comments

Comments
(0)