Skip to content

Commit 04e7e6b

Browse files
committed
fix: Prevent invalid session handlers
1 parent 517aa33 commit 04e7e6b

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

system/Config/BaseService.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
use Config\Optimize;
7676
use Config\Pager as ConfigPager;
7777
use Config\Services as AppServices;
78+
use Config\Session as ConfigSession;
7879
use Config\Toolbar as ConfigToolbar;
7980
use Config\Validation as ConfigValidation;
8081
use Config\View as ConfigView;
@@ -130,7 +131,7 @@
130131
* @method static Router router(RouteCollectionInterface $routes = null, Request $request = null, $getShared = true)
131132
* @method static RouteCollection routes($getShared = true)
132133
* @method static Security security(App $config = null, $getShared = true)
133-
* @method static Session session(App $config = null, $getShared = true)
134+
* @method static Session session(ConfigSession $config = null, $getShared = true)
134135
* @method static SiteURIFactory siteurifactory(App $config = null, Superglobals $superglobals = null, $getShared = true)
135136
* @method static Superglobals superglobals(array $server = null, array $get = null, bool $getShared = true)
136137
* @method static Throttler throttler($getShared = true)

system/Config/Services.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
use CodeIgniter\Router\RouteCollectionInterface;
5252
use CodeIgniter\Router\Router;
5353
use CodeIgniter\Security\Security;
54+
use CodeIgniter\Session\Handlers\BaseHandler as SessionBaseHandler;
5455
use CodeIgniter\Session\Handlers\Database\MySQLiHandler;
5556
use CodeIgniter\Session\Handlers\Database\PostgreHandler;
5657
use CodeIgniter\Session\Handlers\DatabaseHandler;
@@ -88,6 +89,7 @@
8889
use Config\Toolbar as ToolbarConfig;
8990
use Config\Validation as ValidationConfig;
9091
use Config\View as ViewConfig;
92+
use InvalidArgumentException;
9193
use Locale;
9294

9395
/**
@@ -674,17 +676,24 @@ public static function session(?SessionConfig $config = null, bool $getShared =
674676

675677
if ($driverName === DatabaseHandler::class) {
676678
$DBGroup = $config->DBGroup ?? config(Database::class)->defaultGroup;
677-
$db = Database::connect($DBGroup);
678679

679-
$driver = $db->getPlatform();
680+
$driverPlatform = Database::connect($DBGroup)->getPlatform();
680681

681-
if ($driver === 'MySQLi') {
682+
if ($driverPlatform === 'MySQLi') {
682683
$driverName = MySQLiHandler::class;
683-
} elseif ($driver === 'Postgre') {
684+
} elseif ($driverPlatform === 'Postgre') {
684685
$driverName = PostgreHandler::class;
685686
}
686687
}
687688

689+
if (! class_exists($driverName) || ! is_a($driverName, SessionBaseHandler::class, true)) {
690+
throw new InvalidArgumentException(sprintf(
691+
'Invalid session handler "%s" provided.',
692+
$driverName
693+
));
694+
}
695+
696+
/** @var SessionBaseHandler $driver */
688697
$driver = new $driverName($config, AppServices::get('request')->getIPAddress());
689698
$driver->setLogger($logger);
690699

tests/system/Config/ServicesTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@
4646
use Config\App;
4747
use Config\Exceptions;
4848
use Config\Security as SecurityConfig;
49+
use Config\Session as ConfigSession;
50+
use InvalidArgumentException;
51+
use PHPUnit\Framework\Attributes\DataProvider;
4952
use PHPUnit\Framework\Attributes\Group;
5053
use PHPUnit\Framework\Attributes\PreserveGlobalState;
5154
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
@@ -259,6 +262,32 @@ public function testNewSessionWithNullConfig(): void
259262
$this->assertInstanceOf(Session::class, $actual);
260263
}
261264

265+
#[DataProvider('provideNewSessionInvalid')]
266+
#[PreserveGlobalState(false)]
267+
#[RunInSeparateProcess]
268+
public function testNewSessionWithInvalidHandler(string $driver): void
269+
{
270+
$this->expectException(InvalidArgumentException::class);
271+
$this->expectExceptionMessage(sprintf('Invalid session handler "%s" provided.', $driver));
272+
273+
$config = new ConfigSession();
274+
275+
$config->driver = $driver;
276+
Services::session($config, false);
277+
}
278+
279+
/**
280+
* @return iterable<string, array{0: string}>
281+
*/
282+
public static function provideNewSessionInvalid(): iterable
283+
{
284+
yield 'just a string' => ['file'];
285+
286+
yield 'inexistent class' => ['Foo'];
287+
288+
yield 'other class' => [self::class];
289+
}
290+
262291
#[PreserveGlobalState(false)]
263292
#[RunInSeparateProcess]
264293
public function testCallStatic(): void

0 commit comments

Comments
 (0)