Skip to content

Expose the session's accepted conn to stream handlers - #17

Merged
myleshorton merged 1 commit into
mainfrom
afisk/session-conn-context
Oct 19, 2026
Merged

myleshorton merged 1 commit into
mainfrom
afisk/session-conn-context

Conversation

@myleshorton

@myleshorton myleshorton commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Why

lantern-box answers bandit probes on the proxy and judges whether the response reached the client from the kernel's TCP state (TCP_INFO bytes acked, SIOCOUTQ). On samizdat the probe's conn is an HTTP/2 stream with no path back to the socket, so every samizdat probe reports unknown. The API scores unknown as a success, so samizdat arms never take a stall penalty while plain-TCP arms do. Samizdat and QUIC (hysteria2) are the two largest sources of unknown verdicts in the fleet, at roughly 95k each per hour.

getlantern/lantern-box#368 already handles the same problem for sing-mux streams by judging delivery on the shared session socket. This PR gives samizdat streams the same access.

What

  • serveH2 now passes http2.ServeConnOpts.Context, carrying the connection accepted from the listener (beneath TLS). Every CONNECT stream's r.Context() inherits it.
  • New SessionConnFromContext(ctx) (net.Conn, bool) returns it to the Handler. The doc comment states the contract: the conn is shared by every stream of the session, so handlers must not close it, write to it, or set its deadlines.
  • The base ctx stays context.Background(), which is what ServeConn uses when Context is nil, so stream lifetimes don't change.
  • It uses only the mainline x/net/http2 API. There is no fork patch.

lantern-box will copy this conn into the ctx key the probe responder already reads (mux.ContextWithSessionConn), so the responder itself needs no change. That's a follow-up PR once this merges.

Not included: making serverStreamConn.SetWriteDeadline real. In x/net, an expired write deadline resets the whole stream, which would change behaviour for all samizdat traffic. The probe responder already abandons a stuck write after a bounded grace period.

Tests

TestIntegrationSessionConnInContext opens two streams over one client session and asserts that each handler gets:

  • a *net.TCPConn from the ctx;
  • the same remote address as its stream;
  • the same conn as the other stream.

It fails without the ServeConnOpts.Context change ("no session conn in handler ctx") and passes 10/10 under -race. The full suite passes with go test -race ./....

Pre-PR review (local Codex gate)

  • Rounds: 1. Final: CODEX GATE: verdict=approve critical=0 important=0 minor=0 reviewed=6
  • Exit: approve
  • Fixed: none
  • Dismissed: none

🤖 Generated with Claude Code

https://claude.ai/code/session_01EpEDo42eAorMVquPHGmAT6

Summary by CodeRabbit

  • New Features
    • Request handlers can now retrieve the underlying network connection for their session from the request context.
    • Streams within the same session share that connection, making the session-level connection available across stream handlers.

Each CONNECT stream's ctx now carries the connection accepted from the
listener, beneath TLS, via ServeConnOpts.Context. SessionConnFromContext
returns it, so a handler can read socket state such as TCP_INFO that the
HTTP/2 stream cannot reach. The base ctx stays Background, which is what
ServeConn uses when Context is nil, so stream lifetimes are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01EpEDo42eAorMVquPHGmAT6
Copilot AI balanced review requested due to automatic review settings October 19, 2026 13:00
@coderabbitai

coderabbitai Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a62f946a-ad5e-45fb-b8f4-03c634b60885
📥 Commits

Reviewing files that changed from the base of the PR and between 0160bdf and 9b9ba8d.

📒 Files selected for processing: 2
  • integration_test.go
  • server.go

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

📝 Walkthrough

The server now makes the accepted raw connection available in handler context through SessionConnFromContext. An integration test checks that handlers on two streams can retrieve the same TCP session connection.

Changes: Session connection context

Layer / File(s) Summary
Propagate and verify the session connection
server.go, integration_test.go
ServeConn adds the accepted connection to context, and server handling passes it to HTTP/2 processing. SessionConnFromContext retrieves it. The integration test verifies that two streams receive the same TCP connection and that its remote address matches each stream.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Feature

Sequence Diagram(s)

sequenceDiagram
  participant ServeConn
  participant handleAuthenticated
  participant serveH2
  participant Handler
  ServeConn->>handleAuthenticated: Context containing accepted raw connection
  handleAuthenticated->>serveH2: TLS connection and raw connection
  serveH2->>Handler: Handler context containing session connection
  Handler->>SessionConnFromContext: Retrieve session connection
Loading
Merge Risk: ⚪ Minimal · up to 9b9ba

No actionable issue remains from the supplied evidence; the change is ready for normal merge checks.

Security architecture risk: 🔵 Low · up to 9b9ba

Handlers gain access to a shared transport connection, so misuse could disrupt every stream on that connection. Authentication and stream cleanup remain unchanged, and no remotely exploitable boundary bypass was identified. Safe use depends on trusted handlers honoring the documented ownership restrictions.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — Misuse of a retrieved connection can directly affect every stream sharing that session, rather than only the invoking stream. This is authority granted to configured in-process handler code, not a new socket-control interface exposed directly to remote clients. Downstream deployment and tenant-sharing arrangements were not established.

Security Findings and Attack Paths

  • inferred — No new unauthenticated path to the session accessor or bypass of the existing authentication and TLS gates was established. A harmful transport operation would require behavior in the configured handler; the inspected change does not dispatch client-supplied operations onto the returned connection.

Trust Boundaries and Controls

  • observed — The connection value is injected by the server under an unexported context-key type. The handler is selected through ServerConfig, while remote request data supplies the CONNECT destination. The PR preserves the existing authentication, TLS, and request-validation controls.

Resilience and Maintainability Implications

  • inferred — Failure containment remains stream-local when handlers use the existing stream wrapper, but direct mutation through the new accessor can bypass that containment. The integration test establishes shared identity during normal execution; it does not demonstrate concurrent misuse or recovery behavior in downstream consumers.

Hardening Proposals

  • proposed — If handlers later cross a less-trusted extension boundary, consider exposing a socket-inspection-only capability instead of the full net.Conn. This would strengthen enforcement of the intended observational use; it is not a verified vulnerability in the current trusted-handler design.
Pre-merge checks: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: exposing the session's accepted connection to stream handlers.
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 2 files.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

Copilot AI 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.

🟢 Approval recommended

The focused change preserves existing lifetimes, includes integration coverage, and has no identified blocking issues.

No open issues 🎉

What changed in this PR

Exposes the accepted session connection to HTTP/2 stream handlers so downstream probe logic can inspect socket delivery state.

Changes:

  • Adds SessionConnFromContext and documents shared-connection restrictions.
  • Propagates the connection through HTTP/2 request contexts without changing stream lifetimes.
  • Tests that two streams receive the same underlying TCP connection.
File Description
server.go Adds the context accessor and passes the accepted connection to handlers.
integration_test.go Verifies connection type, remote address, and sharing across streams.

Review effort: Balanced

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.

2 participants