Skip to content

Commit fd2620d

Browse files
indutnytargos
authored andcommittedJun 4, 2023
tls: reapply servername on happy eyeballs connect
When establishing a TLS connection to a server with `autoSelectFamily` set to `true`, the `net.Socket` will call `[kWrapConnectedHandle]()` to reinitialize the socket (in case if it got broken during previous connect attempts). Unfortunately, prior to this patch this resulted in a brand new `TLSWrap` instance being created for the socket. While most of the configuration of `TLSWrap` is restored, the `servername` was sadly dropped and not reinitalized. With this patch `servername` will be reinitialized if there are `tls.connect` options present on the `TLSSocket` instance, making it possible to connect with "Happy Eyeballs" to TLS servers that require the servername extension. PR-URL: #48255 Reviewed-By: Paolo Insogna <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Minwoo Jung <[email protected]>
1 parent 3f259b7 commit fd2620d

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed
 

‎lib/_tls_wrap.js

+8
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,14 @@ TLSSocket.prototype._init = function(socket, wrap) {
832832
}
833833
}
834834

835+
// We can only come here via [kWrapConnectedHandle]() call that happens
836+
// if the connection is established with `autoSelectFamily` set to `true`.
837+
const connectOptions = this[kConnectOptions];
838+
if (!options.isServer && connectOptions) {
839+
if (connectOptions.servername) {
840+
this.setServername(connectOptions.servername);
841+
}
842+
}
835843

836844
if (options.handshakeTimeout > 0)
837845
this.setTimeout(options.handshakeTimeout, this._handleTimeout);

‎test/parallel/test-https-autoselectfamily.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ function createDnsServer(ipv6Addr, ipv4Addr, cb) {
7474
// Test that IPV4 is reached if IPV6 is not reachable
7575
{
7676
createDnsServer('::1', '127.0.0.1', common.mustCall(function({ dnsServer, lookup }) {
77-
const ipv4Server = createServer(options, common.mustCall((_, res) => {
77+
const ipv4Server = createServer(options, common.mustCall((req, res) => {
78+
assert.strictEqual(req.socket.servername, 'example.org');
7879
res.writeHead(200, { Connection: 'close' });
7980
res.end('response-ipv4');
8081
}));
@@ -86,6 +87,7 @@ function createDnsServer(ipv6Addr, ipv4Addr, cb) {
8687
lookup,
8788
rejectUnauthorized: false,
8889
autoSelectFamily: true,
90+
servername: 'example.org',
8991
},
9092
(res) => {
9193
assert.strictEqual(res.statusCode, 200);
@@ -111,12 +113,14 @@ function createDnsServer(ipv6Addr, ipv4Addr, cb) {
111113
// Test that IPV4 is NOT reached if IPV6 is reachable
112114
if (common.hasIPv6) {
113115
createDnsServer('::1', '127.0.0.1', common.mustCall(function({ dnsServer, lookup }) {
114-
const ipv4Server = createServer(options, common.mustNotCall((_, res) => {
116+
const ipv4Server = createServer(options, common.mustNotCall((req, res) => {
117+
assert.strictEqual(req.socket.servername, 'example.org');
115118
res.writeHead(200, { Connection: 'close' });
116119
res.end('response-ipv4');
117120
}));
118121

119-
const ipv6Server = createServer(options, common.mustCall((_, res) => {
122+
const ipv6Server = createServer(options, common.mustCall((req, res) => {
123+
assert.strictEqual(req.socket.servername, 'example.org');
120124
res.writeHead(200, { Connection: 'close' });
121125
res.end('response-ipv6');
122126
}));
@@ -131,6 +135,7 @@ if (common.hasIPv6) {
131135
lookup,
132136
rejectUnauthorized: false,
133137
autoSelectFamily: true,
138+
servername: 'example.org',
134139
},
135140
(res) => {
136141
assert.strictEqual(res.statusCode, 200);

0 commit comments

Comments
 (0)
Please sign in to comment.