rproxy: read the rest of a Connection: close visitor's body after an early answer (closes #373)
check / check (push) Waiting to run

Go's server reads the body left unread once the handler returns, but not
for a request with Connection: close: it closes the connection with the
rest unread, and the visitor, still sending, gets a reset. rproxy now
reads that rest itself once it has answered, up to the 256 KiB Go's
server reads, until the deadline already set on the connection.

Model: opus-5-5
This commit is contained in:
clawbot committed 2026-11-12 06:33:29 +00:00
1 parent aaa9719f02
commit 39430dca22
3 files changed
+84 -2

No files matched your search

+61
View File
@@ -131,6 +131,67 @@ func TestADenyOutranksTheBodySizeLimit(t *testing.T) {
}
}
func TestAVisitorSendingConnectionCloseReadsAnAnswerBeforeTheRestOfItsBody(
t *testing.T,
) {
t.Parallel()
for _, tc := range []struct {
name string
waf http.HandlerFunc
status int
}{
{"over the size limit", allow, http.StatusRequestEntityTooLarge},
{"denied", deny, http.StatusForbidden},
} {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
standIn := startWAF(t, tc.waf)
app := startApp(t, func(http.ResponseWriter, *http.Request) {
t.Error("the app was asked")
})
addr, _ := startRProxy(t, standIn.URL, app.URL,
map[string]string{maxRequestSize: "1K"})
const size = 129 << 10
conn := dial(t, addr)
send(t, conn, post+"Connection: close\r\n"+
announcedLength(size)+
strings.Repeat("x", questionBodyMaxBytes))
reader := bufio.NewReader(conn)
res, err := http.ReadResponse(reader, nil)
if err != nil {
t.Fatalf("read the answer: %v", err)
}
_, _ = io.Copy(io.Discard, res.Body)
_ = res.Body.Close()
if res.StatusCode != tc.status {
t.Errorf("status %d, want %d", res.StatusCode, tc.status)
}
// The rest goes in small parts, so that a reset fails a later one.
for sent := questionBodyMaxBytes; sent < size; sent += 1 << 10 {
send(t, conn, strings.Repeat("x", 1<<10))
}
rest, err := io.ReadAll(reader)
if err != nil || len(rest) != 0 {
t.Errorf(
"after the answer, read %q and %v, want the end",
rest,
err,
)
}
})
}
}
func TestABodyNotAnnouncedOverItsLimitIsRefused(t *testing.T) {
t.Parallel()