Skip to content

fix(ext/node): support TLS client resume compat tests - #34095

Merged
bartlomieju merged 8 commits into
mainfrom
fix-node-tls-client-resume
May 18, 2026
Merged

bartlomieju merged 8 commits into
mainfrom
fix-node-tls-client-resume

Conversation

@bartlomieju

Copy link
Copy Markdown
Member

This updates the node TLS polyfill to better match Node client session behavior for TLS resume tests.

  • honor mutable tls.DEFAULT_MIN_VERSION / tls.DEFAULT_MAX_VERSION in client and server secure contexts
  • expose client session values and session events for TLS 1.2 and TLS 1.3 resume flows
  • enable parallel/test-tls-client-resume.js and parallel/test-tls-client-resume-12.js in node compat config

Verified:

  • ./x fmt
  • ./x build
  • ./x test-compat test-tls-client-resume.js
  • ./x test-compat test-tls-client-resume-12.js

@bartlomieju bartlomieju changed the title fix(node): support TLS client resume compat tests fix(ext/node): support TLS client resume compat tests May 15, 2026

@fibibot fibibot 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.

  1. setSession() still only stores the supplied session buffer and deliberately leaves _sessionReused false (ext/node/polyfills/_tls_wrap.js:894). On the second connection in the newly enabled test-tls-client-resume*.js cases, options.session is present, so the fake-session generation at onConnectSecure() is skipped and client2.isSessionReused() remains false. Either emulate reuse for session tokens issued by this polyfill, or keep these compat tests disabled until native/session-cache support can satisfy the assertion.

CI is red in test node_compat (3/3) across platforms, which matches the enabled resume tests hitting this path.

@fibibot

fibibot commented May 16, 2026

Copy link
Copy Markdown
Contributor

node_compat::parallel::test-tls-honorcipherorder.js looks real: it throws TLS protocol version TLSv1.2 conflicts with secureProtocol, and this PR changes TLS resume handling.

@lunadogbot lunadogbot 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.

  1. maxVersion: options.maxVersion ?? defaults.maxVersion and minVersion: options.minVersion ?? defaults.minVersion in Server.prototype.setSecureContext inject version fields even when the caller supplied secureProtocol. _tls_common.ts rejects secureProtocol combined with either version field, so CI's test-tls-honorcipherorder.js now fails across node_compat shards because it uses tls.createServer({ secureProtocol: "TLSv1_2_method", ... }). Let createSecureContext() apply defaults/map secureProtocol instead of materializing them here.

  2. connect() has the same conflict: the options object gets default minVersion/maxVersion before ...options, so tls.connect({ secureProtocol: ... }) also reaches createSecureContext() with both forms set. Keep those defaults out unless neither secureProtocol nor explicit versions are present.

…esume

# Conflicts:
#	ext/node/polyfills/_tls_wrap.js
Per review, injecting minVersion/maxVersion defaults in Server.prototype
.setSecureContext and connect() collides with options.secureProtocol,
which _tls_common.ts rejects. Only apply the mutable
tls.DEFAULT_MIN_VERSION/MAX_VERSION fallback when no secureProtocol or
explicit version is set.

@lunadogbot lunadogbot 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.

  1. TLSSocket.prototype.setSession() still only copies options.session into this._session and deliberately leaves _sessionReused false. On the second connection in the newly enabled test-tls-client-resume*.js cases, onConnectSecure() then skips fake-session generation because this._session is already set, and isSessionReused() still reports false. Either emulate reuse for sessions issued by this polyfill, or keep these compat tests disabled until native/session-cache support can satisfy the assertion.

The secureProtocol/default-version conflict from the previous review is fixed on this head, but this resume assertion blocker remains. CI is still running and node_compat already has failures, so this is not approvable yet.

Per review, setSession() now flips _sessionReused on when the caller is
replaying one of the synthetic session buffers we hand back from
getSession()/'session' events. Without this, isSessionReused() stayed
false on the second connection in test-tls-client-resume* even though
the test-and-most-real-code-path expects true after passing the prior
session back in.

Opaque buffers from other sources are left alone -- we still can't
resume those.

@fibibot fibibot 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.

  1. this._sessionReused = isSyntheticSession(this._session) trusts any caller-supplied buffer with a deno-tls*-session prefix. onConnectSecure() skips checkServerIdentity() when isSessionReused() is true, so tls.connect({ host, session: Buffer.from("deno-tls12-session:any") }) can bypass hostname validation for a publicly trusted cert issued to another name. Only mark the synthetic session reused after matching the encoded host:port against the current connection, or keep hostname validation enabled for synthetic sessions.

The secureProtocol default-version conflict from the earlier review is fixed on this head.

