From edcdfa0ed88f7dcc1c53720d25218fde9d63eb1e Mon Sep 17 00:00:00 2001 From: wangshun <96341168+xixi-box@users.noreply.github.com> Date: Thu, 19 Nov 2026 15:04:44 +0800 Subject: [PATCH] fix: v2.11.9: clearing a handler read deadline installs read_body as a hard deadline, truncating responses of requests longer than read_body (regression from #8284) (#8400) * fix: v2.11.9: clearing a handler read deadline installs read_body as a h Signed-off-by: wangshun <96341168+xixi-box@users.noreply.github.com> * caddyhttp: add regression test for cleared handler deadline after terminal body * fix(http): avoid synthetic read deadline for bodyless requests Keep a handler-cleared read deadline cleared when net/http supplied http.NoBody, and cover the case with an HTTP/1 regression test. Assisted by OpenAI Codex; reviewed and approved by the author. Signed-off-by: wangshun <96341168+xixi-box@users.noreply.github.com> --------- Signed-off-by: wangshun <96341168+xixi-box@users.noreply.github.com> Co-authored-by: xixi-box --- modules/caddyhttp/idletimeout.go | 9 +++++- modules/caddyhttp/idletimeout_test.go | 28 ++++++++++++++++++ modules/caddyhttp/server.go | 5 +++- modules/caddyhttp/server_test.go | 41 +++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 2 deletions(-) diff --git a/modules/caddyhttp/idletimeout.go b/modules/caddyhttp/idletimeout.go index c3967940f..4e1b6aa0a 100644 --- a/modules/caddyhttp/idletimeout.go +++ b/modules/caddyhttp/idletimeout.go @@ -225,9 +225,16 @@ func (r *IdleTimeoutReader) SetReadDeadline(deadline time.Time) error { } // ceilingLocked returns the earliest of HardDeadline and the deadline -// set by the handler, or zero if there is neither. +// set by the handler, or zero if there is neither. Once the body has +// reached a terminal state, HardDeadline no longer applies: it bounds +// reading the body, and re-arming it after the body has been read would +// let it cancel the request long after the handler cleared its own +// deadline (see #8399). func (r *IdleTimeoutReader) ceilingLocked() time.Time { hard := r.Deadline.HardDeadline + if r.terminalErr != nil { + hard = time.Time{} + } if r.handlerDeadline.IsZero() || (!hard.IsZero() && hard.Before(r.handlerDeadline)) { return hard } diff --git a/modules/caddyhttp/idletimeout_test.go b/modules/caddyhttp/idletimeout_test.go index af96381fb..056e56991 100644 --- a/modules/caddyhttp/idletimeout_test.go +++ b/modules/caddyhttp/idletimeout_test.go @@ -1573,6 +1573,34 @@ func TestIdleTimeoutReaderRestoresHardDeadline(t *testing.T) { } } +// TestIdleTimeoutReaderTerminalBodyDropsHardDeadline checks #8399: once +// the body has reached its terminal state, a handler clearing its read +// deadline must not re-arm the hard deadline. The hard deadline bounds +// reading the body, so re-arming it after the body was read would fail +// the request long after the handler cleared its own deadline. +func TestIdleTimeoutReaderTerminalBodyDropsHardDeadline(t *testing.T) { + for _, clear := range []bool{false, true} { + t.Run(fmt.Sprintf("clear=%t", clear), func(t *testing.T) { + hard := time.Now().Add(time.Minute) + rec := &readDeadlineRecorder{ResponseRecorder: httptest.NewRecorder()} + r := &IdleTimeoutReader{ + ReadCloser: io.NopCloser(strings.NewReader("abcd")), + Ctrl: http.NewResponseController(rec), + Deadline: IdleDeadline{Timeout: time.Hour, HardDeadline: hard}, + Logger: zap.NewNop(), + ClearBetweenReads: clear, + } + _, err := io.Copy(io.Discard, r) + require.NoError(t, err) + // The handler clears its own deadline after reading the body. + require.NoError(t, r.SetReadDeadline(time.Time{})) + deadlines := rec.snapshot() + assert.True(t, deadlines[len(deadlines)-1].IsZero(), + "a cleared handler deadline must not re-arm the hard deadline after the body was read") + }) + } +} + // TestIdleTimeoutWriterRestoresHardDeadline checks that another handler's // deadline update cannot bypass the hard ceiling on writes or the final flush. func TestIdleTimeoutWriterRestoresHardDeadline(t *testing.T) { diff --git a/modules/caddyhttp/server.go b/modules/caddyhttp/server.go index 8a7431792..e2902eeac 100644 --- a/modules/caddyhttp/server.go +++ b/modules/caddyhttp/server.go @@ -642,7 +642,10 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { // being silently overwritten by the first read/write. rc := http.NewResponseController(w) var readHardDeadline, writeHardDeadline time.Time - if s.ReadTimeout > 0 { + // There is no body read to bound when net/http already supplied NoBody. + // Installing a synthetic deadline here can cancel a bodyless request + // after its handler explicitly clears the connection read deadline. + if s.ReadTimeout > 0 && r.Body != http.NoBody { readHardDeadline = start.Add(time.Duration(s.ReadTimeout)) } if s.WriteTimeout > 0 { diff --git a/modules/caddyhttp/server_test.go b/modules/caddyhttp/server_test.go index 6692456a6..65a1ce25b 100644 --- a/modules/caddyhttp/server_test.go +++ b/modules/caddyhttp/server_test.go @@ -1666,6 +1666,47 @@ func TestServer_ServeHTTP_HandlerReadDeadline(t *testing.T) { } } +// An HTTP/1 request without a body should not retain a synthetic body read +// deadline after the handler clears it, even if the handler never reads Body. +func TestServer_ServeHTTP_NoBodyClearsReadDeadline(t *testing.T) { + result := make(chan error, 1) + const timeout = 100 * time.Millisecond + s := &Server{ + ReadTimeout: caddy.Duration(timeout), + ReadIdleTimeout: caddy.Duration(timeout), + logger: zap.NewNop(), + errorLogger: zap.NewNop(), + primaryHandlerChain: HandlerFunc(func(w http.ResponseWriter, r *http.Request) error { + if r.ContentLength != 0 { + result <- fmt.Errorf("unexpected content length: %d", r.ContentLength) + return nil + } + if err := http.NewResponseController(w).SetReadDeadline(time.Time{}); err != nil { + result <- err + return nil + } + select { + case <-r.Context().Done(): + result <- r.Context().Err() + case <-time.After(300 * time.Millisecond): + result <- nil + w.WriteHeader(http.StatusNoContent) + } + return nil + }), + } + ts := httptest.NewUnstartedServer(http.HandlerFunc(s.ServeHTTP)) + ts.Config.ReadTimeout = timeout + ts.Start() + t.Cleanup(ts.Close) + + resp, err := ts.Client().Get(ts.URL) + require.NoError(t, err) + t.Cleanup(func() { resp.Body.Close() }) + assert.Equal(t, http.StatusNoContent, resp.StatusCode) + require.NoError(t, <-result, "the bodyless request context should survive its cleared deadline") +} + // TestServer_BuildHTTP3ServerEnablesWebTransport asserts that with // WebTransport set the http3.Server advertises WebTransport in // its SETTINGS and wires the prerequisites webtransport.Server.Upgrade