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 <xixi-box@users.noreply.github.com>
This commit is contained in:
parent
16618f454c
commit
edcdfa0ed8
4 changed files with 81 additions and 2 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue