Skip to content

Commit 9a714b3

Browse files
committed
fix: protect internal session keys in set and __set methods
1 parent ac18654 commit 9a714b3

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

system/Session/Session.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,10 @@ public function set($data, $value = null)
303303
}
304304

305305
foreach ($data as $sessionKey => $sessionValue) {
306+
if (is_string($sessionKey) && str_starts_with($sessionKey, '__ci_')) {
307+
continue;
308+
}
309+
306310
$_SESSION[$sessionKey] = $sessionValue;
307311
}
308312
}
@@ -370,6 +374,10 @@ public function remove($key)
370374
*/
371375
public function __set(string $key, $value)
372376
{
377+
if (str_starts_with($key, '__ci_')) {
378+
return;
379+
}
380+
373381
$_SESSION[$key] = $value;
374382
}
375383

tests/system/Session/SessionTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,30 @@ public function testSetMagicMethod(): void
328328
$this->assertSame('bar', $_SESSION['foo']);
329329
}
330330

331+
public function testSetIgnoresCiVars(): void
332+
{
333+
$session = $this->getInstance();
334+
$session->start();
335+
336+
$session->set('__ci_vars', 'malicious');
337+
$session->set('__ci_last_regenerate', 'malicious');
338+
339+
$this->assertArrayNotHasKey('__ci_vars', $_SESSION);
340+
$this->assertNotSame('malicious', $_SESSION['__ci_last_regenerate']);
341+
}
342+
343+
public function testSetMagicMethodIgnoresCiVars(): void
344+
{
345+
$session = $this->getInstance();
346+
$session->start();
347+
348+
$session->__ci_vars = 'malicious'; // @phpstan-ignore property.notFound
349+
$session->__ci_last_regenerate = 'malicious'; // @phpstan-ignore property.notFound
350+
351+
$this->assertArrayNotHasKey('__ci_vars', $_SESSION);
352+
$this->assertNotSame('malicious', $_SESSION['__ci_last_regenerate']);
353+
}
354+
331355
public function testCanFlashData(): void
332356
{
333357
$session = $this->getInstance();

0 commit comments

Comments
 (0)