proxyproto: pin header deadline, marker order and mapped peers
Review fix round 1 (tests only). Pins that the header deadline is cleared after the parse and counts from accept, that a caller's read deadline set before resolution survives it, that a marker header is closed and counted even with the barrier up, and that an IPv4-mapped peer on a dual-stack listener matches a trusted IPv4 address.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+177
−0
internal/proxyproto/listener_test.go+177 −0
Intent
diff --git a/internal/proxyproto/listener_test.go b/internal/proxyproto/listener_test.go| index 71366bd40..766810bfc 100644 |
| --- a/internal/proxyproto/listener_test.go |
| +++ b/internal/proxyproto/listener_test.go |
| @@ -2,6 +2,7 @@ package proxyproto |
| import ( |
| "bytes" |
| + "fmt" |
| "io" |
| "net" |
| "net/netip" |
| @@ -524,3 +525,179 @@ func TestCloseTrustedClosesPendingAndOpen(t *testing.T) { |
| t.Fatalf("second CloseTrusted = %d, want 0 (closed conns leave the registry)", n) |
| } |
| } |
| + |
| +// readWithin reads len(want) bytes from c without setting a deadline of its |
| +// own, failing if that takes longer than d. |
| +func readWithin(t *testing.T, c net.Conn, want []byte, d time.Duration) { |
| + t.Helper() |
| + type res struct { |
| + b []byte |
| + err error |
| + } |
| + ch := make(chan res, 1) |
| + go func() { |
| + b := make([]byte, len(want)) |
| + _, err := io.ReadFull(c, b) |
| + ch <- res{b, err} |
| + }() |
| + select { |
| + case r := <-ch: |
| + if r.err != nil { |
| + t.Fatalf("read: %v (got %q)", r.err, r.b) |
| + } |
| + if !bytes.Equal(r.b, want) { |
| + t.Fatalf("read %q, want %q", r.b, want) |
| + } |
| + case <-time.After(d): |
| + c.Close() |
| + t.Fatalf("read did not return within %v", d) |
| + } |
| +} |
| + |
| +// TestTrustedHeaderDeadlineCleared: once the header is read, the header |
| +// deadline is gone; a keep-alive read after it would have passed succeeds. |
| +func TestTrustedHeaderDeadlineCleared(t *testing.T) { |
| + l := listen(t, Config{Trusted: loopback, HeaderTimeout: 50 * time.Millisecond}) |
| + client, c := pair(t, l) |
| + write(t, client, proxyHeader("203.0.113.7:51000")) |
| + if s := stateWithin(t, c, time.Second); s != StateProxied { |
| + t.Fatalf("State = %v, want proxied", s) |
| + } |
| + time.Sleep(100 * time.Millisecond) // past accept + HeaderTimeout |
| + write(t, client, []byte("GET")) |
| + readWithin(t, c, []byte("GET"), time.Second) |
| + if n := l.Counters().Timeout; n != 0 { |
| + t.Fatalf("Timeout = %d, want 0", n) |
| + } |
| +} |
| + |
| +// TestTrustedCallerDeadlineSurvivesParse: a read deadline the caller set |
| +// before the state resolved is the one in force after the parse, not the |
| +// header deadline and not none. |
| +func TestTrustedCallerDeadlineSurvivesParse(t *testing.T) { |
| + l := listen(t, Config{Trusted: loopback, HeaderTimeout: 50 * time.Millisecond}) |
| + client, c := pair(t, l) |
| + callerDL := time.Now().Add(400 * time.Millisecond) |
| + if err := c.SetReadDeadline(callerDL); err != nil { |
| + t.Fatalf("SetReadDeadline: %v", err) |
| + } |
| + write(t, client, proxyHeader("203.0.113.7:51000")) |
| + if s := stateWithin(t, c, time.Second); s != StateProxied { |
| + t.Fatalf("State = %v, want proxied", s) |
| + } |
| + time.Sleep(100 * time.Millisecond) // past the header deadline |
| + write(t, client, []byte("GET")) |
| + readWithin(t, c, []byte("GET"), time.Second) |
| + |
| + // No more data: the read must end at the caller's deadline. |
| + errc := make(chan error, 1) |
| + go func() { |
| + var b [1]byte |
| + _, err := c.Read(b[:]) |
| + errc <- err |
| + }() |
| + select { |
| + case err := <-errc: |
| + if !isTimeout(err) { |
| + t.Fatalf("read error = %v, want a timeout", err) |
| + } |
| + if time.Now().Before(callerDL) { |
| + t.Fatal("read timed out before the caller's deadline") |
| + } |
| + case <-time.After(2 * time.Second): |
| + c.Close() |
| + t.Fatal("read did not time out: the caller's deadline was lost") |
| + } |
| +} |
| + |
| +// TestTrustedHeaderDeadlineFromAccept: the header deadline counts from |
| +// accept, so a resolution started after HeaderTimeout has passed times out |
| +// even with the header already sent. |
| +func TestTrustedHeaderDeadlineFromAccept(t *testing.T) { |
| + l := listen(t, Config{Trusted: loopback, HeaderTimeout: 50 * time.Millisecond}) |
| + client, c := pair(t, l) |
| + write(t, client, append(proxyHeader("203.0.113.7:51000"), "GET"...)) |
| + time.Sleep(100 * time.Millisecond) |
| + |
| + if s := stateWithin(t, c, time.Second); s != StateFailed { |
| + t.Fatalf("State = %v, want failed (deadline from accept)", s) |
| + } |
| + cn := l.Counters() |
| + if cn.Timeout != 1 || cn.Proxied != 0 { |
| + t.Fatalf("Timeout = %d, Proxied = %d, want 1 and 0", cn.Timeout, cn.Proxied) |
| + } |
| +} |
| + |
| +// TestBarrierMarkerStillClosed: with the barrier up, a marker header is |
| +// still closed and counted MarkerTrusted, the S3 evidence spec §4.7 and |
| +// §6.4 read before trust-resume. |
| +func TestBarrierMarkerStillClosed(t *testing.T) { |
| + l := listen(t, Config{ |
| + Trusted: loopback, |
| + HeaderTimeout: 5 * time.Second, |
| + Marker: netip.MustParsePrefix("192.0.2.0/24"), |
| + Barrier: func() bool { return true }, |
| + }) |
| + client, c := pair(t, l) |
| + write(t, client, append(proxyHeader("192.0.2.1:4000"), "GET"...)) |
| + |
| + if s := stateWithin(t, c, time.Second); s != StateFailed { |
| + t.Fatalf("State = %v, want failed", s) |
| + } |
| + expectClosed(t, client) |
| + cn := l.Counters() |
| + if cn.MarkerTrusted != 1 || cn.NoClient != 0 || cn.Proxied != 0 { |
| + t.Fatalf("MarkerTrusted = %d, NoClient = %d, Proxied = %d, want 1, 0, 0", |
| + cn.MarkerTrusted, cn.NoClient, cn.Proxied) |
| + } |
| +} |
| + |
| +// TestTrustedIPv4MappedPeer: on a dual-stack listener an IPv4 peer arrives |
| +// as ::ffff:a.b.c.d and must still match a trusted IPv4 address; a |
| +// Trusted entry written IPv4-mapped matches too. |
| +func TestTrustedIPv4MappedPeer(t *testing.T) { |
| + for _, tc := range []struct { |
| + name string |
| + trusted string |
| + }{ |
| + {"trusted-ipv4", "127.0.0.1"}, |
| + {"trusted-mapped", "::ffff:127.0.0.1"}, |
| + } { |
| + t.Run(tc.name, func(t *testing.T) { |
| + inner, err := net.Listen("tcp", "[::]:0") |
| + if err != nil { |
| + t.Skipf("no IPv6 listener: %v", err) |
| + } |
| + l := NewListener(inner, Config{ |
| + Trusted: []netip.Addr{netip.MustParseAddr(tc.trusted)}, |
| + HeaderTimeout: 5 * time.Second, |
| + }) |
| + t.Cleanup(func() { l.Close() }) |
| + port := inner.Addr().(*net.TCPAddr).Port |
| + client, err := net.Dial("tcp", net.JoinHostPort("127.0.0.1", fmt.Sprint(port))) |
| + if err != nil { |
| + t.Skipf("listener is not dual-stack: %v", err) |
| + } |
| + t.Cleanup(func() { client.Close() }) |
| + c := accept(t, l) |
| + kernel := c.NetConn().RemoteAddr().(*net.TCPAddr).AddrPort().Addr() |
| + if !kernel.Is4In6() { |
| + t.Skipf("kernel peer %s is not IPv4-mapped", kernel) |
| + } |
| + |
| + write(t, client, append(proxyHeader("203.0.113.7:51000"), "GET"...)) |
| + if !c.Trusted() { |
| + t.Fatalf("Trusted = false for mapped peer %s, want true", kernel) |
| + } |
| + if s := stateWithin(t, c, time.Second); s != StateProxied { |
| + t.Fatalf("State = %v, want proxied", s) |
| + } |
| + if got := c.RemoteAddr().String(); got != "203.0.113.7:51000" { |
| + t.Fatalf("RemoteAddr = %s, want 203.0.113.7:51000", got) |
| + } |
| + if n := l.Counters().ProxiedByNode[netip.MustParseAddr("127.0.0.1")]; n != 1 { |
| + t.Fatalf("ProxiedByNode[127.0.0.1] = %d, want 1", n) |
| + } |
| + }) |
| + } |
| +} |