Repository navigation
fix(events): write a server-stored State entry only for a token the answer can carry - #869
Merged
Merged
Conversation
…nswer can carry A call that changes server-stored State wrote its new cache entry before Citry built the `state` action that hands the browser the token. A form posted without JavaScript gets an answer that drops `state` actions, so the entry was written and never read, and a `state` action that could not be built left its entry behind. The State-change path now uses the order the renewal path has: prepare the token, build the `state` action, write the entry last. One check, shared by the State change, the renewal, and the signed State size cap, says whether the answer can carry a `state` action. Because of that, signed State is no longer checked against `_max_bytes` for a browser write in such an answer, as it already was not for a call with no caller instance. Closes #860 Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This was referenced Nov 24, 2026
JuroOravec
added a commit
that referenced
this pull request
Nov 24, 2026
#871 (#878) Records the Mechanism 7 security reviews of six merged Events changes, in the form docs/security/reviews/README.md describes. Each record is built from the pull request description, the merged diff and its tests; where a description does not record something the template asks for, the record says so. Co-authored-by: Claude Sonnet 5.5 <[email protected]>
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.
(Sonnet 5.5)
Closes #860
With server-stored State (
_storage = "server"), a call that changes State wrote the new cache entry before Citry built thestateaction that hands the browser the new token. A form posted without JavaScript gets an answer that dropsstateactions, so the entry was written and never read. Nothing broke, since the entry expires after_max_age, but every such post left an unused entry in the cache.How it works
The State-change path now has the order that #858 gave the renewal path: prepare the token, build the
stateaction, write the entry last._new_state_actiondoes those three steps. A State change and a renewal both call it, so the order lives in one place._can_send_state_actionsays whether the answer can carry astateaction: no for the compatibility mode (a form posted without JavaScript, or another request that asks for HTML), otherwise whether the client advertised the kind. The State change, the renewal, and the signed State size cap all ask it.stateaction that cannot be built (a public value the browser cannot read, such as an integer wider than a browser number) now leaves no entry behind. The call still answers 500handler_erroras before.JavaScript calls are unchanged: the same token, the same
stateaction, one new entry.One side effect: signed State is no longer checked against
_max_bytesfor a browser write in a no-JavaScript answer. Nothing signs that State there, and a call with no caller instance was already unchecked. In practice only a hand-built JSON post withAccept: text/htmlcan carry such a write, since the form codec never sendsstateUpdates.Tests
Each new test fails without the change (checked by running it against
origin/main'sdispatcher.py).stateactiontest_a_form_posted_without_javascript_writes_no_entrytest_a_form_that_changes_server_stored_state_writes_no_cache_entrystateaction that cannot be built leaves no entrytest_a_state_action_that_cannot_be_built_leaves_no_entrytest_a_call_that_makes_no_new_token_is_not_cappedtest_a_live_caller_still_gets_a_new_token,test_the_new_token_is_the_one_entry_a_live_caller_reads_back(the second passes before the change too, as a guard on the new order)TestStateRenewal(unchanged, passes)Left out of scope
A
Renderaction in a no-JavaScript answer still writes an entry for a component with server-stored State, and the HTML carries no token for it. The entry comes from the render itself (emission.py,capture_instance), which does not know whether its HTML will reach a client that can use the token. Every page view of such a component has the same property, and the docs already list "every render" as a cost. Fixing it needs a decision about what the render-time token holds when the page's manifest is dropped, so it is not part of this change.A failure after the write, such as an
on_event_resulthook that adds an action the client did not advertise, can still leave the entry until it expires. The_new_state_actiondocstring says so.Security (Mechanism 7)
Places touched: State tokens the browser stores, Events responses that carry
publicState, and server-storage cache writes. The change only removes writes. The token,publicState, and error text are built by the same functions from the same values as before, so no new route carries a value to the page. The caller instance is a value the client sends, so entries per request stay client-driven and bounded by the cache's State budget, as documented. An independent reviewer listed each route and found none that is new or unsafe, and found no hole in a released version. It also checked the change against the kinds of bug that other frameworks' published advisories describe, and none apply.Versioning
No break the 1.x promise covers: the wire format and the token format are unchanged, and the helpers are private. Fewer cache entries and a looser signed-State cap check in a no-JavaScript answer are not in the covered lists. It is a fix and ships in 1.3.0 beside #858, whose renewal path it matches. The changelog has a
### Fixesentry under v1.3.0.Checks
test_events_*.pyand the State tests (2,394 passed, 1 skipped), then every test underpackages/py/citry/tests(15,497 passed, 75 skipped, 1 xfailed).--platform linuxoncitryand the typing contract tests.scripts/validate.pyandpython -m docs_site build-check --strict(0 errors, 0 warnings).Review
An independent reviewer (high effort) checked correctness, versioning, security, the advisory survey, and the prose separately. Fixed during the review: three docstrings and comments that claimed more than the code does (the shared check is for the dispatcher only; the
prepare_state_tokennote applied to renewals only; "form posted without JavaScript" also covers any request that asks for HTML), the design doc's failure mode for a State change, and the commit message now names the signed-State cap change.🤖 Generated with Claude Code