Repository navigation
fix: stop reading oversized Events request bodies, and document the limits - #480
Merged
Merged
Conversation
…imits Citry's Events endpoints already capped a request body at 1 MiB and an envelope at 16 calls, but the ASGI adapter read the whole body into memory before the check, the WSGI adapter read whatever Content-Length claimed, and the Django adapter read request.body whole. Each now reads at most one byte past the route's limit (under ASGI, at most one message past it), also for a chunked body without Content-Length, through a new optional URLRoute.max_body_bytes callable. On Django, an upload that middleware parsed first is checked against its declared size, and the request stays readable afterwards. The undocumented _max_envelope_bytes setting becomes the documented _max_body_bytes, and a new _max_form_fields (default 1,000) limits the fields of a urlencoded form post. Both are engine-wide, reject values that are not positive ints at construction, and answer payload_too_large. The i18n messages route gets its body cap the same way. Fixes #404. Co-Authored-By: Claude Opus 5.5 <[email protected]>
JuroOravec
force-pushed
the
fix/events-body-limits
branch
from
October 28, 2026 15:04
02573a4 to
0727ccd
Compare
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.
What this fixes
Citry's Events endpoints already capped a request at 1 MiB and 16 calls, but the cap was checked only after the adapter had read the body. This PR makes the adapters stop reading, documents the limits as settings, and adds a form-field limit. Issue #404's body has the corrected scope.
ASGI (
contrib/asgi.py, also used by FastAPI and Starlettemount()) stops receiving once the body reaches the limit plus one byte, so it holds at most one message past the limit.WSGI (
contrib/wsgi.py, also used by Flaskmount()) reads at most the limit plus one byte whateverContent-Lengthsays. A body withoutContent-Lengthis read only when the server setswsgi.input_terminated.Django (
contrib/django.py) reads from the stream instead ofrequest.body, and leaves the request asrequest.bodywould (_bodyand_stream), so guards, handlers and codecs can still readrequest.native.body,POSTandFILES. When middleware such asCsrfViewMiddlewarehas already parsed an upload, Citry checks its declared size. On Django these limits now govern event requests in place ofDATA_UPLOAD_MAX_MEMORY_SIZE.Settings, both engine-wide in
extensions_defaults["events"]:_max_body_bytes, default 1 MiB. This is the undocumented_max_envelope_bytes, renamed with no alias (pre-1.0);Citry(...)now rejects the old name._max_form_fields, default 1,000 (Django's default), counted the wayparse_qscounts fields, before parsing.A value that is not a positive int raises
ValueErrorat construction, and declaring either on a component'sEventsclass fails at class definition.Over either limit:
payload_too_large, as before. The batch route answers HTTP 200 with the 413 error in the result envelope (the protocol's shape); the per-event and view-event routes answer 413.How routes tell the adapters
URLRoutegains an optionalmax_body_bytes, a function returning the limit, called per request. The three Events dispatch routes and the i18n messages route (which had the same read-then-check pattern) set it. A non-callable value raisesTypeErrorwhen the route is built; a bad return value fails the request. Routes without it read the whole body, as before.RouteRequestis unchanged: the handler sees at most the limit plus one byte and must reject a longer body.Tests
tests/test_events_body_limits.pycounts the bytes each adapter pulls from the client:Content-Length; the cap accepted and cap + 1 rejected; the reader returns exactly the limit, stops when it is reached exactly, and cuts a message that crosses itContent-Lengthon each route reads exactly cap + 1; a chunked body withoutContent-Lengthreads exactly cap + 1; the cap boundary; a smallerContent-Lengthis respectedrequest.bodyandrequest.read()still return the bytes; a body middleware read first is taken from memory; an upload middleware parsed first is still rejected by its declared sizeAlso: every dispatch route and the i18n route carry the cap; the form field limit at, over, and at the default; bad values for both settings; a component declaring the form limit;
URLRouterejecting a non-callable and failing on bad return values. The existing config-name and cap tests were updated to the new names.Checks: the citry suite (9,667), the Events and i18n browser tests, ruff, mypy (also
--platform linux),scripts/validate.py, and the protocol checks pass. An independent reviewer ran two rounds with a mutation harness and probes for each adapter, and a separate agent did a first-time-reader pass on the docs.Docs
security.md: a new "Limit request sizes" section with both settings, the 16-call limit, the Django behaviour, setups that read the body first, and uploads.advanced/extensions.md:URLRoute(max_body_bytes=...)with an example and the handler's duty.packages/protocol/events/v1/spec.md) and design docs (events.md,events_plan.md): the limits and thepayload_too_largerow.CHANGELOG.md: a Fixed entry (with the rename and the Django change) and an Added entry forURLRoute(max_body_bytes=...).docs/design/issue_207_proposals.mdkeep the old name.Not done, by choice: GET query strings are not field-counted (the server's URL limit bounds them), and a WSGI route without a limit still reads no chunked body, as before.
Fixes #404.
🤖 Generated with Claude Code