Repository navigation
Conversation
The plain-HTTP handler wrote the client's request to the origin as is, so Proxy-Authorization (the proxy password) and Proxy-Connection were forwarded to the origin. Delete Proxy-Authorization, Proxy-Connection and the RFC 7230 hop-by-hop headers, including any header named in Connection, before req.Write. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
serve reads the request through a bufio.Reader but copied the client-to-peer direction straight from the connection, so any bytes already buffered in the reader (a client that pipelines its first payload with the CONNECT request) were dropped. Copy from the reader. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The HTTP CONNECT tunnel, the TCP client/server tunnels, STDIO and the SNI proxy closed both connections as soon as either direction hit EOF. A client that sends a request and shuts down its write side (SHUT_WR) therefore lost the reply. They also allocated a 32 KiB buffer per direction per connection, because gonet.TCPConn has no ReadFrom/WriteTo. Add proxyCopy, used by all of them: it copies each direction with a pooled buffer (sync.Pool, io.CopyBuffer), calls CloseWrite on the destination after that direction's EOF when the connection supports it, and closes both connections only after both directions have finished (or as soon as a copy fails or CloseWrite is unavailable). The SNI wrapper and the HTTP client side (which reads through a bufio.Reader) gain a CloseWrite that delegates to the real connection. connForward is replaced by proxyCopy; STDIO wraps stdin/stdout as one connection. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
An idle or slow client could hold a goroutine and file descriptor forever, because nothing bounded http.ReadRequest. Set a 30 second read deadline before reading the request and clear it afterwards, before the connection is tunnelled (as the SNI proxy does for the ClientHello). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The HTTP and SNI proxies dialled with no deadline (Tnet.Dial / net.Dial), so a dead tunnel or target made the client wait for netstack's SYN retries, about two minutes. Dial with a 30 second context on both the tunnel path (Tnet.DialContext) and the direct path (net.Dialer.DialContext). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Only GET was accepted for plain-HTTP requests (everything else got 405), and after the first request the client connection was piped raw to the origin it named, so a keep-alive follow-up for a different host went to the wrong server. Route every non-CONNECT method through the plain-HTTP handler, send Connection: close to the origin, and relay only that one response before closing both ends. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
After one direction of proxyCopy reached EOF and was half-closed, the other direction could stay open forever, a file descriptor and goroutine leak the previous close-on-first-EOF behaviour did not have. Close both connections when the remaining direction has read no data for proxyIdleTimeout (2 minutes, restarted on every read that returns data). Errors the other direction reports because proxyCopy itself closed the connections (gonet returns opaque errors, not net.ErrClosed, after a local Close) are no longer logged as "Cannot forward traffic". Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The plain-HTTP handler wrote the request and then blindly piped the origin's bytes back, which had three problems: - A WebSocket (Connection: upgrade + Upgrade) request lost its upgrade headers to the hop-by-hop stripping. Keep those two headers when the client asked for an upgrade and, if the origin answers 101, tunnel the connection with proxyCopy (including bytes sent right after the 101). - "Expect: 100-continue" stalled: the request was written in full before anything was read, but the client sends the body only after the 100. Write the request concurrently with relaying the response and forward interim 1xx responses. - An origin that ignored "Connection: close" kept the client waiting after a complete response. Read the response with http.ReadResponse, so the relay ends at the response's framing, and cut the origin off if it sends or receives nothing for proxyIdleTimeout. A chunked request body is covered by a test. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The origin's Keep-Alive, Proxy-Authenticate and any header named in its Connection header describe the origin connection, not the client's, and were passed through. Remove them (the proxy sets Connection: close itself). A 101 response keeps its Connection and Upgrade headers. A test also checks that chunked response trailer values reach the client. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
A connection closed before any request byte (port probe, load balancer health check) made http.ReadRequest return io.EOF, logged as "read request failed". Skip that case; truncated requests (io.ErrUnexpectedEOF), timeouts and other read errors are still logged. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Hardening for the HTTP proxy and the shared TCP forwarding code. Each fix is its own commit with a test that fails without it.
handle()wrote the request to the origin withProxy-Authorizationstill set, so the proxy username and password were sent to every site. Hop-by-hop headers (RFC 9110 §7.6.1, plus any named inConnection) are now stripped from requests and from relayed responses.bufio.Reader, but the tunnel then copied from the raw conn, losing anything already buffered (e.g. a TLS ClientHello sent in the same write).shutdown(SHUT_WR)s never got the reply. A sharedproxyCopyhelper (used by the HTTP proxy, TCP client/server tunnels, STDIO and SNI) now callsCloseWriteand closes both sides once both directions finish; after a half-close the remaining direction is bounded by a 2-minute idle timeout. Copy buffers come from async.Pool.RoutedDial(previously a dead tunnel or target made a CONNECT wait out netstack's SYN retries, about two minutes).http.ReadResponse.Expect: 100-continueno longer stalls, andUpgraderequests (WebSocket) switch to a tunnel on101.This PR touches
http.go,routine.go,sni.goand addsproxy.go; it may conflict with the other open PRs from this branch set inroutine.go. Happy to rebase in whatever order you merge them.Test plan
go test -race ./...,go vet ./...,gofmt -l ., golangci-lint (fork CI green).🤖 Generated with Claude Code