Skip to content

Commit 24774cc

Browse files
committed
tls: initialize session and SNI before connecting
A synchronous custom lookup can abort the socket before tls.connect() applies the session and SNI, leaving the TLS handle unavailable. Initialize both before starting the connection so the original socket error is emitted normally. Assisted-by: Codex Signed-off-by: Steven <steven@ceriously.com>
1 parent 0544741 commit 24774cc

2 files changed

Lines changed: 28 additions & 7 deletions

File tree

lib/internal/tls/wrap.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1898,6 +1898,12 @@ exports.connect = function connect(...args) {
18981898
if (cb)
18991899
tlssock.once('secureConnect', cb);
19001900

1901+
if (options.session)
1902+
tlssock.setSession(options.session);
1903+
1904+
if (options.servername)
1905+
tlssock.setServername(options.servername);
1906+
19011907
if (!options.socket) {
19021908
// If user provided the socket, it's their responsibility to manage its
19031909
// connectivity. If we created one internally, we connect it.
@@ -1910,13 +1916,6 @@ exports.connect = function connect(...args) {
19101916

19111917
tlssock._releaseControl();
19121918

1913-
if (options.session)
1914-
tlssock.setSession(options.session);
1915-
1916-
if (options.servername) {
1917-
tlssock.setServername(options.servername);
1918-
}
1919-
19201919
if (options.socket)
19211920
tlssock._start();
19221921

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
if (!common.hasCrypto)
5+
common.skip('missing crypto');
6+
7+
const tls = require('node:tls');
8+
9+
// Verify that a synchronous lookup cannot interrupt TLS socket initialization.
10+
const controller = new AbortController();
11+
const socket = tls.connect({
12+
host: 'example.com',
13+
servername: 'example.com',
14+
port: 443,
15+
signal: controller.signal,
16+
lookup(_hostname, _options, callback) {
17+
callback(null, [{ address: '2001:db8::1', family: 6 }]);
18+
controller.abort();
19+
},
20+
});
21+
22+
socket.on('error', common.mustCall());

0 commit comments

Comments
 (0)