Skip to content

Commit 4202045

Browse files
islandryuaduh95
authored andcommitted
http2: omit server name when HTTP2 host is IP address
Fixes: #56189 PR-URL: #56530 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 86d7ba0 commit 4202045

File tree

2 files changed

+68
-9
lines changed

2 files changed

+68
-9
lines changed

lib/internal/http2/core.js

+15-9
Original file line numberDiff line numberDiff line change
@@ -645,15 +645,21 @@ function initOriginSet(session) {
645645
if (originSet === undefined) {
646646
const socket = session[kSocket];
647647
session[kState].originSet = originSet = new SafeSet();
648-
if (socket.servername != null) {
649-
let originString = `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+
let hostName = 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+
let originString = `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
return originSet;
659665
}
@@ -3342,7 +3348,7 @@ function connect(authority, options, listener) {
33423348
socket = net.connect({ port, host, ...options });
33433349
break;
33443350
case 'https:':
3345-
socket = tls.connect(port, host, initializeTLSOptions(options, host));
3351+
socket = tls.connect(port, host, initializeTLSOptions(options, net.isIP(host) ? undefined : host));
33463352
break;
33473353
default:
33483354
throw new ERR_HTTP2_UNSUPPORTED_PROTOCOL(protocol);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
if (!common.hasCrypto) { common.skip('missing crypto'); };
5+
const assert = require('assert');
6+
const fixtures = require('../common/fixtures');
7+
const h2 = require('http2');
8+
9+
function loadKey(keyname) {
10+
return fixtures.readKey(keyname, 'binary');
11+
}
12+
13+
const key = loadKey('agent8-key.pem');
14+
const cert = fixtures.readKey('agent8-cert.pem');
15+
16+
const server = h2.createSecureServer({ key, cert });
17+
const hasIPv6 = common.hasIPv6;
18+
const testCount = hasIPv6 ? 2 : 1;
19+
20+
server.on('stream', common.mustCall((stream) => {
21+
const session = 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+
let done = 0;
32+
33+
server.listen(0, common.mustCall(() => {
34+
function handleRequest(url) {
35+
const client = h2.connect(url,
36+
{ rejectUnauthorized: false });
37+
const req = client.request();
38+
let data = '';
39+
req.setEncoding('utf8');
40+
req.on('data', (d) => data += d);
41+
req.on('end', common.mustCall(() => {
42+
const originSet = req.session.originSet;
43+
assert.strictEqual(originSet[0], url);
44+
client.close();
45+
if (++done === testCount) server.close();
46+
}));
47+
}
48+
49+
const ipv4Url = `https://127.0.0.1:${server.address().port}`;
50+
const ipv6Url = `https://[::1]:${server.address().port}`;
51+
handleRequest(ipv4Url);
52+
if (hasIPv6) handleRequest(ipv6Url);
53+
}));

0 commit comments

Comments
 (0)