mirror of
https://github.com/pterodactyl/panel.git
synced 2026-12-02 12:25:44 -05:00
166 lines
6.0 KiB
PHP
166 lines
6.0 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace Pterodactyl\Tests\Integration\Repositories;
|
||
|
|
|
||
|
|
use Illuminate\Support\Str;
|
||
|
|
use Illuminate\Support\Facades\DB;
|
||
|
|
use Illuminate\Database\Connection;
|
||
|
|
use Illuminate\Database\QueryException;
|
||
|
|
use Illuminate\Database\MySqlConnection;
|
||
|
|
use Pterodactyl\Tests\Integration\IntegrationTestCase;
|
||
|
|
use Pterodactyl\Repositories\Eloquent\DatabaseRepository;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Runs the repository against the real test database server, the same way it runs
|
||
|
|
* against a database host, so that the statements are checked by MariaDB or MySQL.
|
||
|
|
*/
|
||
|
|
class DatabaseRepositoryTest extends IntegrationTestCase
|
||
|
|
{
|
||
|
|
private const HOST_CONNECTION = 'database_host_test';
|
||
|
|
|
||
|
|
private const LOGIN_CONNECTION = 'database_host_login_test';
|
||
|
|
|
||
|
|
private DatabaseRepository $repository;
|
||
|
|
|
||
|
|
/** @var string[] */
|
||
|
|
private array $databases = [];
|
||
|
|
|
||
|
|
/** @var array<array{string, string}> */
|
||
|
|
private array $users = [];
|
||
|
|
|
||
|
|
public function setUp(): void
|
||
|
|
{
|
||
|
|
parent::setUp();
|
||
|
|
|
||
|
|
// A separate connection, since these statements commit any open transaction.
|
||
|
|
config()->set('database.connections.' . self::HOST_CONNECTION, config('database.connections.mysql'));
|
||
|
|
|
||
|
|
$this->repository = $this->app->make(DatabaseRepository::class);
|
||
|
|
$this->repository->setConnection(self::HOST_CONNECTION);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function tearDown(): void
|
||
|
|
{
|
||
|
|
DB::purge(self::LOGIN_CONNECTION);
|
||
|
|
|
||
|
|
foreach ($this->users as [$username, $remote]) {
|
||
|
|
$this->repository->dropUser($username, $remote);
|
||
|
|
}
|
||
|
|
|
||
|
|
foreach ($this->databases as $database) {
|
||
|
|
$this->repository->dropDatabase($database);
|
||
|
|
}
|
||
|
|
|
||
|
|
DB::purge(self::HOST_CONNECTION);
|
||
|
|
|
||
|
|
parent::tearDown();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* A grant on a database name containing "_" applies to that name only.
|
||
|
|
*/
|
||
|
|
public function testGrantOnlyMatchesTheExactDatabaseName()
|
||
|
|
{
|
||
|
|
$this->assertGrantIsIsolated();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The same, with MySQL reading "_" literally in grants.
|
||
|
|
*/
|
||
|
|
public function testGrantOnlyMatchesTheExactDatabaseNameWithPartialRevokes()
|
||
|
|
{
|
||
|
|
$connection = DB::connection(self::HOST_CONNECTION);
|
||
|
|
if ($connection instanceof MySqlConnection && $connection->isMaria()) {
|
||
|
|
$this->markTestSkipped('MariaDB has no partial_revokes.');
|
||
|
|
}
|
||
|
|
|
||
|
|
$original = $connection->selectOne('SELECT @@global.partial_revokes AS value')->value;
|
||
|
|
$connection->statement('SET GLOBAL partial_revokes = ON');
|
||
|
|
DB::purge(self::HOST_CONNECTION);
|
||
|
|
|
||
|
|
try {
|
||
|
|
$this->assertGrantIsIsolated();
|
||
|
|
} finally {
|
||
|
|
DB::connection(self::HOST_CONNECTION)->statement('SET GLOBAL partial_revokes = ' . ($original ? 'ON' : 'OFF'));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Names are quoted as identifiers, so quotes in them can't change the statement.
|
||
|
|
*/
|
||
|
|
public function testIdentifiersAndPasswordsAreQuoted()
|
||
|
|
{
|
||
|
|
$suffix = Str::lower(Str::random(8));
|
||
|
|
$database = $this->createDatabase("odd`name_{$suffix}");
|
||
|
|
$username = 'q_' . $suffix;
|
||
|
|
$remote = '10.0.0.%';
|
||
|
|
|
||
|
|
$this->assertTrue($this->repository->createUser($username, $remote, "it's a \\ `pass` \"word\"", 5));
|
||
|
|
$this->users[] = [$username, $remote];
|
||
|
|
$this->assertTrue($this->repository->assignUserToDatabase($database, $username, $remote));
|
||
|
|
|
||
|
|
$connection = DB::connection(self::HOST_CONNECTION);
|
||
|
|
$this->assertNotNull($connection->selectOne('SELECT 1 FROM information_schema.schemata WHERE schema_name = ?', [$database]));
|
||
|
|
$this->assertSame(5, (int) $connection->selectOne(
|
||
|
|
'SELECT max_user_connections AS value FROM mysql.user WHERE User = ? AND Host = ?',
|
||
|
|
[$username, $remote]
|
||
|
|
)->value);
|
||
|
|
|
||
|
|
$this->assertTrue($this->repository->dropUser($username, $remote));
|
||
|
|
$this->assertNull($connection->selectOne('SELECT 1 FROM mysql.user WHERE User = ?', [$username]));
|
||
|
|
}
|
||
|
|
|
||
|
|
private function assertGrantIsIsolated(): void
|
||
|
|
{
|
||
|
|
$suffix = Str::lower(Str::random(8));
|
||
|
|
$other = $this->createDatabase("gx{$suffix}");
|
||
|
|
$own = $this->createDatabase("g_{$suffix}");
|
||
|
|
|
||
|
|
// Use the host the server sees this client as, so an anonymous ''@'localhost'
|
||
|
|
// account on a local server can't take precedence over a '%' remote.
|
||
|
|
$remote = Str::afterLast(DB::connection(self::HOST_CONNECTION)->selectOne('SELECT USER() AS value')->value, '@');
|
||
|
|
$username = 'u_' . $suffix;
|
||
|
|
$password = "it's-" . Str::random(16);
|
||
|
|
$this->assertTrue($this->repository->createUser($username, $remote, $password, null));
|
||
|
|
$this->users[] = [$username, $remote];
|
||
|
|
$this->assertTrue($this->repository->assignUserToDatabase($own, $username, $remote));
|
||
|
|
$this->repository->flush();
|
||
|
|
|
||
|
|
$login = $this->login($username, $password, $own);
|
||
|
|
|
||
|
|
$login->statement("CREATE TABLE `{$own}`.`owned` (`id` INT)");
|
||
|
|
$this->assertNotNull($login->selectOne("SELECT COUNT(*) AS value FROM `{$own}`.`owned`"));
|
||
|
|
|
||
|
|
$visible = array_map(fn ($row) => $row->Database, $login->select('SHOW DATABASES'));
|
||
|
|
$this->assertContains($own, $visible);
|
||
|
|
$this->assertNotContains($other, $visible);
|
||
|
|
|
||
|
|
try {
|
||
|
|
$login->statement("CREATE TABLE `{$other}`.`other` (`id` INT)");
|
||
|
|
$this->fail("The grant on {$own} also applied to {$other}.");
|
||
|
|
} catch (QueryException $exception) {
|
||
|
|
$this->assertStringContainsString('denied', $exception->getMessage());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private function createDatabase(string $name): string
|
||
|
|
{
|
||
|
|
$this->assertTrue($this->repository->createDatabase($name));
|
||
|
|
$this->databases[] = $name;
|
||
|
|
|
||
|
|
return $name;
|
||
|
|
}
|
||
|
|
|
||
|
|
private function login(string $username, string $password, string $database): Connection
|
||
|
|
{
|
||
|
|
DB::purge(self::LOGIN_CONNECTION);
|
||
|
|
config()->set('database.connections.' . self::LOGIN_CONNECTION, array_merge(config('database.connections.mysql'), [
|
||
|
|
'username' => $username,
|
||
|
|
'password' => $password,
|
||
|
|
'database' => $database,
|
||
|
|
]));
|
||
|
|
|
||
|
|
return DB::connection(self::LOGIN_CONNECTION);
|
||
|
|
}
|
||
|
|
}
|