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..2a6935d27931 100644 --- a/tests/system/Session/Handlers/MemcachedHandlerTest.php +++ b/tests/system/Session/Handlers/MemcachedHandlerTest.php @@ -152,6 +152,31 @@ 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)); + + // 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 { $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..2f536ee7b601 --- /dev/null +++ b/tests/system/Session/Handlers/RedisHandlerTest.php @@ -0,0 +1,109 @@ + + * + * 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)); + + // 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)); + } +}