Skip to content

Implement connect handler immediate TLS support - #8471

Merged
fhanau merged 5 commits into
mainfrom
felix/111226-connect-immediate-tls
Nov 19, 2026
Merged

fhanau merged 5 commits into
mainfrom
felix/111226-connect-immediate-tls

Conversation

@fhanau

@fhanau fhanau commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

This is available behind an experimental compat flag for now. The handler can use a requestedSecureTransport flag on the socket to see which transport mode the client requested.

@github-actions

github-actions Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

The generated output of @cloudflare/workers-types matches the snapshot in types/generated-snapshot 🎉

@fhanau
fhanau force-pushed the felix/111226-connect-immediate-tls branch from f805bf6 to e575197 Compare November 16, 2026 03:53
@fhanau
fhanau marked this pull request as ready for review November 16, 2026 04:08
@fhanau
fhanau requested review from a team as code owners November 16, 2026 04:08

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of 488f028a.

This review, of e5751979

🛑 Significant concerns — 1 BLOCKING item: a connect handler that throws synchronously leaves ConnectAcceptance pending, so a later socket.close()/accept() uses the destroyed ConnectResponse (use-after-free).

Findings: 1 blocking (1 is an inline comment on the diff)

Not opened by the correctness review: 2 changed files it was meant to read
  • types/generated-snapshot/experimental/index.d.ts
  • types/generated-snapshot/experimental/index.ts

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.

🔧 Fix with your agent

Paste the following into your coding agent to investigate and fix these issues.

Fix the following code review issues found in pull request #8471.

Before making changes, review each issue and present a brief summary table:
- For each finding, state whether you agree, disagree, or need clarification
- If you disagree (e.g., the fix would require disproportionate changes
  for minimal ROI, or the finding is factually incorrect), explain why
- If you need to clarify something before deciding, ask those questions
- Then share your plan for which issues to tackle and in what order

## BLOCKING (1)

### [Code Quality] A connect handler that throws synchronously leaves ConnectAcceptance pending with a reference to the destroyed ConnectResponse. A later socket.close()/accept() is a use-after-free.
- **File:** src/workerd/api/global-scope.c++:323

📋 Code Quality: 🛑 1 blocking

Use-after-free when the connect handler throws synchronously, plus untested new paths.

Recommendation: 🔴 Serious Concerns

  • [BLOCKING] A connect handler that throws synchronously leaves ConnectAcceptance pending with a reference to the destroyed ConnectResponse. A later socket.close()/accept() is a use-after-free. (src/workerd/api/global-scope.c++:323)
View full review

1. A synchronous throw leaves ConnectAcceptance pending with a dangling response (src/workerd/api/global-scope.c++:318-338)

ConnectAcceptance stores a raw ConnectResponse& (created at line 258), and line 320 hands an IoOwn reference to it to the JS Socket. The guards that call abandon() (the .then and the kj::defer at lines 332-337) are attached only after handler(...) at line 323 returns a promise. A handler that throws synchronously, such as a non-async connect(socket, env, ctx) { ...; throw ... }, leaves the function before either guard exists, and the state stays Pending{response}. The caller then destroys response. If the socket outlives the request (it was captured in a ctx.waitUntil continuation, for instance), a later socket.close() reaches ConnectAcceptance::fail() (sockets.c++:1061-1063, 1123-1130), which calls response.reject(...) on the destroyed object. socket.accept() (sockets.c++:1105-1116) has the same problem.

Fix: install the abandon guard as soon as acceptance is created. A KJ_ON_SCOPE_FAILURE that calls abandon(), placed before the handler is invoked, covers every exit path. Another option is to wrap the handler call and awaitJs in kj::evalNow, so that a synchronous throw becomes a rejected promise that the existing .then/defer handles.

2. Test gaps

These paths have no test: a handler that throws synchronously before accept() (the path in item 1), socket.accept() on a socket that did not come from a connect handler (TypeError at sockets.c++:1162), and serializing a socket whose connection is still pending.


✅ All other checks passed

🔒 Security: ✅ LGTM

No security issues found beyond the lifetime bug described in the code quality section.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md issues found.


⚡ Performance: ✅ LGTM

No performance issues found.


📝 Documentation: ✅ LGTM

No documentation issues found.


Reviewed commit: e5751979

Comment thread src/workerd/api/global-scope.c++ Resolved

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of f6ae991e.

This review, of 488f028a

✅ Approved — The earlier BLOCKING use-after-free on a synchronous handler throw is fixed by a guard set up before the handler runs, and the new tests cover that path. No new issues found.

Not opened by the correctness review: 2 changed files it was meant to read
  • types/generated-snapshot/experimental/index.d.ts
  • types/generated-snapshot/experimental/index.ts

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

Three things hold:

  • A TLS-on connection gets through only when connect_handler_immediate_tls is enabled.
  • No data crosses the connection until accept() is called, because the stream is wrapped in newPromisedStream(whenAccepted()).
  • A socket whose connection is still waiting for accept() cannot be serialized.

The earlier lifetime bug is fixed.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md issues found.


📋 Code Quality: ✅ LGTM

abandonAcceptance (global-scope.c++:269-275) is now set up before anything that can throw. It runs when the handler throws synchronously, when the returned promise is canceled, and when the handler returns normally, so the ConnectAcceptance always gives up its reference to response. Tests now cover a synchronous throw, accept() on a socket that did not come from a connect handler, and serializing a socket that has not been accepted yet.


⚡ Performance: ✅ LGTM

Each connection adds only a small, fixed cost, and only when the peer requested TLS. There is no unbounded buffering.


📝 Documentation: ✅ LGTM

No documentation issues found.


Reviewed commit: 488f028a

@fhanau
fhanau marked this pull request as draft November 16, 2026 05:43
Comment thread src/workerd/api/global-scope.c++ Resolved
Comment thread src/workerd/api/global-scope.c++ Resolved
Comment thread src/workerd/api/sockets.h Resolved
@codecov-commenter

codecov-commenter commented Nov 16, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.87500% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 67.05%. Comparing base (e3f67c7) to head (9188752).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
src/workerd/api/global-scope.c++ 97.50% 0 Missing and 1 partial ⚠️
src/workerd/api/sockets.c++ 97.82% 0 Missing and 1 partial ⚠️
src/workerd/api/sockets.h 87.50% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8471      +/-   ##
==========================================
+ Coverage   67.02%   67.05%   +0.03%     
==========================================
  Files         799      799              
  Lines      206554   206640      +86     
  Branches    32057    32075      +18     
==========================================
+ Hits       138444   138570     +126     
+ Misses      49313    49279      -34     
+ Partials    18797    18791       -6     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 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.

Comment thread src/workerd/api/sockets.c++ Resolved
@fhanau
fhanau marked this pull request as ready for review November 17, 2026 19:18
@fhanau fhanau changed the title [DRAFT] Implement connect handler immediate TLS support Implement connect handler immediate TLS support Nov 17, 2026

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of 2f859808.

This review, of f6ae991e

✅ Approved — The refcount cleanup is correct and no new issues were found; the earlier lifetime fix still holds.

Not opened by the correctness review: 2 changed files it was meant to read
  • types/generated-snapshot/experimental/index.d.ts
  • types/generated-snapshot/experimental/index.ts

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

  • A TLS-on connection gets through only when connect_handler_immediate_tls is enabled.
  • No data crosses the connection until accept() is called.
  • A socket whose connection has not been accepted yet cannot be serialized.
  • The refcount cleanup does not change any of the above.

📖 AGENTS.md: ✅ LGTM

No AGENTS.md issues found.


📋 Code Quality: ✅ LGTM

ConnectAcceptance is now held through kj::Rc without inheriting kj::Refcounted, and IoContext::addObject(kj::Rc<T>) supports this. Calling abandon() or fail() after the state has left pending does nothing, so the deferred guard is safe after a normal return. The false /* isDefaultFetchPort */ label has been added.


⚡ Performance: ✅ LGTM

Each connection adds only a small, fixed allocation, and only when the client requests TLS. Nothing is buffered before accept().


📝 Documentation: ✅ LGTM

No documentation issues found.


Reviewed commit: f6ae991e

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of 57892f03.

This review, of 2f859808

✅ Approved — Folding requestedSecureTransport into the socket keeps the earlier lifetime and refcount fixes intact, and no new issues were found in code quality, security, performance or documentation.

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

  • A TLS-on connection gets through only when connect_handler_immediate_tls is enabled.
  • No data crosses the connection until accept() is called.
  • A socket whose connection has not been accepted yet cannot be serialized.

📖 AGENTS.md: ✅ LGTM

No AGENTS.md issues found.


📋 Code Quality: ✅ LGTM

No issues found.

  • ConnectAcceptance lifetime: Every way out of ServiceWorkerGlobalScope::connect() settles or abandons the acceptance:

    • a synchronous throw
    • an asynchronous throw
    • cancellation, where the deferred guard is destroyed with the returned promise
    • a normal return without accept()

    accept(), fail() and abandon() do nothing once the state has left Pending, so a late accept() cannot touch a released response.

  • Flag guard: WorkerEntrypoint::connect() checks the compat flag before the code reaches the KJ_ASSERT in global-scope.c++, so the assert cannot fire.

  • Serialization: Socket::serialize refuses a socket whose acceptance is still pending.

  • Tests: The tests cover:

    • the success path
    • holding writes until accept()
    • a failed handshake
    • a handler that never accepts
    • closing first
    • a synchronous throw
    • a transfer before accept()
    • a plaintext peer
    • refusal when the flag is off

On the open question in the sockets.c++ thread: an accept() that returns nothing looks fine as written. Without TLS, acceptance has already happened, and with TLS, repeated calls do nothing. A handler that needs to know the handshake finished can wait on opened.


⚡ Performance: ✅ LGTM

Each connection adds only a small, fixed ConnectAcceptance allocation, and only when the client requests TLS. The promised stream holds data back until accept(), so nothing is buffered before then.


📝 Documentation: ✅ LGTM

No documentation issues found.


Reviewed commit: 2f859808

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of 8d3353b8.

This review, of 57892f03

✅ Approved — Only the tests changed since the last review, and the new socket-proxying tests are consistent with their config. No issues were 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 connect_handler_immediate_tls flag gates every TLS-on path.
  • No data crosses the connection until accept() is called.
  • A socket whose acceptance is still pending cannot be serialized.
  • fail() sends the peer only a generic 502, so no internal detail leaks.

📖 AGENTS.md: ✅ LGTM

No AGENTS.md issues found.


📋 Code Quality: ✅ LGTM

The new proxy test and its .wd-test config match: every binding the test worker uses is defined, TLS_PORT is added to listen_port_bindings, and the TLS listener and the internet service use the same certificate. The production code is unchanged since the last review.


⚡ Performance: ✅ LGTM

No performance issues found.


📝 Documentation: ✅ LGTM

No documentation issues found.


Reviewed commit: 57892f03

@fhanau
fhanau force-pushed the felix/111226-connect-immediate-tls branch from 57892f0 to 8d3353b Compare November 19, 2026 04:05

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of 91887521.

This review, of 8d3353b8

✅ Approved — The only change since the last review is the experimental compat flag definition, which is correct: ordinal 208 is unique and the flag is marked experimental. No issues were 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 only change since the last review is the flag's definition and its comment. The connect_handler_immediate_tls flag still gates every path where TLS is on.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md issues found.


📋 Code Quality: ✅ LGTM

connectHandlerImmediateTls @208 follows directly after the previous highest ordinal, @207, and no other field uses 208. It is marked $experimental, and its comment describes how accept() behaves. In worker-entrypoint.c++, featureFlags is defined before the new check reads it.


⚡ Performance: ✅ LGTM

No performance issues found.


📝 Documentation: ✅ LGTM

No documentation issues found.


Reviewed commit: 8d3353b8

This is available behind an experimental compat flag for now. The handler
can use a requestedSecureTransport flag on the socket to see which transport
mode the client requested.
@fhanau
fhanau force-pushed the felix/111226-connect-immediate-tls branch from 8d3353b to 9188752 Compare November 19, 2026 05:43

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Approved — No issues found. The only change since the last review moves the experimental compat flag to a unique ordinal, @​209, and the flag still covers every TLS path.

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 connect_handler_immediate_tls flag still covers every TLS path: the entrypoint rejects useTls without the flag, and the global scope asserts the flag before it creates the acceptance object. Traffic waits until accept() is called, and a socket whose connection is still pending cannot be serialized.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md issues found.


📋 Code Quality: ✅ LGTM

connectHandlerImmediateTls @209 is the highest ordinal in compatibility-date.capnp, no other field uses 209, and it is marked $experimental. In worker-entrypoint.c++, featureFlags is defined before the new check reads it. On every path where the handler fails early, closes its socket or throws, ConnectAcceptance stops using response before response goes away.


⚡ Performance: ✅ LGTM

No performance issues found.


📝 Documentation: ✅ LGTM

No documentation issues found.


Reviewed commit: 91887521

@fhanau
fhanau merged commit 37d6914 into main Nov 19, 2026
36 of 38 checks passed
@fhanau
fhanau deleted the felix/111226-connect-immediate-tls branch November 19, 2026 15:01
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