Skip to content

Commit 80096f4

Browse files
committed
fix(Session): send SameSite attribute in BaseHandler::destroyCookie()
1 parent 3ad5144 commit 80096f4

3 files changed

Lines changed: 190 additions & 4 deletions

File tree

‎docs/changelogs/v4.7.7.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Release Date: Unreleased
1515
## Bugs Fixed
1616

1717
- **Security:** Fixed a bug where `Security::__construct()` threw a `TypeError` when the CSRF cookie was an array, causing an HTTP 500 response for affected requests.
18+
- **Session:** Fixed a bug where `BaseHandler::destroyCookie()` did not send the `SameSite` attribute in the `Set-Cookie` header, which could prevent browsers from properly clearing the session cookie.
1819
- **Worker Mode:** Fixed a bug where every request that ended with an uncaught exception, including a `404 Page Not Found` without a 404 override, called `exit()` in the exception handler. This ended the worker script, so FrankenPHP booted a new one, and the worker loop skipped its cleanup after the request. The exception handler now returns to the worker loop.
1920

2021
See the repo's [CHANGELOG.md](https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md) for a complete list of bugs fixed.

‎system/Session/Handlers/BaseHandler.php‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ abstract class BaseHandler implements SessionHandlerInterface
7070
*/
7171
protected $cookieSecure = false;
7272

