Skip to content

Commit 8592ef6

Browse files
committed
Merge branch '4.2.x' into 5.0.x
* 4.2.x: PDO: Raise a proper exception if user or password is false (#6513)
2 parents d8ba313 + 2a838b2 commit 8592ef6

File tree

13 files changed

+170
-10
lines changed

13 files changed

+170
-10
lines changed

phpstan.neon.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ parameters:
6363
-
6464
message: '~^Parameter #1 \$driverOptions of method Doctrine\\DBAL\\Tests\\Functional\\Driver\\Mysqli\\ConnectionTest\:\:getConnection\(\) expects array<string, mixed>, .* given\.$~'
6565
path: tests/Functional/Driver/Mysqli/ConnectionTest.php
66+
-
67+
message: '~^Parameter #1 \$params of method Doctrine\\DBAL\\Driver\:\:connect\(\) expects array~'
68+
path: tests/Driver/PDO/*/DriverTest.php
6669

6770
# DriverManagerTest::testDatabaseUrl() should be refactored as it's too dynamic.
6871
-

psalm.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
<InvalidArgument>
123123
<errorLevel type="suppress">
124124
<!-- We're testing with invalid input here. -->
125+
<file name="tests/Driver/PDO/*/DriverTest.php"/>
125126
<file name="tests/Functional/Driver/Mysqli/ConnectionTest.php"/>
126127
<file name="tests/Platforms/AbstractPlatformTestCase.php"/>
127128
</errorLevel>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\DBAL\Driver\PDO\Exception;
6+
7+
use Doctrine\DBAL\Driver\AbstractException;
8+
9+
use function get_debug_type;
10+
use function sprintf;
11+
12+
/** @psalm-immutable */
13+
final class InvalidConfiguration extends AbstractException
14+
{
15+
public static function notAStringOrNull(string $key, mixed $value): self
16+
{
17+
return new self(sprintf(
18+
'The %s configuration parameter is expected to be either a string or null, got %s.',
19+
$key,
20+
get_debug_type($value),
21+
));
22+
}
23+
}

src/Driver/PDO/MySQL/Driver.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
use Doctrine\DBAL\Driver\AbstractMySQLDriver;
88
use Doctrine\DBAL\Driver\PDO\Connection;
99
use Doctrine\DBAL\Driver\PDO\Exception;
10+
use Doctrine\DBAL\Driver\PDO\Exception\InvalidConfiguration;
1011
use PDO;
1112
use PDOException;
1213
use SensitiveParameter;
1314

15+
use function is_string;
16+
1417
final class Driver extends AbstractMySQLDriver
1518
{
1619
/**
@@ -26,6 +29,12 @@ public function connect(
2629
$driverOptions[PDO::ATTR_PERSISTENT] = true;
2730
}
2831

32+
foreach (['user', 'password'] as $key) {
33+
if (isset($params[$key]) && ! is_string($params[$key])) {
34+
throw InvalidConfiguration::notAStringOrNull($key, $params[$key]);
35+
}
36+
}
37+
2938
$safeParams = $params;
3039
unset($safeParams['password']);
3140

src/Driver/PDO/OCI/Driver.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
use Doctrine\DBAL\Driver\AbstractOracleDriver;
88
use Doctrine\DBAL\Driver\PDO\Connection;
99
use Doctrine\DBAL\Driver\PDO\Exception;
10+
use Doctrine\DBAL\Driver\PDO\Exception\InvalidConfiguration;
1011
use PDO;
1112
use PDOException;
1213
use SensitiveParameter;
1314

15+
use function is_string;
16+
1417
final class Driver extends AbstractOracleDriver
1518
{
1619
/**
@@ -26,6 +29,12 @@ public function connect(
2629
$driverOptions[PDO::ATTR_PERSISTENT] = true;
2730
}
2831

32+
foreach (['user', 'password'] as $key) {
33+
if (isset($params[$key]) && ! is_string($params[$key])) {
34+
throw InvalidConfiguration::notAStringOrNull($key, $params[$key]);
35+
}
36+
}
37+
2938
$safeParams = $params;
3039
unset($safeParams['password']);
3140

src/Driver/PDO/PgSQL/Driver.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
use Doctrine\DBAL\Driver\AbstractPostgreSQLDriver;
88
use Doctrine\DBAL\Driver\PDO\Connection;
99
use Doctrine\DBAL\Driver\PDO\Exception;
10+
use Doctrine\DBAL\Driver\PDO\Exception\InvalidConfiguration;
1011
use PDO;
1112
use PDOException;
1213
use SensitiveParameter;
1314

15+
use function is_string;
16+
1417
final class Driver extends AbstractPostgreSQLDriver
1518
{
1619
/**
@@ -26,6 +29,12 @@ public function connect(
2629
$driverOptions[PDO::ATTR_PERSISTENT] = true;
2730
}
2831

32+
foreach (['user', 'password'] as $key) {
33+
if (isset($params[$key]) && ! is_string($params[$key])) {
34+
throw InvalidConfiguration::notAStringOrNull($key, $params[$key]);
35+
}
36+
}
37+
2938
$safeParams = $params;
3039
unset($safeParams['password']);
3140

src/Driver/PDO/SQLSrv/Driver.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
use Doctrine\DBAL\Driver\Exception;
1010
use Doctrine\DBAL\Driver\PDO\Connection as PDOConnection;
1111
use Doctrine\DBAL\Driver\PDO\Exception as PDOException;
12+
use Doctrine\DBAL\Driver\PDO\Exception\InvalidConfiguration;
1213
use PDO;
1314
use SensitiveParameter;
1415

1516
use function is_int;
17+
use function is_string;
1618
use function sprintf;
1719

1820
final class Driver extends AbstractSQLServerDriver
@@ -40,6 +42,12 @@ public function connect(
4042
$driverOptions[PDO::ATTR_PERSISTENT] = true;
4143
}
4244

45+
foreach (['user', 'password'] as $key) {
46+
if (isset($params[$key]) && ! is_string($params[$key])) {
47+
throw InvalidConfiguration::notAStringOrNull($key, $params[$key]);
48+
}
49+
}
50+
4351
$safeParams = $params;
4452
unset($safeParams['password']);
4553

src/Driver/PDO/SQLite/Driver.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
use Doctrine\DBAL\Driver\AbstractSQLiteDriver;
88
use Doctrine\DBAL\Driver\PDO\Connection;
99
use Doctrine\DBAL\Driver\PDO\Exception;
10+
use Doctrine\DBAL\Driver\PDO\Exception\InvalidConfiguration;
1011
use PDO;
1112
use PDOException;
1213
use SensitiveParameter;
1314

1415
use function array_intersect_key;
16+
use function is_string;
1517

1618
final class Driver extends AbstractSQLiteDriver
1719
{
@@ -22,6 +24,12 @@ public function connect(
2224
#[SensitiveParameter]
2325
array $params,
2426
): Connection {
27+
foreach (['user', 'password'] as $key) {
28+
if (isset($params[$key]) && ! is_string($params[$key])) {
29+
throw InvalidConfiguration::notAStringOrNull($key, $params[$key]);
30+
}
31+
}
32+
2533
try {
2634
$pdo = new PDO(
2735
$this->constructPdoDsn(array_intersect_key($params, ['path' => true, 'memory' => true])),

tests/Driver/PDO/MySQL/DriverTest.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,31 @@
44

55
namespace Doctrine\DBAL\Tests\Driver\PDO\MySQL;
66

7-
use Doctrine\DBAL\Driver as DriverInterface;
7+
use Doctrine\DBAL\Driver\PDO\Exception\InvalidConfiguration;
88
use Doctrine\DBAL\Driver\PDO\MySQL\Driver;
99
use Doctrine\DBAL\Tests\Driver\AbstractMySQLDriverTestCase;
1010

1111
class DriverTest extends AbstractMySQLDriverTestCase
1212
{
13-
protected function createDriver(): DriverInterface
13+
public function testUserIsFalse(): void
14+
{
15+
$this->expectException(InvalidConfiguration::class);
16+
$this->expectExceptionMessage(
17+
'The user configuration parameter is expected to be either a string or null, got bool.',
18+
);
19+
$this->driver->connect(['user' => false]);
20+
}
21+
22+
public function testPasswordIsFalse(): void
23+
{
24+
$this->expectException(InvalidConfiguration::class);
25+
$this->expectExceptionMessage(
26+
'The password configuration parameter is expected to be either a string or null, got bool.',
27+
);
28+
$this->driver->connect(['password' => false]);
29+
}
30+
31+
protected function createDriver(): Driver
1432
{
1533
return new Driver();
1634
}

tests/Driver/PDO/OCI/DriverTest.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,31 @@
44

55
namespace Doctrine\DBAL\Tests\Driver\PDO\OCI;
66

7-
use Doctrine\DBAL\Driver as DriverInterface;
7+
use Doctrine\DBAL\Driver\PDO\Exception\InvalidConfiguration;
88
use Doctrine\DBAL\Driver\PDO\OCI\Driver;
99
use Doctrine\DBAL\Tests\Driver\AbstractOracleDriverTestCase;
1010

1111
class DriverTest extends AbstractOracleDriverTestCase
1212
{
13-
protected function createDriver(): DriverInterface
13+
public function testUserIsFalse(): void
14+
{
15+
$this->expectException(InvalidConfiguration::class);
16+
$this->expectExceptionMessage(
17+
'The user configuration parameter is expected to be either a string or null, got bool.',
18+
);
19+
$this->driver->connect(['user' => false]);
20+
}
21+
22+
public function testPasswordIsFalse(): void
23+
{
24+
$this->expectException(InvalidConfiguration::class);
25+
$this->expectExceptionMessage(
26+
'The password configuration parameter is expected to be either a string or null, got bool.',
27+
);
28+
$this->driver->connect(['password' => false]);
29+
}
30+
31+
protected function createDriver(): Driver
1432
{
1533
return new Driver();
1634
}

0 commit comments

Comments
 (0)