Skip to content

[PR #12119/0e2d3ec4 backport][3.14] Fix server hang on chunked transfer encoding size mismatch#12122

Merged
Dreamsorcerer merged 1 commit into3.14from
patchback/backports/3.14/0e2d3ec48a950a2aacaf3f5d0d1f1597a1d5385b/pr-12119
Feb 22, 2026
Merged

[PR #12119/0e2d3ec4 backport][3.14] Fix server hang on chunked transfer encoding size mismatch#12122
Dreamsorcerer merged 1 commit into3.14from
patchback/backports/3.14/0e2d3ec48a950a2aacaf3f5d0d1f1597a1d5385b/pr-12119

Conversation

@patchback
Copy link
Copy Markdown
Contributor

@patchback patchback bot commented Feb 22, 2026

This is a backport of PR #12119 as merged into master (0e2d3ec).

Summary

Fixes #10596.

When chunked transfer encoding chunk-size does not match the actual data length, the server hangs indefinitely instead of rejecting the request. Per RFC 9112, chunk-data must be exactly chunk-size octets followed by CRLF.

Root cause: In PARSE_CHUNKED_CHUNK_EOF state (after consuming chunk-size bytes), any data that doesn't start with \r\n is stored in _chunk_tail and the parser returns False — waiting forever for more data. When the chunk-size is wrong (e.g., declared 4 but sent 5 bytes), the byte after the consumed data is not \r and will never become \r\n.

Fix: Before falling through to the wait-for-more-data path, check whether the available data can't possibly be the start of the expected CRLF separator. If we have enough bytes to determine the separator is wrong, raise TransferEncodingError immediately. The legitimate partial-separator case (received \r but not yet \n) is preserved.

Changes:

  • aiohttp/http_parser.py: Add elif branch in PARSE_CHUNKED_CHUNK_EOF that raises TransferEncodingError when data doesn't match CRLF prefix
  • tests/test_http_parser.py: Two regression tests — data too long (5 bytes for chunk-size 4) and data too short (5 bytes for chunk-size 6)
  • CHANGES/10596.bugfix.rst: Changelog entry

Test plan

  • Two new tests pass: test_parse_chunked_payload_size_data_mismatch and test_parse_chunked_payload_size_data_mismatch_too_short
  • All 14 existing chunked payload tests pass (including split-end tests that verify partial CRLF handling)
  • Full test_http_parser.py suite: 299 passed, 4 pre-existing failures (missing C extension/brotli)

@codecov
Copy link
Copy Markdown

codecov bot commented Feb 22, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.25%. Comparing base (6bebdca) to head (41aec21).
⚠️ Report is 1 commits behind head on 3.14.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##             3.14   #12122   +/-   ##
=======================================
  Coverage   98.24%   98.25%           
=======================================
  Files         129      129           
  Lines       45430    45446   +16     
  Branches     2454     2455    +1     
=======================================
+ Hits        44633    44652   +19     
+ Misses        615      612    -3     
  Partials      182      182           
Flag Coverage Δ
CI-GHA 98.12% <100.00%> (+<0.01%) ⬆️
OS-Linux 97.88% <100.00%> (+<0.01%) ⬆️
OS-Windows 95.60% <100.00%> (+<0.01%) ⬆️
OS-macOS 97.15% <100.00%> (+<0.01%) ⬆️
Py-3.10.11 96.68% <100.00%> (+<0.01%) ⬆️
Py-3.10.19 97.17% <100.00%> (+<0.01%) ⬆️
Py-3.11.14 97.40% <100.00%> (+<0.01%) ⬆️
Py-3.11.9 96.92% <100.00%> (+<0.01%) ⬆️
Py-3.12.10 97.01% <100.00%> (+<0.01%) ⬆️
Py-3.12.12 97.49% <100.00%> (+<0.01%) ⬆️
Py-3.13.11 97.49% <100.00%> (+<0.01%) ⬆️
Py-3.13.12 96.99% <100.00%> (+<0.01%) ⬆️
Py-3.14.2 97.44% <100.00%> (+<0.01%) ⬆️
Py-3.14.3 96.94% <100.00%> (+<0.01%) ⬆️
Py-3.14.3t 96.78% <100.00%> (+<0.01%) ⬆️
Py-pypy3.11.13-7.3.20 96.92% <100.00%> (-0.01%) ⬇️
VM-macos 97.15% <100.00%> (+<0.01%) ⬆️
VM-ubuntu 97.88% <100.00%> (+<0.01%) ⬆️
VM-windows 95.60% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Feb 22, 2026

Merging this PR will not alter performance

✅ 59 untouched benchmarks


Comparing patchback/backports/3.14/0e2d3ec48a950a2aacaf3f5d0d1f1597a1d5385b/pr-12119 (41aec21) with 3.14 (6bebdca)

Open in CodSpeed

@Dreamsorcerer Dreamsorcerer merged commit 4bcb942 into 3.14 Feb 22, 2026
38 checks passed
@Dreamsorcerer Dreamsorcerer deleted the patchback/backports/3.14/0e2d3ec48a950a2aacaf3f5d0d1f1597a1d5385b/pr-12119 branch February 22, 2026 15:06
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.

2 participants