73+
/**
74+
* Cookie SameSite setting.
75+
*
76+
* @var ''|'Lax'|'None'|'Strict'
77+
*/
78+
protected string $cookieSameSite = '';
79+
7380
/**
7481
* Cookie name to use.
7582
*
@@ -116,9 +123,10 @@ public function __construct(SessionConfig $config, string $ipAddress)
116123
$cookie = config(CookieConfig::class);
117124

118125
// Session cookies have no prefix.
119-
$this->cookieDomain = $cookie->domain;
120-
$this->cookiePath = $cookie->path;
121-
$this->cookieSecure = $cookie->secure;
126+
$this->cookieDomain = $cookie->domain;
127+
$this->cookiePath = $cookie->path;
128+
$this->cookieSecure = $cookie->secure;
129+
$this->cookieSameSite = $cookie->samesite;
122130

123131
$this->ipAddress = $ipAddress;
124132
}
@@ -132,7 +140,14 @@ protected function destroyCookie(): bool
132140
return setcookie(
133141
$this->cookieName,
134142
'',
135-
['expires' => 1, 'path' => $this->cookiePath, 'domain' => $this->cookieDomain, 'secure' => $this->cookieSecure, 'httponly' => true],
143+
[
144+
'expires' => 1,
145+
'path' => $this->cookiePath,
146+
'domain' => $this->cookieDomain,
147+
'secure' => $this->cookieSecure,
148+
'httponly' => true,
149+
'samesite' => $this->cookieSameSite === '' ? 'Lax' : ucfirst($this->cookieSameSite),
150+
],
136151
);
137152
}
138153

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace CodeIgniter\Session\Handlers;
15+
16+
use CodeIgniter\Config\Factories;
17+
use CodeIgniter\Test\CIUnitTestCase;
18+
use Config\Cookie as CookieConfig;
19+
use Config\Session as SessionConfig;
20+
use PHPUnit\Framework\Attributes\Group;
21+
22+
/**
23+
* Mock setcookie function in CodeIgniter\Session\Handlers namespace.
24+
*
25+
* @param array<string, mixed> $options
26+
*/
27+
function setcookie(string $name, string $value = '', array $options = []): bool
28+
{
29+
BaseHandlerTest::$lastSetCookieArgs = [
30+
'name' => $name,
31+
'value' => $value,
32+
'options' => $options,
33+
];
34+
35+
return true;
36+
}
37+
38+
/**
39+
* @internal
40+
*/
41+
#[Group('Others')]
42+
final class BaseHandlerTest extends CIUnitTestCase
43+
{
44+
/**
45+
* @var array{name: string, value: string, options: array<string, mixed>}|null
46+
*/
47+
public static ?array $lastSetCookieArgs = null;
48+
49+
protected function setUp(): void
50+
{
51+
parent::setUp();
52+
53+
self::$lastSetCookieArgs = null;
54+
Factories::reset('config');
55+
}
56+
57+
protected function tearDown(): void
58+
{
59+
parent::tearDown();
60+
61+
self::$lastSetCookieArgs = null;
62+
Factories::reset('config');
63+
}
64+
65+
private function createHandler(?CookieConfig $cookieConfig = null): BaseHandler
66+
{
67+
if ($cookieConfig instanceof CookieConfig) {
68+
Factories::injectMock('config', CookieConfig::class, $cookieConfig);
69+
}
70+
71+
$sessionConfig = new SessionConfig();
72+
$sessionConfig->cookieName = 'ci_session';
73+
74+
return new class ($sessionConfig, '127.0.0.1') extends BaseHandler {
75+
public function open(string $path, string $name): bool
76+
{
77+
return true;
78+
}
79+
80+
public function close(): bool
81+
{
82+
return true;
83+
}
84+
85+
public function read(string $id): string
86+
{
87+
return '';
88+
}
89+
90+
public function write(string $id, string $data): bool
91+
{
92+
return true;
93+
}
94+
95+
public function destroy(string $id): bool
96+
{
97+
return $this->destroyCookie();
98+
}
99+
100+
public function gc(int $maxLifetime): int
101+
{
102+
return 1;
103+
}
104+
};
105+
}
106+
107+
public function testConstructorInitializesCookieSameSiteFromConfig(): void
108+
{
109+
$cookieConfig = new CookieConfig();
110+
$cookieConfig->samesite = 'Strict';
111+
112+
$handler = $this->createHandler($cookieConfig);
113+
114+
$this->assertSame('Strict', $this->getPrivateProperty($handler, 'cookieSameSite'));
115+
}
116+
117+
public function testConstructorDefaultsToCookieConfigSameSite(): void
118+
{
119+
$handler = $this->createHandler();
120+
121+
$this->assertSame('Lax', $this->getPrivateProperty($handler, 'cookieSameSite'));
122+
}
123+
124+
public function testDestroyCookiePassesSameSiteInOptions(): void
125+
{
126+
$cookieConfig = new CookieConfig();
127+
$cookieConfig->domain = 'example.com';
128+
$cookieConfig->path = '/app';
129+
$cookieConfig->secure = true;
130+
$cookieConfig->samesite = 'Strict';
131+
132+
$handler = $this->createHandler($cookieConfig);
133+
$result = $handler->destroy('ci_session');
134+
135+
$this->assertTrue($result);
136+
$this->assertNotNull(self::$lastSetCookieArgs);
137+
$this->assertSame('ci_session', self::$lastSetCookieArgs['name']);
138+
$this->assertSame('', self::$lastSetCookieArgs['value']);
139+
140+
$options = self::$lastSetCookieArgs['options'];
141+
$this->assertSame(1, $options['expires']);
142+
$this->assertSame('/app', $options['path']);
143+
$this->assertSame('example.com', $options['domain']);
144+
$this->assertTrue($options['secure']);
145+
$this->assertTrue($options['httponly']);
146+
$this->assertSame('Strict', $options['samesite']);
147+
}
148+
149+
public function testDestroyCookieNormalizesEmptySameSiteToLax(): void
150+
{
151+
$cookieConfig = new CookieConfig();
152+
$cookieConfig->samesite = '';
153+
154+
$handler = $this->createHandler($cookieConfig);
155+
$handler->destroy('ci_session');
156+
157+
$this->assertNotNull(self::$lastSetCookieArgs);
158+
$this->assertSame('Lax', self::$lastSetCookieArgs['options']['samesite']);
159+
}
160+
161+
public function testDestroyCookieNormalizesLowercaseSameSite(): void
162+
{
163+
$handler = $this->createHandler();
164+
$this->setPrivateProperty($handler, 'cookieSameSite', 'none');
165+
$handler->destroy('ci_session');
166+
167+
$this->assertNotNull(self::$lastSetCookieArgs);
168+
$this->assertSame('None', self::$lastSetCookieArgs['options']['samesite']);
169+
}
170+
}

0 commit comments

Comments
 (0)