Skip to content

Fix FormData.add_field() mutating _is_multipart on rejected fields - #14579

Open
xtianWang wants to merge 2 commits into
aio-libs:masterfrom
xtianWang:fix/formdata-rejected-field-encoding
Open

xtianWang wants to merge 2 commits into
aio-libs:masterfrom
xtianWang:fix/formdata-rejected-field-encoding

Conversation

@xtianWang

Copy link
Copy Markdown

What do these changes do?

Ensure \FormData.add_field()\ only sets \self._is_multipart = True\ after all argument validations succeed and the field is appended.

Previously, \self._is_multipart\ was mutated before validating
ame, \ ilename, and \content_type. If validation raised \TypeError\ or \ValueError\ and the caller caught the exception and reused the form, previously accepted fields were unexpectedly encoded as \multipart/form-data\ instead of \�pplication/x-www-form-urlencoded.

Are there changes in behavior for the user?

No breaking changes; rejected \�dd_field()\ calls no longer leave \FormData\ in a partially mutated multipart state.

Checklist

  • I think the code is well written
  • Unit tests for the changes exist (\pytest tests/test_formdata.py: 40 passed)

@xtianWang
xtianWang requested a review from asvetlov as a code owner December 3, 2026 11:22
@agentscanapp

agentscanapp Bot commented Dec 3, 2026

Copy link
Copy Markdown

Insufficient data

Not enough activity yet to make a reliable assessment.

View full analysis →

This is an automated analysis by AgentScan

@xtianWang
xtianWang requested a review from webknjaz as a code owner December 3, 2026 11:23
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided There is a change note present in this PR label Dec 3, 2026
@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
  • All header and type validations in add_field() (_safe_header on name, filename, and content_type, plus the filename and content_type isinstance checks) still run and run before any field is stored.
  • The delayed _is_multipart assignment sets the flag on exactly the same conditions as before (binary value, filename, or content_type), so successful calls encode identically to the old code.
  • _is_multipart only picks between the urlencoded and multipart encoders and gates no permission, tenant, or trust decision, so no security check moved or weakened.
  • The form builder takes application-supplied values, not network input, and the change adds no await points or state visible across requests.

@greptile-apps

greptile-apps Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5 Tier: apexRetriggerView in Greptile

[Low impact] This PR appears safe to merge.

Reviews (1) · Last reviewed commit: "Add changelog entry for PR #14579" · Reviewed by Greptile

@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 (b5acd43) to head (6e41af3).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #14579   +/-   ##
=======================================
  Coverage   99.13%   99.13%           
=======================================
  Files         135      135           
  Lines       54630    54634    +4     
  Branches     2808     2808           
=======================================
+ Hits        54158    54162    +4     
  Misses        357      357           
  Partials      115      115           
Flag Coverage Δ
Autobahn 21.53% <25.00%> (+<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.38% <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.02%) ⬇️
Py-3.14 98.46% <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.00% <100.00%> (-0.01%) ⬇️
VM-macos-latest 98.38% <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% <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 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

⚠️ 51 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 xtianWang:fix/formdata-rejected-field-encoding (6e41af3) with master (b5acd43)

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. ↩

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