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.
|
* By default, only the characters in allowedParameterCharacters() are allowed.
|
||||||
*
|
*
|
||||||
* Null parameters are skipped.
|
|
||||||
*
|
|
||||||
* @throws InvalidArgumentException
|
* @throws InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
protected function validateParameter(string|array|null $parameters, string|null $allowedCharacters = null): void
|
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();
|
$allowedCharacters ??= $this->allowedParameterCharacters();
|
||||||
|
|
||||||
foreach (Arr::wrap($parameters) as $parameter) {
|
foreach (Arr::wrap($parameters) as $parameter) {
|
||||||
if (is_null($parameter)) {
|
if (is_null($parameter)) {
|
||||||
// Skip if there's nothing to validate
|
throw new InvalidArgumentException('Parameter cannot be null.');
|
||||||
// (e.g. when $tenant->database()->getUsername() of an
|
|
||||||
// improperly created tenant is null and it gets passed).
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_numeric($parameter)) {
|
if (is_numeric($parameter)) {
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ class MySQLDatabaseManager extends TenantDatabaseManager
|
||||||
$charset = $this->connection()->getConfig('charset');
|
$charset = $this->connection()->getConfig('charset');
|
||||||
$collation = $this->connection()->getConfig('collation');
|
$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
|
// MySQL defaults to the server's charset and collation
|
||||||
// if charset and collation are not specified.
|
// 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([
|
$tenantWithInvalidDatabase = Tenant::make([
|
||||||
'tenancy_db_name' => $invalidDatabaseName,
|
'tenancy_db_name' => $invalidDatabaseName,
|
||||||
'tenancy_db_username' => 'valid_USERNAME',
|
'tenancy_db_username' => 'valid_USERNAME',
|
||||||
|
'tenancy_db_password' => 'valid_password',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
expect(fn () => $manager->createUser($tenantWithInvalidDatabase->database()))
|
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()))
|
expect(fn () => $manager->createUser($tenantWithValidPassword->database()))
|
||||||
->not()->toThrow(InvalidArgumentException::class, 'Forbidden character');
|
->not()->toThrow(InvalidArgumentException::class, 'Forbidden character');
|
||||||
|
|
||||||
$tenantWithNullDbParameters = Tenant::make([
|
$tenantWithNullCredentials = Tenant::make([
|
||||||
'tenancy_db_name' => null,
|
'tenancy_db_name' => 'valid_db_name',
|
||||||
'tenancy_db_username' => null,
|
'tenancy_db_username' => null,
|
||||||
'tenancy_db_password' => null,
|
'tenancy_db_password' => null,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// validateParameter() doesn't throw InvalidArgumentException if a parameter is null
|
expect(fn () => $manager->createUser($tenantWithNullCredentials->database()))
|
||||||
// (an exception will be thrown, but not by validateParameter()).
|
->toThrow(InvalidArgumentException::class, 'Parameter cannot be null.');
|
||||||
expect(fn () => $manager->createUser($tenantWithNullDbParameters->database()))
|
|
||||||
->not()->toThrow(InvalidArgumentException::class);
|
|
||||||
}
|
}
|
||||||
})->with('database_managers');
|
})->with('database_managers');
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue