mirror of
https://github.com/caddyserver/caddy.git
synced 2026-10-05 20:31:45 -04:00
A request whose body has no length of its own — Transfer-Encoding: chunked, or HTTP/2 and HTTP/3 requests sent without the header — cannot be forwarded over FastCGI until it has been buffered in full, because CGI/1.1 requires CONTENT_LENGTH and php-fpm hangs when it is absent or wrong and the body is not empty. When request_buffers is too small to hold the whole body, the length stays unknown and the request is refused with 411. The refusal said none of that. RoundTrip passes r.ContentLength to Post, which is -1 for such a request, so the operator got a bare "411 Length Required" with either no error at all or strconv's "invalid syntax" on a value they never wrote. On #7386 that sent two people looking for the fault in their backend. The 411 now carries the reason and the remedy, and the unknown-length case is told apart from a genuinely malformed value. ParseUint becomes ParseInt plus an explicit negative check, which is strictly tighter: values above MaxInt64 used to be accepted and are unreachable anyway, since CONTENT_LENGTH is always FormatInt of an int64 by the time Do sees it. No status code changes. Every request that was refused before is still refused; raising request_buffers is still what makes a large chunked body work. Tests: the reachable path through Post, the unusable CONTENT_LENGTH values Do itself guards against, an integration test driving a chunked body through a fastcgi reverse_proxy over a unix socket at each side of the buffer boundary (including request_buffers -1, which succeeds at any size and is the remedy), and a boundary test recording that a body exactly the size of the buffer counts as partial. That last one is deliberately not changed here. Telling "exactly the limit" apart from "more to come" costs a read that a paused stream may never answer, and bufferedBody also backs response_buffers: measured against a body that delivers exactly the limit and stops, the current code hands the buffered prefix on immediately, while peeking one byte first delivers nothing at all. Co-authored-by: Aditya <205600203+Rohilalala@users.noreply.github.com> Co-authored-by: Zen Dodd <mail@steadytao.com>
135 lines
4.4 KiB
Go
135 lines
4.4 KiB
Go
// Copyright 2015 Matthew Holt and The Caddy Authors
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package fastcgi
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
|
)
|
|
|
|
// TestDoRejectsUnusableContentLength pins what the client does with a
|
|
// CONTENT_LENGTH it cannot use, and which of those cases carries which message.
|
|
//
|
|
// Only "-1" is reachable through Transport.RoundTrip, which is the shape a
|
|
// chunked request takes: Get/Head/Options/Post all set CONTENT_LENGTH before
|
|
// Do sees it, from the r.ContentLength that net/http reports as -1 when it has
|
|
// no length. The absent and empty cases guard Do's own contract for any future
|
|
// caller; an absent key was the one that used to return a 411 with no error at
|
|
// all, while an empty value carried strconv's syntax error, true but useless
|
|
// to an operator. TestPostRejectsUnknownLengthWithRemedy covers the reachable
|
|
// path end to end.
|
|
func TestDoRejectsUnusableContentLength(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
env map[string]string
|
|
wantErr error
|
|
}{
|
|
{
|
|
name: "absent",
|
|
env: map[string]string{},
|
|
wantErr: errNoContentLength,
|
|
},
|
|
{
|
|
name: "empty",
|
|
env: map[string]string{"CONTENT_LENGTH": ""},
|
|
wantErr: errNoContentLength,
|
|
},
|
|
{
|
|
// The reported case: what a chunked request becomes by the time it
|
|
// reaches here, and the only one production can produce.
|
|
name: "negative, as a chunked request arrives",
|
|
env: map[string]string{"CONTENT_LENGTH": "-1"},
|
|
wantErr: errNoContentLength,
|
|
},
|
|
{
|
|
name: "not a number",
|
|
env: map[string]string{"CONTENT_LENGTH": "banana"},
|
|
wantErr: errBadContentLength,
|
|
},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
c := &client{}
|
|
_, err := c.Do(tc.env, strings.NewReader("body"))
|
|
if err == nil {
|
|
t.Fatal("expected an error")
|
|
}
|
|
|
|
var handlerErr caddyhttp.HandlerError
|
|
if !errors.As(err, &handlerErr) {
|
|
t.Fatalf("error %v is not a caddyhttp.HandlerError", err)
|
|
}
|
|
if handlerErr.StatusCode != http.StatusLengthRequired {
|
|
t.Errorf("status = %d, want %d", handlerErr.StatusCode, http.StatusLengthRequired)
|
|
}
|
|
// The status alone is what sent people hunting through their
|
|
// backend logs, so the cause has to travel with it.
|
|
if handlerErr.Err == nil {
|
|
t.Fatal("the 411 carries no error to explain it")
|
|
}
|
|
if !errors.Is(err, tc.wantErr) {
|
|
t.Errorf("error %v does not wrap %v", err, tc.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestNoContentLengthErrorNamesTheRemedy keeps the message actionable: the
|
|
// operator has to learn that buffering is what supplies the missing length.
|
|
func TestNoContentLengthErrorNamesTheRemedy(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
if !strings.Contains(errNoContentLength.Error(), "request_buffers") {
|
|
t.Errorf("error %q does not name request_buffers as the remedy", errNoContentLength)
|
|
}
|
|
}
|
|
|
|
// TestPostRejectsUnknownLengthWithRemedy goes through the entry point RoundTrip
|
|
// actually uses. Transport.RoundTrip passes r.ContentLength straight to Post,
|
|
// and net/http reports -1 for a chunked body, so this is the exact call the
|
|
// reporter of #7386 made. Asserting it only against Do would let the message
|
|
// rot on a branch production never reaches.
|
|
func TestPostRejectsUnknownLengthWithRemedy(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
c := &client{}
|
|
env := map[string]string{}
|
|
|
|
_, err := c.Post(env, http.MethodPost, "application/octet-stream", strings.NewReader("body"), -1)
|
|
if err == nil {
|
|
t.Fatal("expected an error")
|
|
}
|
|
if !errors.Is(err, errNoContentLength) {
|
|
t.Errorf("error %v does not wrap errNoContentLength", err)
|
|
}
|
|
if !strings.Contains(err.Error(), "request_buffers") {
|
|
t.Errorf("the error operators see, %q, does not name the remedy", err)
|
|
}
|
|
|
|
var handlerErr caddyhttp.HandlerError
|
|
if !errors.As(err, &handlerErr) || handlerErr.StatusCode != http.StatusLengthRequired {
|
|
t.Errorf("error %v is not a 411 HandlerError", err)
|
|
}
|
|
}
|