CakePHP 2.3 sets the Session variables (including cookie attributes) in the core.php file. I need to set samesite=None and Secure=true for the session cookie, but it doesn't appear to have those settings available in the configuration, which shows only the following options:
Session.cookie- The name of the cookie to use. Defaults to 'CAKEPHP'Session.timeout- The number of minutes you want sessions to live for. This timeout is handled by CakePHPSession.cookieTimeout- The number of minutes you want session cookies to live for.Session.checkAgent- Do you want the user agent to be checked when starting sessions? You might want to set the value to false, when dealing with older versions of IE, Chrome Frame or certain web-browsing devices and AJAXSession.defaults- The default configuration set to use as a basis for your session. There are four builtins: php, cake, cache, database.Session.handler- Can be used to enable a custom session handler. Expects an array of of callables, that can be used withsession_save_handler. Using this option will automatically addsession.save_handlerto the ini array.Session.autoRegenerate- Enabling this setting, turns on automatic renewal of sessions, and sessionids that change frequently. See CakeSession::$requestCountdown.Session.ini- An associative array of additional ini values to set.
This is how I have it now:
Configure::write('Session', array(
'defaults' => 'database',
'handler' => array('model' => 'cake_sessions'),
'timeout' => 60
));
Is there a workaround for this? I've been looking at how to do this with php but I'm not sure how I would edit the session cookie that CakePHP creates with the attributes I want, or if that is possible at all once the cookie has been created.
Before PHP 7.3
In PHP versions earlier than PHP 7.3, you can inject the
SameSiteattribute by utilizing the cookie path hack, which consists of appending further cookie attributes to the path, by simply closing the path of with a semicolon.Simply configure the
session.cookie_pathini option inapp/Config/core.phpaccordingly, for example like this in case your application's base path is/:The
Secureattribute (ie thesession.cookie_secureini option) will automatically be configured by CakePHP when you're visiting your site viahttps.As of PHP 7.3
In PHP versions as of PHP 7.3 you would use the
session.cookie_samesiteini option instead:Other cookies
All of this of course only applies to session cookies, if you're using additional cookies via the Cookie component, then you'd have to utilize the path hack there too, by modifying the
$pathproperty accordingly, and unlike with sessions, you'd have to explicitly enable secure cookies:With PHP 7.3+ you'd have to use a custom/extended cookie component, and an extended/custom response class where you'd override the
CookieComponent::_write(),CakeResponse::cookie()andCakeResponse::_setCookies()methods accordingly, so that the component allows to set an option for same site, and the response will pass it over to thesetcookie()call.Example:
Inject the custom response in the front controller:
Alias the
Cookiecomponent with the custom component class:and then configure the component accordingly before using it:
or use the response object directly to set your cookies: