Skip to content

HTTP proxy hardening: don't leak proxy credentials, keep early bytes, half-close, timeouts - #229

Open
dividedby wants to merge 10 commits into
windtf:masterfrom
dividedby:feat/http-proxy-hardening
Open

dividedby wants to merge 10 commits into
windtf:masterfrom
dividedby:feat/http-proxy-hardening

Conversation

@dividedby

Copy link
Copy Markdown

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.

  • Proxy credentials leak to the origin (security). For a plain-HTTP request, handle() wrote the request to the origin with Proxy-Authorization still set, so the proxy username and password were sent to every site. Hop-by-hop headers (RFC 9110 §7.6.1, plus any named in Connection) are now stripped from requests and from relayed responses.
  • Bytes sent right after CONNECT were dropped. The request is read through a 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).
  • Half-close. The forwarders closed both connections on the first EOF, so a client that sends a request and shutdown(SHUT_WR)s never got the reply. A shared proxyCopy helper (used by the HTTP proxy, TCP client/server tunnels, STDIO and SNI) now calls CloseWrite and 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 a sync.Pool.
  • Timeouts. A 30 s read deadline before the request line (cleared before tunnelling), and a 30 s connect timeout in RoutedDial (previously a dead tunnel or target made a CONNECT wait out netstack's SYN retries, about two minutes).
  • Plain HTTP beyond GET. Any method is proxied, with one origin connection per request; responses are framed with http.ReadResponse. Expect: 100-continue no longer stalls, and Upgrade requests (WebSocket) switch to a tunnel on 101.
  • A client that connects and closes without sending anything is no longer logged as an error.

This PR touches http.go, routine.go, sni.go and adds proxy.go; it may conflict with the other open PRs from this branch set in routine.go. Happy to rebase in whatever order you merge them.

Test plan

  • go test -race ./..., go vet ./..., gofmt -l ., golangci-lint (fork CI green).
  • New tests: header stripping (request and response), early bytes after CONNECT, half-close for CONNECT/TCP/SNI, idle bound after half-close, read deadline, dial timeout on both paths, non-GET methods, chunked request bodies, 100-continue, upgrade, response framing.

🤖 Generated with Claude Code

dividedby and others added 10 commits November 19, 2026 21:31
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>
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