Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelogs/v4.7.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
19 changes: 16 additions & 3 deletions system/Session/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
Expand Down
33 changes: 33 additions & 0 deletions tests/system/Session/SessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}
Loading