Title: A 304 with content-length is accepted or rejected by framing alone · Issue #1330 · python-hyper/h2 · GitHub
Open Graph Title: A 304 with content-length is accepted or rejected by framing alone · Issue #1330 · python-hyper/h2
X Title: A 304 with content-length is accepted or rejected by framing alone · Issue #1330 · python-hyper/h2
Description: While working on #1328 I found two more holes in the same function, and they turn out to be two halves of one problem. I have not touched anything — the measurements are in a standalone probe, and I would rather agree on the direction fi...
Open Graph Description: While working on #1328 I found two more holes in the same function, and they turn out to be two halves of one problem. I have not touched anything — the measurements are in a standalone probe, and ...
X Description: While working on #1328 I found two more holes in the same function, and they turn out to be two halves of one problem. I have not touched anything — the measurements are in a standalone probe, and ...
Opengraph URL: https://github.com/python-hyper/h2/issues/1330
X: @github
Domain: github.com
IPv4 address: 140.82.112.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"A 304 with content-length is accepted or rejected by framing alone","articleBody":"While working on #1328 I found two more holes in the same function, and they turn out to be two halves of one problem. I have not touched anything — the measurements are in a standalone probe, and I would rather agree on the direction first.\n\n## A 304 or 204 carrying `content-length` is accepted or rejected depending on framing\n\n`_initialize_content_length` (`src/h2/stream.py:1369`) implements exactly one exemption, `self.request_method == b\"HEAD\"` (`:1375`). RFC 9113 § 8.1.1 names three messages that are \"defined as having no content\": 204, 304, and the response to HEAD, and it says a message defined to have no content \"MAY have a non-zero content-length header field\". RFC 9110 § 8.6 repeats it for 304 — that is how a client learns the size of the cached entity. `H2Stream` never stores `:status`, so the other two cannot be written as the code stands. Observable today, on `master` and on #1329 alike:\n\n```\n304 + content-length: 1234 + empty DATA with END_STREAM -> InvalidBodyLengthError: Expected 1234 bytes, received 0\n304 + content-length: 1234 + END_STREAM on the HEADERS -> accepted ['ResponseReceived', 'StreamEnded']\n204 + content-length: 1234 + empty DATA with END_STREAM -> InvalidBodyLengthError: Expected 1234 bytes, received 0\n```\n\nOne message, two verdicts, decided purely by which frame carried END_STREAM. That is an enforcement that rejects something the RFC permits, and it is reachable from a plain `H2Connection.receive_data` on a legitimate response.\n\n## The same hole #1328 fixed, in the other branch of the same `if`\n\n`receive_headers` (`:1113`) — the `else` branch, i.e. a HEADERS block that is not trailers, calls `_initialize_content_length(headers)` and nothing else. So a bodyless message that ends on the HEADERS is never policed at all:\n\n```\nPOST content-length: 15 + END_STREAM on the HEADERS, no DATA -> accepted ['RequestReceived', 'StreamEnded']\n200 content-length: 1234 + END_STREAM on the HEADERS -> accepted ['ResponseReceived', 'StreamEnded']\n```\n\n`content-length: 15` with nothing sent is the same lie #1328 is about, and `_actual_content_length` stays 0.\n\nThe obvious one-line fix is not safe on its own. Adding `if end_stream: self._track_content_length(0, end_stream=True)` to that branch, measured:\n\n```\n before after that line\nPOST content-length: 15, no body accepted InvalidBodyLengthError(15, 0) \u003c- intended\n304 content-length: 1234, no body accepted InvalidBodyLengthError(1234, 0) \u003c- new breakage\n204 content-length: 1234, no body accepted InvalidBodyLengthError(1234, 0) \u003c- new breakage\nPOST content-length: 0, no body accepted accepted \u003c- control\nGET no content-length, no body accepted accepted \u003c- control\n```\n\nSo the 204/304 exemption has to land first, or with it. That is why I am asking rather than sending a patch: the exemption is a design call. Setting `self._expected_content_length = 0` for 204/304 mirrors the HEAD precedent and additionally makes DATA after a 204 an error, which RFC 9110 § 6.3 implies; setting it to `None` only skips the comparison. I lean to `0`, but that is your call, and `H2Stream` would need to start recording `:status`.\n\n\u003cdetails>\u003csummary>Two smaller ones in the same file, and what I am not reporting\u003c/summary>\n\n## Two smaller ones in the same file, both one-line\n\n- `send_headers` (`:909`) assigns `self.request_method = extract_method_header(bytes_headers)` on *every* header block, including a trailers block, which has no `:method` — so outbound trailers overwrite a remembered `b\"HEAD\"` with `None`. `H2Connection.send_headers(1, HEAD)` then a trailers block, then a `200` + `content-length: 1234` + empty DATA + END_STREAM, raises where it should not. Guarding on `method is not None` fixes it.\n- `remotely_pushed` (`:1052`) records `self._authority` but never the method, so a pushed stream never gets the HEAD exemption at all. A `PUSH_PROMISE` with `:method: HEAD` followed by `200` + `content-length: 1234` + empty DATA + END_STREAM raises; the identical push with `:method: GET` raises the same way, which is the tell that the method is simply not consulted.\n\n## Not reported\n\n- **RST_STREAM or a local reset mid-body** runs no length check, and should not: the message is abandoned and the stream is closed, so nothing later can contradict the sender. Measured, no error, and I read that as specified rather than a bug.\n- **A `content-length` in *outbound* trailers** is a real asymmetry — h2 will send a trailers block carrying `content-length: 0`, and feeding those exact bytes back into an h2 client raises `ProtocolError: Received content-length header in trailer`. But fixing it means changing the trailer field in four existing tests in `tests/test_basic_logic.py`, so I would file that separately rather than widen this.\n- **A 1xx carrying `content-length`** is accepted and then overwritten. Inconsistent with the new trailers rule, but the RFC puts that obligation on the sender and no wrong byte is ever compared, so I do not think it is worth an interop risk.\n\n\u003c/details>\n\n## Verification\n\n`uv run --frozen pytest tests/ -q` is 1670 passed at 100.00% coverage (466 statements, 98 branches in `stream.py`, 0 missed), on `d2b43d0`. Every scenario above is a runtime measurement against the real `H2Connection`, not a reading. I have the probe as a standalone script and the 17 assertions as a pytest file; say the word and I will open a PR with the exemption and the HEADERS-frame check together, at 100% coverage.\n","author":{"url":"https://github.com/feiiiiii5","@type":"Person","name":"feiiiiii5"},"datePublished":"2026-09-27T10:16:22Z","url":"https://github.com/python-hyper/h2/issues/1330"}
| 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:7d18e459-724e-9415-36a6-05a285816ff7 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A4E2:3AC956:361C605:487D9F5:6B06EF96 |
| html-safe-nonce | 785693ec8728d60613ded7049cb9a70833ed4bdd82a812f3af1ff3fd1579d8a8 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNEUyOjNBQzk1NjozNjFDNjA1OjQ4N0Q5RjU6NkIwNkVGOTYiLCJ2aXNpdG9yX2lkIjoiMjI4NzA5NDUwODA5OTY2MTcxOCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | ac35dc08c465ac1df0f7389737df13c1ca2d29a3c61367b23172ea9006fb347f |
| hovercard-subject-tag | issue:5601832896 |
| 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/1330/issue_layout |
| twitter:image | https://opengraph.githubassets.com/513daa60069ee3af383adcd98f4d0f926a852551e80a5e2bf8c683b8ffc54d98/python-hyper/h2/issues/1330 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/513daa60069ee3af383adcd98f4d0f926a852551e80a5e2bf8c683b8ffc54d98/python-hyper/h2/issues/1330 |
| og:image:alt | While working on #1328 I found two more holes in the same function, and they turn out to be two halves of one problem. I have not touched anything — the measurements are in a standalone probe, and ... |
| 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