Repository navigation
fix(events): run guards before checking a call's data - #875
Merged
Merged
Conversation
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
This was referenced Nov 25, 2026
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>
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.
(Opus 5.5)
On each event call, Citry now runs the
on_eventhook,_contextand the guards before it checks the call's args against the handler'sdataclass. 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 andstateUpdateswrites, then the host CSRF field strip, thenon_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.on_eventanswers a call, Citry skips the args check, because only the handler reads the checked data.type(args)(args), which keeps the form-text class), so a guard that changesself.event.argscannot change what thedataclass checks.on_event_errornow receives the events instance when thedataclass 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.argsholds unchecked values, so read them with.get()and check the type.self.statealready holds the browser's_modelwrites, 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_modeltuple does.docs/design/events.md: the call order in 3.6, theon_eventnote 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_contextread. Two existing line references in the State token row are also corrected.packages/protocol/events/v1/spec.mddoes not say whether a guard error orinvalid_argscomes 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;_contextand the guard each run once; per-call answers in a batch; a per-handler guard; a handler withoutdata; 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: theViewEventsverb route.test_on_event_fires_before_validation_and_can_veto(was..._after_validation_...), andtest_the_on_event_error_hook_sees_a_data_class_that_raises, which now gets the events instance.Local checks: ruff check and format, mypy (macOS and
--platform linux), protocol contracts,scripts/validate.py, and thecitrypackage'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:
_contextand the guards now also run for calls whose args are invalid.self.event.argsand raises on a missing key now answers 500 instead of the earlier 422.on_eventhook moved too. It is listed as not covered by the 1.x promise.No wire format changes.
Closes #861
🤖 Generated with Claude Code