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. diff --git a/system/Session/Session.php b/system/Session/Session.php index 1cb47c2ee260..36408de497bb 100644 --- a/system/Session/Session.php +++ b/system/Session/Session.php @@ -108,16 +108,29 @@ 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); + + $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; } $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..a41271f45ea2 100644 --- a/tests/system/Session/SessionTest.php +++ b/tests/system/Session/SessionTest.php @@ -694,4 +694,37 @@ 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')); + } + + 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')); + } }