Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 25b87d1

Browse files
indutnydanielleadams
authored andcommittedJul 6, 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 68f2c49 commit 25b87d1

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed
 

‎lib/_tls_wrap.js

+8
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,14 @@ TLSSocket.prototype._init = function(socket, wrap) {
814814
}
815815
}
816816

817+
// We can only come here via [kWrapConnectedHandle]() call that happens
818+
// if the connection is established with `autoSelectFamily` set to `true`.
819+
const connectOptions = this[kConnectOptions];
820+
if (!options.isServer && connectOptions) {
821+
if (connectOptions.servername) {
822+
this.setServername(connectOptions.servername);
823+
}
824+
}
817825

818826
if (options.handshakeTimeout > 0)
819827
this.setTimeout(options.handshakeTimeout, this._handleTimeout);

‎test/parallel/test-https-happy-eyeballs.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ function createDnsServer(ipv6Addr, ipv4Addr, cb) {
8080
// Test that IPV4 is reached if IPV6 is not reachable
8181
{
8282
createDnsServer('::1', '127.0.0.1', common.mustCall(function({ dnsServer, lookup }) {
83-
const ipv4Server = createServer(options, common.mustCall((_, res) => {
83+
const ipv4Server = createServer(options, common.mustCall((req, res) => {
84+
assert.strictEqual(req.socket.servername, 'example.org');
8485
res.writeHead(200, { Connection: 'close' });
8586
res.end('response-ipv4');
8687
}));
@@ -92,7 +93,8 @@ function createDnsServer(ipv6Addr, ipv4Addr, cb) {
9293
lookup,
9394
rejectUnauthorized: false,
9495
autoSelectFamily: true,
95-
autoSelectFamilyAttemptTimeout
96+
autoSelectFamilyAttemptTimeout,
97+
servername: 'example.org',
9698
},
9799
(res) => {
98100
assert.strictEqual(res.statusCode, 200);
@@ -118,12 +120,14 @@ function createDnsServer(ipv6Addr, ipv4Addr, cb) {
118120
// Test that IPV4 is NOT reached if IPV6 is reachable
119121
if (common.hasIPv6) {
120122
createDnsServer('::1', '127.0.0.1', common.mustCall(function({ dnsServer, lookup }) {
121-
const ipv4Server = createServer(options, common.mustNotCall((_, res) => {
123+
const ipv4Server = createServer(options, common.mustNotCall((req, res) => {
124+
assert.strictEqual(req.socket.servername, 'example.org');
122125
res.writeHead(200, { Connection: 'close' });
123126
res.end('response-ipv4');
124127
}));
125128

126-
const ipv6Server = createServer(options, common.mustCall((_, res) => {
129+
const ipv6Server = createServer(options, common.mustCall((req, res) => {
130+
assert.strictEqual(req.socket.servername, 'example.org');
127131
res.writeHead(200, { Connection: 'close' });
128132
res.end('response-ipv6');
129133
}));
@@ -139,6 +143,7 @@ if (common.hasIPv6) {
139143
rejectUnauthorized: false,
140144
autoSelectFamily: true,
141145
autoSelectFamilyAttemptTimeout,
146+
servername: 'example.org',
142147
},
143148
(res) => {
144149
assert.strictEqual(res.statusCode, 200);

0 commit comments

Comments
 (0)
Please sign in to comment.