Skip to content

Commit e049ce2

Browse files
indutnyMoLow
authored andcommitted
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 <paolo@cowtech.it> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
1 parent 033d0bb commit e049ce2

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

‎lib/_tls_wrap.js‎

Lines changed: 8 additions & 0 deletions
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+
constconnectOptions=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‎

Lines changed: 9 additions & 4 deletions
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-
constipv4Server=createServer(options,common.mustCall((_,res)=>{
83+
constipv4Server=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-
constipv4Server=createServer(options,common.mustNotCall((_,res)=>{
123+
constipv4Server=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-
constipv6Server=createServer(options,common.mustCall((_,res)=>{
129+
constipv6Server=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)