Skip to content

Add max_nesting_depth to MultipartReader - #14585

Open
Franco944796 wants to merge 1 commit into
aio-libs:masterfrom
Franco944796:multipart-max-nesting-depth
Open

Franco944796 wants to merge 1 commit into
aio-libs:masterfrom
Franco944796:multipart-max-nesting-depth

Conversation

@Franco944796

Copy link
Copy Markdown

What do these changes do?

MultipartReader builds a new reader for every nested multipart/* part, with no limit on how deep that goes, and release() recurses once per level. A ~70 KB body nested 1000 levels deep makes release() raise RecursionError. This is threat 4.3 in THREAT_MODEL.md, which recommends a max_nesting_depth parameter.

This adds max_nesting_depth (default 10). Each nested reader gets one level less, and a part nested deeper than the limit raises BadHttpMessage before another reader is built. That is the same exception the per-part max_headers limit already raises.

Are there changes in behavior for the user?

A body nested more than 10 levels deep now raises BadHttpMessage from next() / release() instead of being parsed (or failing with RecursionError once it is deep enough). Code that needs deeper nesting can pass a larger max_nesting_depth. Request.post() is unchanged: it already rejects nested multipart parts.

Is it a substantial burden for the maintainers to support this?

No. It is one keyword argument, passed to nested readers the same way as max_headers, including through multipart_reader_cls.

Related issue number

No issue; THREAT_MODEL.md threat 4.3. The PR updates its mitigation row and the recap of §5.4.

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • If you provide code modifications, please add yourself to CONTRIBUTORS.txt
  • Add a new news fragment into the CHANGES/ folder
Test run (pure-Python mode, AIOHTTP_NO_EXTENSIONS=1)
# master, with only the new regression test applied
$ pytest tests/test_multipart.py -k test_release_deeply_nested_body
E       RecursionError: maximum recursion depth exceeded
aiohttp/http_parser.py:301: RecursionError
1 failed, 182 deselected in 0.49s

# this branch
$ pytest tests/test_multipart.py -k "nesting or nested"
4 passed, 179 deselected in 0.13s
$ pytest tests/test_multipart.py tests/test_web_request.py tests/test_web_functional.py
506 passed, 9 skipped in 4.23s

Every step verified

Drafted with Claude Opus 5.5; reviewed by @Franco944796.

Each nested multipart/* part builds another MultipartReader and release() recursed once per level with no cap, so a ~70 KB body nested 1000 levels deep raised RecursionError. Nested readers now get one level less and a part nested deeper than max_nesting_depth (default 10) raises BadHttpMessage before another reader is built (THREAT_MODEL.md threat 4.3).
@Franco944796
Franco944796 force-pushed the multipart-max-nesting-depth branch from 54b73aa to 69f3ef2 Compare December 3, 2026 19:43
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided There is a change note present in this PR label Dec 3, 2026
@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.13%. Comparing base (abbb10c) to head (69f3ef2).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #14585   +/-   ##
=======================================
  Coverage   99.13%   99.13%           
=======================================
  Files         135      135           
  Lines       54630    54673   +43     
  Branches     2808     2811    +3     
=======================================
+ Hits        54158    54201   +43     
  Misses        357      357           
  Partials      115      115           
Flag Coverage Δ
Autobahn 21.53% <13.95%> (-0.01%) ⬇️
CI-GHA 99.06% <100.00%> (+<0.01%) ⬆️
OS-Linux 98.75% <100.00%> (+<0.01%) ⬆️
OS-Windows 97.52% <100.00%> (+<0.01%) ⬆️
OS-macOS 98.37% <100.00%> (-0.01%) ⬇️
Py-3.11 98.31% <100.00%> (-0.01%) ⬇️
Py-3.12 98.40% <100.00%> (+<0.01%) ⬆️
Py-3.13 98.39% <100.00%> (-0.01%) ⬇️
Py-3.14 98.45% <100.00%> (+<0.01%) ⬆️
Py-3.15 98.45% <100.00%> (+<0.01%) ⬆️
Py-3.15t 98.30% <100.00%> (+<0.01%) ⬆️
Py-pypy-3.12 97.01% <100.00%> (+0.02%) ⬆️
VM-macos-latest 98.37% <100.00%> (-0.01%) ⬇️
VM-ubuntu-latest 98.75% <100.00%> (+<0.01%) ⬆️
VM-windows-latest 97.52% <100.00%> (+<0.01%) ⬆️
cython-coverage 84.03% <33.33%> (-0.01%) ⬇️

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

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

@codspeed

codspeed Bot commented Dec 3, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 48 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 102 untouched benchmarks
⏩ 83 skipped benchmarks1


Comparing Franco944796:multipart-max-nesting-depth (69f3ef2) with master (abbb10c)

Open in CodSpeed

Footnotes

  1. 83 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@Franco944796
Franco944796 marked this pull request as ready for review December 3, 2026 20:16
@greptile-apps

greptile-apps Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

security Security Review

This PR appears safe to merge from a security standpoint.

What was checked
  • The new depth limit is checked before any nested reader is built, and both nested-reader paths (type(self) and multipart_reader_cls) pass one level less, so the limit cannot be skipped on either branch.
  • The check fails closed: max_nesting_depth of 0 or negative rejects nested multipart instead of allowing it.
  • Both untrusted entry points get the safe default of 10: Request.multipart() on the server and MultipartReader.from_response() on the client, so neither a remote client posting a body nor a malicious server sending a response can drive release() into deep recursion anymore.
  • Request.post() still rejects nested multipart parts with ValueError, so the form-parsing path is unchanged and protected.
  • The unbounded part count per nesting level (threat 4.2) is pre-existing and documented in THREAT_MODEL.md; this PR does not cause or worsen it.

@greptile-apps

greptile-apps Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5 Tier: apexRetriggerView in Greptile

[Medium impact] The PR appears safe to merge.

Reviews (1) · Last reviewed commit: "Add max_nesting_depth to MultipartReader" · Reviewed by Greptile

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided There is a change note present in this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant