Skip to content

feat(events): expire signed State after 24 hours by default, and answer stale_state for unknown token versions - #706

Merged
JuroOravec merged 3 commits into
mainfrom
feat/state-token-default-lifetime
Nov 15, 2026
Merged

JuroOravec merged 3 commits into
mainfrom
feat/state-token-default-lifetime

Conversation

@JuroOravec

Copy link
Copy Markdown
Member

Part of the 1.0.0 work (#624, decision 3 and the later State token version decision).

What changes

  1. Signed State expires after 24 hours by default. When a component's State sets no _max_age, its signed State token now expires 24 hours after Citry issues it, the same lifetime server-stored State already had. An expired token answers stale_state (409), and Citry's browser code asks the visitor once per page whether to reload, which brings a fresh token. This is the one breaking change recorded for 1.0.0, approved by the maintainer, and the changelog lists it under "Upgrading from 0.9.x".

  2. A token from an unknown token version answers stale_state, not invalid_state. The server checks the version first and reads nothing else from such a token:

    • the prefix: a prefix shaped ce, a letter, a number (such as cev2.) that is neither cev1 nor ces1;
    • the payload: an integer v other than 1, checked before the shape check, in the signed path whether or not the signature matched, and for server-stored entries too.

    A version that is not an integer, and every other unreadable token, stays invalid_state (403). This makes the rule "an internal format may move to a new version in a 1.x minor release" true for State tokens: during a deploy, a page carrying a newer token gets a reload offer instead of an error.

How a page opts out

There is no "never expires" value. _max_age = None keeps meaning "use the default" in both storage modes, as it already did for server storage, so it means the same thing everywhere. A page that must stay open longer sets a longer timedelta, for example _max_age = timedelta(days=7). A "never" value would have meant None does different things per storage mode (server storage must never keep entries forever), and it would bring back the unbounded replay that the default exists to stop. The Versioning page already describes this rule.

A token from 0.9.x holds "x": null. Verification reads it as 24 hours after its signed mint time t, so a token copied before the upgrade does not stay valid until the secret changes. _max_age under one second now raises ValueError at class definition: token times are whole seconds, so such a value expired every token at once (and timedelta(0) must not look like "off"). Server-stored entries now also have their expiry field checked on verify, so the lifetime holds on a backend that ignores the cache time-to-live. A render-cache entry that holds an expired token renders again.

Versioning check

  • The default lifetime is a break that the 1.x promise would cover. It ships in 1.0.0 as the maintainer's recorded exception to "1.0.0 adds no breaking change", with an upgrade note.
  • The stale_state change alters one check in packages/protocol/events/v1/spec.md. It lands before 1.0.0, which freezes v1. No field, value or route changes, and the spec's Versioning section now says the token format is not defined there and may change in 1.x.
  • State tokens and server-stored entries are an internal format. After this change, every 1.x release treats a token version it does not know as stale, so a later minor release may raise the version. A new token version must keep the ce, letter, number prefix shape; docs/codebase.md says so.
  • Release needed: 1.0.0 (this PR belongs to it).

Files

  • packages/py/citry/citry/ext/events/tokens.py: default lifetime, version checks, docstrings.
  • packages/py/citry/citry/ext/events/state.py, component.py: docstrings and the subclass warning text.
  • Tests: test_events_tokens.py (new TestDefaultLifetime, TestUnknownTokenVersion; older tests that expected invalid_state for a version-2 payload now expect stale_state, which is the new contract), test_events_dispatch.py, test_events.py.
  • Spec, docs/design/events.md, docs/codebase.md (Contract versions), docs_site/content/security.md, events/state.md, advanced/subclassing.md, reference/browser-apis.md, CHANGELOG.md.

Checks

  • packages/py/citry/tests (excluding e2e), run with the worktree on PYTHONPATH: 13589 passed, 9 skipped, 1 xfailed.
  • ruff check and ruff format --check on packages/py/citry: clean.
  • mypy packages/py/citry/citry, and again with --platform linux: clean.
  • scripts/validate.py: all validators pass.
  • I did not run scripts/check.py, because its fast profile includes the cargo phases and this lane was told not to build Rust. CI runs the full gate.

Review

An independent reviewer did a technical review, a security review (Mechanism 7) and a Mechanism 8 review, then re-checked each fix commit. The saved docs-cold-reader agent read the changed pages.

Security (Mechanism 7). Places touched: State tokens, Events error responses, and the reload prompt. The reviewer classified every route the token takes:

  • An unknown prefix is refused before anything after the prefix is decoded: safe.
  • A forged payload with an unknown v: the constant-time HMAC compare runs first, and the payload is parsed only to choose the error code. The shape check, State restore and class check are never reached, and a test hooks the shape check to prove it: safe.
  • A validly signed unknown version can come only from someone who holds the secret: safe by design, since only the server holds it.
  • A server token reads only an entry Citry wrote: safe.
  • Render-cache replay checks the token the same way, and a refused token is a cache miss: safe.
  • Turning a 403 into a 409 was already possible, and it only shows a prompt in the attacker's own browser: safe.
  • Clock skew only moves expiry by the skew: safe.
  • No server-minted token can now go without an expiry. timedelta.max is something only the author can set.

No hole was found in this change or in a released version.

Mechanism 8. The reviewer read the advisory survey. This change closes its "State never expires unless _max_age is set" item and adds no instance of the island, secret-timing, deserialize, cache-key or cache-xuser bug classes.

Fixed during the review:

  • Tokens from 0.9.x with "x": null were unbounded; they now expire 24 hours after their mint time.
  • _max_age under one second was accepted; it now raises ValueError.
  • An unknown storage value now answers 403 before the prefix check runs.
  • Server-stored entries now have their expiry field checked.
  • The render-cache behavior is tested and documented.
  • The docs overstated what the version check can see; a new version must change the prefix.
  • Docs now say when Citry issues a token and that the handler of an expired call does not run.
  • The audit doc row is marked decided.
  • Prose and cold-read items were applied, including that the spec contradicted itself ("the one part"), the clock-start wording, and the changelog rewrite.

Left for others:

  • The reload prompt text in _vue/client.js (and the generated runtime) says "This page and the server are running different versions of the app". Expiry is now the most common cause of stale_state. The text lives in the generated runtime bundle that the C6 lane is changing, so this PR leaves it alone and the coordinator will hand it to C6.
  • Two private security-repo notes still describe the old "never expires" behavior. The coordinator will note them in the private tracker.

🤖 Generated with Claude Code

JuroOravec and others added 3 commits November 15, 2026 13:35
A signed State token now expires 24 hours after Citry issues it when the
component's State sets no _max_age, the same lifetime server-stored State
already had. An expired token answers stale_state (409), and Citry's
browser code offers a reload. A page that must stay open longer sets a
longer _max_age; None keeps meaning "the default" in both storage modes.

A State token whose version the server does not know, by its prefix or
by the integer version in its payload, now answers stale_state instead
of invalid_state, so a later 1.x release can move State tokens to a new
version. The version is checked before anything else is read from the
token. The Events spec, design doc, contract-version notes, security and
State docs, and the changelog are updated to match.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…x_age

Fold in the review findings:

- A token that holds "x": null (minted by a release before 1.0.0) now
  expires 24 hours after its signed mint time, so a token copied before
  the upgrade does not stay valid until the secret changes.
- State._max_age under one second raises ValueError when the class is
  defined: token times are whole seconds, so such a lifetime expired every
  token at once.
- An unknown storage value answers invalid_state before the token prefix
  is looked at.
- A render-cache entry renders again once its State token expires; a
  test covers it and the caching page says so.
- Docs say when Citry issues a token, that the handler of a call with an
  expired token does not run, and that a new token version must change
  the prefix; the spec step stays format-neutral.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Verify now checks the expiry field of a server-stored entry too, so the
24-hour lifetime holds even when a cache backend ignores the time-to-live.
Wording fixes from the follow-up review.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@JuroOravec
JuroOravec merged commit 1a9f168 into main Nov 15, 2026
33 checks passed
@JuroOravec
JuroOravec deleted the feat/state-token-default-lifetime branch November 15, 2026 14:26
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