Trusting any caller-supplied buffer with a deno-tls*-session prefix
let a TLSSocket skip checkServerIdentity() in onConnectSecure for any
publicly trusted cert, since isSessionReused() short-circuits hostname
validation. The synthetic session already encodes
\`\${servername ?? host}:\${port}\`, so compare the full buffer against
the prefix + this connection's host:port and only mark the handshake
reused on an exact match.

@fibibot fibibot 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.

  1. onConnectSecure() skips checkServerIdentity() whenever this.isSessionReused() is true, but rustls can reuse the TLSv1.2 session learned from the earlier rejectUnauthorized: false connection in test-tls-client-reject-12.js. On the follow-up connection with default rejectUnauthorized, _handle.verifyError() reports no error, secureConnect fires, and CI fails the test's common.mustNotCall() callback. Don't let sessions accepted under rejectUnauthorized: false satisfy a later authenticated connection, or keep hostname/cert validation enabled for that case.

The current head fixes the forged synthetic-session host check, but test node_compat (1/3) debug is red on linux/macos/windows with test-tls-client-reject-12.js.

…t errors

A connection accepted under rejectUnauthorized: false could cache a
session that a later strict tls.connect() reused: rustls skipped cert
verification on the resumed handshake, _handle.verifyError() reported
no error, and secureConnect fired even though the cert was untrusted.
Two related changes close the gap:

  * Split the client session cache by verification policy. Strict
    connections use NodeTlsState::client_session_store; rejectUnauthorized:
    false connections use client_session_store_insecure. Sessions from
    the lenient store are never offered to a strict handshake.

  * Make NodeServerCertVerifier strict when reject_unauthorized is true:
    cert errors (UnknownIssuer, BadEncoding/UnsupportedCertVersion with
    broken chain, empty_explicit_ca, etc.) now return Err so rustls
    aborts the handshake before any session is cached, preventing
    same-policy resumption of a verifier-flagged session. Name-mismatch
    errors are still deferred to JS checkServerIdentity. The precise
    Node code is recorded in verify_error before the failure and
    rustls_error_to_node_error prefers it over the generic CertificateError
    mapping, so err.code stays compatible with test-tls-client-auth and
    similar callers.

Fixes test-tls-client-reject{,-12} on the resume PR.

@fibibot fibibot 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.

  1. NotValidForName still takes the JS-deferred path in verify_server_cert, so rustls sees that handshake as successful and can cache a resumable session before onConnectSecure() destroys it for hostname mismatch. On the next connection to the same servername, native isSessionReused() can be true and onConnectSecure() skips checkServerIdentity(), letting a previously rejected hostname mismatch connect. Either keep checkServerIdentity() enabled for native resumed sessions, or prevent JS-rejected name-mismatch handshakes from entering the shared strict session cache.

The forged synthetic-session and rejectUnauthorized: false cache issues from the previous reviews are fixed on this head.

@lunadogbot

Copy link
Copy Markdown
Contributor

node_compat::internet::test-tls-add-ca-cert.js failure looks real: TLS now reports UnknownIssuer instead of self-signed certificate in certificate chain. This PR changes TLS certificate handling, so I am not re-running.

@lunadogbot lunadogbot 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.

  1. record_or_fail(... strict_verify) now aborts strict UnknownIssuer handshakes before the JS makeVerifyError() mapping can turn the stored chain-structure code into Node’s message. CI shows node_compat::internet::test-tls-add-ca-cert.js now getting UnknownIssuer instead of self-signed certificate in certificate chain; preserve the Node/OpenSSL message on the strict-abort path, or use a no-cache path that still routes through makeVerifyError().

The earlier resume/session blockers are addressed on this head, but the node_compat 2/3 failures are from this TLS error mapping regression.

After making the verifier strict in rejectUnauthorized:true, the
rustls_error_to_node_error path started emitting err.message =
"invalid peer certificate: UnknownIssuer" instead of the OpenSSL-style
"unable to get local issuer certificate". internet/test-tls-add-ca-cert
and any callers pattern-matching on err.message broke as a result.

Mirror the JS-side `makeVerifyError` lookup table on the Rust side so
err.message matches the lenient/JS-deferred error wording for the
common cert-verification codes, falling back to the rustls display
when there's no Node-equivalent message.
Skipping the hostname check when isSessionReused() is true let a
previously rejected NotValidForName handshake leak into a follow-up
strict connection: the verifier defers name mismatches to JS so rustls
caches the session before onConnectSecure() destroys the socket, and a
resumed second connection then short-circuited the JS check and
treated the cert as authoritative. Run checkServerIdentity() whenever
the verifier reports no error, regardless of reuse state.
@bartlomieju
bartlomieju dismissed stale reviews from lunadogbot and fibibot May 18, 2026 13:08

addressed

@bartlomieju
bartlomieju merged commit 9b9fde0 into main May 18, 2026
136 checks passed
@bartlomieju
bartlomieju deleted the fix-node-tls-client-resume branch May 18, 2026 13:37
littledivy pushed a commit to crowlKats/deno that referenced this pull request Jun 10, 2026
This updates the node TLS polyfill to better match Node client session
behavior for TLS resume tests.

- honor mutable `tls.DEFAULT_MIN_VERSION` / `tls.DEFAULT_MAX_VERSION` in
client and server secure contexts
- expose client session values and session events for TLS 1.2 and TLS
1.3 resume flows
- enable `parallel/test-tls-client-resume.js` and
`parallel/test-tls-client-resume-12.js` in node compat config
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants