1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-05-06 15:24:03 +00:00

Validate in-memory DBs outside of isInMemory

isInMemory should check if the name looks ilke an in-memory database name and return bool (it shouldn't throw validation errors).

Also, make the validation methods non-static.
This commit is contained in:
lukinovec 2026-05-01 15:22:40 +02:00
parent 429e0985fd
commit ea20eb13b6
2 changed files with 17 additions and 23 deletions

View file

@ -23,7 +23,7 @@ trait ValidatesDatabaseParameters
* Used as the default allowlist in validateParameter(), which validates non-password * Used as the default allowlist in validateParameter(), which validates non-password
* parameters such as database names or usernames. * parameters such as database names or usernames.
*/ */
protected static function allowedParameterCharacters(): string protected function allowedParameterCharacters(): string
{ {
return 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-'; return 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-';
} }
@ -36,7 +36,7 @@ trait ValidatesDatabaseParameters
* characters that can break out of the quoted SQL strings (so e.g. * characters that can break out of the quoted SQL strings (so e.g.
* ', ", \, and ` aren't allowed). * ', ", \, and ` aren't allowed).
*/ */
protected static function allowedPasswordCharacters(): string protected function allowedPasswordCharacters(): string
{ {
return ' !#$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~'; return ' !#$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~';
} }
@ -46,15 +46,15 @@ trait ValidatesDatabaseParameters
* only contain allowed characters before used in SQL statements * only contain allowed characters before used in SQL statements
* (or paths in the case of SQLiteDatabaseManager). * (or paths in the case of SQLiteDatabaseManager).
* *
* By default, only the characters in static::allowedParameterCharacters() are allowed. * By default, only the characters in allowedParameterCharacters() are allowed.
* *
* Null parameters are skipped. * Null parameters are skipped.
* *
* @throws InvalidArgumentException * @throws InvalidArgumentException
*/ */
protected static function validateParameter(string|array|null $parameters, string|null $allowedCharacters = null): void protected function validateParameter(string|array|null $parameters, string|null $allowedCharacters = null): void
{ {
$allowedCharacters ??= static::allowedParameterCharacters(); $allowedCharacters ??= $this->allowedParameterCharacters();
foreach ((array) $parameters as $parameter) { foreach ((array) $parameters as $parameter) {
if (is_null($parameter)) { if (is_null($parameter)) {
@ -78,7 +78,7 @@ trait ValidatesDatabaseParameters
} }
/** /**
* Ensure password only contains allowed characters (static::allowedPasswordCharacters()) * Ensure password only contains allowed characters (allowedPasswordCharacters())
* before used in SQL statements. * before used in SQL statements.
* *
* Used in permission controlled managers as a shorthand for calling validateParameter() * Used in permission controlled managers as a shorthand for calling validateParameter()
@ -86,8 +86,8 @@ trait ValidatesDatabaseParameters
* *
* @throws InvalidArgumentException * @throws InvalidArgumentException
*/ */
protected static function validatePassword(string|null $password): void protected function validatePassword(string|null $password): void
{ {
static::validateParameter($password, allowedCharacters: static::allowedPasswordCharacters()); $this->validateParameter($password, allowedCharacters: $this->allowedPasswordCharacters());
} }
} }

View file

@ -136,6 +136,9 @@ class SQLiteDatabaseManager implements TenantDatabaseManager
public function makeConnectionConfig(array $baseConfig, string $databaseName): array public function makeConnectionConfig(array $baseConfig, string $databaseName): array
{ {
if ($this->isInMemory($databaseName)) { if ($this->isInMemory($databaseName)) {
// Named in-memory DBs are formatted like 'file:_tenancy_inmemory_tenant123?mode=memory&cache=shared'
$this->validateDatabaseName($databaseName, ':?=&');
$baseConfig['database'] = $databaseName; $baseConfig['database'] = $databaseName;
if (static::$persistInMemoryConnectionUsing !== null) { if (static::$persistInMemoryConnectionUsing !== null) {
@ -162,30 +165,21 @@ class SQLiteDatabaseManager implements TenantDatabaseManager
public static function isInMemory(string $name): bool public static function isInMemory(string $name): bool
{ {
if ($name === ':memory:') { $isNamed = str_starts_with($name, 'file:_tenancy_inmemory_') &&
return true; str_ends_with($name, '?mode=memory&cache=shared');
}
if (str_starts_with($name, 'file:_tenancy_inmemory_') && return $name === ':memory:' || $isNamed;
str_ends_with($name, '?mode=memory&cache=shared')) {
// Named in-memory DBs are formatted like 'file:_tenancy_inmemory_tenant123?mode=memory&cache=shared'
static::validateDatabaseName($name, ':?=&');
return true;
}
return false;
} }
/** /**
* Ensure database name only contains allowed characters * Ensure database name only contains allowed characters
* (static::allowedDatabaseNameCharacters() + $extraAllowedCharacters) and is not a directory name. * (allowedDatabaseNameCharacters() + $extraAllowedCharacters) and is not a directory name.
* *
* @throws InvalidArgumentException * @throws InvalidArgumentException
*/ */
protected static function validateDatabaseName(string $name, string $extraAllowedCharacters = ''): void protected function validateDatabaseName(string $name, string $extraAllowedCharacters = ''): void
{ {
static::validateParameter($name, static::allowedDatabaseNameCharacters() . $extraAllowedCharacters); $this->validateParameter($name, $this->allowedDatabaseNameCharacters() . $extraAllowedCharacters);
if ($name === '') { if ($name === '') {
throw new InvalidArgumentException('Database name cannot be empty.'); throw new InvalidArgumentException('Database name cannot be empty.');