From 342dfe4f97e136c601cd6a5215edb7caae46c22d Mon Sep 17 00:00:00 2001 From: sneak Date: Thu, 19 Nov 2026 08:11:12 +0000 Subject: [PATCH] Test rproxy's 32 KiB header limit at its edge (closes #415) The limit was already a constant with no setting. These tests pin it: a request line and headers of exactly 32 KiB reach the app, and one byte more is answered 431 with the connection closed and waf not asked. Model: opus-5-5 --- internal/rproxy/limits_test.go | 53 ++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/internal/rproxy/limits_test.go b/internal/rproxy/limits_test.go index ce326f5..8035e4e 100644 --- a/internal/rproxy/limits_test.go +++ b/internal/rproxy/limits_test.go @@ -71,6 +71,59 @@ func TestServerHasTheFixedLimits(t *testing.T) { } } +// getOfSize is a GET whose request line and headers, the blank line that ends +// them included, are size bytes long. +func getOfSize(size int) string { + const start, end = "GET / HTTP/1.1\r\nHost: site.example\r\nX-Pad: ", "\r\n\r\n" + + return start + strings.Repeat("x", size-len(start)-len(end)) + end +} + +func TestARequestLineAndHeadersOf32KiBReachTheApp(t *testing.T) { + t.Parallel() + + standIn := startWAF(t, allow) + app := startApp(t, greet) + addr, _ := startRProxy(t, standIn.URL, app.URL, nil) + + conn := dial(t, addr) + send(t, conn, getOfSize(32<<10)) + + if status := readStatus(t, conn); status != http.StatusOK { + t.Errorf("status %d, want the app's 200", status) + } +} + +func TestARequestLineAndHeadersOver32KiBAreRefusedAndTheConnectionClosed( + t *testing.T, +) { + t.Parallel() + + standIn := startWAF(t, allow) + app := startApp(t, func(http.ResponseWriter, *http.Request) { + t.Error("the app was asked") + }) + addr, _ := startRProxy(t, standIn.URL, app.URL, nil) + + conn := dial(t, addr) + send(t, conn, getOfSize(32<<10+1)) + + // io.ReadAll returns without an error only once rproxy has closed the + // connection. + text, err := io.ReadAll(conn) + if err != nil || !strings.HasPrefix(string(text), "HTTP/1.1 431 ") { + t.Errorf( + "read %q (%v), want 431 and the connection closed", + text, + err, + ) + } + + if len(standIn.questions) != 0 { + t.Error("waf was asked") + } +} + func TestABodyAnnouncedOverItsLimitIsRefusedOnceWAFAllowsIt(t *testing.T) { t.Parallel()