Skip to content

Commit

Permalink
Merge pull request #9036 from paulbalandan/session-class
Browse files Browse the repository at this point in the history
fix: Prevent invalid session handlers
  • Loading branch information
kenjis authored Jul 17, 2024
2 parents 0ffe391 + 04e7e6b commit 17d47f1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
3 changes: 2 additions & 1 deletion system/Config/BaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
use Config\Optimize;
use Config\Pager as ConfigPager;
use Config\Services as AppServices;
use Config\Session as ConfigSession;
use Config\Toolbar as ConfigToolbar;
use Config\Validation as ConfigValidation;
use Config\View as ConfigView;
Expand Down Expand Up @@ -130,7 +131,7 @@
* @method static Router router(RouteCollectionInterface $routes = null, Request $request = null, $getShared = true)
* @method static RouteCollection routes($getShared = true)
* @method static Security security(App $config = null, $getShared = true)
* @method static Session session(App $config = null, $getShared = true)
* @method static Session session(ConfigSession $config = null, $getShared = true)
* @method static SiteURIFactory siteurifactory(App $config = null, Superglobals $superglobals = null, $getShared = true)
* @method static Superglobals superglobals(array $server = null, array $get = null, bool $getShared = true)
* @method static Throttler throttler($getShared = true)
Expand Down
17 changes: 13 additions & 4 deletions system/Config/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
use CodeIgniter\Router\RouteCollectionInterface;
use CodeIgniter\Router\Router;
use CodeIgniter\Security\Security;
use CodeIgniter\Session\Handlers\BaseHandler as SessionBaseHandler;
use CodeIgniter\Session\Handlers\Database\MySQLiHandler;
use CodeIgniter\Session\Handlers\Database\PostgreHandler;
use CodeIgniter\Session\Handlers\DatabaseHandler;
Expand Down Expand Up @@ -88,6 +89,7 @@
use Config\Toolbar as ToolbarConfig;
use Config\Validation as ValidationConfig;
use Config\View as ViewConfig;
use InvalidArgumentException;
use Locale;

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

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

$driver = $db->getPlatform();
$driverPlatform = Database::connect($DBGroup)->getPlatform();

if ($driver === 'MySQLi') {
if ($driverPlatform === 'MySQLi') {
$driverName = MySQLiHandler::class;
} elseif ($driver === 'Postgre') {
} elseif ($driverPlatform === 'Postgre') {
$driverName = PostgreHandler::class;
}
}

if (! class_exists($driverName) || ! is_a($driverName, SessionBaseHandler::class, true)) {
throw new InvalidArgumentException(sprintf(
'Invalid session handler "%s" provided.',
$driverName
));
}

/** @var SessionBaseHandler $driver */
$driver = new $driverName($config, AppServices::get('request')->getIPAddress());
$driver->setLogger($logger);

Expand Down
29 changes: 29 additions & 0 deletions tests/system/Config/ServicesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
use Config\App;
use Config\Exceptions;
use Config\Security as SecurityConfig;
use Config\Session as ConfigSession;
use InvalidArgumentException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\PreserveGlobalState;
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
Expand Down Expand Up @@ -259,6 +262,32 @@ public function testNewSessionWithNullConfig(): void
$this->assertInstanceOf(Session::class, $actual);
}

#[DataProvider('provideNewSessionInvalid')]
#[PreserveGlobalState(false)]
#[RunInSeparateProcess]
public function testNewSessionWithInvalidHandler(string $driver): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(sprintf('Invalid session handler "%s" provided.', $driver));

$config = new ConfigSession();

$config->driver = $driver;
Services::session($config, false);
}

/**
* @return iterable<string, array{0: string}>
*/
public static function provideNewSessionInvalid(): iterable
{
yield 'just a string' => ['file'];

yield 'inexistent class' => ['Foo'];

yield 'other class' => [self::class];
}

#[PreserveGlobalState(false)]
#[RunInSeparateProcess]
public function testCallStatic(): void
Expand Down

0 comments on commit 17d47f1

Please sign in to comment.