mirror of
https://github.com/archtechx/tenancy.git
synced 2026-06-20 22:54:05 +00:00
Consider null parameters invalid
Parameters passed to validateParameter should always be non-null, and if they're null, an exception is thrown.
This commit is contained in:
parent
fbffeb84b3
commit
48b8aac42d
3 changed files with 11 additions and 13 deletions
|
|
@ -50,20 +50,19 @@ trait ValidatesDatabaseParameters
|
|||
*
|
||||
* By default, only the characters in allowedParameterCharacters() are allowed.
|
||||
*
|
||||
* Null parameters are skipped.
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
protected function validateParameter(string|array|null $parameters, string|null $allowedCharacters = null): void
|
||||
{
|
||||
if ($parameters === null) {
|
||||
throw new InvalidArgumentException('Parameter cannot be null.');
|
||||
}
|
||||
|
||||
$allowedCharacters ??= $this->allowedParameterCharacters();
|
||||
|
||||
foreach (Arr::wrap($parameters) as $parameter) {
|
||||
if (is_null($parameter)) {
|
||||
// Skip if there's nothing to validate
|
||||
// (e.g. when $tenant->database()->getUsername() of an
|
||||
// improperly created tenant is null and it gets passed).
|
||||
continue;
|
||||
throw new InvalidArgumentException('Parameter cannot be null.');
|
||||
}
|
||||
|
||||
if (is_numeric($parameter)) {
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ class MySQLDatabaseManager extends TenantDatabaseManager
|
|||
$charset = $this->connection()->getConfig('charset');
|
||||
$collation = $this->connection()->getConfig('collation');
|
||||
|
||||
$this->validateParameter([$database, $charset, $collation]);
|
||||
$this->validateParameter(array_filter([$database, $charset, $collation], fn ($param) => $param !== null));
|
||||
|
||||
// MySQL defaults to the server's charset and collation
|
||||
// if charset and collation are not specified.
|
||||
|
|
|
|||
|
|
@ -591,6 +591,7 @@ test('database managers validate parameters used in raw sql statements', functio
|
|||
$tenantWithInvalidDatabase = Tenant::make([
|
||||
'tenancy_db_name' => $invalidDatabaseName,
|
||||
'tenancy_db_username' => 'valid_USERNAME',
|
||||
'tenancy_db_password' => 'valid_password',
|
||||
]);
|
||||
|
||||
expect(fn () => $manager->createUser($tenantWithInvalidDatabase->database()))
|
||||
|
|
@ -615,16 +616,14 @@ test('database managers validate parameters used in raw sql statements', functio
|
|||
expect(fn () => $manager->createUser($tenantWithValidPassword->database()))
|
||||
->not()->toThrow(InvalidArgumentException::class, 'Forbidden character');
|
||||
|
||||
$tenantWithNullDbParameters = Tenant::make([
|
||||
'tenancy_db_name' => null,
|
||||
$tenantWithNullCredentials = Tenant::make([
|
||||
'tenancy_db_name' => 'valid_db_name',
|
||||
'tenancy_db_username' => null,
|
||||
'tenancy_db_password' => null,
|
||||
]);
|
||||
|
||||
// validateParameter() doesn't throw InvalidArgumentException if a parameter is null
|
||||
// (an exception will be thrown, but not by validateParameter()).
|
||||
expect(fn () => $manager->createUser($tenantWithNullDbParameters->database()))
|
||||
->not()->toThrow(InvalidArgumentException::class);
|
||||
expect(fn () => $manager->createUser($tenantWithNullCredentials->database()))
|
||||
->toThrow(InvalidArgumentException::class, 'Parameter cannot be null.');
|
||||
}
|
||||
})->with('database_managers');
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue