Skip to content

fix(events): run guards before checking a call's data - #875

Merged
JuroOravec merged 3 commits into
mainfrom
events/guards-before-validation
Nov 25, 2026
Merged

JuroOravec merged 3 commits into
mainfrom
events/guards-before-validation

Conversation

@JuroOravec

Copy link
Copy Markdown
Member

(Opus 5.5)

On each event call, Citry now runs the on_event hook, _context and the guards before it checks the call's args against the handler's data class. A caller that a guard refuses gets the guard's error (such as 403) even when its args are invalid, and the server no longer checks a large payload for a call it then refuses.

What changed

  • EventsDispatcher._prepare_call (packages/py/citry/citry/ext/events/dispatcher.py): the order is now State token and stateUpdates writes, then the host CSRF field strip, then on_event, _context, the guard, and last the args check. Only the args check moved. State is still rebuilt and written before the guards, as decided in Events: run guards before validating the call's data #861, so a refused State write still answers 422 before any guard runs.
  • When on_event answers a call, Citry skips the args check, because only the handler reads the checked data.
  • The guards get a copy of the args (type(args)(args), which keeps the form-text class), so a guard that changes self.event.args cannot change what the data class checks.
  • on_event_error now receives the events instance when the data class itself raises, because that instance exists by then.

Docs

  • docs_site/content/security.md ("Authorize every event"): a guard runs before the args check. self.event.args holds unchecked values, so read them with .get() and check the type. self.state already holds the browser's _model writes, so a guard must never decide on a field the browser can write; keep such fields out of _model.
  • docs_site/content/events/state.md: the same point next to _model, plus what an empty _model tuple does.
  • docs/design/events.md: the call order in 3.6, the on_event note in 3.7, the 6.1 responsibilities table, and the order summary near the State token section.
  • docs/design/trust_boundaries.md: a new row in section 9 for what guards and _context read. Two existing line references in the State token row are also corrected.
  • packages/protocol/events/v1/spec.md does not say whether a guard error or invalid_args comes first, so it is unchanged.

Tests

  • TestGuardsBeforeDataCheck (dispatcher, sync and async): a refused caller with invalid args gets 403 with no field errors; an allowed caller with invalid args still gets 422; _context and the guard each run once; per-call answers in a batch; a per-handler guard; a handler without data; a guard that changes the args; a guard that crashes on invalid args (500).
  • TestGuardsSeeStateWrites: a guard sees the written State value, and a refused State write answers 422 before the guard.
  • TestGuardsBeforeDataCheckOverHttp: the per-event route, the batch route, a form post without JavaScript, and a GET handler.
  • TestVerbRouteGuardOrder: the ViewEvents verb route.
  • Two existing tests are updated because they encoded the old order: test_on_event_fires_before_validation_and_can_veto (was ..._after_validation_...), and test_the_on_event_error_hook_sees_a_data_class_that_raises, which now gets the events instance.
  • The new order tests fail against the old dispatcher.

Local checks: ruff check and format, mypy (macOS and --platform linux), protocol contracts, scripts/validate.py, and the citry package's pytest suite without e2e (15898 passed before the review fixes; all event tests passed again after them).

Versioning

This is a minor-release change for citry 1.3.0, marked "Security:" in the changelog. No call that 1.2.x accepted is now refused. A call that a guard refuses gets the guard's error where 1.2.x answered 422. Visible effects, all in the changelog:

  • _context and the guards now also run for calls whose args are invalid.
  • A guard that indexes self.event.args and raises on a missing key now answers 500 instead of the earlier 422.
  • The on_event hook moved too. It is listed as not covered by the 1.x promise.

No wire format changes.

Closes #861

🤖 Generated with Claude Code

JuroOravec and others added 3 commits November 24, 2026 22:21
On each event call the dispatcher now runs the on_event hook, _context
and the guards before it checks the args against the handler's data
class. A caller a guard refuses gets the guard's error (403) even when
its data is invalid, and the server no longer checks a payload for a
call it then refuses. An on_event answer replaces the handler, so the
data check is skipped for it.

Guards keep seeing State after the browser's _model writes; the docs
now say to keep the fields a guard relies on out of _model.

Closes #861

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The guards now get a copy of the call's args, so a guard that changes
self.event.args cannot change what the data class checks. Tests cover
a guard that crashes on invalid args, a guard that sees the browser's
State writes, and a refused State write that still answers 422 before
the guards. The docs and changelog say that only the args check moved
after the guards, and what a guard can and cannot trust.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…validation

# Conflicts:
#	docs/design/trust_boundaries.md
@JuroOravec
JuroOravec merged commit 0a77df4 into main Nov 25, 2026
33 checks passed
JuroOravec added a commit that referenced this pull request Nov 25, 2026
… route for advisory fixes (#884)

Add public review records for GHSA-29wx-2wvw-8962, GHSA-gj9m-wwqx-m37j
and GHSA-2jch-pr4c-gf9m, published 2026-11-25, and for #875 (guards run
before the data check).

security.md and the Events design doc now say that reloading a module
that applies @event(...) to a function it imports raises the stacked
@event error. docs/codebase.md "Releasing advisory fixes" describes
landing the fix chain through a public pull request merged with a merge
commit (1.3.0, #881) as the preferred route, next to the fast-forward
push used for 0.7.0.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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: run guards before validating the call's data

1 participant