Securing Cookies in Magento on an SSL Site
Magento’s cookie handling is designed to ensure the security of your e-commerce site, but sometimes it can lead to confusion during PCI audits. Let me walk you through this situation step by step.
Understanding the Magento Cookie Handling Architecture
In Magento, cookies are managed by the Mage_Core_Model_Cookie class located in /app/code/core/Mage/Core/Model/Cookie.php. The crucial part of this model is the setting of a cookie using the PHP native setcookie() function.
public function set($name, $value)
{
//...
if (is_null($secure)) {
$secure = $this->isSecure();
}
if (is_null($httponly)) {
$httponly = $this->getHttponly();
}
setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);
}
The isSecure() Function - Where the Logic Lies
The key to this puzzle lies in the isSecure() function used in the code above. This function determines whether the current request is secure (i.e., using HTTPS).
private function isSecure()
{
return $this->_request->getServer('HTTPS') == '1';
}
In a properly configured Magento site with an SSL certificate, this function should return true when called.
Why the Cookie Doesn’t Contain the Text ‘secure’
The setcookie() function in PHP does not require you to explicitly specify the word ‘secure’ in the cookie. Instead, it uses the boolean value returned by $secure parameter.
If $secure is true (which happens when your site is accessed over HTTPS), the browser will only send this cookie back if the request is also secure. This is why you see the HttpOnly flag set but no explicit ‘secure’ keyword in the actual cookie header:
frontend=sj4j9kltv7nc00gk8s0i81koi3; expires=Thu, 06-Nov-2014 23:39:11 GMT;
path=/; domain=www.mydomaine.com; HttpOnly
Ensuring Your Site is Configured Correctly
To resolve this issue and pass your PCI audit:
-
Check your base URL settings: Go to System > Configuration > General > Web. Ensure both the ‘Base URL (Secure)’ and ‘Base URL (Unsecure)’ are set to point at your HTTPS site.
-
Verify your .htaccess file: The
.htaccessfile should redirect all HTTP requests to HTTPS usingRewriteEngine Onfollowed by:
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.mydomaine.com/$1 [R=301,L]
- Update your cookie domain: In System > Configuration > General > Web, ensure the ‘Cookie Domain’ matches your SSL certificate’s domain.
Conclusion
Magento’s cookie handling is designed to be secure and PCI compliant when configured correctly. The absence of the explicit ‘secure’ keyword in the cookie header does not mean it isn’t secure; it means Magento is using the correct PHP function with the right parameters based on your site’s configuration.
If you’re still having issues, consider reaching out to a professional Magento development team at LaravelCompany. They can help audit your system and ensure all components are working together securely.