Repository navigation
Keep a CONNECT's startTls slot alive with its connection - #8789
Conversation
connect() with secureTransport 'starttls' offers whoever serves the CONNECT a slot to fill in with the socket's startTls() callback. The slot was heap-allocated and owned by the Socket's ConnectionData, while kj::HttpConnectSettings carried only a reference to it. The ConnectionData is an IoOwn, so it went away with the caller's IoContext. The connection can outlive that: the Socket's readable and writable hold their own references to it, and a response body keeps reading from it after the request has ended. For a connection into another Worker, kj's HttpClientAdapter forwards the settings to the target's connect() and attaches the pending call to the connection. When that call is served late, as by a Durable Object still in blockConcurrencyWhile(), the target's ServiceWorkerGlobalScope::connect() read the freed slot, and setupInternalTlsRendezvous() wrote a kj::Function into it. If the freed memory had been reallocated with an engaged value, the target moved it into its own Socket, whose teardown then called through it. The slot is now refcounted, and connectImpl() attaches a reference to it to the connection, so it lives as long as the connection, and with it the pending call that can fill it in. The Socket holds another reference, as before. connect-handler-starttls-lifetime-test has a Worker open a starttls tunnel to a blocked Durable Object, return the socket's readable as its response, and end its request before the Durable Object serves the tunnel. Without the fix, AddressSanitizer reports a heap-use-after-free in ServiceWorkerGlobalScope::connect() on every run (5 of 5), and a build without it crashes with a segmentation fault (5 of 5). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
✅ Approved — This is a small, correct lifetime fix for the starttls slot, with a deterministic regression test. No issues found.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
The slot is now reference-counted and the connection keeps it alive, so the CONNECT target can no longer write into freed memory or read freed memory from it. The change adds no new exposure, and the path stays behind the experimental connect_handler_starttls flag.
📖 AGENTS.md: ✅ LGTM
No issues found.
📋 Code Quality: ✅ LGTM
No issues found. The kj::rc slot, the extra reference attached to the connection and toOwn() into setupSocket() are correct and stay local to connectImpl().
⚡ Performance: ✅ LGTM
No issues found. The only added cost is one refcount increment per connect.
📝 Documentation: ✅ LGTM
No issues found.
Reviewed commit: 2d461da4
connectImpl()heap-allocated thekj::TlsStarterCallbackslot of asecureTransport: 'starttls'socket, put only a reference to it inkj::HttpConnectSettings, and moved theOwnintoSocket::ConnectionData. That is anIoOwn, so the slot was freedwith the caller's
IoContext. The connection can outlive thatcontext: the Socket's readable and writable hold their own
references to it, and
new Response(socket.readable)keeps readingfrom it after the request ends. kj's
HttpClientAdapterforwards thereference to the target's
connect()and attaches that pending callto the connection. A target that serves the CONNECT late, such as a
Durable Object in
blockConcurrencyWhile(), then read the freed slotin
ServiceWorkerGlobalScope::connect(), and either wrote akj::Functioninto it (setupInternalTlsRendezvous()) or moved astale value out of it into its own Socket. This is only reachable
with the experimental
connect_handler_starttlsflag.Fix
The slot's lifetime now follows the connection, which owns the only
call that can fill it in:
connectImpl()allocates the slot withkj::rc<>()and attaches areference to
request.connection, next to theHttpClient.Attachments are destroyed after the stream, so the slot outlives
the pending call that holds the reference to it.
toOwn()intosetupSocket(),whose
ConnectionDataalready stores akj::Rc), sostartTls()works as before.
kj::HttpConnectSettings,global-scope.c++and the Socketsignatures are unchanged.
Testing
The new
connect-handler-starttls-lifetime-testhas a Worker open astarttls tunnel to a Durable Object held in
blockConcurrencyWhile(),return
new Response(socket.readable), and end its request. The testthen releases the Durable Object, which serves the tunnel and writes
back its
socket.secureTransport. The release arrives as a streamclosing rather than as a promise settlement, since a settlement from
another request reaches the Durable Object only once its input gate
opens. Nothing sleeps.
heap-use-after-free read in
ServiceWorkerGlobalScope::connect(),freed by
~ConnectionDatafrom~IoContext. Without ASan itcrashes with SIGSEGV, 5/5.
connect-handler, connect-neuter, connect-half-open,
sockets-connect-error, sockets-test, socket-proxy-to, socket-close,
js-rpc-socket, http-socket, outbound-interceptor-socket and node
net/tls tests, in all variants.
//src/workerd/io:io.Interaction with #7362
#7362 rewrites the handler side of this wiring but keeps the raw
reference:
takeConnectTlsUpgrade()copiessettings.tlsStarter, andconnectImpl()still setshttpConnectSettings.tlsStarter = tlsStarterfrom akj::heap. This change'ssockets.c++hunkapplies cleanly to #7362's head (897562b); only the
api/tests/BUILD.bazelhunk has an adjacent-context conflict. TheproxyTo()andsetInboundTlsUpgrade()paths in #7362 that moveconnData->tlsStarterout ofConnectionDatakeep working, sincethe connection's reference keeps the slot alive independently.
🤖 Generated with Claude Code