Repository navigation
Description
Summary
connect() with secureTransport: 'on', and startTls(), give a Worker no control over the TLS handshake: SocketOptions has only secureTransport and allowHalfOpen. This proposes a tls options object that configures the ClientHello. It builds on the alpn option already requested in #8287, which follows the WinterTC sockets draft.
Why
- Protocols chosen by ALPN. HTTP/2 (
h2) over a socket, or PostgreSQL direct TLS, need ALPN (TCP sockets: allow setting ALPN on connect() and startTls() (needed for PostgreSQL 17+ direct TLS) #8287). node:tlscompatibility. Node'stls.connect()definesALPNProtocols,ciphers,ecdhCurve,sigalgs,minVersionandmaxVersion. workerd'snode:tlsthrows onALPNProtocolsand silently drops the others (internal_tls_wrap.ts,internal_tls_common.ts), becauseconnect()has nowhere to carry them.- Matching a specific client's TLS profile. Some servers expect the ClientHello of a particular client, such as a mainstream browser, and treat other handshakes differently. Today a Worker can only do this by bringing its own TLS stack.
I built that workaround: BoringSSL compiled to WebAssembly, driven over a raw connect() socket. It reproduces the target ClientHello exactly (identical JA4), but it is expensive. Measured on deployed Workers with 88 KB brotli responses:
| CPU per response | |
|---|---|
fetch() |
~1.4 ms |
BoringSSL in Wasm over connect() |
~7 ms, ~4.5 ms after moving AES-GCM decryption and brotli to the runtime's native implementations |
On top of that come ~5 ms per full handshake and ~40 ms on a cold isolate to load the Wasm and parse a root store. It also leaves every user maintaining a security-sensitive TLS client. The runtime already has a native TLS client; letting the Worker configure it would remove all of this.
Proposed API
const socket = connect(address, {
secureTransport: 'on',
alpn: ['h2', 'http/1.1'], // #8287
tls: {
minVersion: 'TLSv1.2', // node:tls minVersion / maxVersion
maxVersion: 'TLSv1.3',
ciphers: 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256', // node:tls ciphers, ordered
groups: ['X25519', 'P-256', 'P-384'], // node:tls ecdhCurve, ordered
signatureAlgorithms: ['ecdsa_secp256r1_sha256', 'rsa_pss_rsae_sha256'], // node:tls sigalgs
},
});
socket.startTls({ expectedServerHostname, alpn, tls: { /* same options */ } });A second group of options, specific to BoringSSL, would let a Worker reproduce a browser's ClientHello rather than approximate it. Each maps to a single BoringSSL call:
| Option | BoringSSL | Per connection? |
|---|---|---|
keyShares: string[] |
SSL_set1_client_key_shares |
Yes |
permuteExtensions: boolean |
SSL_set_permute_extensions |
Yes |
echGrease: boolean |
SSL_set_enable_ech_grease |
Yes |
requestOcsp, requestSct: boolean |
SSL_enable_ocsp_stapling, SSL_enable_signed_cert_timestamps |
Yes |
applicationSettings: Record<string, Uint8Array> (ALPS) |
SSL_add_application_settings, SSL_set_alps_use_new_codepoint |
Yes |
grease: boolean |
SSL_CTX_set_grease_enabled |
No, SSL_CTX only |
certificateCompression: ('brotli' | 'zlib')[] |
SSL_CTX_add_cert_compression_alg |
No, SSL_CTX only |
The node:tls group maps to per-connection calls as well: SSL_set_alpn_protos, SSL_set_min_proto_version / SSL_set_max_proto_version, SSL_set_strict_cipher_list, SSL_set1_groups_list and SSL_set_verify_algorithm_prefs. It also maps onto rustls, which workerd's own network service uses: alpn_protocols, protocol versions, and the order of the crypto provider's cipher_suites and kx_groups. rustls has no equivalent for GREASE or extension permutation, so the BoringSSL group only makes sense where the handshake runs on BoringSSL.
Every option would be rejected with a TypeError at connect() unless secureTransport is 'on' or 'starttls', and unknown names or values would be rejected rather than ignored. SocketInfo could report the negotiated ALPN protocol and TLS version.
Where it would be implemented
As far as I can tell from the code, connect() doesn't run TLS itself. It sends an HTTP CONNECT with kj::HttpConnectSettings { .useTls = true } (src/workerd/api/sockets.c++), and whatever serves the CONNECT does the handshake. In workerd that is the network service (src/workerd/server/server/services/mod.rs), which does TLS with rustls through kj-hyper. On the edge it is a different implementation that isn't public, and #6903 suggests the two already behave differently, since the edge ignores expectedServerHostname. So this needs:
- A field on
kj::HttpConnectSettingsto carry the options (capnproto), and its mirror in workerd's Rust bridge. - workerd's network service building a rustls
ClientConfigfrom them. - The JS API behind an experimental compat flag, with
node:tlsmapped onto it. - Support on the edge.
Questions
- Would you accept this, and in which shape: only the options
node:tlsalready defines, or the BoringSSL group as well? - Could the edge honour per-connection TLS options carried on the CONNECT, and does its handshake run on BoringSSL?
I'm building a prototype of steps 1 to 3 for alpn and the node:tls group on forks of capnproto and workerd, and will link it here once it builds and passes tests.
Activity