From 8df2b00130e569b134143275966afdfae9dd8e31 Mon Sep 17 00:00:00 2001 From: banghamid <62824084+banghamid@users.noreply.github.com> Date: Wed, 11 Nov 2026 16:40:06 +0700 Subject: [PATCH 1/3] fix(Session): sync Request cookie global when unsetting invalid session cookie --- system/Session/Session.php | 9 ++++++--- tests/system/Session/SessionTest.php | 13 +++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/system/Session/Session.php b/system/Session/Session.php index 1cb47c2ee260..455097bc6686 100644 --- a/system/Session/Session.php +++ b/system/Session/Session.php @@ -108,16 +108,19 @@ public function start() $this->setSaveHandler(); // Sanitize the cookie, because apparently PHP doesn't do that for userspace handlers - $cookie = service('superglobals')->cookie($this->config->cookieName); + $superglobals = service('superglobals'); + $cookie = $superglobals->cookie($this->config->cookieName); if (isset($cookie) && (! is_string($cookie) || preg_match('#\A' . $this->sidRegexp . '\z#', $cookie) !== 1)) { - service('superglobals')->unsetCookie($this->config->cookieName); + $superglobals->unsetCookie($this->config->cookieName); + service('request')->setGlobal('cookie', $superglobals->getCookieArray()); + $cookie = null; } $this->startSession(); // Is session ID auto-regeneration configured? (ignoring ajax requests) - $requestedWith = service('superglobals')->server('HTTP_X_REQUESTED_WITH'); + $requestedWith = $superglobals->server('HTTP_X_REQUESTED_WITH'); if (($requestedWith === null || strtolower($requestedWith) !== 'xmlhttprequest') && ($regenerateTime = $this->config->timeToUpdate) > 0 ) { diff --git a/tests/system/Session/SessionTest.php b/tests/system/Session/SessionTest.php index 91ed72173097..b0c290b1cb27 100644 --- a/tests/system/Session/SessionTest.php +++ b/tests/system/Session/SessionTest.php @@ -694,4 +694,17 @@ public function testExpiresOnClose(): void $this->assertCount(1, $cookies); $this->assertSame(0, $cookies[0]->getExpiresTimestamp()); } + + public function testStartUnsetsInvalidSessionCookieInSuperglobalsAndRequest(): void + { + service('superglobals')->setCookie('ci_session', 'invalid-session-id!'); + + $this->assertSame('invalid-session-id!', service('request')->getCookie('ci_session')); + + $session = $this->getInstance(); + $session->start(); + + $this->assertNull(service('superglobals')->cookie('ci_session')); + $this->assertNull(service('request')->getCookie('ci_session')); + } } From 66e6d23e3fe09b585d58562f7bd9edb8ad9f0fb6 Mon Sep 17 00:00:00 2001 From: banghamid <62824084+banghamid@users.noreply.github.com> Date: Sat, 14 Nov 2026 21:39:26 +0700 Subject: [PATCH 2/3] fix(Session): scope invalid session cookie removal in Request --- system/Session/Session.php | 12 +++++++++++- tests/system/Session/SessionTest.php | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/system/Session/Session.php b/system/Session/Session.php index 455097bc6686..36408de497bb 100644 --- a/system/Session/Session.php +++ b/system/Session/Session.php @@ -113,7 +113,17 @@ public function start() if (isset($cookie) && (! is_string($cookie) || preg_match('#\A' . $this->sidRegexp . '\z#', $cookie) !== 1)) { $superglobals->unsetCookie($this->config->cookieName); - service('request')->setGlobal('cookie', $superglobals->getCookieArray()); + + $request = service('request'); + $requestCookies = $request->getCookie(); + + if (is_array($requestCookies) && array_key_exists($this->config->cookieName, $requestCookies)) { + $superglobalsCookies = $superglobals->getCookieArray(); + unset($requestCookies[$this->config->cookieName]); + $request->setGlobal('cookie', $requestCookies); + $superglobals->setCookieArray($superglobalsCookies); + } + $cookie = null; } diff --git a/tests/system/Session/SessionTest.php b/tests/system/Session/SessionTest.php index b0c290b1cb27..a41271f45ea2 100644 --- a/tests/system/Session/SessionTest.php +++ b/tests/system/Session/SessionTest.php @@ -707,4 +707,24 @@ public function testStartUnsetsInvalidSessionCookieInSuperglobalsAndRequest(): v $this->assertNull(service('superglobals')->cookie('ci_session')); $this->assertNull(service('request')->getCookie('ci_session')); } + + public function testStartScopesInvalidSessionCookieRemovalToSessionCookie(): void + { + $superglobals = service('superglobals'); + $superglobals->setCookie('ci_session', 'invalid-session-id!'); + $superglobals->setCookie('other_cookie', 'initial-value'); + + $request = service('request'); + $this->assertSame('initial-value', $request->getCookie('other_cookie')); + + $superglobals->setCookie('other_cookie', 'updated-value'); + + $session = $this->getInstance(); + $session->start(); + + $this->assertNull($superglobals->cookie('ci_session')); + $this->assertNull($request->getCookie('ci_session')); + $this->assertSame('initial-value', $request->getCookie('other_cookie')); + $this->assertSame('updated-value', $superglobals->cookie('other_cookie')); + } } From 3e06b701d381f3afadc66344ff0bc50a2afa41a6 Mon Sep 17 00:00:00 2001 From: banghamid <62824084+banghamid@users.noreply.github.com> Date: Sun, 15 Nov 2026 22:05:49 +0700 Subject: [PATCH 3/3] docs: add changelog entry for Session::start() invalid cookie removal --- docs/changelogs/v4.7.7.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/changelogs/v4.7.7.md b/docs/changelogs/v4.7.7.md index d817d9932f94..0d1e299b0fa6 100644 --- a/docs/changelogs/v4.7.7.md +++ b/docs/changelogs/v4.7.7.md @@ -34,6 +34,7 @@ Release Date: Unreleased - **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. - **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. - **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. +- **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`. - **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. See the repo's [CHANGELOG.md](https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md) for a complete list of bugs fixed.