Skip to content
Open
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
2 changes: 1 addition & 1 deletion system/Session/Handlers/MemcachedHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion system/Session/Handlers/RedisHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand Down
25 changes: 25 additions & 0 deletions tests/system/Session/Handlers/MemcachedHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
109 changes: 109 additions & 0 deletions tests/system/Session/Handlers/RedisHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* 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<string>
*/
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));
}
}
Loading