Repository navigation
Conversation
bindingCookieName() used session_name(), which PHP only sets when a session starts. On a site that starts sessions for some visitors only ($config->sessionAllow), a session-less page named the cookie PHPSESSID_csrf while requests with a session looked for wires_csrf, so a token rendered to a guest never validated once a session started (e.g. a login form posted by a guest without a session cookie). Add Session::sessionName(), the name ProcessWire gives a session (sessionNameSecure or sessionName + 's' on HTTPS with sessionCookieSecure, else sessionName), known before and without a session start. init() and hasCookie() use it in place of their copies of that logic, and bindingCookieName() uses it when no session is active (an active session keeps using session_name(), which also covers external session providers). Tests: testSessionName covers sessionName() and, when run without a session, the binding cookie name; testHostPrefixedCookie no longer depends on session_name() matching the configured name. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
|
Thanks for this. I wrote signed mode (#356), and this is a real gap in it: I only ever exercised it on sites that start a session on every request. I read the change against current
Three small things:
I haven't run it myself, only reviewed the code. |
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.
Problem
In signed mode,
SessionCSRF::bindingCookieName()builds the cookie name fromsession_name(). PHP only sets that once ProcessWire starts a session (Session::init()callssession_name($config->sessionName…)), so the name depends on whether the current request has a session.That matters for the case signed mode suits best: a site that starts sessions only for some visitors via
$config->sessionAllow(e.g. none for guests and bots browsing a catalogue, one on POST or once a cookie exists). On such a site:__Host-PHPSESSID_csrf;sessionAllowallows POSTs), sosession_name()is nowwiresand the token is checked against__Host-wires_csrf, which doesn't exist yet and is minted fresh;Reproduced over HTTPS on a 3.0.274 site with
'mode' => 'signed'and asessionAllowcallable that allows a session only with a session cookie, in the admin, or on POST: a guest's login POST from the front-end form failed CSRF every time.Fix
Session::sessionName(): the name ProcessWire gives a session (sessionNameSecure, orsessionName+s, on HTTPS withsessionCookieSecure; otherwisesessionName), known before and without a session start.init()andhasCookie()use it in place of their two copies of that logic, so behaviour there is unchanged.bindingCookieName()uses$session->sessionName()when no session is active. With an active session it still usessession_name(), so external session providers (whereinit()returns early and keeps the existing name) behave as before.Session/API.md: documents$session->sessionName()and notes signed mode with$config->sessionAllow.Sites that always start a session see no change: the active session's name is what they use today.
Tests
SessionCSRF.test.php:testSessionName():sessionName()over HTTP, over HTTPS with and withoutsessionNameSecureandsessionCookieSecure, and, when run without a session, that the binding cookie is named after it;testHostPrefixedCookie()no longer assumessession_name()already equals the configured name (it pinssessionCookieSecureoff for the test, so only the prefix differs between HTTP and HTTPS).Verification
__Host-wires_csrfand start no session; the token on the page, from a second page and from an AJAX endpoint is the same; a guest's login POST passes CSRF and logs in with valid credentials; a bad token is still rejected; admin login and a page save work.dev; it doesn't overlap the recentSession.phpchanges and lints clean.