Skip to content

feat(render): add Experimental CitryRender.needs_csrf_token() and has_server_events() - #821

Merged
JuroOravec merged 3 commits into
mainfrom
feat/has-server-events
Nov 21, 2026
Merged

JuroOravec merged 3 commits into
mainfrom
feat/has-server-events

Conversation

@JuroOravec

@JuroOravec JuroOravec commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

(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 with class Events.

Today a host has to read occurrences[].eventContext from 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:

  1. Events calls, a POST to the events route (citry-events-vue.ts).
  2. The i18n messages request, a POST to ext/i18n/messages (citry-i18n-core.ts fetchArtifact, through the runtime's csrfHeaders). The browser sends it when a <c-i18n client> provider needs a locale the page did not include, such as on $i18n.switchLocale() or ensureMessages(), 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 from analyze_vue_serialization() before serialization. Serialization writes eventContext in DirectVueEventsProducer._prepare_holding_assets() (_vue/events.py) for each EventInstanceEntry in the selected tree. The i18n extension writes its browser data, with messages_url, in emit_i18n_dependencies() and prepared_i18n_payload() (ext/i18n/emission.py) from the I18nRenderRecords 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(), which analyze_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's render_may_request_messages(). That one checks mounted routes, then whether a selected record is a client provider, has a $c-tr binding, or has client content with an owner (its own, or the provider passed in through render(provides=...), which is all a record replayed from the cache has). These are the rules _provider_manifest_entries() uses, without building the message artifacts.
  • Both ignore serialize() arguments, like needs_browser_runtime(). needs_csrf_token() may answer True where serialization writes no request, such as content a client barrier hides. That costs a spare cookie, never a refused request.

Alternatives rejected.

What would show it is wrong. A render where an answer and the serialized output disagree. tests/test_has_server_events.py compares both answers with both serialize strategies. The cases: Events components (with and without handlers), nested ones, a component in a slot, one inside a c-if that is off, an Events child of a simple='vue' component, islands serialized on their own, a render whose context holds a sibling's Events record, a cached replay, a Render action's fragment, a page with only a locale switch (mounted and unmounted), a page with both, server-only translations, client content and a $c-tr binding 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

  • ruff check and ruff format (0.14.13), and mypy on packages/py/citry/citry on macOS and with --platform linux: clean.
  • The new file has 18 tests. With the related suites (separate assets, browser analysis, Events dispatch, actions, Vue events, cache artifact, tokens, markers, i18n client, bindings, cache, CSRF), 1214 passed and 1 skipped.
  • Benchmark: I compared str(ProjectPage(...)) from test_benchmark_citry.py and a small Events page against origin/main's serialization.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:

  • A locale switch also sends the CSRF token, and has_server_events() does not report it. This led to needs_csrf_token().
  • A component cached with no provider and replayed into one got a false "no". The fix counts the provider passed in, as serialization does, and adds a regression test.
  • The $c-tr binding rule and the simple='vue' call path had no tests. Both have tests now.
  • Docstring, docs and changelog wording.

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

JuroOravec and others added 3 commits November 21, 2026 13:19
A host that embeds Citry renders, such as citry-django, needs to know
whether a page's event calls will need the CSRF cookie before it
serializes. The method answers from the Events records of the render's
own selected tree, which is the set serialization writes an
eventContext for, without building the manifest.

Part of #818.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017pjjQc1w8PkP2VuK3qFsKw
…ation's selection

A locale switch and a hand-written form posting to an event URL also
need the CSRF token, so the docstring and docs say a host still needs
the cookie for them. The tests now also compare the answer with the
Events records preparation selects, and cover an Events component that
a simple='vue' component calls.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017pjjQc1w8PkP2VuK3qFsKw
Besides Events calls, Citry's browser code sends the page's CSRF token
when the i18n runtime posts to the messages route, which a client
provider does on a locale switch to a locale the page did not include.
needs_csrf_token() answers for both from the render's own records, so a
host can decide on the CSRF cookie before it serializes, including for
content rendered into a provider and replayed from the render cache.
has_server_events() still answers for Events calls alone.

Part of #818.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017pjjQc1w8PkP2VuK3qFsKw
@JuroOravec JuroOravec changed the title feat(render): add Experimental CitryRender.has_server_events() feat(render): add Experimental CitryRender.needs_csrf_token() and has_server_events() Nov 21, 2026
@JuroOravec
JuroOravec merged commit 93bb0ee into main Nov 21, 2026
33 checks passed
@JuroOravec
JuroOravec deleted the feat/has-server-events branch November 21, 2026 15:41
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.

1 participant