Skip to content

fastcgi: explain 411 responses for unknown-length bodies - #7956

Merged
mholt merged 2 commits into
caddyserver:masterfrom
Rohilalala:fix/fastcgi-chunked-411
Sep 23, 2026
Merged

mholt merged 2 commits into
caddyserver:masterfrom
Rohilalala:fix/fastcgi-chunked-411

Conversation

@Rohilalala

Copy link
Copy Markdown
Contributor

Closes #7386.

What happens

A request whose body has no length of its own — Transfer-Encoding: chunked, or an HTTP/2 or HTTP/3 request 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.

That part is working as intended. The problem is that the refusal said none of it.

Transport.RoundTrip passes r.ContentLength to client.Post, and net/http reports -1 for such a request, so the operator got a bare 411 Length Required carrying 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, and the reporter closed by saying they were "still not 100% sure this issue is on the caddy side".

What this changes

The 411 now carries the reason and the remedy, and the unknown-length case is told apart from a genuinely malformed value:

request body has no known length; increase request_buffers so the body can be
buffered in full to determine it

ParseUint becomes ParseInt plus an explicit negative check. That is strictly tighter — values above MaxInt64 used to be accepted — and unreachable anyway, since CONTENT_LENGTH is always strconv.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 (or setting it to -1) is still what makes a large chunked body work, and the integration test demonstrates that it does.

Why not just make it work

Two things I deliberately did not do, in case they come up in review:

  • I did not raise the default buffer size. The 4096 default exists to bound memory and slowloris exposure. Making it unlimited by default would trade a 411 for a memory-exhaustion vector.
  • I did not change the behaviour at the exact buffer boundary. A body of precisely request_buffers bytes is treated as partial, because io.CopyN filling the buffer cannot be distinguished from a body with more to come. Finding out 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 then stops, the current code hands the buffered prefix on immediately, while peeking one byte first delivers nothing at all. TestBufferingBoundary records the behaviour rather than changing it.

Tests

  • TestPostRejectsUnknownLengthWithRemedy goes through Post, the entry point RoundTrip actually uses, so the message cannot rot on a branch production never reaches.
  • TestDoRejectsUnusableContentLength covers the values Do itself guards against.
  • TestFastCGIChunkedRequestBody drives a chunked body through a fastcgi reverse_proxy over a unix socket against a real net/http/fcgi responder, at each side of the buffer boundary, including request_buffers -1 succeeding at any size. The responder reports how many body bytes arrived, so a truncated body fails rather than passing quietly.

Verified with gofmt, go vet, golangci-lint (0 issues), and go test ./modules/caddyhttp/reverseproxy/... -race -count=2 -shuffle=on.

Assistance Disclosure

AI used, extensively. Claude (Claude Code) did the investigation, wrote the change and the tests, and ran the verification described above. Notably it also talked me out of its own first attempt: the initial fix was reverted after review showed the boundary change could block a paused stream, and the measurement quoted above is why the current patch leaves that alone.

@CLAassistant

CLAassistant commented Aug 24, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@steadytao

Copy link
Copy Markdown
Member

Commits should resolve to the same account as the PR submitter. Sign the CLA and address CI failures if any occur.

@steadytao
steadytao self-requested a review August 24, 2026 16: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 caddyserver#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.
@Rohilalala
Rohilalala force-pushed the fix/fastcgi-chunked-411 branch from edb1d2f to 6ea2962 Compare August 24, 2026 16:50
@steadytao steadytao added the bug 🐞 Something isn't working label Aug 25, 2026
@steadytao steadytao changed the title fastcgi: explain the 411 a body without a length gets fastcgi: explain 411 responses for unknown-length bodies Aug 25, 2026

@steadytao steadytao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@steadytao steadytao added this to the v2.11.5 milestone Aug 25, 2026

@mholt mholt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@mholt
mholt merged commit c798d4c into caddyserver:master Sep 23, 2026
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug 🐞 Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error 411 when using Transfer-Encoding: chunked via reverse proxy over unix-socket & fastcgi

4 participants