Skip to content

Commit 168eeca

Browse files
committed
Add support for SameSite cookie session setting
Starting from PHP 7.3 there's native support for SameSite cookies (RFC6265bis) which requires using a new session_get_cookie_params() parameter syntax
1 parent fd31f99 commit 168eeca

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

lib/response/sfWebResponse.class.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,11 @@ public function isHeaderOnly()
158158
* @param string $domain Domain name
159159
* @param bool $secure If secure
160160
* @param bool $httpOnly If uses only HTTP
161+
* @param string $samesite SameSite cookies
161162
*
162163
* @throws <b>sfException</b> If fails to set the cookie
163164
*/
164-
public function setCookie($name, $value, $expire = null, $path = '/', $domain = '', $secure = false, $httpOnly = false)
165+
public function setCookie($name, $value, $expire = null, $path = '/', $domain = '', $secure = false, $httpOnly = false, $samesite = '')
165166
{
166167
if ($expire !== null)
167168
{
@@ -187,6 +188,7 @@ public function setCookie($name, $value, $expire = null, $path = '/', $domain =
187188
'domain' => $domain,
188189
'secure' => $secure ? true : false,
189190
'httpOnly' => $httpOnly,
191+
'samesite' => $samesite
190192
);
191193
}
192194

@@ -365,7 +367,18 @@ public function sendHttpHeaders()
365367
// cookies
366368
foreach ($this->cookies as $cookie)
367369
{
368-
setrawcookie($cookie['name'], $cookie['value'], $cookie['expire'], $cookie['path'], $cookie['domain'], $cookie['secure'], $cookie['httpOnly']);
370+
if (PHP_VERSION_ID < 70300) {
371+
setrawcookie($cookie['name'], $cookie['value'], $cookie['expire'], $cookie['path'], $cookie['domain'], $cookie['secure'], $cookie['httpOnly']);
372+
} else {
373+
setrawcookie($cookie['name'], $cookie['value'], array(
374+
'expires' => $cookie['expire'],
375+
'path' => $cookie['path'],
376+
'domain' => $cookie['domain'],
377+
'secure' => $cookie['secure'],
378+
'httpOnly' => $cookie['httpOnly'],
379+
'samesite' => $cookie['samesite'],
380+
));
381+
}
369382

370383
if ($this->options['logging'])
371384
{

lib/storage/sfSessionStorage.class.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public function initialize($options = null)
6262
'session_cookie_domain' => $cookieDefaults['domain'],
6363
'session_cookie_secure' => $cookieDefaults['secure'],
6464
'session_cookie_httponly' => isset($cookieDefaults['httponly']) ? $cookieDefaults['httponly'] : false,
65+
'session_cookie_samesite' => isset($cookieDefaults['samesite']) ? $cookieDefaults['samesite'] : '',
6566
'session_cache_limiter' => null,
6667
'gc_maxlifetime' => 1800,
6768
), $options);
@@ -84,7 +85,19 @@ public function initialize($options = null)
8485
$domain = $this->options['session_cookie_domain'];
8586
$secure = $this->options['session_cookie_secure'];
8687
$httpOnly = $this->options['session_cookie_httponly'];
87-
session_set_cookie_params($lifetime, $path, $domain, $secure, $httpOnly);
88+
$samesite = $this->options['session_cookie_samesite'];
89+
if (PHP_VERSION_ID < 70300) {
90+
session_set_cookie_params($lifetime, $path, $domain, $secure, $httpOnly);
91+
} else {
92+
session_set_cookie_params(array(
93+
'lifetime' => $lifetime,
94+
'path' => $path,
95+
'domain' => $domain,
96+
'secure' => $secure,
97+
'httponly' => $httpOnly,
98+
'samesite' => $samesite
99+
));
100+
}
88101

89102
if (null !== $this->options['session_cache_limiter'])
90103
{

test/unit/response/sfWebResponseTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ public function normalizeHeaderName($name)
288288
// ->setCookie() ->getCookies()
289289
$t->diag('->setCookie() ->getCookies()');
290290
$response->setCookie('foo', 'bar');
291-
$t->is($response->getCookies(), array('foo' => array('name' => 'foo', 'value' => 'bar', 'expire' => null, 'path' => '/', 'domain' => '', 'secure' => false, 'httpOnly' => false)), '->setCookie() adds a cookie for the response');
291+
$t->is($response->getCookies(), array('foo' => array('name' => 'foo', 'value' => 'bar', 'expire' => null, 'path' => '/', 'domain' => '', 'secure' => false, 'httpOnly' => false, 'samesite' => '')), '->setCookie() adds a cookie for the response');
292292

293293
// ->setHeaderOnly() ->getHeaderOnly()
294294
$t->diag('->setHeaderOnly() ->isHeaderOnly()');

0 commit comments

Comments
 (0)