From 97110b37d6c0458468addbef71130e5c53743f98 Mon Sep 17 00:00:00 2001 From: Bogdan Date: Sat, 5 Dec 2026 21:29:35 +0100 Subject: [PATCH 1/2] fix(Session): destroy Redis and Memcached session records without a lock RedisHandler::destroy() and MemcachedHandler::destroy() only removed the session record when $lockKey was set. A handler that had not acquired the lock (no prior read(), or a lock already released) skipped the delete and returned false, leaving the record in the store and the session cookie in place. FileHandler and DatabaseHandler don't depend on the lock here. Remove the record whenever the connection is available, and always destroy the cookie through destroyCookie(). Adds regression tests: a MemcachedHandler test in the existing suite and a new RedisHandlerTest. Both fail on the previous code. Co-Authored-By: Claude Haiku 5.5 --- system/Session/Handlers/MemcachedHandler.php | 2 +- system/Session/Handlers/RedisHandler.php | 2 +- .../Session/Handlers/MemcachedHandlerTest.php | 23 ++++ .../Session/Handlers/RedisHandlerTest.php | 108 ++++++++++++++++++ 4 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 tests/system/Session/Handlers/RedisHandlerTest.php diff --git a/system/Session/Handlers/MemcachedHandler.php b/system/Session/Handlers/MemcachedHandler.php index ecb21f9e3880..18bc103dd27b 100644 --- a/system/Session/Handlers/MemcachedHandler.php +++ b/system/Session/Handlers/MemcachedHandler.php @@ -234,7 +234,7 @@ public function close(): bool */ public function destroy($id): bool { - if (isset($this->memcached, $this->lockKey)) { + if (isset($this->memcached)) { $this->memcached->delete($this->keyPrefix . $id); return $this->destroyCookie(); diff --git a/system/Session/Handlers/RedisHandler.php b/system/Session/Handlers/RedisHandler.php index 0ce94ce80791..3933e8c6aafd 100644 --- a/system/Session/Handlers/RedisHandler.php +++ b/system/Session/Handlers/RedisHandler.php @@ -320,7 +320,7 @@ public function close(): bool */ public function destroy($id): bool { - if (isset($this->redis, $this->lockKey)) { + if (isset($this->redis)) { if (($result = $this->redis->del($this->keyPrefix . $id)) !== 1) { $this->logger->debug( 'Session: Redis::del() expected to return 1, got ' . var_export($result, true) . ' instead.', diff --git a/tests/system/Session/Handlers/MemcachedHandlerTest.php b/tests/system/Session/Handlers/MemcachedHandlerTest.php index 08d64eaceac5..1c52c5854463 100644 --- a/tests/system/Session/Handlers/MemcachedHandlerTest.php +++ b/tests/system/Session/Handlers/MemcachedHandlerTest.php @@ -152,6 +152,29 @@ public function testReadEmptySession(): void $handler->close(); } + public function testDestroyRemovesRecordWithoutLock(): void + { + $writer = $this->getInstance(); + $writer->open($this->sessionSavePath, $this->sessionName); + + $sessionId = $this->generateSessionId('777777'); + + $this->assertSame('', $writer->read($sessionId)); + $this->assertTrue($writer->write($sessionId, '__ci_last_regenerate|i:1664607454;')); + $this->assertTrue($writer->close()); + + // A handler that never acquired the session lock must still remove the record. + $handler = $this->getInstance(); + $handler->open($this->sessionSavePath, $this->sessionName); + + $this->assertTrue($handler->destroy($sessionId)); + + $reader = $this->getInstance(); + $reader->open($this->sessionSavePath, $this->sessionName); + $this->assertSame('', $reader->read($sessionId)); + $this->assertTrue($reader->close()); + } + public function testGC(): void { $handler = $this->getInstance(); diff --git a/tests/system/Session/Handlers/RedisHandlerTest.php b/tests/system/Session/Handlers/RedisHandlerTest.php new file mode 100644 index 000000000000..9657b8d6b50d --- /dev/null +++ b/tests/system/Session/Handlers/RedisHandlerTest.php @@ -0,0 +1,108 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace CodeIgniter\Session\Handlers; + +use CodeIgniter\Test\CIUnitTestCase; +use CodeIgniter\Test\TestLogger; +use Config\Logger as LoggerConfig; +use Config\Session as SessionConfig; +use PHPUnit\Framework\Attributes\Group; +use PHPUnit\Framework\Attributes\RequiresPhpExtension; +use Redis; + +/** + * @internal + */ +#[Group('DatabaseLive')] +#[RequiresPhpExtension('redis')] +final class RedisHandlerTest extends CIUnitTestCase +{ + private string $sessionName = 'ci_session'; + private string $sessionSavePath = 'tcp://127.0.0.1:6379'; + private string $userIpAddress = '127.0.0.1'; + + /** + * @var list + */ + private array $sessionIds = []; + + protected function getInstance(): RedisHandler + { + $sessionConfig = new SessionConfig(); + $sessionConfig->driver = RedisHandler::class; + $sessionConfig->cookieName = $this->sessionName; + $sessionConfig->expiration = 7200; + $sessionConfig->savePath = $this->sessionSavePath; + $sessionConfig->matchIP = false; + $sessionConfig->timeToUpdate = 300; + $sessionConfig->regenerateDestroy = false; + + $handler = new RedisHandler($sessionConfig, $this->userIpAddress); + $handler->setLogger(new TestLogger(new LoggerConfig())); + + return $handler; + } + + protected function tearDown(): void + { + if ($this->sessionIds !== []) { + $redis = new Redis(); + $redis->connect('127.0.0.1', 6379); + + $prefix = 'ci_session:' . $this->sessionName . ':'; + + foreach ($this->sessionIds as $sessionId) { + $redis->del($prefix . $sessionId); + $redis->del($prefix . $sessionId . ':lock'); + } + + $this->sessionIds = []; + } + + parent::tearDown(); + + RedisHandler::resetPersistentConnections(); + } + + private function generateSessionId(string $prefix): string + { + $sessionId = $prefix . bin2hex(random_bytes(13)); + $this->sessionIds[] = $sessionId; + + return $sessionId; + } + + public function testDestroyRemovesRecordWithoutLock(): void + { + $writer = $this->getInstance(); + $writer->open($this->sessionSavePath, $this->sessionName); + + $sessionId = $this->generateSessionId('888888'); + + $this->assertSame('', $writer->read($sessionId)); + $this->assertTrue($writer->write($sessionId, '__ci_last_regenerate|i:1664607454;')); + $this->assertTrue($writer->close()); + + // A handler that never acquired the session lock must still remove the record. + $handler = $this->getInstance(); + $handler->open($this->sessionSavePath, $this->sessionName); + + $this->assertTrue($handler->destroy($sessionId)); + + $reader = $this->getInstance(); + $reader->open($this->sessionSavePath, $this->sessionName); + $this->assertSame('', $reader->read($sessionId)); + $this->assertTrue($reader->close()); + } +} From 47aa480bc7116504e9f7d0c7ec6822956051f982 Mon Sep 17 00:00:00 2001 From: Bogdan Date: Sat, 5 Dec 2026 21:43:49 +0100 Subject: [PATCH 2/2] test(Session): assert destroyed session record is removed from the store The regression tests read the session back through the handler, which only checks that the value is unreadable. Check the Memcached and Redis store directly so a record left behind by destroy() fails the test. --- tests/system/Session/Handlers/MemcachedHandlerTest.php | 10 ++++++---- tests/system/Session/Handlers/RedisHandlerTest.php | 9 +++++---- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/tests/system/Session/Handlers/MemcachedHandlerTest.php b/tests/system/Session/Handlers/MemcachedHandlerTest.php index 1c52c5854463..2a6935d27931 100644 --- a/tests/system/Session/Handlers/MemcachedHandlerTest.php +++ b/tests/system/Session/Handlers/MemcachedHandlerTest.php @@ -169,10 +169,12 @@ public function testDestroyRemovesRecordWithoutLock(): void $this->assertTrue($handler->destroy($sessionId)); - $reader = $this->getInstance(); - $reader->open($this->sessionSavePath, $this->sessionName); - $this->assertSame('', $reader->read($sessionId)); - $this->assertTrue($reader->close()); + // Check the store directly: the record must be gone, not just unreadable. + $memcached = new Memcached(); + $memcached->setOption(Memcached::OPT_BINARY_PROTOCOL, true); + $memcached->addServer('127.0.0.1', 11211); + + $this->assertFalse($memcached->get('ci_session:' . $this->sessionName . ':' . $sessionId)); } public function testGC(): void diff --git a/tests/system/Session/Handlers/RedisHandlerTest.php b/tests/system/Session/Handlers/RedisHandlerTest.php index 9657b8d6b50d..2f536ee7b601 100644 --- a/tests/system/Session/Handlers/RedisHandlerTest.php +++ b/tests/system/Session/Handlers/RedisHandlerTest.php @@ -100,9 +100,10 @@ public function testDestroyRemovesRecordWithoutLock(): void $this->assertTrue($handler->destroy($sessionId)); - $reader = $this->getInstance(); - $reader->open($this->sessionSavePath, $this->sessionName); - $this->assertSame('', $reader->read($sessionId)); - $this->assertTrue($reader->close()); + // Check the store directly: the record must be gone, not just unreadable. + $redis = new Redis(); + $redis->connect('127.0.0.1', 6379); + + $this->assertFalse($redis->get('ci_session:' . $this->sessionName . ':' . $sessionId)); } }