Skip to content

tls: initialize session and SNI before connecting - #65624

Open
styfle wants to merge 1 commit into
nodejs:mainfrom
styfle:styfle/tls-sync-lookup
Open

tls: initialize session and SNI before connecting#65624
styfle wants to merge 1 commit into
nodejs:mainfrom
styfle:styfle/tls-sync-lookup

Conversation

@styfle

@styfle styfle commented Aug 28, 2026

Copy link
Copy Markdown
Member

A synchronous custom lookup callback can cause the connection to fail or abort before tls.connect() applies the TLS session and SNI. The failure clears the TLS handle, causing setServername() to throw a synchronous TypeError.

The original failure surfaced through an HTTP client as:

node:events:497
      throw er; // Unhandled 'error' event
      ^
Error: connect ENETUNREACH 2001:db8::1:443 - Local (:::0)
    at internalConnect (node:net:1111:16)
    at defaultTriggerAsyncIdScope (node:internal/async_hooks:472:18)
    at emitLookup (node:net:1524:9)
    at lookup (/app/services-ts/dist/src/request.js:103:33)
    at emitLookup (node:net:1447:5)
    at defaultTriggerAsyncIdScope (node:internal/async_hooks:472:18)
    at lookupAndConnectMultiple (node:net:1446:3)
    at node:net:1392:7
    at defaultTriggerAsyncIdScope (node:internal/async_hooks:472:18)
    at lookupAndConnect (node:net:1391:5)
Emitted 'error' event on TLSSocket instance at:
    at emitErrorNT (node:internal/streams/destroy:170:8)
    at emitErrorCloseNT (node:internal/streams/destroy:129:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:89:21) {
  errno: -101,
  code: 'ENETUNREACH',
  syscall: 'connect',
  address: '2001:db8::1',
  port: 443
}

Initialize the session and SNI before starting the underlying TCP connection so the original connection error is emitted normally.

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/crypto
  • @nodejs/net

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. tls Issues and PRs related to the tls subsystem. labels Aug 28, 2026
@styfle
styfle force-pushed the styfle/tls-sync-lookup branch from 5f0fe2b to 737a73d Compare August 28, 2026 17:30
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>
@styfle
styfle force-pushed the styfle/tls-sync-lookup branch from 737a73d to 24774cc Compare August 28, 2026 17:31
@codecov

codecov Bot commented Aug 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.04%. Comparing base (0544741) to head (24774cc).
⚠️ Report is 12 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #65624      +/-   ##
==========================================
- Coverage   90.07%   90.04%   -0.04%     
==========================================
  Files         751      751              
  Lines      254921   254920       -1     
  Branches    48129    48124       -5     
==========================================
- Hits       229627   229545      -82     
- Misses      16479    16544      +65     
- Partials     8815     8831      +16     
Files with missing lines Coverage Δ
lib/internal/tls/wrap.js 95.17% <100.00%> (-0.01%) ⬇️

... and 35 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina mcollina added the request-ci Add this label to start a Jenkins CI on a PR. label Aug 28, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Aug 28, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@aduh95 aduh95 added the author ready PRs with CI started, the required approvals, and no outstanding review comments. label Aug 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs with CI started, the required approvals, and no outstanding review comments. needs-ci PRs that need a full CI run. tls Issues and PRs related to the tls subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants