SessionManager::start() ignores the new_session_id pointer, so a concurrent request re-sends a superseded session ID (incomplete fix for #12362)
Preconditions and environment
- Magento 2.4.8-p5 (Commerce), PHP 8.3,
session.save_handler = files - Verified present and unchanged in
2.4-develop— the line numbers below are the same in both web/cookie/cookie_lifetimeset to a non-zero value (default3600; any non-zero value is enough)
This is a follow-up to #12362, which reported the same underlying problem in 2017 ("previous request regenerated a new session_id, but this request has old session_id"). That issue was closed as fixed in 2.2.x/2.3.x. The forwarding-pointer mechanism added for it is still in the code, but the check that consumes it sits on the wrong side of session_start(), so it never runs for the concurrent-request case the issue was filed for.
Steps to reproduce
The race itself is timing-dependent, so here is a deterministic way to create the same server-side state that a concurrent login produces. The point is what SessionManager::start() does when a request loads a session that another request has already regenerated.
Make a request as a guest and note the
PHPSESSIDvalue — call itA.Simulate what
regenerateId()leaves behind in sessionAwhen another request logs the customer in. Append tovar/session/sess_A(defaultphpserialize format):new_session_id|s:26:"bbbbbbbbbbbbbbbbbbbbbbbbbb";destroyed|i:<current unix timestamp>;These are exactly the two keys
SessionManager::regenerateId()writes into the old session before committing it (lines 527 and 530).Make any request that starts a session, sending
Cookie: PHPSESSID=A.Look at the
Set-Cookieheaders on that response.
In the real flow this happens without any file editing: POST customer/account/loginPost takes the session lock and regenerates A → B, and any GET that arrived a moment later blocks inside session_start(), is released when login commits the old session, and therefore loads a session that already carries the pointer to B.
Expected result
The request loads session A, sees that A has been superseded by B, and either adopts B or at minimum does not tell the browser to go back to A.
regenerateId() already writes new_session_id specifically so this can happen, and start() already contains the code to follow it (lines 182-186).
Actual result
The pointer is ignored. The response contains:
Set-Cookie: PHPSESSID=A; Max-Age=86400; path=/; ...
Set-Cookie: X-Magento-Vary=deleted; expires=Thu, 01 Jan 1970 00:00:01 GMT; ...The request re-emits the superseded session id, and because its own context was built from the pre-login session, Response\Http::sendVary() also clears X-Magento-Vary.
If that response reaches the browser after the login redirect — which is a matter of milliseconds — the browser reverts to the old session and the customer is silently logged out. The next page is then rendered from the guest full-page-cache bucket as well, because the vary cookie is gone.
Additional information
Why the existing fix never fires.
In lib/internal/Magento/Framework/Session/SessionManager.php::start():
182 if (isset($_SESSION['new_session_id'])) {
183 // Not fully expired yet. Could be lost cookie by unstable network.
184 session_commit();
185 session_id($_SESSION['new_session_id']);
186 }
187 session_start();The check on line 182 reads $_SESSION, but $_SESSION is not populated until session_start() on line 187. On the first start() of a request it is always empty, so the branch is dead.
It is reachable only on a second start() within the same request, after a session_write_close() — isSessionExists() (line 271) returns false when session_status() is PHP_SESSION_NONE, and $_SESSION survives a write-close. That is the "lost cookie by unstable network" case the comment describes. It is not the concurrent-request case from #12362.
Eight lines later, renewCookie(null) (line 195) reads the session id straight back out of the request cookie and re-sends it:
233 $cookieValue = $sid ?: $this->cookieManager->getCookie($this->getName());So the superseded id is written back to the browser. renewCookie() is a no-op only when getCookieLifetime() is 0, which is not the default.
Suggested fix.
Move the check to after session_start(), so it reads the session that was actually loaded, and run it before validator->validate() and renewCookie() so both operate on the correct session. Roughly:
session_start();
if (!empty($_SESSION['new_session_id'])
&& isset($_SESSION['destroyed'])
&& $_SESSION['new_session_id'] !== session_id()
) {
$newSessionId = $_SESSION['new_session_id'];
session_commit();
session_id($newSessionId);
session_start();
}Notes on that:
- It should be a single hop, not a loop, to avoid following a chain of pointers.
- Gating on
destroyedkeeps it to sessions that were genuinely superseded. - The second
session_start()may briefly block on the new session's lock if the regenerating request still holds it. - The pre-
session_start()block on lines 182-186 can stay for the write-close case, or be dropped if the post-start check covers it.
Related.
Response\Http::sendVary() (lib/internal/Magento/Framework/App/Response/Http.php, lines 108-123) makes this worse in both directions: a superseded request either writes a stale X-Magento-Vary or, when Context::getVaryString() returns null because nothing in the guest context deviates from its default, deletes the cookie outright. Fixing start() addresses this too, since the request would then be running on the correct session and produce the correct vary string.
Release note
Fixed an issue where a request that loaded a session which another request had already regenerated would send the old session ID back to the browser, silently logging the customer out.
Triage and priority
- Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
Source: magento/magento2