Repository navigation
SessionHandler, DialSession and a certificate pin for reversed roles - #22
Conversation
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01KqE58aYaai3u8CFZe5nXdE
…ptional certificate pin Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01KqE58aYaai3u8CFZe5nXdE
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01KqE58aYaai3u8CFZe5nXdE
The ctx was derived from the server's alone, so only Server.Close ended it. A handler that waits on ctx.Done() leaked on every disconnect or short ID revocation. Each session now has its own ctx, cancelled by revocation (ErrSessionRevoked), by the first failed Read or Write on the conn the handler gets, or by closing it. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01KqE58aYaai3u8CFZe5nXdE
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01KqE58aYaai3u8CFZe5nXdE
crypto/tls fails every Write after one times out, so treating a write deadline like a read deadline left the session's ctx live on an unusable conn. Only read timeouts are ignored now. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01KqE58aYaai3u8CFZe5nXdE
|
Warning Review limit reachedThis review ran on the open-source allowance, not this organization's plan, because the pull request author doesn't have an assigned seat. Waiting won't change this — ask an organization admin to assign them a seat, or add seats in Billing if every seat is already assigned, then retry. Next included review available in 24 minutes. View limit details
|
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues remain for the authenticated-session and certificate-pinning changes.
No open issues 🎉
What changed in this PR
Adds authenticated sessions that callers can use for their own protocols, enabling reversed HTTP/2 roles for personal proxies. Optional certificate pinning lets clients verify the server’s public key.
Changes:
- Adds
SessionHandlerand unpooledDialSession, while retaining the existing HTTP/2 CONNECT path. - Adds SHA-256 SPKI certificate pinning and a helper to compute pins.
- Adds tests for session lifecycle, cancellation, timeouts, certificate pins, and reversed HTTP/2 roles.
| File | Description |
|---|---|
shortids_test.go |
Adapts session-tracking tests to the cancellation parameter. |
session_test.go |
Tests session APIs, lifecycle behavior, timeouts, and certificate pinning. |
server.go |
Dispatches custom session handlers and manages session cancellation. |
samizdat.go |
Defines and documents the session handler and certificate-pin configuration. |
reverse_test.go |
Verifies reversed HTTP/2 roles with PING and CONNECT exchanges. |
client.go |
Adds raw session dialing, certificate pinning, and shared TLS connection setup. |
Review effort: Balanced
Lets a caller take samizdat's authenticated connection on either end and run its own protocol on it, and lets a client pin the server's certificate. Lantern's reverse direct mode for personal proxies needs both: the TLS server (the inviter's portal) runs an HTTP/2 client, and the TLS client (the helper's bridge) runs an HTTP/2 server.
Why
Today the TLS client is always the HTTP/2 client. In reverse direct mode the helper dials out to the inviter, because the inviter can accept connections and the helper can't. The inviter's traffic then flows back down that connection and exits from the helper. So the roles reverse. The helper also learns the inviter's certificate from a sealed blob, and must refuse anyone else listening on that address.
What changes
ServerConfig.SessionHandler.func(ctx, conn net.Conn, shortID [8]byte), called once per authenticated connection in place of the built-in HTTP/2 CONNECT server.Handlermay be nil when it's set.connis the TLS connection (ALPNh2negotiated).SessionConnFromContextandShortIDFromContextwork on its ctx.SetShortIDsrevokes the session's short ID (causeErrSessionRevoked);Server.Closeruns (ErrServerClosed);Server.Closewaits for handlers, so they must return once ctx is done.(*Client).DialSession(ctx)returns a new authenticated connection that is not pooled and runs no HTTP/2. The caller owns it.Close.ConnectTimeoutto the whole dial and handshake when ctx has no deadline.net.Conn, not a typed nil.ClientConfig.ServerCertSPKISHA256. When set, the leaf certificate's SPKI SHA-256 must match it, or the handshake fails withErrCertPinMismatch.NewClientrejects a pin that is neither empty nor 32 bytes. The certificate is still not checked against a CA.SPKISHA256(certDER)computes that pin.createTransportis split intodialConnplus the HTTP/2 transport.DialContextand its pooling behave as before.isClosed()helper replaces a duplicated check.Testing
go build ./...andgo vet ./...are clean, andgo test -race -count=1 -timeout 5m ./...passes.NewServerneeds a handler.ConnectionStateis still reachable through the wrapper.ErrCertPinMismatch.DialSessionafterClose, with a cancelled ctx, and underConnectTimeoutreturns a nil conn.TestReversedHTTP2Roles: the TLS server runshttp2.Transport.NewClientConnand the TLS client runshttp2.Server.ServeConn. A PING and a CONNECT round trip pass, and the handler returns when the client closes, withoutServer.Close.Risk
Serve/Handler/DialContextsee no change. The new paths are opt-in.server.go, where the session wrapper and its cancel paths live.DialSessionerrors include the server address, asDialContext's do, so callers must redact them before logging.How to revert
Revert the merge commit. Nothing depends on these APIs until lantern-box's reverse direct mode lands.
Pre-PR review (local Codex gate)
CODEX GATE: verdict=approve critical=0 important=0 minor=0 preexisting=0 reviewed=12808687aapproveclient.go:134:DialSessionreturned a typed-nilnet.Connon a failed dial (r1, fixed in 61018a9).server.go:404: a write timeout poisoned the TLS conn but left the session ctx live (r2, fixed in 808687a).Server.Close, so a disconnect or revocation leaked the handler (fixed in 091456c).AGENTS.mdadditions: none🤖 Generated with Claude Code
https://claude.ai/code/session_01KqE58aYaai3u8CFZe5nXdE