Repository navigation
feat(events): CSRF names in the configuration block and the page nonce for host requests - #822
Merged
Merged
Conversation
…e for host requests Part of #818 (parts 2 and 3). - Experimental `_csrf_cookie` and `_csrf_header` Events settings, checked at startup and written into each app's configuration block as `csrfNames`. The runtime reads them with own-key lookups, takes them only from blocks it trusts, and an explicit `Citry.configure({csrf})` wins for each name it sets. - Experimental `Citry.pageNonce()`, the `X-Citry-Page-Nonce` request header, and `page_nonce_from_headers()` / `PAGE_NONCE_HEADER`. An Events `html-fragment/1` Render uses the header's nonce when the app sets no `_csp_nonce`, and every answer to a call that sends the header gets `Cache-Control: no-store` and `Vary: X-Citry-Page-Nonce`. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017pjjQc1w8PkP2VuK3qFsKw
- Citry.configure counts a csrf key given as undefined as not given, as 1.1.1 did. - Every Events answer lists X-Citry-Page-Nonce in Vary; an answer to a call that sent it also gets no-store. The docs recipe does the same. - One CSP nonce pattern, shared by serialize() and page_nonce_from_headers(). - Docs: a page without a nonce that only gets fragments needs Citry.configure; custom transports must add the cache marks; the header route comes before _csp_nonce; prose fixes from both reviews. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017pjjQc1w8PkP2VuK3qFsKw
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017pjjQc1w8PkP2VuK3qFsKw
This was referenced Nov 21, 2026
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)
Part of #818.
Adds two Experimental methods that a host such as citry-django calls before it serializes a render:
CitryRender.needs_csrf_token()returns whether the browser may send Citry's CSRF token from the render. A host uses it to set the CSRF cookie only on pages that need it, because the cookie keeps shared caches from storing the page.CitryRender.has_server_events()returns the narrower fact: whether the render has a component withclass Events.Today a host has to read
occurrences[].eventContextfrom the serialized configuration JSON, which is not a public format, and that check misses the i18n requests.Requests that send the CSRF token
A sweep of Citry's browser code (
packages/js/citry-client/src,_vue/*.js, the extension client folders) found two:POSTto the events route (citry-events-vue.ts).POSTtoext/i18n/messages(citry-i18n-core.tsfetchArtifact, through the runtime'scsrfHeaders). The browser sends it when a<c-i18n client>provider needs a locale the page did not include, such as on$i18n.switchLocale()orensureMessages(), and only with mounted routes.Not covered, and documented as such: forms the app author writes that post to an event URL, and a third-party browser plugin that sends
csrfHeaders()itself.Plan (Mechanism 2)
Prior art.
CitryRender.needs_browser_runtime()answers fromanalyze_vue_serialization()before serialization. Serialization writeseventContextinDirectVueEventsProducer._prepare_holding_assets()(_vue/events.py) for eachEventInstanceEntryin the selected tree. The i18n extension writes its browser data, withmessages_url, inemit_i18n_dependencies()andprepared_i18n_payload()(ext/i18n/emission.py) from theI18nRenderRecords of the selected tree.Design.
has_server_events():render_target == "prepared"and an Events record in the selected tree. The selection moves into_selected_render_ids(), whichanalyze_vue_serialization()also calls. A render with no Events records returns without walking the tree.needs_csrf_token():has_server_events(), or the i18n extension'srender_may_request_messages(). That one checks mounted routes, then whether a selected record is a client provider, has a$c-trbinding, or has client content with an owner (its own, or the provider passed in throughrender(provides=...), which is all a record replayed from the cache has). These are the rules_provider_manifest_entries()uses, without building the message artifacts.serialize()arguments, likeneeds_browser_runtime().needs_csrf_token()may answerTruewhere serialization writes no request, such as content a client barrier hides. That costs a spare cookie, never a refused request.Alternatives rejected.
"events"runtime requirement fromanalyze_vue_serialization(). Element bindings add it too, so it answers a different question.What would show it is wrong. A render where an answer and the serialized output disagree.
tests/test_has_server_events.pycompares both answers with both serialize strategies. The cases: Events components (with and without handlers), nested ones, a component in a slot, one inside ac-ifthat is off, an Events child of asimple='vue'component, islands serialized on their own, a render whose context holds a sibling's Events record, a cached replay, aRenderaction's fragment, a page with only a locale switch (mounted and unmounted), a page with both, server-only translations, client content and a$c-trbinding rendered into a page's provider, a cached replay into a provider, and pages with neither. Each comparison also checks the Events records that preparation selects. Mutation checks on each rule fail a test.Checks
packages/py/citry/citryon macOS and with--platform linux: clean.str(ProjectPage(...))fromtest_benchmark_citry.pyand a small Events page againstorigin/main'sserialization.py, alternating the run order. The difference stays within 1%, in both directions. The new methods are not on the serialize path.Versioning
This adds Experimental methods only, so nothing the 1.x promise covers changes. It needs a minor release (1.2.0). The changelog entry is under
## Unreleased.Review
Independent reviewers ran technical, security (Mechanisms 7 and 8) and prose passes. They found these, all fixed in this PR:
has_server_events()does not report it. This led toneeds_csrf_token().$c-trbinding rule and thesimple='vue'call path had no tests. Both have tests now.Every Events call in the browser goes through an occurrence's
eventContext. A third kind of request was ruled out across the runtime, the Vue plugins and the extension client folders. Advisory survey kinds checked: csrf, xsrf-leak, cache-http, cache-xuser, client-url. The change introduces none of them.🤖 Generated with Claude Code
https://claude.ai/code/session_017pjjQc1w8PkP2VuK3qFsKw