diff --git a/system/Config/Services.php b/system/Config/Services.php index f5c5a2db4a99..c9266a892e38 100644 --- a/system/Config/Services.php +++ b/system/Config/Services.php @@ -683,6 +683,11 @@ public static function session(?SessionConfig $config = null, bool $getShared = $driverName = MySQLiHandler::class; } elseif ($driverPlatform === 'Postgre') { $driverName = PostgreHandler::class; + } else { + throw new InvalidArgumentException(sprintf( + 'Invalid session database handler "%s" provided. Only "MySQLi" and "Postgre" are supported.', + $driverPlatform, + )); } } diff --git a/tests/system/Config/ServicesTest.php b/tests/system/Config/ServicesTest.php index 0e1c80afc75f..162d95705933 100644 --- a/tests/system/Config/ServicesTest.php +++ b/tests/system/Config/ServicesTest.php @@ -36,6 +36,7 @@ use CodeIgniter\Router\RouteCollection; use CodeIgniter\Router\Router; use CodeIgniter\Security\Security; +use CodeIgniter\Session\Handlers\DatabaseHandler; use CodeIgniter\Session\Session; use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\Mock\MockResponse; @@ -46,6 +47,7 @@ use CodeIgniter\View\Cell; use CodeIgniter\View\Parser; use Config\App; +use Config\Database as DatabaseConfig; use Config\Exceptions; use Config\Security as SecurityConfig; use Config\Session as ConfigSession; @@ -275,6 +277,25 @@ public function testNewSessionWithInvalidHandler(string $driver): void Services::session($config, false); } + #[PreserveGlobalState(false)] + #[RunInSeparateProcess] + public function testNewSessionWithInvalidDatabaseHandler(): void + { + $driver = config(DatabaseConfig::class)->tests['DBDriver']; + + if (in_array($driver, ['MySQLi', 'Postgre'], true)) { + $this->markTestSkipped('This test case does not work with MySQLi and Postgre'); + } + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage(sprintf('Invalid session database handler "%s" provided. Only "MySQLi" and "Postgre" are supported.', $driver)); + + $config = new ConfigSession(); + + $config->driver = DatabaseHandler::class; + Services::session($config, false); + } + /** * @return iterable */ diff --git a/user_guide_src/source/changelogs/v4.6.2.rst b/user_guide_src/source/changelogs/v4.6.2.rst index 089fb01e1514..4bc92fb6d038 100644 --- a/user_guide_src/source/changelogs/v4.6.2.rst +++ b/user_guide_src/source/changelogs/v4.6.2.rst @@ -36,6 +36,7 @@ Bugs Fixed ********** - **Security:** Fixed a bug where the ``sanitize_filename()`` function from the Security helper would throw an error when used in CLI requests. +- **Session:** Fixed a bug where using the ``DatabaseHandler`` with an unsupported database driver (such as ``SQLSRV``, ``OCI8``, or ``SQLite3``) did not throw an appropriate error. See the repo's `CHANGELOG.md `_