Skip to content

SessionCSRF: name the binding cookie the same with and without a session - #407

Open
theoretic wants to merge 1 commit into
processwire:devfrom
theoretic:csrf-binding-cookie-name
Open

theoretic wants to merge 1 commit into
processwire:devfrom
theoretic:csrf-binding-cookie-name

Conversation

@theoretic

Copy link
Copy Markdown

Problem

In signed mode, SessionCSRF::bindingCookieName() builds the cookie name from session_name(). PHP only sets that once ProcessWire starts a session (Session::init() calls session_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:

  • a session-less page renders a login form and issues __Host-PHPSESSID_csrf;
  • the login POST gets a session (sessionAllow allows POSTs), so session_name() is now wires and the token is checked against __Host-wires_csrf, which doesn't exist yet and is minted fresh;
  • the token never validates, and the guest can't log in.

Reproduced over HTTPS on a 3.0.274 site with 'mode' => 'signed' and a sessionAllow callable 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

  • New Session::sessionName(): the name ProcessWire gives a session (sessionNameSecure, or sessionName + s, on HTTPS with sessionCookieSecure; otherwise sessionName), known before and without a session start. init() and hasCookie() 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 uses session_name(), so external session providers (where init() 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:

  • new testSessionName(): sessionName() over HTTP, over HTTPS with and without sessionNameSecure and sessionCookieSecure, and, when run without a session, that the binding cookie is named after it;
  • testHostPrefixedCookie() no longer assumes session_name() already equals the configured name (it pins sessionCookieSecure off for the test, so only the prefix differs between HTTP and HTTPS).

Verification

  • The SessionCSRF test passes (60 checks) from CLI, where no session is active, so the new no-session check runs. Run with a minimal stand-in for the WireTests runner against 3.0.274 with these changes applied; not yet run through the full WireTests suite.
  • Over HTTPS on the site above, with the site-side workaround removed: guest pages issue __Host-wires_csrf and 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.
  • Rebased onto current dev; it doesn't overlap the recent Session.php changes and lints clean.

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>
@adrianbj

Copy link
Copy Markdown
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 dev and agree with both the diagnosis and the fix:

  • bindingCookieName() uses session_name(), which is still PHP's default (PHPSESSID) on a request where $config->sessionAllow refused a session, because Session::init() returns before it sets the name. So the cookie issued with a session-less page and the one looked up on the POST that follows have different names, and the token can't validate.
  • Session::sessionName() is the same logic init() and hasCookie() each had a copy of, so those two behave as before.
  • Keeping session_name() when a session is active means nothing changes for sites that always start one, or for sites with an external session provider.

Three small things:

  1. @since 3.0.275 (the docblock and API.md) is behind now: dev is already at 3.0.276.
  2. A site with an external session provider that also serves session-less requests would still get two names (the provider's when a session is active, ProcessWire's when not). That's a narrow case and I wouldn't hold the PR for it, but it may be worth a sentence in the signed-mode notes.
  3. Sites that hit this bug will have a leftover __Host-PHPSESSID_csrf cookie in browsers until it expires. Harmless, just noting it.

I haven't run it myself, only reviewed the code.

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