Skip to content

fix: stop reading oversized Events request bodies, and document the limits - #480

Merged
JuroOravec merged 2 commits into
mainfrom
fix/events-body-limits
Oct 28, 2026
Merged

JuroOravec merged 2 commits into
mainfrom
fix/events-body-limits

Conversation

@JuroOravec

Copy link
Copy Markdown
Member

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.

  1. ASGI (contrib/asgi.py, also used by FastAPI and Starlette mount()) stops receiving once the body reaches the limit plus one byte, so it holds at most one message past the limit.

  2. WSGI (contrib/wsgi.py, also used by Flask mount()) reads at most the limit plus one byte whatever Content-Length says. A body without Content-Length is read only when the server sets wsgi.input_terminated.

  3. Django (contrib/django.py) reads from the stream instead of request.body, and leaves the request as request.body would (_body and _stream), so guards, handlers and codecs can still read request.native.body, POST and FILES. When middleware such as CsrfViewMiddleware has already parsed an upload, Citry checks its declared size. On Django these limits now govern event requests in place of DATA_UPLOAD_MAX_MEMORY_SIZE.

  4. 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 way parse_qs counts fields, before parsing.

    A value that is not a positive int raises ValueError at construction, and declaring either on a component's Events class fails at class definition.

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

URLRoute gains an optional max_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 raises TypeError when the route is built; a bad return value fails the request. Routes without it read the whole body, as before. RouteRequest is unchanged: the handler sees at most the limit plus one byte and must reject a longer body.

Tests

tests/test_events_body_limits.py counts the bytes each adapter pulls from the client:

Adapter Cases
ASGI a chunked body far over the cap on the batch, per-event and view-event routes; a lying 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 it
WSGI a 10 MB Content-Length on each route reads exactly cap + 1; a chunked body without Content-Length reads exactly cap + 1; the cap boundary; a smaller Content-Length is respected
Django each route reads exactly cap + 1 from a 10 MB request; after Citry's read, request.body and request.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 size

Also: 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; URLRoute rejecting 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.
  • Events spec (packages/protocol/events/v1/spec.md) and design docs (events.md, events_plan.md): the limits and the payload_too_large row.
  • CHANGELOG.md: a Fixed entry (with the rename and the Django change) and an Added entry for URLRoute(max_body_bytes=...).
  • Left as is: the archived versioned docs and the dated record in docs/design/issue_207_proposals.md keep 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

…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
JuroOravec force-pushed the fix/events-body-limits branch from 02573a4 to 0727ccd Compare October 28, 2026 15:04
@JuroOravec
JuroOravec merged commit b86164d into main Oct 28, 2026
0 of 30 checks passed
@JuroOravec
JuroOravec deleted the fix/events-body-limits branch October 28, 2026 15:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Events: stop reading oversized request bodies, and document the size limits

1 participant