Files

136 lines
5.1 KiB
PHP
Raw Permalink Normal View History

2026-12-01 04:58:46 +09:00
<?php
declare(strict_types=1);
namespace Pterodactyl\Tests\Pest\Integration\Services\Databases\DatabaseHostGatewayTest;
use Illuminate\Database\Connection;
use Illuminate\Database\MySqlConnection;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Pterodactyl\Services\Databases\DatabaseHostGateway;
use Pterodactyl\Tests\Integration\IntegrationTestCase;
// Runs the gateway 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.
uses(IntegrationTestCase::class);
const LOGIN_CONNECTION = 'database_host_login_test';
beforeEach(function (): void {
// The gateway's own connection, pointed at the test server. These statements commit any
// open transaction, so they must not share the test's connection.
config()->set('database.connections.'.DatabaseHostGateway::DEFAULT_CONNECTION_NAME, config('database.connections.mysql'));
$this->gateway = new DatabaseHostGateway();
$this->databases = [];
$this->users = [];
});
afterEach(function (): void {
DB::purge(LOGIN_CONNECTION);
foreach ($this->users as [$username, $remote]) {
$this->gateway->dropUser($username, $remote);
}
foreach ($this->databases as $database) {
$this->gateway->dropDatabase($database);
}
DB::purge(DatabaseHostGateway::DEFAULT_CONNECTION_NAME);
});
test('a grant only matches the exact database name', function (): void {
assertGrantIsIsolated($this);
});
test('a grant only matches the exact database name with partial_revokes enabled', function (): void {
$connection = DB::connection(DatabaseHostGateway::DEFAULT_CONNECTION_NAME);
if ($connection instanceof MySqlConnection && $connection->isMaria()) {
$this->markTestSkipped('MariaDB has no partial_revokes.');
}
$original = (bool) $connection->scalar('SELECT @@global.partial_revokes');
$connection->statement('SET GLOBAL partial_revokes = ON');
DB::purge(DatabaseHostGateway::DEFAULT_CONNECTION_NAME);
try {
assertGrantIsIsolated($this);
} finally {
DB::connection(DatabaseHostGateway::DEFAULT_CONNECTION_NAME)->statement('SET GLOBAL partial_revokes = '.($original ? 'ON' : 'OFF'));
}
});
test('identifiers and passwords are quoted', function (): void {
$suffix = Str::lower(Str::random(8));
$database = createHostDatabase($this, "odd`name_{$suffix}");
$username = 'q_'.$suffix;
$remote = '10.0.0.%';
expect($this->gateway->createUser($username, $remote, "it's a \\ `pass` \"word\"", 5))->toBeTrue();
$this->users[] = [$username, $remote];
expect($this->gateway->assignUserToDatabase($database, $username, $remote))->toBeTrue();
$connection = DB::connection(DatabaseHostGateway::DEFAULT_CONNECTION_NAME);
expect($connection->scalar('SELECT COUNT(*) FROM information_schema.schemata WHERE schema_name = ?', [$database]))->toEqual(1)
->and($connection->scalar('SELECT max_user_connections FROM mysql.user WHERE User = ? AND Host = ?', [$username, $remote]))->toEqual(5);
expect($this->gateway->dropUser($username, $remote))->toBeTrue()
->and($connection->scalar('SELECT COUNT(*) FROM mysql.user WHERE User = ?', [$username]))->toEqual(0);
});
/**
* A grant on a database name containing "_" applies to that name only.
*/
function assertGrantIsIsolated(object $test): void
{
$suffix = Str::lower(Str::random(8));
$other = createHostDatabase($test, "gx{$suffix}");
$own = createHostDatabase($test, "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((string) DB::connection(DatabaseHostGateway::DEFAULT_CONNECTION_NAME)->scalar('SELECT USER()'), '@');
$username = 'u_'.$suffix;
$password = "it's-".Str::random(16);
expect($test->gateway->createUser($username, $remote, $password, null))->toBeTrue();
$test->users[] = [$username, $remote];
expect($test->gateway->assignUserToDatabase($own, $username, $remote))->toBeTrue();
$test->gateway->flush();
$login = login($username, $password, $own);
$login->statement("CREATE TABLE `{$own}`.`owned` (`id` INT)");
expect($login->scalar("SELECT COUNT(*) FROM `{$own}`.`owned`"))->toEqual(0);
$visible = array_map(fn (object $row) => $row->Database, $login->select('SHOW DATABASES'));
expect($visible)->toContain($own)->not->toContain($other);
expect(fn () => $login->statement("CREATE TABLE `{$other}`.`other` (`id` INT)"))
->toThrow(QueryException::class, 'denied');
}
function createHostDatabase(object $test, string $name): string
{
expect($test->gateway->createDatabase($name))->toBeTrue();
$test->databases[] = $name;
return $name;
}
function login(string $username, string $password, string $database): Connection
{
DB::purge(LOGIN_CONNECTION);
config()->set('database.connections.'.LOGIN_CONNECTION, [
...config('database.connections.mysql'),
'username' => $username,
'password' => $password,
'database' => $database,
]);
return DB::connection(LOGIN_CONNECTION);
}