Repository navigation
Description
Two bugs in how the forward proxy runs a CONNECT tunnel. Both break protocols that work fine when connecting direct, and both leave no error anywhere.
1. With the TLS bridge on, a server that speaks first hangs forever
What happens
Some protocols send a banner before the client sends anything: SSH, SMTP, FTP, many databases. Through a CONNECT with the TLS bridge enabled (the laptop default), the client receives nothing, ever. Each such tunnel holds two file descriptors (fds) and a goroutine until the client gives up.
Reproduction
A listener that writes SSH-2.0-banner\r\n on accept, reached through CONNECT:
tlsBridge=false: server-first banner through CONNECT: got "SSH-2.0-banner\r\n" err=<nil> after 0s
tlsBridge=true: server-first banner through CONNECT: got "" err=i/o timeout after 8.001s
50 idle CONNECTs held 100 fds until the client closed them.
Why
core/listener/forwardproxy/server.go:2257, which runs on every CONNECT when s.TLSBridge != nil, after the upstream has already been dialed and before any port or host check:
pc := &peekedConn{Conn: clientConn, r: bufio.NewReaderSize(clientConn, sniffBufSize)}
clientConn = pc
first, _ := pc.Peek(5) // no read deadlineThe proxy waits for the client's first 5 bytes, to tell a TLS ClientHello from anything else. A server-first protocol's client sends nothing until it sees the banner, so both sides wait on each other. The tunnel that would carry the banner is not started until the peek returns.
Possible fix
Bound the peek the way sniffHost already does (sniff.go:61):
_ = clientConn.SetReadDeadline(time.Now().Add(sniffTimeout))
first, _ := pc.Peek(5)
_ = clientConn.SetReadDeadline(time.Time{})A short or empty first already classifies as not-TLS, so the request falls through to the opaque tunnel. The banner then arrives after at most sniffTimeout (5 s); something shorter, around 500 ms, would make the delay unnoticeable.
2. Half-close is lost: the tunnel closes both directions when one side finishes sending
What happens
A client that sends its request, closes its sending side (TCP half-close, shutdown(SHUT_WR)), then reads the reply, gets nothing through an opaque tunnel.
Reproduction
A server that reads to EOF, then replies GOT <n> BYTES. The client writes hello and then calls CloseWrite():
direct: reply "GOT 5 BYTES" err=<nil>
through CONNECT: reply "" err=<nil>
Why
tunnel in core/listener/forwardproxy/transparent.go:327-339 closes both connections as soon as either copy finishes:
go func() {
n, _ := io.Copy(upstream, client)
_ = upstream.Close() // the client's EOF closes the upstream completely,
_ = client.Close() // so the reply never makes it back
upc <- n
}()Possible fix
When one copy ends, half-close the other side's write half (CloseWrite() when it is a *net.TCPConn, or anything with a CloseWrite() error method). Close both fully only after both copies have returned.
The comment above tunnel says closing both ends first is what keeps the wait for the byte counts short. Keep that property with a deadline after the half-close, not a full close: if the peer has not finished within N seconds, close both.
Reproduction source
Both tests (drop into core/listener/forwardproxy/; reuse mustBridgeDecision and mustParseURL)
package forwardproxy
import (
"bufio"
"fmt"
"io"
"net"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/rossoctl/cortex/core/pipeline"
"github.com/rossoctl/cortex/core/tlsbridge"
)
func connectVia(t *testing.T, proxy, target string) (net.Conn, *bufio.Reader) {
c, err := net.Dial("tcp", strings.TrimPrefix(proxy, "http://"))
if err != nil {
t.Fatal(err)
}
fmt.Fprintf(c, "CONNECT %s HTTP/1.1\r\nHost: %s\r\n\r\n", target, target)
br := bufio.NewReader(c)
resp, err := http.ReadResponse(br, nil)
if err != nil || resp.StatusCode != 200 {
t.Fatalf("CONNECT: %v %v", err, resp)
}
return c, br
}
func tunnelProxy(t *testing.T, bridge bool) string {
pipe, _ := pipeline.New(nil)
srv, _ := NewServer(pipeline.NewHolder(pipe), nil, nil)
if bridge {
src, _ := tlsbridge.NewEphemeralSource()
up, _ := tlsbridge.NewUpstreamClient(nil, true)
srv.TLSBridge = &tlsbridge.Engine{
Decision: mustBridgeDecision(t, 443), // bridges :443 only, so the test port tunnels
Term: tlsbridge.NewTerminator(tlsbridge.NewMinter(src, tlsbridge.MinterOpts{})),
Skip: tlsbridge.NewSkipSet(), Programs: tlsbridge.NewProgramSkipSet(), Upstream: up, CAPEM: src.CACertPEM(),
}
}
p := httptest.NewServer(srv.Handler())
t.Cleanup(p.Close)
return p.URL
}
func TestServerFirstProtocolThroughBridge(t *testing.T) {
ln, _ := net.Listen("tcp", "127.0.0.1:0")
defer ln.Close()
go func() {
for {
c, err := ln.Accept()
if err != nil {
return
}
io.WriteString(c, "SSH-2.0-banner\r\n")
}
}()
c, br := connectVia(t, tunnelProxy(t, true), ln.Addr().String())
defer c.Close()
c.SetReadDeadline(time.Now().Add(8 * time.Second))
if line, err := br.ReadString('\n'); err != nil {
t.Fatalf("banner never arrived: %q %v", line, err)
}
}
func TestTunnelKeepsHalfClose(t *testing.T) {
ln, _ := net.Listen("tcp", "127.0.0.1:0")
defer ln.Close()
go func() {
for {
c, err := ln.Accept()
if err != nil {
return
}
go func() { b, _ := io.ReadAll(c); fmt.Fprintf(c, "GOT %d BYTES", len(b)); c.Close() }()
}
}()
c, br := connectVia(t, tunnelProxy(t, false), ln.Addr().String())
defer c.Close()
io.WriteString(c, "hello")
c.(*net.TCPConn).CloseWrite()
c.SetReadDeadline(time.Now().Add(3 * time.Second))
if b, _ := io.ReadAll(br); string(b) != "GOT 5 BYTES" {
t.Fatalf("reply = %q, want %q", b, "GOT 5 BYTES")
}
}Acceptance
- Both tests above pass.
- Tunnel rows still carry correct
bytesUp/bytesDown, anddurationMsstill ends when the tunnel does.
Found by an experiment-driven audit of main at 43cf553; every number above was re-measured on that commit.
Assisted-By: Claude (Anthropic AI) [email protected]
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
huang195 commentedon Oct 29, 2026
CI test for this bug class: #1553.
Test: Check that tunnels behave like direct connections