Skip to content

Commit af05a29

Browse files
committed
test(Session): improve MemcachedHandler lock regeneration tests
1 parent 73d2e27 commit af05a29

2 files changed

Lines changed: 63 additions & 4 deletions

File tree

‎system/Session/Handlers/MemcachedHandler.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ protected function lockSession(string $sessionID): bool
267267
{
268268
$lockKey = $this->keyPrefix . $sessionID . ':lock';
269269

270-
// PHP 7 reuses the SessionHandler object on regeneration,
270+
// PHP reuses the SessionHandler object on regeneration,
271271
// so we need to check here if the lock key is for the
272272
// correct session ID.
273273
if ($this->lockKey === $lockKey) {

‎tests/system/Session/Handlers/MemcachedHandlerTest.php‎

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use CodeIgniter\Test\TestLogger;
1919
use Config\Logger as LoggerConfig;
2020
use Config\Session as SessionConfig;
21+
use Memcached;
2122
use PHPUnit\Framework\Attributes\Group;
2223
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
2324

@@ -33,6 +34,11 @@ final class MemcachedHandlerTest extends CIUnitTestCase
3334
private string $sessionSavePath = '127.0.0.1:11211';
3435
private string $userIpAddress = '127.0.0.1';
3536

37+
/**
38+
* @var list<string>
39+
*/
40+
private array $sessionIds = [];
41+
3642
/**
3743
* @param array<string, bool|int|string|null> $options Replace values for `Config\Session`.
3844
*/
@@ -62,11 +68,34 @@ protected function getInstance($options = []): MemcachedHandler
6268

6369
protected function tearDown(): void
6470
{
71+
if ($this->sessionIds !== []) {
72+
$memcached = new Memcached();
73+
$memcached->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
74+
$memcached->addServer('127.0.0.1', 11211);
75+
76+
$prefix = 'ci_session:' . $this->sessionName . ':';
77+
78+
foreach ($this->sessionIds as $sessionId) {
79+
$memcached->delete($prefix . $sessionId);
80+
$memcached->delete($prefix . $sessionId . ':lock');
81+
}
82+
83+
$this->sessionIds = [];
84+
}
85+
6586
parent::tearDown();
6687

6788
MemcachedHandler::resetPersistentConnections();
6889
}
6990

91+
private function generateSessionId(string $prefix): string
92+
{
93+
$sessionId = $prefix . bin2hex(random_bytes(13));
94+
$this->sessionIds[] = $sessionId;
95+
96+
return $sessionId;
97+
}
98+
7099
public function testConstructorThrowsWithEmptySavePath(): void
71100
{
72101
$this->expectException(SessionException::class);
@@ -134,7 +163,7 @@ public function testReopenAndReadWithSameHandlerInstance(): void
134163
$handler = $this->getInstance();
135164
$handler->open($this->sessionSavePath, $this->sessionName);
136165

137-
$sessionId = '666666b43phsnnf8if6bo33b635e4448';
166+
$sessionId = $this->generateSessionId('666666');
138167

139168
$this->assertSame('', $handler->read($sessionId));
140169

@@ -153,8 +182,8 @@ public function testRegenerateSessionIdWithSameHandlerInstance(): void
153182
$handler = $this->getInstance();
154183
$handler->open($this->sessionSavePath, $this->sessionName);
155184

156-
$oldSessionId = '777777b43phsnnf8if6bo33b635e4449';
157-
$newSessionId = '888888b43phsnnf8if6bo33b635e4450';
185+
$oldSessionId = $this->generateSessionId('777777');
186+
$newSessionId = $this->generateSessionId('888888');
158187

159188
$this->assertSame('', $handler->read($oldSessionId));
160189

@@ -172,4 +201,34 @@ public function testRegenerateSessionIdWithSameHandlerInstance(): void
172201
$this->assertSame($data, $handler->read($newSessionId));
173202
$this->assertTrue($handler->close());
174203
}
204+
205+
public function testRegenerateSessionIdWriteWithoutClose(): void
206+
{
207+
$handler = $this->getInstance();
208+
$handler->open($this->sessionSavePath, $this->sessionName);
209+
210+
$oldSessionId = $this->generateSessionId('999999');
211+
$newSessionId = $this->generateSessionId('aaaaaa');
212+
213+
$this->assertSame('', $handler->read($oldSessionId));
214+
215+
$oldData = '__ci_last_regenerate|i:1664607454;role|s:5:"guest";';
216+
$this->assertTrue($handler->write($oldSessionId, $oldData));
217+
218+
// During PHP session_regenerate_id(), write() is called with the new ID
219+
// while the lock for the old session ID is still held.
220+
$newData = '__ci_last_regenerate|i:1664607500;role|s:5:"admin";';
221+
$this->assertTrue($handler->write($newSessionId, $newData));
222+
$this->assertTrue($handler->close());
223+
224+
// Verify the old lock was released and the new session data is readable
225+
$handler2 = $this->getInstance();
226+
$this->assertTrue($handler2->open($this->sessionSavePath, $this->sessionName));
227+
$this->assertSame($oldData, $handler2->read($oldSessionId));
228+
$this->assertTrue($handler2->close());
229+
230+
$this->assertTrue($handler2->open($this->sessionSavePath, $this->sessionName));
231+
$this->assertSame($newData, $handler2->read($newSessionId));
232+
$this->assertTrue($handler2->close());
233+
}
175234
}

0 commit comments

Comments
 (0)