Skip to content

fix(gateway-client): verify TLS pin before websocket upgrade - #131435

Merged
steipete merged 2 commits into
mainfrom
steipete/transport-pin-first
Aug 28, 2026
Merged

steipete merged 2 commits into
mainfrom
steipete/transport-pin-first

Conversation

@steipete

Copy link
Copy Markdown
Contributor

What Problem This Solves

When connecting to a Gateway with TLS certificate pinning, the client validated the pin only after the WebSocket opened. Because pinning sets rejectUnauthorized: false (Node skips checkServerIdentity once chain verification has already failed), the HTTP Upgrade request — including any configured edge-auth headers — was written to the destination socket before the pin was checked. A wrong or self-signed endpoint therefore received request bytes (and edge-auth header material) before the mismatch aborted the connection.

Why This Change Was Made

The pin must be verified on the actual destination TLS socket before any HTTP bytes leave the client. The fix moves validation into the existing ws finishRequest seam: perform the TLS handshake, verify the SHA-256 pin on the secure socket, and only then call request.end(). Expect headers are withheld from request construction (Node can flush them early). The same gate covers managed CONNECT proxy sockets (which arrive already-secure via @openclaw/proxyline). A typed GatewayWebSocketTlsPinError preserves the mismatch identity across the request→WebSocket error boundary, and worker admission maps it to the existing terminal endpoint error so a pin mismatch never becomes a retry loop; ordinary pre-open failures stay retryable. An open event now means the transport already validated, so the post-open check in client.ts is deleted — one canonical flow.

A deep-context review found and fixed a second instance of the same flaw: src/node-host/node-stream-transport.ts (desktop/portal streams) had its own request finisher waiting unconditionally for secureConnect, so managed-CONNECT streams leaked the same pre-pin bytes. It now shares the extracted applyGatewayWebSocketTlsPin helper.

Contracts verified against installed sources: ws 8.21.3 (request construction/error forwarding/redirect defaults), @openclaw/proxyline 0.3.7 (CONNECT socket delivered only after secureConnect), and Node v26.5.0 + v24.19.0 built-in _http_client/_http_outgoing/internal/tls/wrap (Expect flush timing, chain-check/callback ordering).

User Impact

Pinned Gateway connections that fail the pin now send zero application bytes to the untrusted endpoint — no edge-auth headers, no Upgrade. Correct-pin, no-pin (CA-validated), and trusted-CA connections behave exactly as before. No config, dependency, schema, or protocol change.

Evidence

  • Production net -60 (the transport change mostly relocates existing code); tests net +408; assertion baseline shrinks by one.
  • Byte-exposure proof, before → after on unchanged base: 309–390 decrypted bytes across direct, direct+Expect, direct+URL-auth, managed-CONNECT, probe, bootstrap, and worker-admission flows → 0 bytes, 0 upgrades after, with correct-pin controls still upgrading.
  • Adversarial sweep: 52 scenarios on Node 26 and 52 on Node 24 — IPv4/IPv6/hostname-SNI, correct/wrong pins, URL auth + Expect, subprotocol and per-message-deflate negotiation, unsolicited 100-Continue, 20 extra mismatches per direct/proxy route. Every rejected destination: zero application bytes, zero upgrades, all tracked sockets drained to zero.
  • Slowloris trickle (one byte / 20 ms partial TLS record): both pinned and unpinned paths time out and drain; managed-CONNECT stall (10 timeout + 10 cancellation) drains all peers.
  • Fresh currently-valid local CA cert: no-pin and correct-pin upgrade, wrong-pin rejects with zero bytes, both Node versions.
  • 600 tests across 38 files green; core + all 14 test-typecheck graphs pass; changed-file type-aware lint on all 11 files; Knip zero entries; two independent Codex autoreviews clean. Worker pin-mismatch-stays-terminal regression included.
  • Follow-up (not here): remove the superseded mismatch-only client.watchdog.test.ts inline HTTPS setup, now covered by the stronger direct/proxy TLS suite.

Reuse the common request setup for auxiliary streams so managed connections can attach after the connection event has already completed. Preserve stream endpoint policy and ordering, and cover direct and managed routes with real-socket tests.
@clawsweeper

clawsweeper Bot commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime size: L maintainer Maintainer-authored PR labels Aug 28, 2026
@steipete
steipete marked this pull request as ready for review August 28, 2026 02:37
@steipete
steipete merged commit a9d3e13 into main Aug 28, 2026
299 checks passed
@steipete
steipete deleted the steipete/transport-pin-first branch August 28, 2026 02:59
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Aug 28, 2026
…w#131435)

* fix(gateway-client): verify TLS pin before websocket upgrade

* fix(node-host): share websocket request preparation

Reuse the common request setup for auxiliary streams so managed connections can attach after the connection event has already completed. Preserve stream endpoint policy and ordering, and cover direct and managed routes with real-socket tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gateway Gateway runtime maintainer Maintainer-authored PR size: L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant