Magento 2 Login Glitch: How Canonical Redirects Can Break Customer Sessions
Magento 2 Login Glitch: How Canonical Redirects Can Break Customer Sessions
At Shopping Mover, we understand that a seamless customer experience is paramount for any e-commerce platform. Even the most robust systems like Magento 2 can sometimes harbor subtle bugs that significantly impact user journeys. One such intricate issue, recently highlighted on GitHub, involves a specific interaction between Magento's 'Auto-redirect to Base URL' feature and PHP session cookie management, potentially leading to frustrating customer login failures.
The Core Problem: Conflicting Session Cookies
The reported bug (#41820) surfaces when a Magento 2 store, configured with 'Auto-redirect to Base URL' set to 'Yes (301 Moved Permanently)', is accessed via a non-canonical hostname (e.g., magento-redirect.test instead of www.magento-redirect.test). The intended behavior is a clean 301 redirect to the canonical URL. However, the issue describes a scenario where this redirect inadvertently issues a PHPSESSID cookie for the *parent domain* (e.g., magento-redirect.test).
The real trouble begins if this parent-domain session expires while its cookie persists in the user's browser. When the customer then attempts to log in successfully at the *canonical domain* (e.g., www.magento-redirect.test), a new PHPSESSID cookie is issued, correctly scoped to the www subdomain. The critical flaw emerges when both the stale parent-domain cookie and the new canonical-domain cookie are present during a request to 'My Account'. Instead of granting access, Magento redirects the user back to the sign-in page, creating an endless login loop. The reporter confirmed that if only the new, canonical-domain cookie is present, the login proceeds successfully.
Technical Deep Dive: What's Happening Under the Hood?
The issue author, atIOCrON, meticulously detailed the HTTP request flow:
GET apex /customer/account/login/ -> 301
Location: http://www.magento-redirect.test:18765/customer/account/login/
Set-Cookie: PHPSESSID=[redacted]; Domain=magento-redirect.test; Path=/; HttpOnly; SameSite=Lax
POST www /customer/account/loginPost/ -> 302
Location: http://www.magento-redirect.test:18765/customer/account/
Set-Cookie: PHPSESSID=[redacted]; Domain=www.magento-redirect.test; Path=/; HttpOnly; SameSite=Lax
GET www /customer/account/index/ with both cookies -> 302 to sign-in
GET www /customer/account/index/ with only the www cookie -> 200
This sequence clearly illustrates the conflict: the initial 301 redirect, intended to guide the user to the correct URL, prematurely sets a session cookie for the broader domain. Magento's front controller appears to initiate the session even before the canonical redirect plugin can execute its redirect logic, leading to this unintended cookie issuance.
Community Response and Current Status
The issue was reported on a Magento Open Source 2.4-develop instance. Following standard procedure, the Magento Contributor Assistant bot prompted for verification. Crucially, a subsequent comment from engcom-Bravo indicated that the Magento team was "not able to reproduce the issue in Latest 2.4-develop instance" and requested further details like screenshots or screencasts.
This "cannot reproduce" status is significant. It suggests that the bug might be highly environment-specific, dependent on a very particular setup, or potentially resolved in a more recent commit to the 2.4-develop branch than the one initially reported. While no immediate solution or workaround was provided within the thread, the proposed release note – "Cross-host storefront canonical redirects omit the PHP session cookie" – hints at an understanding of the root cause and a potential future fix aimed at preventing session cookies from being set during these initial redirects.
Implications for Magento Users and Developers
For merchants and developers managing Magento 2 stores, especially those on the 2.4.x branch with 'Auto-redirect to Base URL' enabled, this issue serves as a valuable insight. While not universally reproducible, it highlights a potential vulnerability in session management that could lead to customer login failures and a degraded user experience. It underscores the importance of rigorous testing across various access points (e.g., with and without 'www', different subdomains) and careful monitoring of user session behavior. If you encounter similar login loops, investigating cookie domains and expiration might be a crucial first step.