From 16e9f225d883998f572193435304606296956a2f Mon Sep 17 00:00:00 2001 From: walsykerem Date: Sat, 21 Nov 2026 20:45:07 +0300 Subject: [PATCH 1/3] fix(egress): bound idle CONNECT tunnels after hijack --- lib/egressproxy/service.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/egressproxy/service.go b/lib/egressproxy/service.go index acd6172c8..25e71f006 100644 --- a/lib/egressproxy/service.go +++ b/lib/egressproxy/service.go @@ -36,6 +36,15 @@ type headerInjectRule struct { const defaultLeafCertCacheLimit = 512 +// Hijacking drops http.Server's ReadHeaderTimeout, so the phases after Hijack +// must bound themselves or an idle client pins a goroutine, fd and TLS buffers. +const ( + connectIdleTimeout = 3 * time.Second + // A stalled client must not hold the goroutine, but a slow transfer needs + // more than the idle bound. + connectWriteTimeout = 60 * time.Second +) + // Service is a host-side per-process HTTP/HTTPS MITM egress proxy. type Service struct { mu sync.RWMutex @@ -440,6 +449,9 @@ func (s *Service) handleConnect(w http.ResponseWriter, r *http.Request, sourceIP _, _ = io.WriteString(clientConn, "HTTP/1.1 200 Connection Established\r\n\r\n") + // Bound the handshake: the server's read timeout no longer covers this conn. + _ = clientConn.SetDeadline(time.Now().Add(connectIdleTimeout)) + cert, err := s.getOrCreateLeafCert(targetHost) if err != nil { log := s.loggerForContext(r.Context()) @@ -458,9 +470,12 @@ func (s *Service) handleConnect(w http.ResponseWriter, r *http.Request, sourceIP return } defer tlsConn.Close() + _ = clientConn.SetDeadline(time.Time{}) reader := bufio.NewReader(tlsConn) for { + // Idle tunnels are reaped; active ones reset this on every request. + _ = clientConn.SetReadDeadline(time.Now().Add(connectIdleTimeout)) req, err := http.ReadRequest(reader) if err != nil { if err == io.EOF { @@ -495,11 +510,13 @@ func (s *Service) handleConnect(w http.ResponseWriter, r *http.Request, sourceIP } removeHopByHopHeaders(resp.Header) + _ = clientConn.SetWriteDeadline(time.Now().Add(connectWriteTimeout)) if err := resp.Write(tlsConn); err != nil { resp.Body.Close() return } resp.Body.Close() + _ = clientConn.SetWriteDeadline(time.Time{}) s.metrics.recordRequest(r.Context(), "https", "success", injected) s.metrics.recordUpstreamDuration(r.Context(), "https", "success", time.Since(start).Seconds()) From 9eadabd7776507d50fe6b5646b1852a393a49a29 Mon Sep 17 00:00:00 2001 From: walsykerem Date: Sat, 21 Nov 2026 22:01:32 +0300 Subject: [PATCH 2/3] fix(egress): refresh CONNECT tunnel idle deadline on body progress The post-hijack read deadline that bounds the tunnel header read stayed in force during RoundTrip, so a request body streamed over more than 3s failed even while the client was actively uploading. Refresh the deadline on every successful body read and keep the 3s bound when no data arrives, so idle and stalled tunnels are still reaped. --- lib/egressproxy/service.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/lib/egressproxy/service.go b/lib/egressproxy/service.go index 25e71f006..d18b9092b 100644 --- a/lib/egressproxy/service.go +++ b/lib/egressproxy/service.go @@ -483,6 +483,11 @@ func (s *Service) handleConnect(w http.ResponseWriter, r *http.Request, sourceIP } return } + // Body reads extend the idle bound as data arrives. + _ = clientConn.SetReadDeadline(time.Now().Add(connectIdleTimeout)) + if req.Body != nil { + req.Body = &connectBodyReader{r: req.Body, conn: clientConn} + } if req.URL == nil { req.URL = &url.URL{} @@ -526,6 +531,24 @@ func (s *Service) handleConnect(w http.ResponseWriter, r *http.Request, sourceIP } } +// connectBodyReader refreshes the tunnel's idle read deadline on every +// successful read so a slow but active upload is not cut off by the deadline +// that bounded the header read. +type connectBodyReader struct { + r io.ReadCloser + conn net.Conn +} + +func (b *connectBodyReader) Read(p []byte) (int, error) { + n, err := b.r.Read(p) + if n > 0 { + _ = b.conn.SetReadDeadline(time.Now().Add(connectIdleTimeout)) + } + return n, err +} + +func (b *connectBodyReader) Close() error { return b.r.Close() } + func (s *Service) getOrCreateLeafCert(host string) (*tls.Certificate, error) { s.mu.RLock() cached := s.certCache[host] From 97507d74aea5c2c1462888edfbd21b1854514c29 Mon Sep 17 00:00:00 2001 From: kerem Date: Sat, 21 Nov 2026 22:28:45 +0300 Subject: [PATCH 3/3] fix(egress): arm the tunnel idle bound at body read time The bound was armed before the origin connection was established, so a slow dial or TLS handshake could expire it before the first body read; arm it when each body read starts instead. Wrapping http.NoBody also turned bodyless requests into chunked uploads, so leave it unwrapped. --- lib/egressproxy/service.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/egressproxy/service.go b/lib/egressproxy/service.go index d18b9092b..22b25dd4d 100644 --- a/lib/egressproxy/service.go +++ b/lib/egressproxy/service.go @@ -483,9 +483,9 @@ func (s *Service) handleConnect(w http.ResponseWriter, r *http.Request, sourceIP } return } - // Body reads extend the idle bound as data arrives. - _ = clientConn.SetReadDeadline(time.Now().Add(connectIdleTimeout)) - if req.Body != nil { + // Headers are in; each body read arms its own idle bound. + _ = clientConn.SetReadDeadline(time.Time{}) + if req.Body != nil && req.Body != http.NoBody { req.Body = &connectBodyReader{r: req.Body, conn: clientConn} } @@ -531,15 +531,16 @@ func (s *Service) handleConnect(w http.ResponseWriter, r *http.Request, sourceIP } } -// connectBodyReader refreshes the tunnel's idle read deadline on every -// successful read so a slow but active upload is not cut off by the deadline -// that bounded the header read. +// connectBodyReader arms the tunnel's idle read deadline when each body read +// starts, so slow connection setup cannot expire it, and keeps it refreshed on +// progress so only a client that stops sending is cut off. type connectBodyReader struct { r io.ReadCloser conn net.Conn } func (b *connectBodyReader) Read(p []byte) (int, error) { + _ = b.conn.SetReadDeadline(time.Now().Add(connectIdleTimeout)) n, err := b.r.Read(p) if n > 0 { _ = b.conn.SetReadDeadline(time.Now().Add(connectIdleTimeout))