Skip to content

return 415 for undecodable multipart field charset in post - #13462

Draft
dxbjavid wants to merge 3 commits into
aio-libs:masterfrom
dxbjavid:multipart-charset-415
Draft

return 415 for undecodable multipart field charset in post#13462
dxbjavid wants to merge 3 commits into
aio-libs:masterfrom
dxbjavid:multipart-charset-415

Conversation

@dxbjavid

Copy link
Copy Markdown
Contributor

What do these changes do?

BaseRequest.post() decodes a plain multipart/form-data text field with value.decode(charset), where charset comes from that part's own Content-Type header and is fully attacker controlled. An unknown codec name (charset=not-a-real-codec) raises LookupError and undecodable bytes raise UnicodeDecodeError, and neither is caught, so a malformed part turns into an uncaught 500. The sibling urlencoded branch a few lines below, and BaseRequest.text(), already wrap the same decode in except (LookupError, UnicodeDecodeError) and raise HTTPUnsupportedMediaType. This brings the multipart branch in line with those two, so a bad part charset now yields a 415 like the other body-parsing paths rather than a server error.

Are there changes in behavior for the user?

A multipart field whose charset is unknown or whose bytes do not decode now surfaces as HTTPUnsupportedMediaType (415) instead of an unhandled exception. Valid fields are unaffected.

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

No. It is a two-line guard that mirrors existing code in the same function.

Related issue number

N/A

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt
  • Add a new news fragment into the CHANGES/ folder

@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided There is a change note present in this PR label Aug 17, 2026
@codecov

codecov Bot commented Aug 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.02%. Comparing base (7dc43ae) to head (c615ad2).
⚠️ Report is 45 commits behind head on master.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #13462      +/-   ##
==========================================
+ Coverage   98.42%   99.02%   +0.60%     
==========================================
  Files         133      135       +2     
  Lines       49747    50469     +722     
  Branches     2628     2647      +19     
==========================================
+ Hits        48961    49976    +1015     
+ Misses        662      370     -292     
+ Partials      124      123       -1     
Flag Coverage Δ
Autobahn 22.03% <15.38%> (+0.07%) ⬆️
CI-GHA 98.91% <100.00%> (+0.59%) ⬆️
OS-Linux 98.69% <100.00%> (+0.59%) ⬆️
OS-Windows 97.09% <100.00%> (+0.63%) ⬆️
OS-macOS 97.97% <100.00%> (+0.62%) ⬆️
Py-3.10 98.11% <100.00%> (+0.57%) ⬆️
Py-3.11 98.35% <100.00%> (+0.55%) ⬆️
Py-3.12 98.43% <100.00%> (+0.54%) ⬆️
Py-3.13 98.42% <100.00%> (+0.55%) ⬆️
Py-3.14 98.45% <100.00%> (+0.56%) ⬆️
Py-3.14t 97.62% <100.00%> (+0.63%) ⬆️
Py-pypy-3.11 97.40% <100.00%> (+0.55%) ⬆️
VM-macos 97.97% <100.00%> (+0.62%) ⬆️
VM-ubuntu 98.69% <100.00%> (+0.59%) ⬆️
VM-windows 97.09% <100.00%> (+0.63%) ⬆️
cython-coverage 83.08% <100.00%> (+0.89%) ⬆️

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-hq

codspeed-hq Bot commented Aug 17, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 16.21%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 1 improved benchmark
✅ 93 untouched benchmarks
⏩ 83 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
test_list_element_pattern_redos_payload[whitespace_run_after_content] 32.8 ms 28.3 ms +16.21%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing dxbjavid:multipart-charset-415 (c615ad2) with master (4602990)2

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.

  2. No successful run was found on master (eb38b3c) during the generation of this report, so 4602990 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

Comment thread tests/test_web_request.py Outdated
assert dict(result) == {"a": "b", "c": "d"}


async def test_multipart_formdata_field_unknown_charset(protocol: BaseProtocol) -> None:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be parametrized. There's only 2 words different between the tests.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, merged them into one parametrized test covering the unknown-codec and undecodable-bytes cases.

Signed-off-by: Javid Khan <dxbjavid@gmail.com>
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.

2 participants