Skip to content

fix(ext/node): implement ALPNCallback and SNICallback for TLS server#33360

Open
bartlomieju wants to merge 3 commits intomainfrom
fix/tls-alpn-sni-callbacks
Open

fix(ext/node): implement ALPNCallback and SNICallback for TLS server#33360
bartlomieju wants to merge 3 commits intomainfrom
fix/tls-alpn-sni-callbacks

Conversation

@bartlomieju
Copy link
Copy Markdown
Member

Summary

Implements ALPNCallback and SNICallback options for node:tls.createServer(), which were previously accepted but never invoked.

  • ALPNCallback: Called during TLS handshake with the client's offered protocols, returns the selected protocol
  • SNICallback: Called with the client's SNI hostname, returns a SecureContext for certificate selection
  • getServername(): Now returns the client's SNI on server-side TLS connections
  • Validation: ERR_TLS_ALPN_CALLBACK_WITH_PROTOCOLS when both ALPNCallback and ALPNProtocols are set, ERR_TLS_ALPN_CALLBACK_INVALID_RESULT when callback returns a protocol not in the client's list
  • Alert mapping: NoApplicationProtocol alert now correctly maps to ERR_SSL_TLSV1_ALERT_NO_APPLICATION_PROTOCOL

Approach

Uses rustls's Acceptor API to intercept the ClientHello before creating the ServerConnection. This allows extracting SNI and ALPN from the client hello, calling back to JS for callback-based selection, then building a per-connection ServerConfig. The existing fast path (direct ServerConnection::new) is preserved when no callbacks are set.

Closes #33346

Test plan

  • New spec tests: tests/specs/node/tls_alpn_sni_callbacks/ (ALPNCallback, SNICallback, both together)
  • Existing TLS tests pass (tls_jsstreamsocket_close, tls-connect-simple)
  • Static ALPNProtocols (without callback) still works
  • cargo clippy clean

Implements ALPNCallback and SNICallback options for node:tls server,
fixing protocol negotiation and certificate selection that previously
reported false/null.

Uses rustls's Acceptor API to intercept the ClientHello before creating
the ServerConnection, allowing JS callbacks to select certificates and
ALPN protocols per-connection. The fast path (no callbacks) is preserved.

Also adds getServername() support for server-side TLS connections,
NoApplicationProtocol alert mapping, and ALPN callback validation
(ERR_TLS_ALPN_CALLBACK_INVALID_RESULT, ERR_TLS_ALPN_CALLBACK_WITH_PROTOCOLS).

Closes #33346
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.

node:tls server does not correctly negotiate ALPN via ALPNCallback (returns false instead of selected protocol)

1 participant