Skip to content

Reject if Content-Length+Transfer-Encoding present - #164

Open
Willenbrink wants to merge 1 commit into
anmonteiro:masterfrom
Willenbrink:drop-cl-if-te-present
Open

Willenbrink wants to merge 1 commit into
anmonteiro:masterfrom
Willenbrink:drop-cl-if-te-present

Conversation

@Willenbrink

Copy link
Copy Markdown

Reject requests containing both Content-Length and Transfer-Encoding headers by returning bad_request in body_length.

RFC 9112 states:

If a message is received with both a Transfer-Encoding and a Content-Length header field, the Transfer-Encoding overrides the Content-Length. Such a message might indicate an attempt to perform request smuggling (Section 11.2) or response splitting (Section 11.1) and ought to be handled as an error. An intermediary that chooses to forward the message MUST first remove the received Content-Length field and process the Transfer-Encoding (as described below) prior to forwarding the message downstream.

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.

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant