Skip to content

Commit ab82363

Browse files
authored
PDO: Raise a proper exception if user or password is false (#6513)
| Q | A |------------- | ----------- | Type | bug | Fixed issues | #6512 #### Summary If `false` (or anything that is not a string) is passed as `user` or `password`, we run into a `TypeError` because we pass that value as-is to the constructor of PDO. This started to happen after we enabled strict types on our driver classes in 4.0. On 3.9, `false` would implicitly be cast to an empty string which is either desired or leads to more obscure connection errors. We could restore the behavior of 3.9 by adding explicit type casts to the two parameters. But since we don't document `false` as a valid value for either parameter, my preference would indeed be raising an exception.
1 parent b9183ca commit ab82363

File tree

13 files changed

+170
-10
lines changed

13 files changed

+170
-10
lines changed

phpstan.neon.dist

+3
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

+1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
<InvalidArgument>
135135
<errorLevel type="suppress">
136136
<!-- We're testing with invalid input here. -->
137+
<file name="tests/Driver/PDO/*/DriverTest.php"/>
137138
<file name="tests/Functional/Driver/Mysqli/ConnectionTest.php"/>
138139
<file name="tests/Platforms/AbstractPlatformTestCase.php"/>
139140
</errorLevel>
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

+9
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

+9
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

+9
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

+8
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

+8
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

+20-2
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

+20-2
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
}

tests/Driver/PDO/PgSQL/DriverTest.php

+20-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

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

7-
use Doctrine\DBAL\Driver as DriverInterface;
87
use Doctrine\DBAL\Driver\Connection;
98
use Doctrine\DBAL\Driver\PDO;
9+
use Doctrine\DBAL\Driver\PDO\Exception\InvalidConfiguration;
1010
use Doctrine\DBAL\Driver\PDO\PgSQL\Driver;
1111
use Doctrine\DBAL\Tests\Driver\AbstractPostgreSQLDriverTestCase;
1212
use Doctrine\DBAL\Tests\TestUtil;
@@ -60,7 +60,25 @@ public function testConnectionDisablePreparesWhenDisablePreparesIsExplicitlyDefi
6060
);
6161
}
6262

63-
protected function createDriver(): DriverInterface
63+
public function testUserIsFalse(): void
64+
{
65+
$this->expectException(InvalidConfiguration::class);
66+
$this->expectExceptionMessage(
67+
'The user configuration parameter is expected to be either a string or null, got bool.',
68+
);
69+
$this->driver->connect(['user' => false]);
70+
}
71+
72+
public function testPasswordIsFalse(): void
73+
{
74+
$this->expectException(InvalidConfiguration::class);
75+
$this->expectExceptionMessage(
76+
'The password configuration parameter is expected to be either a string or null, got bool.',
77+
);
78+
$this->driver->connect(['password' => false]);
79+
}
80+
81+
protected function createDriver(): Driver
6482
{
6583
return new Driver();
6684
}

tests/Driver/PDO/SQLSrv/DriverTest.php

+20-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,31 @@
44

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

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

1111
class DriverTest extends AbstractSQLServerDriverTestCase
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/SQLite/DriverTest.php

+20-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,31 @@
44

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

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

1111
class DriverTest extends AbstractSQLiteDriverTestCase
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)