Skip to content

Commit

Permalink
Merge branch '4.1.x' into 4.2.x
Browse files Browse the repository at this point in the history
* 4.1.x:
  PDO: Raise a proper exception if user or password is false (#6513)
  • Loading branch information
derrabus committed Sep 3, 2024
2 parents c56a608 + ab82363 commit 2a838b2
Show file tree
Hide file tree
Showing 13 changed files with 170 additions and 10 deletions.
3 changes: 3 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ parameters:
-
message: '~^Parameter #1 \$driverOptions of method Doctrine\\DBAL\\Tests\\Functional\\Driver\\Mysqli\\ConnectionTest\:\:getConnection\(\) expects array<string, mixed>, .* given\.$~'
path: tests/Functional/Driver/Mysqli/ConnectionTest.php
-
message: '~^Parameter #1 \$params of method Doctrine\\DBAL\\Driver\:\:connect\(\) expects array~'
path: tests/Driver/PDO/*/DriverTest.php

# DriverManagerTest::testDatabaseUrl() should be refactored as it's too dynamic.
-
Expand Down
1 change: 1 addition & 0 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
<InvalidArgument>
<errorLevel type="suppress">
<!-- We're testing with invalid input here. -->
<file name="tests/Driver/PDO/*/DriverTest.php"/>
<file name="tests/Functional/Driver/Mysqli/ConnectionTest.php"/>
<file name="tests/Platforms/AbstractPlatformTestCase.php"/>
</errorLevel>
Expand Down
23 changes: 23 additions & 0 deletions src/Driver/PDO/Exception/InvalidConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Driver\PDO\Exception;

use Doctrine\DBAL\Driver\AbstractException;

use function get_debug_type;
use function sprintf;

/** @psalm-immutable */
final class InvalidConfiguration extends AbstractException
{
public static function notAStringOrNull(string $key, mixed $value): self
{
return new self(sprintf(
'The %s configuration parameter is expected to be either a string or null, got %s.',
$key,
get_debug_type($value),
));
}
}
9 changes: 9 additions & 0 deletions src/Driver/PDO/MySQL/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
use Doctrine\DBAL\Driver\AbstractMySQLDriver;
use Doctrine\DBAL\Driver\PDO\Connection;
use Doctrine\DBAL\Driver\PDO\Exception;
use Doctrine\DBAL\Driver\PDO\Exception\InvalidConfiguration;
use PDO;
use PDOException;
use SensitiveParameter;

use function is_string;

final class Driver extends AbstractMySQLDriver
{
/**
Expand All @@ -26,6 +29,12 @@ public function connect(
$driverOptions[PDO::ATTR_PERSISTENT] = true;
}

foreach (['user', 'password'] as $key) {
if (isset($params[$key]) && ! is_string($params[$key])) {
throw InvalidConfiguration::notAStringOrNull($key, $params[$key]);
}
}

$safeParams = $params;
unset($safeParams['password']);

Expand Down
9 changes: 9 additions & 0 deletions src/Driver/PDO/OCI/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
use Doctrine\DBAL\Driver\AbstractOracleDriver;
use Doctrine\DBAL\Driver\PDO\Connection;
use Doctrine\DBAL\Driver\PDO\Exception;
use Doctrine\DBAL\Driver\PDO\Exception\InvalidConfiguration;
use PDO;
use PDOException;
use SensitiveParameter;

use function is_string;

final class Driver extends AbstractOracleDriver
{
/**
Expand All @@ -26,6 +29,12 @@ public function connect(
$driverOptions[PDO::ATTR_PERSISTENT] = true;
}

foreach (['user', 'password'] as $key) {
if (isset($params[$key]) && ! is_string($params[$key])) {
throw InvalidConfiguration::notAStringOrNull($key, $params[$key]);
}
}

$safeParams = $params;
unset($safeParams['password']);

Expand Down
9 changes: 9 additions & 0 deletions src/Driver/PDO/PgSQL/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
use Doctrine\DBAL\Driver\AbstractPostgreSQLDriver;
use Doctrine\DBAL\Driver\PDO\Connection;
use Doctrine\DBAL\Driver\PDO\Exception;
use Doctrine\DBAL\Driver\PDO\Exception\InvalidConfiguration;
use PDO;
use PDOException;
use SensitiveParameter;

use function is_string;

final class Driver extends AbstractPostgreSQLDriver
{
/**
Expand All @@ -26,6 +29,12 @@ public function connect(
$driverOptions[PDO::ATTR_PERSISTENT] = true;
}

foreach (['user', 'password'] as $key) {
if (isset($params[$key]) && ! is_string($params[$key])) {
throw InvalidConfiguration::notAStringOrNull($key, $params[$key]);
}
}

$safeParams = $params;
unset($safeParams['password']);

Expand Down
8 changes: 8 additions & 0 deletions src/Driver/PDO/SQLSrv/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
use Doctrine\DBAL\Driver\Exception;
use Doctrine\DBAL\Driver\PDO\Connection as PDOConnection;
use Doctrine\DBAL\Driver\PDO\Exception as PDOException;
use Doctrine\DBAL\Driver\PDO\Exception\InvalidConfiguration;
use PDO;
use SensitiveParameter;

use function is_int;
use function is_string;
use function sprintf;

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

foreach (['user', 'password'] as $key) {
if (isset($params[$key]) && ! is_string($params[$key])) {
throw InvalidConfiguration::notAStringOrNull($key, $params[$key]);
}
}

$safeParams = $params;
unset($safeParams['password']);

Expand Down
8 changes: 8 additions & 0 deletions src/Driver/PDO/SQLite/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
use Doctrine\DBAL\Driver\AbstractSQLiteDriver;
use Doctrine\DBAL\Driver\PDO\Connection;
use Doctrine\DBAL\Driver\PDO\Exception;
use Doctrine\DBAL\Driver\PDO\Exception\InvalidConfiguration;
use PDO;
use PDOException;
use SensitiveParameter;

use function array_intersect_key;
use function is_string;

final class Driver extends AbstractSQLiteDriver
{
Expand All @@ -22,6 +24,12 @@ public function connect(
#[SensitiveParameter]
array $params,
): Connection {
foreach (['user', 'password'] as $key) {
if (isset($params[$key]) && ! is_string($params[$key])) {
throw InvalidConfiguration::notAStringOrNull($key, $params[$key]);
}
}

try {
$pdo = new PDO(
$this->constructPdoDsn(array_intersect_key($params, ['path' => true, 'memory' => true])),
Expand Down
22 changes: 20 additions & 2 deletions tests/Driver/PDO/MySQL/DriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,31 @@

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

use Doctrine\DBAL\Driver as DriverInterface;
use Doctrine\DBAL\Driver\PDO\Exception\InvalidConfiguration;
use Doctrine\DBAL\Driver\PDO\MySQL\Driver;
use Doctrine\DBAL\Tests\Driver\AbstractMySQLDriverTestCase;

class DriverTest extends AbstractMySQLDriverTestCase
{
protected function createDriver(): DriverInterface
public function testUserIsFalse(): void
{
$this->expectException(InvalidConfiguration::class);
$this->expectExceptionMessage(
'The user configuration parameter is expected to be either a string or null, got bool.',
);
$this->driver->connect(['user' => false]);
}

public function testPasswordIsFalse(): void
{
$this->expectException(InvalidConfiguration::class);
$this->expectExceptionMessage(
'The password configuration parameter is expected to be either a string or null, got bool.',
);
$this->driver->connect(['password' => false]);
}

protected function createDriver(): Driver
{
return new Driver();
}
Expand Down
22 changes: 20 additions & 2 deletions tests/Driver/PDO/OCI/DriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,31 @@

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

use Doctrine\DBAL\Driver as DriverInterface;
use Doctrine\DBAL\Driver\PDO\Exception\InvalidConfiguration;
use Doctrine\DBAL\Driver\PDO\OCI\Driver;
use Doctrine\DBAL\Tests\Driver\AbstractOracleDriverTestCase;

class DriverTest extends AbstractOracleDriverTestCase
{
protected function createDriver(): DriverInterface
public function testUserIsFalse(): void
{
$this->expectException(InvalidConfiguration::class);
$this->expectExceptionMessage(
'The user configuration parameter is expected to be either a string or null, got bool.',
);
$this->driver->connect(['user' => false]);
}

public function testPasswordIsFalse(): void
{
$this->expectException(InvalidConfiguration::class);
$this->expectExceptionMessage(
'The password configuration parameter is expected to be either a string or null, got bool.',
);
$this->driver->connect(['password' => false]);
}

protected function createDriver(): Driver
{
return new Driver();
}
Expand Down
22 changes: 20 additions & 2 deletions tests/Driver/PDO/PgSQL/DriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

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

use Doctrine\DBAL\Driver as DriverInterface;
use Doctrine\DBAL\Driver\Connection;
use Doctrine\DBAL\Driver\PDO;
use Doctrine\DBAL\Driver\PDO\Exception\InvalidConfiguration;
use Doctrine\DBAL\Driver\PDO\PgSQL\Driver;
use Doctrine\DBAL\Tests\Driver\AbstractPostgreSQLDriverTestCase;
use Doctrine\DBAL\Tests\TestUtil;
Expand Down Expand Up @@ -60,7 +60,25 @@ public function testConnectionDisablePreparesWhenDisablePreparesIsExplicitlyDefi
);
}

protected function createDriver(): DriverInterface
public function testUserIsFalse(): void
{
$this->expectException(InvalidConfiguration::class);
$this->expectExceptionMessage(
'The user configuration parameter is expected to be either a string or null, got bool.',
);
$this->driver->connect(['user' => false]);
}

public function testPasswordIsFalse(): void
{
$this->expectException(InvalidConfiguration::class);
$this->expectExceptionMessage(
'The password configuration parameter is expected to be either a string or null, got bool.',
);
$this->driver->connect(['password' => false]);
}

protected function createDriver(): Driver
{
return new Driver();
}
Expand Down
22 changes: 20 additions & 2 deletions tests/Driver/PDO/SQLSrv/DriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,31 @@

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

use Doctrine\DBAL\Driver as DriverInterface;
use Doctrine\DBAL\Driver\PDO\Exception\InvalidConfiguration;
use Doctrine\DBAL\Driver\PDO\SQLSrv\Driver;
use Doctrine\DBAL\Tests\Driver\AbstractSQLServerDriverTestCase;

class DriverTest extends AbstractSQLServerDriverTestCase
{
protected function createDriver(): DriverInterface
public function testUserIsFalse(): void
{
$this->expectException(InvalidConfiguration::class);
$this->expectExceptionMessage(
'The user configuration parameter is expected to be either a string or null, got bool.',
);
$this->driver->connect(['user' => false]);
}

public function testPasswordIsFalse(): void
{
$this->expectException(InvalidConfiguration::class);
$this->expectExceptionMessage(
'The password configuration parameter is expected to be either a string or null, got bool.',
);
$this->driver->connect(['password' => false]);
}

protected function createDriver(): Driver
{
return new Driver();
}
Expand Down
22 changes: 20 additions & 2 deletions tests/Driver/PDO/SQLite/DriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,31 @@

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

use Doctrine\DBAL\Driver as DriverInterface;
use Doctrine\DBAL\Driver\PDO\Exception\InvalidConfiguration;
use Doctrine\DBAL\Driver\PDO\SQLite\Driver;
use Doctrine\DBAL\Tests\Driver\AbstractSQLiteDriverTestCase;

class DriverTest extends AbstractSQLiteDriverTestCase
{
protected function createDriver(): DriverInterface
public function testUserIsFalse(): void
{
$this->expectException(InvalidConfiguration::class);
$this->expectExceptionMessage(
'The user configuration parameter is expected to be either a string or null, got bool.',
);
$this->driver->connect(['user' => false]);
}

public function testPasswordIsFalse(): void
{
$this->expectException(InvalidConfiguration::class);
$this->expectExceptionMessage(
'The password configuration parameter is expected to be either a string or null, got bool.',
);
$this->driver->connect(['password' => false]);
}

protected function createDriver(): Driver
{
return new Driver();
}
Expand Down

0 comments on commit 2a838b2

Please sign in to comment.