From 97389e44216f40476f4417a460b82458f92580d7 Mon Sep 17 00:00:00 2001 From: banghamid <62824084+banghamid@users.noreply.github.com> Date: Wed, 11 Nov 2026 16:49:52 +0700 Subject: [PATCH 1/8] fix(Session): release lock state on close and check session ID in MemcachedHandler --- system/Session/Handlers/MemcachedHandler.php | 12 +++-- .../Session/Handlers/MemcachedHandlerTest.php | 44 +++++++++++++++++++ 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/system/Session/Handlers/MemcachedHandler.php b/system/Session/Handlers/MemcachedHandler.php index c64ba65f6454..460bcc21947c 100644 --- a/system/Session/Handlers/MemcachedHandler.php +++ b/system/Session/Handlers/MemcachedHandler.php @@ -221,8 +221,8 @@ public function write($id, $data): bool public function close(): bool { if (isset($this->memcached)) { - if (isset($this->lockKey)) { - $this->memcached->delete($this->lockKey); + if (isset($this->lockKey) && ! $this->releaseLock()) { + return false; } return true; @@ -265,11 +265,15 @@ public function gc($max_lifetime): int */ protected function lockSession(string $sessionID): bool { - if (isset($this->lockKey)) { + $lockKey = $this->keyPrefix . $sessionID . ':lock'; + + // PHP 7 reuses the SessionHandler object on regeneration, + // so we need to check here if the lock key is for the + // correct session ID. + if ($this->lockKey === $lockKey) { return $this->memcached->replace($this->lockKey, Time::now()->getTimestamp(), 300); } - $lockKey = $this->keyPrefix . $sessionID . ':lock'; $attempt = 0; do { diff --git a/tests/system/Session/Handlers/MemcachedHandlerTest.php b/tests/system/Session/Handlers/MemcachedHandlerTest.php index 8a24a7377722..b7a90c2f768f 100644 --- a/tests/system/Session/Handlers/MemcachedHandlerTest.php +++ b/tests/system/Session/Handlers/MemcachedHandlerTest.php @@ -128,4 +128,48 @@ public function testGC(): void $handler = $this->getInstance(); $this->assertSame(1, $handler->gc(3600)); } + + public function testReopenAndReadWithSameHandlerInstance(): void + { + $handler = $this->getInstance(); + $handler->open($this->sessionSavePath, $this->sessionName); + + $sessionId = '666666b43phsnnf8if6bo33b635e4448'; + + $this->assertSame('', $handler->read($sessionId)); + + $data = '__ci_last_regenerate|i:1664607454;key|s:5:"value";'; + $this->assertTrue($handler->write($sessionId, $data)); + $this->assertTrue($handler->close()); + + // Reopen with the same handler instance (e.g. session_write_close() + session_start()) + $this->assertTrue($handler->open($this->sessionSavePath, $this->sessionName)); + $this->assertSame($data, $handler->read($sessionId)); + $this->assertTrue($handler->close()); + } + + public function testRegenerateSessionIdWithSameHandlerInstance(): void + { + $handler = $this->getInstance(); + $handler->open($this->sessionSavePath, $this->sessionName); + + $oldSessionId = '777777b43phsnnf8if6bo33b635e4449'; + $newSessionId = '888888b43phsnnf8if6bo33b635e4450'; + + $this->assertSame('', $handler->read($oldSessionId)); + + $data = '__ci_last_regenerate|i:1664607454;role|s:5:"admin";'; + $this->assertTrue($handler->write($oldSessionId, $data)); + $this->assertTrue($handler->close()); + + // PHP reuses the same SessionHandler object on regeneration + $this->assertTrue($handler->open($this->sessionSavePath, $this->sessionName)); + $this->assertSame('', $handler->read($newSessionId)); + $this->assertTrue($handler->write($newSessionId, $data)); + $this->assertTrue($handler->close()); + + $this->assertTrue($handler->open($this->sessionSavePath, $this->sessionName)); + $this->assertSame($data, $handler->read($newSessionId)); + $this->assertTrue($handler->close()); + } } From 4715db25f3ce2355864302a165891c6e8c5f6581 Mon Sep 17 00:00:00 2001 From: banghamid <62824084+banghamid@users.noreply.github.com> Date: Thu, 12 Nov 2026 01:52:57 +0700 Subject: [PATCH 2/8] test(Session): improve MemcachedHandler lock regeneration tests --- system/Session/Handlers/MemcachedHandler.php | 2 +- .../Session/Handlers/MemcachedHandlerTest.php | 65 ++++++++++++++++++- 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/system/Session/Handlers/MemcachedHandler.php b/system/Session/Handlers/MemcachedHandler.php index 460bcc21947c..5f71800db014 100644 --- a/system/Session/Handlers/MemcachedHandler.php +++ b/system/Session/Handlers/MemcachedHandler.php @@ -267,7 +267,7 @@ protected function lockSession(string $sessionID): bool { $lockKey = $this->keyPrefix . $sessionID . ':lock'; - // PHP 7 reuses the SessionHandler object on regeneration, + // PHP reuses the SessionHandler object on regeneration, // so we need to check here if the lock key is for the // correct session ID. if ($this->lockKey === $lockKey) { diff --git a/tests/system/Session/Handlers/MemcachedHandlerTest.php b/tests/system/Session/Handlers/MemcachedHandlerTest.php index b7a90c2f768f..d00489d61ce5 100644 --- a/tests/system/Session/Handlers/MemcachedHandlerTest.php +++ b/tests/system/Session/Handlers/MemcachedHandlerTest.php @@ -18,6 +18,7 @@ use CodeIgniter\Test\TestLogger; use Config\Logger as LoggerConfig; use Config\Session as SessionConfig; +use Memcached; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\Attributes\RequiresPhpExtension; @@ -33,6 +34,11 @@ final class MemcachedHandlerTest extends CIUnitTestCase private string $sessionSavePath = '127.0.0.1:11211'; private string $userIpAddress = '127.0.0.1'; + /** + * @var list + */ + private array $sessionIds = []; + /** * @param array $options Replace values for `Config\Session`. */ @@ -62,11 +68,34 @@ protected function getInstance($options = []): MemcachedHandler protected function tearDown(): void { + if ($this->sessionIds !== []) { + $memcached = new Memcached(); + $memcached->setOption(Memcached::OPT_BINARY_PROTOCOL, true); + $memcached->addServer('127.0.0.1', 11211); + + $prefix = 'ci_session:' . $this->sessionName . ':'; + + foreach ($this->sessionIds as $sessionId) { + $memcached->delete($prefix . $sessionId); + $memcached->delete($prefix . $sessionId . ':lock'); + } + + $this->sessionIds = []; + } + parent::tearDown(); MemcachedHandler::resetPersistentConnections(); } + private function generateSessionId(string $prefix): string + { + $sessionId = $prefix . bin2hex(random_bytes(13)); + $this->sessionIds[] = $sessionId; + + return $sessionId; + } + public function testConstructorThrowsWithEmptySavePath(): void { $this->expectException(SessionException::class); @@ -134,7 +163,7 @@ public function testReopenAndReadWithSameHandlerInstance(): void $handler = $this->getInstance(); $handler->open($this->sessionSavePath, $this->sessionName); - $sessionId = '666666b43phsnnf8if6bo33b635e4448'; + $sessionId = $this->generateSessionId('666666'); $this->assertSame('', $handler->read($sessionId)); @@ -153,8 +182,8 @@ public function testRegenerateSessionIdWithSameHandlerInstance(): void $handler = $this->getInstance(); $handler->open($this->sessionSavePath, $this->sessionName); - $oldSessionId = '777777b43phsnnf8if6bo33b635e4449'; - $newSessionId = '888888b43phsnnf8if6bo33b635e4450'; + $oldSessionId = $this->generateSessionId('777777'); + $newSessionId = $this->generateSessionId('888888'); $this->assertSame('', $handler->read($oldSessionId)); @@ -172,4 +201,34 @@ public function testRegenerateSessionIdWithSameHandlerInstance(): void $this->assertSame($data, $handler->read($newSessionId)); $this->assertTrue($handler->close()); } + + public function testRegenerateSessionIdWriteWithoutClose(): void + { + $handler = $this->getInstance(); + $handler->open($this->sessionSavePath, $this->sessionName); + + $oldSessionId = $this->generateSessionId('999999'); + $newSessionId = $this->generateSessionId('aaaaaa'); + + $this->assertSame('', $handler->read($oldSessionId)); + + $oldData = '__ci_last_regenerate|i:1664607454;role|s:5:"guest";'; + $this->assertTrue($handler->write($oldSessionId, $oldData)); + + // During PHP session_regenerate_id(), write() is called with the new ID + // while the lock for the old session ID is still held. + $newData = '__ci_last_regenerate|i:1664607500;role|s:5:"admin";'; + $this->assertTrue($handler->write($newSessionId, $newData)); + $this->assertTrue($handler->close()); + + // Verify the old lock was released and the new session data is readable + $handler2 = $this->getInstance(); + $this->assertTrue($handler2->open($this->sessionSavePath, $this->sessionName)); + $this->assertSame($oldData, $handler2->read($oldSessionId)); + $this->assertTrue($handler2->close()); + + $this->assertTrue($handler2->open($this->sessionSavePath, $this->sessionName)); + $this->assertSame($newData, $handler2->read($newSessionId)); + $this->assertTrue($handler2->close()); + } } From 2e9668d64d233a652152ff1140dbf9ad56f9dd42 Mon Sep 17 00:00:00 2001 From: banghamid <62824084+banghamid@users.noreply.github.com> Date: Thu, 12 Nov 2026 05:26:07 +0700 Subject: [PATCH 3/8] style(Session): simplify close() return in MemcachedHandler --- system/Session/Handlers/MemcachedHandler.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/system/Session/Handlers/MemcachedHandler.php b/system/Session/Handlers/MemcachedHandler.php index 5f71800db014..7b43bc31bda4 100644 --- a/system/Session/Handlers/MemcachedHandler.php +++ b/system/Session/Handlers/MemcachedHandler.php @@ -221,11 +221,7 @@ public function write($id, $data): bool public function close(): bool { if (isset($this->memcached)) { - if (isset($this->lockKey) && ! $this->releaseLock()) { - return false; - } - - return true; + return ! isset($this->lockKey) || $this->releaseLock(); } return false; From b408f256680d077a92b946bb339df099723e7b8a Mon Sep 17 00:00:00 2001 From: banghamid <62824084+banghamid@users.noreply.github.com> Date: Thu, 12 Nov 2026 07:15:11 +0700 Subject: [PATCH 4/8] trigger ci From 34aa9927eaad40712d2a63a222744429d7e85598 Mon Sep 17 00:00:00 2001 From: banghamid <62824084+banghamid@users.noreply.github.com> Date: Thu, 12 Nov 2026 07:16:31 +0700 Subject: [PATCH 5/8] trigger ci From 32eed809ac45d251d40efea86fd38fc327a8fac5 Mon Sep 17 00:00:00 2001 From: banghamid <62824084+banghamid@users.noreply.github.com> Date: Sat, 14 Nov 2026 20:00:38 +0700 Subject: [PATCH 6/8] fix(Session): release previous lock before acquiring new one in MemcachedHandler --- system/Session/Handlers/MemcachedHandler.php | 4 +++ .../Session/Handlers/MemcachedHandlerTest.php | 27 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/system/Session/Handlers/MemcachedHandler.php b/system/Session/Handlers/MemcachedHandler.php index 7b43bc31bda4..ecb21f9e3880 100644 --- a/system/Session/Handlers/MemcachedHandler.php +++ b/system/Session/Handlers/MemcachedHandler.php @@ -270,6 +270,10 @@ protected function lockSession(string $sessionID): bool return $this->memcached->replace($this->lockKey, Time::now()->getTimestamp(), 300); } + if (isset($this->lockKey) && ! $this->releaseLock()) { + return false; + } + $attempt = 0; do { diff --git a/tests/system/Session/Handlers/MemcachedHandlerTest.php b/tests/system/Session/Handlers/MemcachedHandlerTest.php index d00489d61ce5..47cfa95b7361 100644 --- a/tests/system/Session/Handlers/MemcachedHandlerTest.php +++ b/tests/system/Session/Handlers/MemcachedHandlerTest.php @@ -231,4 +231,31 @@ public function testRegenerateSessionIdWriteWithoutClose(): void $this->assertSame($newData, $handler2->read($newSessionId)); $this->assertTrue($handler2->close()); } + + public function testReadDifferentSessionIdsWithoutCloseReleasesPreviousLock(): void + { + $handler = $this->getInstance(); + $handler->open($this->sessionSavePath, $this->sessionName); + + $firstSessionId = $this->generateSessionId('bbbbbb'); + $secondSessionId = $this->generateSessionId('cccccc'); + + $firstData = '__ci_last_regenerate|i:1664607454;user|s:5:"alice";'; + $this->assertSame('', $handler->read($firstSessionId)); + $this->assertTrue($handler->write($firstSessionId, $firstData)); + $this->assertTrue($handler->close()); + + // Read two different session IDs on the same handler without closing between them + $this->assertTrue($handler->open($this->sessionSavePath, $this->sessionName)); + $this->assertSame($firstData, $handler->read($firstSessionId)); + $this->assertSame('', $handler->read($secondSessionId)); + + // Another handler should be able to acquire the lock for the first session ID + $handler2 = $this->getInstance(); + $this->assertTrue($handler2->open($this->sessionSavePath, $this->sessionName)); + $this->assertSame($firstData, $handler2->read($firstSessionId)); + $this->assertTrue($handler2->close()); + + $this->assertTrue($handler->close()); + } } From eafeef68cb684a7e84dbb6389107858c1f1449dd Mon Sep 17 00:00:00 2001 From: banghamid <62824084+banghamid@users.noreply.github.com> Date: Sun, 15 Nov 2026 13:14:48 +0700 Subject: [PATCH 7/8] test(Session): assert lock keys directly in MemcachedHandlerTest --- .../Session/Handlers/MemcachedHandlerTest.php | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/tests/system/Session/Handlers/MemcachedHandlerTest.php b/tests/system/Session/Handlers/MemcachedHandlerTest.php index 47cfa95b7361..08d64eaceac5 100644 --- a/tests/system/Session/Handlers/MemcachedHandlerTest.php +++ b/tests/system/Session/Handlers/MemcachedHandlerTest.php @@ -215,13 +215,28 @@ public function testRegenerateSessionIdWriteWithoutClose(): void $oldData = '__ci_last_regenerate|i:1664607454;role|s:5:"guest";'; $this->assertTrue($handler->write($oldSessionId, $oldData)); - // During PHP session_regenerate_id(), write() is called with the new ID - // while the lock for the old session ID is still held. + $memcached = $this->getPrivateProperty($handler, 'memcached'); + $this->assertInstanceOf(Memcached::class, $memcached); + + $prefix = 'ci_session:' . $this->sessionName . ':'; + $memcached->get($prefix . $oldSessionId . ':lock'); + $this->assertSame(Memcached::RES_SUCCESS, $memcached->getResultCode()); + + // Exercise write() with a different session ID without closing the handler. $newData = '__ci_last_regenerate|i:1664607500;role|s:5:"admin";'; $this->assertTrue($handler->write($newSessionId, $newData)); + + $this->assertFalse($memcached->get($prefix . $oldSessionId . ':lock')); + $this->assertSame(Memcached::RES_NOTFOUND, $memcached->getResultCode()); + + $memcached->get($prefix . $newSessionId . ':lock'); + $this->assertSame(Memcached::RES_SUCCESS, $memcached->getResultCode()); + $this->assertTrue($handler->close()); + $this->assertFalse($memcached->get($prefix . $newSessionId . ':lock')); + $this->assertSame(Memcached::RES_NOTFOUND, $memcached->getResultCode()); - // Verify the old lock was released and the new session data is readable + // Verify that both session IDs retain their respective data. $handler2 = $this->getInstance(); $this->assertTrue($handler2->open($this->sessionSavePath, $this->sessionName)); $this->assertSame($oldData, $handler2->read($oldSessionId)); From 08ace1be81379973c98f27f809e69985e78a1e61 Mon Sep 17 00:00:00 2001 From: banghamid <62824084+banghamid@users.noreply.github.com> Date: Sun, 15 Nov 2026 23:48:09 +0700 Subject: [PATCH 8/8] docs: add changelog entry for MemcachedHandler session lock fix --- 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 5a5ea3539895..ad7bd92d967f 100644 --- a/docs/changelogs/v4.7.7.md +++ b/docs/changelogs/v4.7.7.md @@ -35,6 +35,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 `MemcachedHandler` did not reset lock state in `close()` or release an existing lock for a different session ID in `lockSession()`, causing subsequent reads and session ID regeneration on the same handler instance to fail. - **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.