Skip to content

fix(csrf): a form token is bound to the session that rendered it - #218

Merged
tina4stack merged 1 commit into
tina4stack:v3from
MichaelC8E:fix/a-form-token-is-bound-to-the-session-that-rendered-it
Oct 13, 2026
Merged

tina4stack merged 1 commit into
tina4stack:v3from
MichaelC8E:fix/a-form-token-is-bound-to-the-session-that-rendered-it

Conversation

@MichaelC8E

Copy link
Copy Markdown
Contributor

The CSRF check compares a form token's session with the session of the request that sends it back, but only when the token carries one. A token rendered by form_token() during a request carried none, because the generator read an id that no dispatch path set. The session binding the docs describe ("Session-Bound Tokens") therefore never applied.

The dispatcher now publishes the request's session for the duration of the request (the same per-request channel the CSP nonce uses), and form_token() binds the token to it. A render outside a request is unbound, as before. A page that renders a token for a visitor with no session yet marks that session, so the visitor gets a cookie and their own form passes. The response cache now treats such a page as per-visitor and does not store it, so it cannot hand one visitor's token to the next.

Behaviour change: a bound token is accepted only on the session that rendered it. A client that posts a token without sending the session cookie it was rendered under is now refused (403), which is what the PHP port already does.

Test. tests/test_csrf_form_token_session_binding.py renders the token through the real dispatcher in one cookie jar and posts it from another, a fresh visitor, a cached page, and a render after the request. The cross-session and fresh-visitor cases fail on v3 without this change.

A token rendered by form_token() during a request is now bound to that
request's session, so the CSRF check compares it with the session that
sends it back. A page that renders a token for a visitor with no session
yet marks the session so it gets a cookie, and the response cache does
not store such a page.

Signed-off-by: Michael <michael@codeinfinity.co.za>
@MichaelC8E

Copy link
Copy Markdown
Contributor Author

I have read the Tina4 Contributor Licence Agreement and I agree to it.

@MichaelC8E MichaelC8E closed this Oct 12, 2026
@MichaelC8E MichaelC8E reopened this Oct 12, 2026
@tina4stack
tina4stack merged commit 206d62c into tina4stack:v3 Oct 13, 2026
21 checks passed
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.

2 participants