Repository navigation
Reject if Content-Length+Transfer-Encoding present - #164
Open
Willenbrink wants to merge 1 commit into
Open
Willenbrink wants to merge 1 commit into
Willenbrink wants to merge 1 commit into
Conversation
cuihtlauac
added a commit
to cuihtlauac/dream
that referenced
this pull request
Nov 2, 2026
Point the h2 stream-reset TODOs at anmonteiro/ocaml-h2#279, and add a TODO on the Content-Length fast path tracking the upstream rejection of requests that carry both Content-Length and Transfer-Encoding (anmonteiro/httpun#164, robur-coop/ocaml-h1#21; see also camlworks#184). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Willenbrink
added a commit
to camlworks/dream
that referenced
this pull request
Nov 2, 2026
* Add Dream.body_size_limit middleware (#456) Dream.body buffers the entire request body into memory, and run/serve expose no maximum body size, so a public no-auth endpoint has to rely on an external reverse proxy to avoid memory exhaustion from oversized payloads. Add a Dream.body_size_limit middleware that rejects over-sized request bodies with 413 Payload Too Large: - A Content-Length that exceeds the limit is rejected immediately, without reading the body. - Otherwise the request body stream is wrapped to count bytes and reject as soon as the running total passes the limit, so a chunked upload with no Content-Length is also handled and no more than the limit, plus one chunk, is ever buffered. A handler that never reads the body is unaffected. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * Reject negative body size limits * Add server-wide request body limit * Close the body stream and connection on 413 * Add TODOs * Reference upstream issues in body size limit TODOs Point the h2 stream-reset TODOs at anmonteiro/ocaml-h2#279, and add a TODO on the Content-Length fast path tracking the upstream rejection of requests that carry both Content-Length and Transfer-Encoding (anmonteiro/httpun#164, robur-coop/ocaml-h1#21; see also #184). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Send a complete 413 over HTTP/2 and test body limits end to end Resetting the stream with H2.Reqd.report_exn after the 413 headers made clients report a stream error (curl: "HTTP/2 stream 1 was not closed cleanly: INTERNAL_ERROR") instead of seeing a clean 413. Close the request body and complete the response instead: h2 then stops accepting the rest of the upload, so the client still stops sending. Add cram tests for the default 10 MiB limit: the boundary, 413 with Connection: close over HTTP/1.1 (with and without Content-Length), and a clean 413 over HTTP/2. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Document the body limit's Content-Length check and default Say that a declared Content-Length over the limit is rejected before the handler runs, how the connection or stream ends, and that Dream.run and Dream.serve apply a 10 MiB limit by default. Rewrap the docstring. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Take a plain int for ~max_body_size ?max_body_size:int option forced callers to write ~max_body_size:(Some n). Take an int instead; max_int disables the limit (the middleware is then not installed at all). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Expose the body size limit in dream-mirage Export Dream.body_size_limit from the Mirage functor, and give its http and https functions the same ~max_body_size argument and 10 MiB default as Dream.run and Dream.serve. The default now lives in Body_size_limit so that both backends share it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Co-authored-by: Sebastian Willenbrink <sebastian.willenbrink@gmail.com> Co-authored-by: Sebastian Willenbrink <sebastian.willenbrink@mailbox.org>
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.
Reject requests containing both Content-Length and Transfer-Encoding headers by returning
bad_requestinbody_length.RFC 9112 states:
I think rejecting such a request is reasonable: If I read the Content-Length of a request I expect that header to be relevant and not overridden by Transfer-Encoding. This comes up in e.g. camlworks/dream#461 where Content-Length is used to decide whether the request should be rejected.
One alternative would be to silently drop the Content-Length header and continue. That would be less obvious behavior IMHO.