Skip to content

fix: skip empty DATA frames when receiving body data - #373

Open
cfal wants to merge 1 commit into
hyperium:masterfrom
cfal:fix/empty-data-frame-eof
Open

cfal wants to merge 1 commit into
hyperium:masterfrom
cfal:fix/empty-data-frame-eof

Conversation

@cfal

@cfal cfal commented Oct 16, 2026

Copy link
Copy Markdown

fixes #372

fabriziosalmi added a commit to fabriziosalmi/zion that referenced this pull request Nov 25, 2026
…TP/3 length mismatches get 400 (#816)

* fix(security): an upstream request carries the Content-Length of the body zion sends, and an HTTP/2 or HTTP/3 request whose Content-Length does not hold is refused

zion forwarded the client's Content-Length lines verbatim, and hyper writes
a request's own lines while it frames the body by itself. Over HTTP/3, where
h3 checks neither the header nor the DATA, a WAF-route request that declared
content-length: 1000 and ended with no body went upstream with
content-length: 1000 and zero body bytes on a connection hyper returned to
its keep-alive pool; an upstream that answers before it reads the body read
the next request on that connection as the missing bytes (request smuggling
from zion into the upstream). Over HTTP/2, h2 checks only the first
content-length line, so a second line with another value went upstream as
written.

Three layers, each break-tested on its own:

- proxy: apply_forwarding_hygiene (every forwarded request: single and
  failover routes, the cache, the WebSocket handshake) drops the client's
  Content-Length lines and writes one from the body that goes: 0 for an
  empty body the client declared a length for, the exact length of a body
  of known size, the client's single valid value for a streamed body (hyper
  holds the body to it), none otherwise.
- accept: an HTTP/2 request whose content-length lines do not declare one
  valid length is refused with 400 (RFC 9113 8.1.1).
- quic: an HTTP/3 request with such lines is refused with 400, and the
  bridge holds the body to a valid declared length: a FIN short of it or
  DATA past it ends the body with an error (and STOP_SENDING
  H3_MESSAGE_ERROR past it), so the WAF, a buffer or the upstream request
  sees a broken body, never a whole one of another length (RFC 9114 4.1.2).

The length parser (http_util::content_length) reads lines and
comma-separated elements as hyper reads an HTTP/1.1 request.

Tests: the reproduction is kept in tests/h3_request_body.rs as
a_h3_body_shorter_than_its_content_length_is_refused_and_the_upstream_stays_in_sync,
with a_h3_body_longer_than_its_content_length_is_refused and
a_h3_request_whose_content_length_lines_disagree_is_refused;
tests/http_conformance.rs gains
http2_content_length_is_one_valid_length_and_is_reframed_from_the_body;
unit tests for the parser, the HTTP/2 check and the re-framing. Break
tests: all three layers off brings the reproduction's desync back
(DesyncSeen declared 1000, body_prefix "POST /waf/y HTTP/1.1"); the HTTP/3
check off fails the three HTTP/3 tests; the HTTP/2 check off gets 200 for
the 0-then-1000 case; the re-framing off fails its three unit tests and the
repeated-length conformance case.

Signed-off-by: Fabrizio Salmi <1569108+fabriziosalmi@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(security): an HTTP/3 body that runs past its Content-Length in a later DATA frame is refused, h2c on the plaintext listener is held to one length, and shadow mode forwards a flagged body whole

Review of the previous commit on this branch; every finding was checked
against the code and reproduced before the change (the HTTP/3 and h2c
ones on the real binary, the shadow-mode one in the real pipeline in a
unit test).

- quic, hold-back: the bridge refused only a chunk that took the body past
  its declared length. A chunk that brought it exactly to the length went
  on at once, hyper wrote the last declared byte and stopped reading the
  body, and DATA after it was never seen: on a streamed route the declared
  10 bytes went upstream as the whole body and the upstream's 200 came
  back. The chunk that completes the length is now held until the stream
  shows whether more follows. A declared 0 is a body hyper never reads at
  all, so the listener now reads such a body to its end before the request
  goes on (400 on DATA, 408 after the 30 s body-frame idle bound).
