Skip to content

Proxy: speak TLS 1.2 as well as 1.3 through the CONNECT tunnel - #105

Merged
Digidai merged 3 commits into
mainfrom
proxy-tls12
Nov 21, 2026
Merged

Digidai merged 3 commits into
mainfrom
proxy-tls12

Conversation

@Digidai

@Digidai Digidai commented Nov 21, 2026 •

Copy link
Copy Markdown
Owner

Why

The proxy path did TLS with subtls, which speaks TLS 1.3 only. Every HTTPS site that tops out at TLS 1.2 failed there. Of the 40 mainland China https hosts I probed, 18 offer TLS 1.2 at most, so even with a mainland proxy exit about half of them could not be reached.

What changes

  • src/proxy-tls.ts (new): a TLS client over the CONNECT tunnel, using Mbed TLS 4.2.0 compiled to Wasm (@ai-ecoverse/wasm-tls-engine, Apache-2.0). It negotiates TLS 1.2 or 1.3, builds the chain from the certificates the server sends, and verifies it against the same Mozilla roots (proxy-roots.ts), the host name and the validity dates. No verification flag is relaxed.
  • src/proxy.ts: uses it in place of subtls. The deadline, root-dot handling and CONNECT handling are unchanged; the session is freed in finally.
  • Workers compile no Wasm from bytes, so the .wasm is imported as a module (Wrangler compiles it at deploy time) and handed to the Emscripten glue for its one instantiate call. vitest.config.ts loads .wasm imports the same way.
  • subtls is removed from the dependencies.
  • Kept from subtls's behaviour, after review: a certificate without a subjectAltName is refused (Mbed TLS alone would match the Common Name), and a tunnel that closes in the middle of a TLS record is an error, not the end of the response (tracked from the record framing, since the engine reports both closes the same way).
  • A handshake that completes after the deadline or an abort already failed the request has its session freed.

Evidence

Run in workerd (wrangler dev) against the 40 hosts directly, without a proxy:

  • 38 handshake and return a page, including the TLS 1.2-only hosts (www.nmgacc.cn, sjt.zj.gov.cn, www.bijie.gov.cn, kjw.hlj.gov.cn, …).
  • www.als.gov.cn is refused: it sends no intermediate certificate (OpenSSL also reports "unable to verify the first certificate").
  • www.cec.org.cn closed the connection; curl from the same machine fails too.
  • expired, wrong.host, self-signed and untrusted-root.badssl.com are all refused with the matching verification error.
  • Handshakes take roughly 100–1,300 ms.

Tests

  • The two subtls-mocked tests are replaced with real handshakes against a local Node TLS server limited to TLS 1.2 or to TLS 1.3, through a mocked CONNECT proxy, plus the root-dot case and a wrong-host refusal.
  • Also: a CN-only certificate is refused, a 64 KB body across several records arrives whole, and a tunnel cut inside a record rejects. The last two fail on the first commit of this PR.
  • The fixtures under src/__tests__/fixtures/proxy-tls/ are a test-only CA and leaf (valid until 2126); the key protects nothing.
  • Mutation check: with verification skipped, the wrong-host test fails.
  • Re-run in workerd after the review fixes: the same hosts load with the same byte counts.
  • npx tsc --noEmit -p . && npx tsc --noEmit -p web/tsconfig.json; npx vitest run: 954 pass. wrangler deploy --dry-run bundles the .wasm as its own module (320 KB).

subtls speaks TLS 1.3 only, so every HTTPS site that tops out at TLS 1.2
failed on the proxy path. Of the 40 mainland China https hosts probed,
18 offer TLS 1.2 at most.

Replace subtls with an Mbed TLS 4.2.0 client compiled to Wasm
(@ai-ecoverse/wasm-tls-engine). It handshakes TLS 1.2 or 1.3 over the
tunnel, builds the certificate chain from what the server sends and
verifies it against the same Mozilla roots, checks the name and the
validity dates. Wrangler compiles the .wasm at deploy time, since
Workers compile no Wasm from bytes; vitest loads it the same way.

Run locally in workerd against those 40 hosts directly (no proxy): 38
complete; the two that do not are a site that sends no intermediate
certificate and one that closed the connection. badssl.com's expired,
wrong-host, self-signed and untrusted-root certificates are refused.

The tests now handshake for real with a local Node TLS server limited to
TLS 1.2 or 1.3, using a test-only CA, instead of mocking the TLS layer.

Co-authored-by: Amp <[email protected]>
Amp-Thread-ID: https://ampcode.com/threads/T-01a19236-e961-759a-a320-167408397ed5
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-21T13:52:28.640419Z 685f1e1 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
website2markdown 685f1e1 Nov 21 2026, 01:46 PM

Digidai and others added 2 commits November 21, 2026 21:37
If the engine asked for more ciphertext while refusing what it already
had, the handshake and read loops would await only resolved promises,
so the timer behind the proxy deadline could never fire. Retry the feed
once (room may have freed) and throw if it still takes nothing.

Co-authored-by: Amp <[email protected]>
Amp-Thread-ID: https://ampcode.com/threads/T-01a19236-e961-759a-a320-167408397ed5
… sessions

From review of the TLS 1.2 change:

- A tunnel that closed in the middle of a TLS record ended the response
  as if it were complete, so a truncated page could come back as 200.
  The engine reports either close the same way, so track the record
  framing of the ciphertext and accept a close only between records.
- Mbed TLS matches the Common Name when the leaf has no subjectAltName;
  subtls and browsers refuse such certificates. Require the extension.
- A handshake that finished after the deadline or an abort had already
  failed the request left its session in the shared Wasm heap. Free it.

Amp-Thread-ID: https://ampcode.com/threads/T-01a19236-e961-759a-a320-167408397ed5
Co-authored-by: Amp <[email protected]>
@Digidai

Digidai commented Nov 21, 2026

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 685f1e1be8

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Digidai
Digidai merged commit c209e05 into main Nov 21, 2026
2 checks passed
@Digidai
Digidai deleted the proxy-tls12 branch November 21, 2026 14:42
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.

1 participant