Files
pterodactyl-panel/tests/Integration/Api/Client/Server/Database/DatabaseAuthorizationTest.php
T

57 lines
3.4 KiB
PHP
Raw Normal View History

<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
2026-10-05 18:10:18 -04:00
namespace Pterodactyl\Tests\Pest\Integration\Api\Client\Server\Database\DatabaseAuthorizationTest;
use Pterodactyl\Contracts\Extensions\HashidsInterface;
use Pterodactyl\Models\Database;
use Pterodactyl\Models\DatabaseHost;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Models\Subuser;
use Pterodactyl\Services\Databases\DatabaseHostGateway;
use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Tests\Support\Fakes\FakeDatabaseHostGateway;
uses(ClientApiIntegrationTestCase::class);
dataset('methodDataProvider', function () {
return [['POST', '/rotate-password'], ['DELETE', '']];
});
test('access to a servers databases is restricted properly', function (string $method, string $endpoint) {
// The API $user is the owner of $server1.
[$user, $server1] = $this->generateTestAccount();
// Will be a subuser of $server2.
$server2 = $this->createServerModel();
// And as no access to $server3.
$server3 = $this->createServerModel();
$host = DatabaseHost::factory()->create([]);
// Set the API $user as a subuser of server 2, but with no permissions
// to do anything with the databases for that server.
Subuser::factory()->create(['server_id' => $server2->id, 'user_id' => $user->id]);
$database1 = Database::factory()->create(['server_id' => $server1->id, 'database_host_id' => $host->id]);
$database2 = Database::factory()->create(['server_id' => $server2->id, 'database_host_id' => $host->id]);
$database3 = Database::factory()->create(['server_id' => $server3->id, 'database_host_id' => $host->id]);
$this->app->instance(DatabaseHostGateway::class, $gateway = new FakeDatabaseHostGateway());
$hashids = $this->app->make(HashidsInterface::class);
// This is the only valid call for this test, accessing the database for the same
// server that the API user is the owner of.
$this->actingAs($user)->json($method, $this->link($server1, '/databases/'.$hashids->encode($database1->id).$endpoint))->assertStatus($method === 'DELETE' ? 204 : 200);
// This request fails because the database is valid for that server but the user
// making the request is not authorized to perform that action.
$this->actingAs($user)->json($method, $this->link($server2, '/databases/'.$hashids->encode($database2->id).$endpoint))->assertForbidden();
// Both of these should report a 404 error due to the database being linked to
// servers that are not the same as the server in the request, or are assigned
// to a server for which the user making the request has no access to.
$this->actingAs($user)->json($method, $this->link($server1, '/databases/'.$hashids->encode($database2->id).$endpoint))->assertNotFound();
$this->actingAs($user)->json($method, $this->link($server1, '/databases/'.$hashids->encode($database3->id).$endpoint))->assertNotFound();
$this->actingAs($user)->json($method, $this->link($server2, '/databases/'.$hashids->encode($database3->id).$endpoint))->assertNotFound();
$this->actingAs($user)->json($method, $this->link($server3, '/databases/'.$hashids->encode($database3->id).$endpoint))->assertNotFound();
if ($method === 'POST') {
$gateway->assertFlushed(1);
expect($gateway->count('createUser'))->toBe(1);
} else {
$gateway->assertDroppedDatabase($database1->database);
$gateway->assertDroppedUser($database1->username, $database1->remote);
$gateway->assertFlushed(1);
}
2026-10-05 18:10:18 -04:00
})->with('methodDataProvider');