- quic, no stop_sending: h3-quinn unwraps the stream inside stop_sending,
  and a read in flight holds it, so DATA past the length on a stream the
  client kept open panicked (h3-quinn-0.0.10 lib.rs:394, seen in the
  daemon log); the release profile sets panic = "abort". The bridge now
  drops the receive half instead, and quinn's RecvStream Drop sends
  STOP_SENDING for the unread bytes.
- accept: the plaintext listener speaks h2c and its ACME fallback forwards
  without the HTTPS pipeline, and it never ran the HTTP/2 Content-Length
  check: lines 0 then 1000 over h2c were forwarded and answered 200. It
  runs it first now.
- waf_gate: in shadow mode a streaming early match forwarded only the
  frames read before the flagged one, and with the length now written from
  the body that goes, the upstream took the truncated body as complete. A
  pattern flag now forwards the read frames, the flagged one and the rest
  as it arrives, held to the profile's cap by the proxy (BodyCap, 413 past
  it); a size flag is answered 413 with nothing forwarded, as the buffered
  path answers it in shadow mode.
- docs: http_util::content_length now says it reads lines as hyper's
  HTTP/1.1 client does (content_length_parse_all), not the server, which
  refuses comma lists. http-conformance.md and the CHANGELOG say that h2
  resets the stream when the first line is not a number, name both
  listeners, and list only the cases the tests run.

Tests: h3_request_body gains
a_h3_body_that_does_not_end_at_its_content_length_is_refused_on_every_route
(shorter, longer in a later frame, 0 with DATA, on /open, /waf, /framed,
with whole-body controls) and
a_h3_body_past_its_length_on_an_open_stream_is_refused_and_zion_keeps_serving;
the disagreeing-lines test covers /framed. The HTTP/2 conformance case
covers h2c on the plaintext listener and a first line of five or "5, 5"
(stream reset, never forwarded). waf_gate has an end-to-end shadow test.

Break tests (fix reverted, test kept): no hold-back, /open/long-later is
taken upstream as a whole 10-byte body; no pre-read, /open/long-zero goes
upstream as an empty request; stop_sending back, /open/held answers 502 and
the daemon log shows the h3-quinn panic; no plaintext check, the h2c case
gets 200; forwarding only what was read, the upstream gets 65536 of 131102
bytes as a complete request; no BodyCap, a 1.5 MiB body passes a 1 MiB
profile with 200; no size 413, a request for the over-cap body reaches the
upstream.

Still open, measured with a throwaway test: h3 0.0.8 returns the end of the
body at a zero-length DATA frame, so DATA after one is not seen (10 declared
with DATA 10, 0, 990 gets 200 with a whole 10-byte body; no length with
DATA 500, 0, 500 gets 500 bytes as complete on /open and /waf).

Signed-off-by: Fabrizio Salmi <1569108+fabriziosalmi@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(http3): a zero-length DATA frame no longer ends a request body: the DATA after it reaches the upstream, or is refused past a Content-Length

h3 0.0.8, the newest release, answers recv_data with Ok(None) at a
zero-length DATA frame as it does at the end of the stream:
RequestStream::poll_recv_data reads one frame header, and
FrameStream::poll_data returns None while remaining_data is 0
(hyperium/h3#372, open; the fix in hyperium/h3#373 is not merged, and
main at 43745ad has the same code). RFC 9114 does not forbid an empty
DATA frame, and the bridge took h3's first None as the end of the body.

Measured on the real binary before the change, on /open, /waf and
/framed: no Content-Length with DATA 500, 0, 500 went upstream as a
complete 500-byte body; Content-Length 1000 with the same frames was
taken as short and never reached the upstream whole; Content-Length 10
with DATA 10, 0, 990 went upstream as a whole 10-byte body and got 200;
Content-Length 0 with DATA 0, 1000 went upstream on /open as an empty
request. Every reader of the body (the WAF, a buffer, the upstream
request) takes it from the one bridge task, so they all saw the same cut
bytes: a truncation, not a split between what the WAF checked and what
the upstream got.

The bridge, and the pre-read of a body declared 0, now read through
recv_body_data, which asks h3 again after None: at the end of the stream
h3 answers None again at once, after an empty frame it reads on to the
next one. The body ends at 16 answers of None in a row, so a run of up to
15 empty DATA frames is read through. Only recv_data is called:
recv_trailers after an empty frame reads the next DATA frame and closes
the connection with H3_FRAME_UNEXPECTED (how #373's own test fails on
main), and after a DATA frame cut short by FIN it would reach the assert
in h3's poll_next (hyperium/h3#386).

Still open, measured with a throwaway test on all three routes: a 16th
empty DATA frame in a row still ends the body (Content-Length 10 with
DATA 10, sixteen empty frames, 990 goes upstream as a whole 10-byte
body; no length with 500, sixteen empty frames, 500 as a whole 500-byte
one), and DATA after a trailer section, which h3 answers with the same
None, is read as more of the body (10 bytes, trailers, 10 more go
upstream as 20 bytes; with Content-Length 10 they are refused with 400).

Test: h3_request_body gains a_zero_length_h3_data_frame_does_not_end_the_body
(three routes, frames together and, for the short sequences, 300 ms
apart; whole bodies with empty frames inside, runs of 15, an empty frame
last, trailers; DATA past the length after an empty frame or a run of 15,
and DATA after an empty frame on a body declared 0, refused). Break test:
with the bound set to 1, /open/unframed-0 gets 200 and the upstream
takes 500 of the 1000 bytes as a complete body.

README and architecture stats: 1675 unit tests, 86 modules, ~87,700
lines.

Signed-off-by: Fabrizio Salmi <1569108+fabriziosalmi@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(waf): shadow mode under require_content_length holds a flagged body to the profile's max_body_mb, not 10 MiB

Review of the branch (three of three reviewers on each finding, each
checked against the code).

- body_gate: in WAF shadow mode on a streaming profile, a body flagged
  for a pattern goes on unread, with no known length, carrying the
  profile's max_body_mb as its BodyCap. On a route with
  require_content_length the body stage then read it whole within its
  10 MiB default for a route without a cap of its own, so a flagged body
  over 10 MiB that the profile allowed was refused with 413. read_whole
  now takes the BodyCap the WAF stage set when the route has no cap.
- docs: a mismatched HTTP/3 body on a streamed route answers 400 only
  when the upstream did not answer first; an upstream that answers
  before it reads the body has its own response reach the client.
  http-conformance.md and the CHANGELOG now say so.
- docs: DATA after a trailer section is an invalid frame sequence that
  RFC 9114 4.1 makes a connection error (H3_FRAME_UNEXPECTED), not
  "malformed", which 4.1.2 reserves for other cases.

Test: waf_gate::shadow_tests gains
a_flagged_body_in_shadow_mode_under_require_content_length_is_held_to_the_profile_cap
(12 MiB profile: a flagged 11 MiB body, with and without a declared
length, reaches the upstream whole with its length; 13 MiB gets 413 and
is never forwarded). Break test: without the BodyCap lookup,
/shadow/eleven-true gets 413 and the upstream gets nothing of the
11534366 bytes sent.

README and architecture stats: 1676 unit tests, 86 modules, ~87,800
lines.

Signed-off-by: Fabrizio Salmi <1569108+fabriziosalmi@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(conformance): a well-formed h2c POST to the :80 ACME fallback is redirected, not forwarded

After the merge of #818 the :80 fallback forwards only a bodyless GET or
HEAD, so this test's control (a valid h2c POST forwarded with one length)
no longer holds. It now asserts the POST is redirected and never reaches the
upstream; the malformed cases still get 400 and the TLS cases still check
the re-framing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Fabrizio Salmi <1569108+fabriziosalmi@users.noreply.github.com>

---------

Signed-off-by: Fabrizio Salmi <1569108+fabriziosalmi@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

zero-length DATA frames cause recv_data() to report premature EOF

1 participant