Skip to content

Commit 48b8aac

Browse files
committed
Consider null parameters invalid
Parameters passed to validateParameter should always be non-null, and if they're null, an exception is thrown.
1 parent fbffeb8 commit 48b8aac

3 files changed

Lines changed: 11 additions & 13 deletions

File tree

src/Database/Concerns/ValidatesDatabaseParameters.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,19 @@ protected function allowedPasswordCharacters(): string
5050
*
5151
* By default, only the characters in allowedParameterCharacters() are allowed.
5252
*
53-
* Null parameters are skipped.
54-
*
5553
* @throws InvalidArgumentException
5654
*/
5755
protected function validateParameter(string|array|null $parameters, string|null $allowedCharacters = null): void
5856
{
57+
if ($parameters === null) {
58+
throw new InvalidArgumentException('Parameter cannot be null.');
59+
}
60+
5961
$allowedCharacters ??= $this->allowedParameterCharacters();
6062

6163
foreach (Arr::wrap($parameters) as $parameter) {
6264
if (is_null($parameter)) {
63-
// Skip if there's nothing to validate
64-
// (e.g. when $tenant->database()->getUsername() of an
65-
// improperly created tenant is null and it gets passed).
66-
continue;
65+
throw new InvalidArgumentException('Parameter cannot be null.');
6766
}
6867

6968
if (is_numeric($parameter)) {

src/Database/TenantDatabaseManagers/MySQLDatabaseManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function createDatabase(TenantWithDatabase $tenant): bool
1414
$charset = $this->connection()->getConfig('charset');
1515
$collation = $this->connection()->getConfig('collation');
1616

17-
$this->validateParameter([$database, $charset, $collation]);
17+
$this->validateParameter(array_filter([$database, $charset, $collation], fn ($param) => $param !== null));
1818

1919
// MySQL defaults to the server's charset and collation
2020
// if charset and collation are not specified.

tests/TenantDatabaseManagerTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,7 @@
591591
$tenantWithInvalidDatabase = Tenant::make([
592592
'tenancy_db_name' => $invalidDatabaseName,
593593
'tenancy_db_username' => 'valid_USERNAME',
594+
'tenancy_db_password' => 'valid_password',
594595
]);
595596

596597
expect(fn () => $manager->createUser($tenantWithInvalidDatabase->database()))
@@ -615,16 +616,14 @@
615616
expect(fn () => $manager->createUser($tenantWithValidPassword->database()))
616617
->not()->toThrow(InvalidArgumentException::class, 'Forbidden character');
617618

618-
$tenantWithNullDbParameters = Tenant::make([
619-
'tenancy_db_name' => null,
619+
$tenantWithNullCredentials = Tenant::make([
620+
'tenancy_db_name' => 'valid_db_name',
620621
'tenancy_db_username' => null,
621622
'tenancy_db_password' => null,
622623
]);
623624

624-
// validateParameter() doesn't throw InvalidArgumentException if a parameter is null
625-
// (an exception will be thrown, but not by validateParameter()).
626-
expect(fn () => $manager->createUser($tenantWithNullDbParameters->database()))
627-
->not()->toThrow(InvalidArgumentException::class);
625+
expect(fn () => $manager->createUser($tenantWithNullCredentials->database()))
626+
->toThrow(InvalidArgumentException::class, 'Parameter cannot be null.');
628627
}
629628
})->with('database_managers');
630629

0 commit comments

Comments
 (0)