Skip to content

Commit 07b812e

Browse files
authored
fix(Session): sync Request cookie global when unsetting invalid session cookie (#10759)
1 parent ae19ffd commit 07b812e

3 files changed

Lines changed: 50 additions & 3 deletions

File tree

‎docs/changelogs/v4.7.7.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Release Date: Unreleased
3434
- **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.
3535
- **Security:** Fixed a bug in `strip_image_tags()` where an invalid PCRE character class caused attribute injection and improper quote handling, and ensured tags without `src` are stripped.
3636
- **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.
37+
- **Session:** Fixed a bug where `Session::start()` did not remove an invalid session cookie from `Request`'s cached cookie global if cookies had already been read from `Request`.
3738
- **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.
3839

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

‎system/Session/Session.php‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,29 @@ public function start()
108108
$this->setSaveHandler();
109109

110110
// Sanitize the cookie, because apparently PHP doesn't do that for userspace handlers
111-
$cookie = service('superglobals')->cookie($this->config->cookieName);
111+
$superglobals = service('superglobals');
112+
$cookie = $superglobals->cookie($this->config->cookieName);
112113

113114
if (isset($cookie) && (! is_string($cookie) || preg_match('#\A' . $this->sidRegexp . '\z#', $cookie) !== 1)) {
114-
service('superglobals')->unsetCookie($this->config->cookieName);
115+
$superglobals->unsetCookie($this->config->cookieName);
116+
117+
$request = service('request');
118+
$requestCookies = $request->getCookie();
119+
120+
if (is_array($requestCookies) && array_key_exists($this->config->cookieName, $requestCookies)) {
121+
$superglobalsCookies = $superglobals->getCookieArray();
122+
unset($requestCookies[$this->config->cookieName]);
123+
$request->setGlobal('cookie', $requestCookies);
124+
$superglobals->setCookieArray($superglobalsCookies);
125+
}
126+
127+
$cookie = null;
115128
}
116129

117130
$this->startSession();
118131

119132
// Is session ID auto-regeneration configured? (ignoring ajax requests)
120-
$requestedWith = service('superglobals')->server('HTTP_X_REQUESTED_WITH');
133+
$requestedWith = $superglobals->server('HTTP_X_REQUESTED_WITH');
121134
if (($requestedWith === null || strtolower($requestedWith) !== 'xmlhttprequest')
122135
&& ($regenerateTime = $this->config->timeToUpdate) > 0
123136
) {

‎tests/system/Session/SessionTest.php‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,4 +694,37 @@ public function testExpiresOnClose(): void
694694
$this->assertCount(1, $cookies);
695695
$this->assertSame(0, $cookies[0]->getExpiresTimestamp());
696696
}
697+
698+
public function testStartUnsetsInvalidSessionCookieInSuperglobalsAndRequest(): void
699+
{
700+
service('superglobals')->setCookie('ci_session', 'invalid-session-id!');
701+
702+
$this->assertSame('invalid-session-id!', service('request')->getCookie('ci_session'));
703+
704+
$session = $this->getInstance();
705+
$session->start();
706+
707+
$this->assertNull(service('superglobals')->cookie('ci_session'));
708+
$this->assertNull(service('request')->getCookie('ci_session'));
709+
}
710+
711+
public function testStartScopesInvalidSessionCookieRemovalToSessionCookie(): void
712+
{
713+
$superglobals = service('superglobals');
714+
$superglobals->setCookie('ci_session', 'invalid-session-id!');
715+
$superglobals->setCookie('other_cookie', 'initial-value');
716+
717+
$request = service('request');
718+
$this->assertSame('initial-value', $request->getCookie('other_cookie'));
719+
720+
$superglobals->setCookie('other_cookie', 'updated-value');
721+
722+
$session = $this->getInstance();
723+
$session->start();
724+
725+
$this->assertNull($superglobals->cookie('ci_session'));
726+
$this->assertNull($request->getCookie('ci_session'));
727+
$this->assertSame('initial-value', $request->getCookie('other_cookie'));
728+
$this->assertSame('updated-value', $superglobals->cookie('other_cookie'));
729+
}
697730
}

0 commit comments

Comments
 (0)