Skip to content

Commit 045e12b

Browse files
authored
fix: throw exception for unsupported DB drivers in session (#9574)
1 parent 3638a49 commit 045e12b

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

system/Config/Services.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,11 @@ public static function session(?SessionConfig $config = null, bool $getShared =
683683
$driverName = MySQLiHandler::class;
684684
} elseif ($driverPlatform === 'Postgre') {
685685
$driverName = PostgreHandler::class;
686+
} else {
687+
throw new InvalidArgumentException(sprintf(
688+
'Invalid session database handler "%s" provided. Only "MySQLi" and "Postgre" are supported.',
689+
$driverPlatform,
690+
));
686691
}
687692
}
688693

tests/system/Config/ServicesTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use CodeIgniter\Router\RouteCollection;
3737
use CodeIgniter\Router\Router;
3838
use CodeIgniter\Security\Security;
39+
use CodeIgniter\Session\Handlers\DatabaseHandler;
3940
use CodeIgniter\Session\Session;
4041
use CodeIgniter\Test\CIUnitTestCase;
4142
use CodeIgniter\Test\Mock\MockResponse;
@@ -46,6 +47,7 @@
4647
use CodeIgniter\View\Cell;
4748
use CodeIgniter\View\Parser;
4849
use Config\App;
50+
use Config\Database as DatabaseConfig;
4951
use Config\Exceptions;
5052
use Config\Security as SecurityConfig;
5153
use Config\Session as ConfigSession;
@@ -275,6 +277,25 @@ public function testNewSessionWithInvalidHandler(string $driver): void
275277
Services::session($config, false);
276278
}
277279

280+
#[PreserveGlobalState(false)]
281+
#[RunInSeparateProcess]
282+
public function testNewSessionWithInvalidDatabaseHandler(): void
283+
{
284+
$driver = config(DatabaseConfig::class)->tests['DBDriver'];
285+
286+
if (in_array($driver, ['MySQLi', 'Postgre'], true)) {
287+
$this->markTestSkipped('This test case does not work with MySQLi and Postgre');
288+
}
289+
290+
$this->expectException(InvalidArgumentException::class);
291+
$this->expectExceptionMessage(sprintf('Invalid session database handler "%s" provided. Only "MySQLi" and "Postgre" are supported.', $driver));
292+
293+
$config = new ConfigSession();
294+
295+
$config->driver = DatabaseHandler::class;
296+
Services::session($config, false);
297+
}
298+
278299
/**
279300
* @return iterable<string, array{0: string}>
280301
*/

user_guide_src/source/changelogs/v4.6.2.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Bugs Fixed
3737

3838
- **Database:** Fixed a bug where ``when()`` and ``whenNot()`` in ``ConditionalTrait`` incorrectly evaluated certain falsy values (such as ``[]``, ``0``, ``0.0``, and ``'0'``) as truthy, causing callbacks to be executed unexpectedly. These methods now cast the condition to a boolean using ``(bool)`` to ensure consistent behavior with PHP's native truthiness.
3939
- **Security:** Fixed a bug where the ``sanitize_filename()`` function from the Security helper would throw an error when used in CLI requests.
40+
- **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.
4041

4142
See the repo's
4243
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_

0 commit comments

Comments
 (0)