Skip to content

tls: fix newSession after destroy and OCSP cert - #67650

Open
ShogunPanda wants to merge 1 commit into
nodejs:mainfrom
ShogunPanda:tls-ignore-handshake-after-destroy
Open

ShogunPanda wants to merge 1 commit into
nodejs:mainfrom
ShogunPanda:tls-ignore-handshake-after-destroy

Conversation

@ShogunPanda

Copy link
Copy Markdown
Contributor

Two small fixes in lib/internal/tls/wrap.js.

The callback of the newSession event only checked that the handle was still set. If a TLS error destroyed the socket while the callback was pending, calling it afterwards still completed the handshake and emitted 'secure' and 'secureConnection' on the destroyed socket. It now returns early if the socket is already destroyed. The callbacks of OCSPRequest, SNICallback and resumeSession do not need the same check: the handshake does not read while they are pending, so a TLS error cannot destroy the socket in the meantime, and the existing _handle checks cover the other ways of closing it.

When the SNICallback returns a context that has no certificate, for example one that only provides CAs, the handshake uses the certificate of the server. The OCSPRequest event was still emitted with the certificate and the issuer of that context, which are both null. It now uses the context of the server in that case, so that the event reports the certificate that is actually in use.

Testing: added test-tls-handshake-complete-after-destroy-newsession.js and test-tls-ocsp-request-sni-without-certificate.js; both fail without the change. The parallel/test-tls-* and parallel/test-https-* suites and eslint pass.

The callback of the `newSession` event only checked that the handle was
still set. If a TLS error destroyed the socket while the callback was
pending, calling it afterwards still completed the handshake and
emitted `'secure'` and `'secureConnection'` on the destroyed socket.
Return early from it if the socket is already destroyed.

The callbacks of `OCSPRequest`, `SNICallback` and `resumeSession` do not
need the same check: the handshake does not read while they are
pending, so a TLS error cannot destroy the socket in the meantime, and
if the socket is closed some other way the existing `_handle` checks
already cover it.

Also, when the `SNICallback` returns a context that has no certificate,
for example one that only provides CAs, the handshake uses the
certificate of the server. The `OCSPRequest` event was still emitted
with the certificate and the issuer of that context, which are both
`null`. Use the context of the server in that case, so that the event
reports the certificate that is actually in use.

Assisted-By: OpenAI GPT-5.6 Sol <openai/gpt-5.6-sol>
Signed-off-by: Paolo Insogna <paolo@cowtech.it>
@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 Nov 20, 2026
@codecov

codecov Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.73%. Comparing base (5884b8e) to head (704ee02).
⚠️ Report is 10 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67650      +/-   ##
==========================================
+ Coverage   90.71%   90.73%   +0.01%     
==========================================
  Files         801      801              
  Lines      280906   280915       +9     
  Branches    54249    54266      +17     
==========================================
+ Hits       254832   254883      +51     
+ Misses      16246    16197      -49     
- Partials     9828     9835       +7     
Files with missing lines Coverage Δ
lib/internal/tls/wrap.js 95.81% <100.00%> (+0.01%) ⬆️

... and 37 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.

valorjesson-commits

This comment was marked as spam.

@Ethan-Arrowood Ethan-Arrowood left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lgtm!

@Ethan-Arrowood Ethan-Arrowood added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 20, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 20, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

4 participants