Skip to content

fix(events): write a server-stored State entry only for a token the answer can carry - #869

Merged
JuroOravec merged 1 commit into
mainfrom
events/skip-unread-state-cache-write
Nov 24, 2026
Merged

JuroOravec merged 1 commit into
mainfrom
events/skip-unread-state-cache-write

Conversation

@JuroOravec

Copy link
Copy Markdown
Member

(Sonnet 5.5)

Closes #860

With server-stored State (_storage = "server"), a call that changes State wrote the new cache entry before Citry built the state action that hands the browser the new token. A form posted without JavaScript gets an answer that drops state actions, 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 state action, write the entry last.

  • _new_state_action does those three steps. A State change and a renewal both call it, so the order lives in one place.
  • _can_send_state_action says whether the answer can carry a state action: 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.
  • A state action 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 500 handler_error as before.

JavaScript calls are unchanged: the same token, the same state action, one new entry.

One side effect: signed State is no longer checked against _max_bytes for 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 with Accept: text/html can carry such a write, since the form codec never sends stateUpdates.

Tests

Each new test fails without the change (checked by running it against origin/main's dispatcher.py).

Case Test
A form posted without JavaScript that changes server-stored State writes no entry, and its answer holds no state action test_a_form_posted_without_javascript_writes_no_entry
The same through the real route and a 303 answer test_a_form_that_changes_server_stored_state_writes_no_cache_entry
A state action that cannot be built leaves no entry test_a_state_action_that_cannot_be_built_leaves_no_entry
Signed State is not capped when the answer cannot carry the token test_a_call_that_makes_no_new_token_is_not_capped
A live JavaScript caller still gets one new token and reads it back test_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)
Renewal as in #858 TestStateRenewal (unchanged, passes)

Left out of scope

A Render action 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_result hook that adds an action the client did not advertise, can still leave the entry until it expires. The _new_state_action docstring 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 ### Fixes entry under v1.3.0.

Checks

  • Focused: test_events_*.py and the State tests (2,394 passed, 1 skipped), then every test under packages/py/citry/tests (15,497 passed, 75 skipped, 1 xfailed).
  • ruff check and format, mypy and mypy --platform linux on citry and the typing contract tests.
  • scripts/validate.py and python -m docs_site build-check --strict (0 errors, 0 warnings).
  • The cargo and pnpm phases are not run locally because no Rust or JavaScript changed; CI runs them.

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_token note 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

…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]>
@JuroOravec
JuroOravec merged commit 57776bc into main Nov 24, 2026
34 checks passed
@JuroOravec
JuroOravec deleted the events/skip-unread-state-cache-write branch November 24, 2026 21:07
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]>
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.

Server-stored State: a no-JavaScript form post that changes State writes a cache entry nobody reads

1 participant