Do not apply chunked encoding to 204/304 responses - #392
Open
DmitryBoiadji wants to merge 1 commit into
Open
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Http1Driverspecial-casesHEADrequests but not bodyless status codes: a 204 or 304 response without acontent-lengthheader getstransfer-encoding: chunkedand the0\r\n\r\nterminator.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('')setscontent-length: 0), but passing any header array to the constructor makessetHeaders()replace all headers, dropping the auto-setcontent-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
HEADrequests already do. Setting$need = nullin that branch also stops thefinallyblock from treating the intentionally unwritten body as a truncated write and abruptly closing the connection (previously reachable with aHEADresponse carrying acontent-length).Repro (v3.4.6, PHP 8.4):
The three added tests fail on 3.x without the driver change and pass with it; the rest of the suite,
composer code-styleand Psalm are unaffected.