Skip to content

Do not apply chunked encoding to 204/304 responses - #392

Open
DmitryBoiadji wants to merge 1 commit into
amphp:3.xfrom
DmitryBoiadji:fix-bodyless-response-framing
Open

Do not apply chunked encoding to 204/304 responses#392
DmitryBoiadji wants to merge 1 commit into
amphp:3.xfrom
DmitryBoiadji:fix-bodyless-response-framing

Conversation

@DmitryBoiadji

Copy link
Copy Markdown

Http1Driver special-cases HEAD requests but not bodyless status codes: a 204 or 304 response without a content-length header gets transfer-encoding: chunked and the 0\r\n\r\n terminator.

RFC 9112 §6.1: "A server MUST NOT send a Transfer-Encoding header field in any response with a status code of 1xx (Informational) or 204 (No Content)." Per §6.3, clients terminate 204/304 responses at the end of the header section regardless of framing headers - so the terminator bytes stay in the socket and are parsed as the start of the next response, killing the next request on the keep-alive connection. Observed against Chrome: every CORS preflight (204) poisoned its connection and the following fetch failed with a network error.

A bare new Response(204) is unaffected (setBody('') sets content-length: 0), but passing any header array to the constructor makes setHeaders() replace all headers, dropping the auto-set content-length - which is how adapters that copy headers from another response object hit this.

The fix excludes 204/304 from chunked encoding and skips the body write the same way HEAD requests already do. Setting $need = null in that branch also stops the finally block from treating the intentionally unwritten body as a truncated write and abruptly closing the connection (previously reachable with a HEAD response carrying a content-length).

Repro (v3.4.6, PHP 8.4):

$server = SocketHttpServer::createForDirectAccess(new NullLogger());
$server->expose('127.0.0.1:1337');
$server->start(
    new ClosureRequestHandler(fn () => new Response(204, ['x-demo' => '1'])),
    new DefaultErrorHandler(),
);
$ printf 'GET / HTTP/1.1\r\nHost: l\r\nConnection: keep-alive\r\n\r\n' | nc 127.0.0.1 1337
HTTP/1.1 204 No Content
x-demo: 1
connection: keep-alive
keep-alive: timeout=15
transfer-encoding: chunked

0        <- stray bytes; a compliant client stops reading at the blank line above

The three added tests fail on 3.x without the driver change and pass with it; the rest of the suite, composer code-style and Psalm are unaffected.

RFC 9112 forbids Transfer-Encoding on 204 responses (section 6.1), and
clients terminate 204/304 responses at the end of the header section
regardless of framing headers (section 6.3). The chunk terminator written
after the headers stayed in the socket and was parsed as the start of the
next response, breaking the next request on the keep-alive connection.

Observed against Chrome: every CORS preflight (204 without content-length)
poisoned its connection and the following fetch failed with a network error.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant