Skip to content

fix(Session): sync Request cookie global when unsetting invalid session cookie - #10759

Open
banghamid wants to merge 1 commit into
codeigniter4:developfrom
banghamid:fix/session-sync-request-cookie
Open

banghamid wants to merge 1 commit into
codeigniter4:developfrom
banghamid:fix/session-sync-request-cookie

Conversation

@banghamid

Copy link
Copy Markdown
Contributor

Description
In Session::start(), when an incoming session cookie fails validation (! is_string($cookie) || preg_match('#\A' . $this->sidRegexp . '\z#', $cookie) !== 1), it is removed via service('superglobals')->unsetCookie($this->config->cookieName).

However, if service('request')->getCookie() was called prior to $session->start() (for example, in a before filter such as CSRF), Request::$globals['cookie'] has already cached the cookie array. Because Session::start() did not update Request::setGlobal('cookie', ...) (unlike Security::removeTokenInRequest()), subsequent calls to $request->getCookie($this->config->cookieName) continued returning the invalid session cookie value.

This PR:

  • Synchronizes service('request')->setGlobal('cookie', $superglobals->getCookieArray()) and resets $cookie = null when Session::start() unsets an invalid session cookie.
  • Adds testStartUnsetsInvalidSessionCookieInSuperglobalsAndRequest() in SessionTest.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@carson-codeigniter4 carson-codeigniter4 Bot added the bug Verified issues on the current code behavior or pull requests that will fix them label Nov 11, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Verified issues on the current code behavior or pull requests that will fix them

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants