Repository navigation
fix(csrf): a form token is bound to the session that rendered it - #218
Merged
tina4stack merged 1 commit intoOct 13, 2026
Conversation
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>
Contributor
Author
|
I have read the Tina4 Contributor Licence Agreement and I agree to it. |
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.
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.pyrenders 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 onv3without this change.