Skip to content

Commit 71883c6

Browse files
committed
Allow to specify the cookie type for appframework responses
In general it is good to set them to Lax. But also to give devs more control over them is not a bad thing. Helps with #21474 Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
1 parent dd0b965 commit 71883c6

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

lib/private/AppFramework/App.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,20 @@ public static function main(string $controllerName, string $methodName, DIContai
151151
if ($value['expireDate'] instanceof \DateTime) {
152152
$expireDate = $value['expireDate']->getTimestamp();
153153
}
154+
$sameSite = 'Lax';
155+
if (isset($value['sameSite'])) {
156+
$sameSite = $value['sameSite'];
157+
}
158+
154159
$io->setCookie(
155160
$name,
156161
$value['value'],
157162
$expireDate,
158163
$container->getServer()->getWebRoot(),
159164
null,
160165
$container->getServer()->getRequest()->getServerProtocol() === 'https',
161-
true
166+
true,
167+
$sameSite
162168
);
163169
}
164170

lib/private/AppFramework/Http/Output.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,20 @@ public function getHttpResponseCode() {
9292
* @param bool $secure
9393
* @param bool $httpOnly
9494
*/
95-
public function setCookie($name, $value, $expire, $path, $domain, $secure, $httpOnly) {
95+
public function setCookie($name, $value, $expire, $path, $domain, $secure, $httpOnly, $sameSite = 'Lax') {
9696
$path = $this->webRoot ? : '/';
97-
setcookie($name, $value, $expire, $path, $domain, $secure, $httpOnly);
97+
98+
if (PHP_VERSION_ID < 70300) {
99+
setcookie($name, $value, $expire, $path, $domain, $secure, $httpOnly);
100+
} else {
101+
setcookie($name, $value, [
102+
'expires' => $expire,
103+
'path' => $path,
104+
'domain' => $domain,
105+
'secure' => $secure,
106+
'httponly' => $httpOnly,
107+
'samesite' => $sameSite
108+
]);
109+
}
98110
}
99111
}

lib/public/AppFramework/Http/IOutput.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ public function setHttpResponseCode($code);
7272
* @param string $domain
7373
* @param bool $secure
7474
* @param bool $httpOnly
75+
* @param string $sameSite (added in 20)
7576
* @since 8.1.0
7677
*/
77-
public function setCookie($name, $value, $expire, $path, $domain, $secure, $httpOnly);
78+
public function setCookie($name, $value, $expire, $path, $domain, $secure, $httpOnly, $sameSite = 'Lax');
7879
}

lib/public/AppFramework/Http/Response.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,12 @@ public function cacheFor(int $cacheSeconds, bool $public = false) {
133133
* @param \DateTime|null $expireDate Date on that the cookie should expire, if set
134134
* to null cookie will be considered as session
135135
* cookie.
136+
* @param string $sameSite The samesite value of the cookie. Defaults to Lax. Other possibilities are Strict or None
136137
* @return $this
137138
* @since 8.0.0
138139
*/
139-
public function addCookie($name, $value, \DateTime $expireDate = null) {
140-
$this->cookies[$name] = ['value' => $value, 'expireDate' => $expireDate];
140+
public function addCookie($name, $value, \DateTime $expireDate = null, $sameSite = 'Lax') {
141+
$this->cookies[$name] = ['value' => $value, 'expireDate' => $expireDate, 'sameSite' => $sameSite];
141142
return $this;
142143
}
143144

0 commit comments

Comments
 (0)