Title: content-length is not policed when a stream ends with a trailers section · Issue #1328 · python-hyper/h2 · GitHub
Open Graph Title: content-length is not policed when a stream ends with a trailers section · Issue #1328 · python-hyper/h2
X Title: content-length is not policed when a stream ends with a trailers section · Issue #1328 · python-hyper/h2
Description: Summary A stream that ends with a trailers section never has its content-length policed, and a content-length in the trailers silently overrides the one declared in the header section. Description H2Stream.receive_headers is the handler ...
Open Graph Description: Summary A stream that ends with a trailers section never has its content-length policed, and a content-length in the trailers silently overrides the one declared in the header section. Description ...
X Description: Summary A stream that ends with a trailers section never has its content-length policed, and a content-length in the trailers silently overrides the one declared in the header section. Description ...
Opengraph URL: https://github.com/python-hyper/h2/issues/1328
X: @github
Domain: github.com
IPv4 address: 140.82.114.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"content-length is not policed when a stream ends with a trailers section","articleBody":"## Summary\n\nA stream that ends with a trailers section never has its `content-length` policed, and a `content-length` in the trailers silently overrides the one declared in the header section.\n\n## Description\n\n`H2Stream.receive_headers` is the handler for every received HEADERS block — initial headers, informational responses and trailers. It called `self._initialize_content_length(headers)` unconditionally. But `_track_content_length`, which is where the actual comparison happens, was only ever called from `receive_data`:\n\n```python\nif expected is not None:\n if expected \u003c actual:\n raise InvalidBodyLengthError(expected, actual)\n\n if end_stream and expected != actual:\n raise InvalidBodyLengthError(expected, actual)\n```\n\nTrailers never go through `receive_data`, so `end_stream` is never `True` here and the short-body check never runs. Two concrete consequences:\n\n- **Trailers without `content-length`**: `_initialize_content_length` sets `_expected_content_length` back to `None`, so `if expected is not None:` skips validation altogether.\n- **Trailers with `content-length`**: the trailers value replaces the header-section value, so a peer could declare `content-length: 10` in the headers and `content-length: 3` in the trailers and have a 13-byte body accepted.\n\nThe too-much-data direction still errors, because `expected \u003c actual` trips while receiving DATA frames. Only the short-body direction is silently accepted.\n\n## RFC 9113 § 8.1.1\n\n> A request or response is also malformed if the value of a content-length header field does not equal the sum of the DATA frame payload lengths that form the content, unless the message is defined as having no content. For example, 204 or 304 responses contain no content, as does the response to a HEAD request.\n>\n> Malformed requests or responses that are detected MUST be treated as a stream error of type PROTOCOL_ERROR.\n\nThe listed exemptions are 204, 304 and HEAD. A trailers section is not one of them. This is the same check the project adopted in #123 (\"Police content lengths when provided\"), proposed in #114.\n\n## Steps to reproduce\n\nUnit level, no network, in-memory bytes:\n\n| scenario | result on 4.4.1 |\n|---|---|\n| `content-length: 3`, DATA 10, END_STREAM on DATA | `InvalidBodyLengthError` |\n| `content-length: 15`, DATA 13, END_STREAM on DATA | `InvalidBodyLengthError` |\n| **`content-length: 15`, DATA 13, trailers END_STREAM** | **accepted** |\n| **`content-length: 15`, no DATA, trailers END_STREAM** | **accepted** |\n| **`content-length: 15`, DATA 13, trailers `content-length: 13`** | **accepted** |\n\nExisting coverage only exercises the DATA-frame case: `test_insufficient_data` and `test_insufficient_data_empty_frame` in `tests/test_invalid_content_lengths.py`.\n\n## Environment\n\n- h2 4.4.1, `master` at `bc239af1d1b85bc70482804f30a0e0e587d90a08`\n- Python 3.14, macOS\n\n## Fix\n\nIn `receive_headers`, when the block is trailers: run the same `content-length` parse so a syntactically invalid value there is still a `ProtocolError`, then restore the previous expectation, and finally validate the body where the stream actually ends.\n","author":{"url":"https://github.com/feiiiiii5","@type":"Person","name":"feiiiiii5"},"datePublished":"2026-09-25T22:01:18Z","url":"https://github.com/python-hyper/h2/issues/1328"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:2352c639-c84e-eb8a-cfff-6f26f13d1da1 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9316:27372C:D66C6B:12EA58D:6B06EF8E |
| html-safe-nonce | 419daec3b7d5dd756cfdfb03b12ff2ed7ab6ad078ce938e5f5f10e11b0ea472b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MzE2OjI3MzcyQzpENjZDNkI6MTJFQTU4RDo2QjA2RUY4RSIsInZpc2l0b3JfaWQiOiI3Mjc1NDA0MTQyODM0MDIwMjM4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 9e8c241bf24ccb95e74dd84fb20dd4138ea33acc78f3943757fc61a7227a90ef |
| hovercard-subject-tag | issue:5589032820 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python-hyper/h2/1328/issue_layout |
| twitter:image | https://opengraph.githubassets.com/6c6928caf4d7f84894710b90c16014b34cc4de56ebf8b2c5318b073bacedd653/python-hyper/h2/issues/1328 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/6c6928caf4d7f84894710b90c16014b34cc4de56ebf8b2c5318b073bacedd653/python-hyper/h2/issues/1328 |
| og:image:alt | Summary A stream that ends with a trailers section never has its content-length policed, and a content-length in the trailers silently overrides the one declared in the header section. Description ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | feiiiiii5 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 351ea5ca38a5758919d44ee52c70fe9003c0c2dfa20f04effd7fac0c5330c16b |
| turbo-cache-control | no-preview |
| go-import | github.com/python-hyper/h2 git https://github.com/python-hyper/h2.git |
| octolytics-dimension-user_id | 13035907 |
| octolytics-dimension-user_login | python-hyper |
| octolytics-dimension-repository_id | 37996233 |
| octolytics-dimension-repository_nwo | python-hyper/h2 |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 37996233 |
| octolytics-dimension-repository_network_root_nwo | python-hyper/h2 |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 891bde3f438884e9c6c7ccd826f33805910a978b |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width