content-length is not policed when a stream ends with a trailers section
維護者通常 4 天內回覆
評估
- 難度
- 3/5
- 預估耗時
- 1-2 天
- 新手友好度
- 76/100
- Issue 類型
- 缺陷
- 描述清晰度
- 描述清楚
- 活躍度
- 活躍
- 技術堆疊
- python
- 領域
- networking
研究方向
Start with H2Stream.receive_headers and the existing insufficient-body cases in tests/test_invalid_content_lengths.py, then trace how receive_data calls _track_content_length. Add coverage for trailers ending the stream, including missing and conflicting trailer content-length values, while preserving validation of syntactically invalid trailer values. Done means the trailer cases raise InvalidBodyLengthError when the DATA length is wrong and do not replace the header expectation.
由索引模型根據 Issue 內容生成。
描述
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 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:
if expected is not None:
if expected < actual:
raise InvalidBodyLengthError(expected, actual)
if end_stream and expected != actual:
raise InvalidBodyLengthError(expected, actual)
Trailers never go through receive_data, so end_stream is never True here and the short-body check never runs. Two concrete consequences:
- Trailers without
content-length:_initialize_content_lengthsets_expected_content_lengthback toNone, soif expected is not None:skips validation altogether. - Trailers with
content-length: the trailers value replaces the header-section value, so a peer could declarecontent-length: 10in the headers andcontent-length: 3in the trailers and have a 13-byte body accepted.
The too-much-data direction still errors, because expected < actual trips while receiving DATA frames. Only the short-body direction is silently accepted.
RFC 9113 § 8.1.1
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.
Malformed requests or responses that are detected MUST be treated as a stream error of type PROTOCOL_ERROR.
The 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.
Steps to reproduce
Unit level, no network, in-memory bytes:
| scenario | result on 4.4.1 |
|---|---|
content-length: 3, DATA 10, END_STREAM on DATA |
InvalidBodyLengthError |
content-length: 15, DATA 13, END_STREAM on DATA |
InvalidBodyLengthError |
content-length: 15, DATA 13, trailers END_STREAM |
accepted |
content-length: 15, no DATA, trailers END_STREAM |
accepted |
content-length: 15, DATA 13, trailers content-length: 13 |
accepted |
Existing coverage only exercises the DATA-frame case: test_insufficient_data and test_insufficient_data_empty_frame in tests/test_invalid_content_lengths.py.
Environment
- h2 4.4.1,
masteratbc239af1d1b85bc70482804f30a0e0e587d90a08 - Python 3.14, macOS
Fix
In 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.
- 主要語言
- Python
- 星號
- 1k
- 分支
- 197
- PR 合併指標
- 30 天內沒有已合併 PR
環境準備
這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
python-hyper/h2 的其他 Issue
-
難度 4/5 3-5 天 新手友好度 45/100
python-hyper/h2#1330 ·
維護者通常 4 天內回覆
-
難度 5/5 一週以上 新手友好度 35/100
python-hyper/h2#1312 ·
維護者通常 4 天內回覆
-
難度 4/5 3-5 天 新手友好度 35/100
python-hyper/h2#1280 · 2 則留言 ·
維護者通常 4 天內回覆
-
難度 3/5 1-2 天 新手友好度 45/100
python-hyper/h2#1239 · 3 個 reaction ·
維護者通常 4 天內回覆
-
ProtocolError on receive_data after connection is closed可能已有人在做 @adarshx01 於 67 天前認領。 未關閉
難度 4/5 3-5 天 新手友好度 42/100
python-hyper/h2#1199 · 3 則留言 ·
維護者通常 4 天內回覆
相似的 Issue
-
push_gh-pages fails on master branch on forks可能已有人在做 關聯的 PR 仍在進行中或已合併。 未關閉good first issue infrastructure
難度 2/5 1-3 小時 新手友好度 76/100
kuznia-rdzeni/coreblocks#1135 ·
-
good first issue server
難度 2/5 1-3 小時 新手友好度 85/100
維護者通常 1 天內回覆
-
documentation priority: high
難度 2/5 1-3 小時 新手友好度 78/100
a-lex-s/mealie-telegram-bot#54 ·
維護者通常 1 天內回覆
-
type/test
難度 2/5 1-3 小時 新手友好度 76/100
SaltedTan/virgil-language-server#218 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 83/100
btclib-org/btclib-wallet#541 ·
維護者通常 1 天內回覆