Enforce FormParser limits in parser callbacks#3331
Merged
Conversation
The limit checks ran while draining buffered messages, after `QuerystringParser.write()` had already tokenized the whole chunk. For a body delivered as a single chunk (e.g. via the test client or after `request.body()` cached it), every field was buffered before the limit was reached. Move the count and size checks into the callbacks so parsing stops as soon as a limit is crossed.
Contributor
|
Docs preview: https://e683706e-starlette.marcelotryle.workers.dev |
Kludex
added a commit
that referenced
this pull request
Jun 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #3329. The
max_fieldsandmax_part_sizechecks ran while draining buffered messages, afterQuerystringParser.write()had already tokenized the whole chunk. When aapplication/x-www-form-urlencodedbody arrives as a single chunk (e.g. via the test client, or afterrequest.body()has cached it), the parser tokenizes and buffers every field before the drain loop reaches the limit check.This moves the count and size checks into the
on_field_*callbacks, which run duringwrite(), so parsing stops as soon as a limit is crossed regardless of how the body is chunked. Behaviour and error messages are unchanged; only the point at which parsing aborts moves earlier.AI Disclaimer
This PR was developed with the assistance of either Claude or Codex. I've reviewed and verified the changes.