Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

content-length is not policed when a stream ends with a trailers section

Đang mở
#1,328 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 4 ngày

@feiiiiii5 đang làm issue này rồi.

Từ ngày 25/9/2026.

  • #1329 của @feiiiiii5 — đang mở

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
76/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
networking

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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_length sets _expected_content_length back to None, so if expected is not None: skips validation altogether.
  • 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.

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, master at bc239af1d1b85bc70482804f30a0e0e587d90a08
  • 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.

Ngôn ngữ chính
Python
Star
1k
Fork
197
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của python-hyper/h2

Tất cả issue của python-hyper/h2

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.