Path: blob/1.0-develop/tests/Integration/Api/Client/Server/Database/DatabaseAuthorizationTest.php
7461 views
<?php12namespace Pterodactyl\Tests\Integration\Api\Client\Server\Database;34use Pterodactyl\Models\Subuser;5use Pterodactyl\Models\Database;6use Pterodactyl\Models\DatabaseHost;7use Pterodactyl\Contracts\Extensions\HashidsInterface;8use Pterodactyl\Services\Databases\DatabasePasswordService;9use Pterodactyl\Services\Databases\DatabaseManagementService;10use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;1112class DatabaseAuthorizationTest extends ClientApiIntegrationTestCase13{14#[\PHPUnit\Framework\Attributes\DataProvider('methodDataProvider')]15public function testAccessToAServersDatabasesIsRestrictedProperly(string $method, string $endpoint)16{17// The API $user is the owner of $server1.18[$user, $server1] = $this->generateTestAccount();19// Will be a subuser of $server2.20$server2 = $this->createServerModel();21// And as no access to $server3.22$server3 = $this->createServerModel();2324$host = DatabaseHost::factory()->create([]);2526// Set the API $user as a subuser of server 2, but with no permissions27// to do anything with the databases for that server.28Subuser::factory()->create(['server_id' => $server2->id, 'user_id' => $user->id]);2930$database1 = Database::factory()->create(['server_id' => $server1->id, 'database_host_id' => $host->id]);31$database2 = Database::factory()->create(['server_id' => $server2->id, 'database_host_id' => $host->id]);32$database3 = Database::factory()->create(['server_id' => $server3->id, 'database_host_id' => $host->id]);3334$this35->mock($method === 'POST' ? DatabasePasswordService::class : DatabaseManagementService::class)36->expects($method === 'POST' ? 'handle' : 'delete')37->andReturn($method === 'POST' ? 'foo' : null);3839$hashids = $this->app->make(HashidsInterface::class);40// This is the only valid call for this test, accessing the database for the same41// server that the API user is the owner of.42$this->actingAs($user)->json($method, $this->link($server1, '/databases/' . $hashids->encode($database1->id) . $endpoint))43->assertStatus($method === 'DELETE' ? 204 : 200);4445// This request fails because the database is valid for that server but the user46// making the request is not authorized to perform that action.47$this->actingAs($user)->json($method, $this->link($server2, '/databases/' . $hashids->encode($database2->id) . $endpoint))->assertForbidden();4849// Both of these should report a 404 error due to the database being linked to50// servers that are not the same as the server in the request, or are assigned51// to a server for which the user making the request has no access to.52$this->actingAs($user)->json($method, $this->link($server1, '/databases/' . $hashids->encode($database2->id) . $endpoint))->assertNotFound();53$this->actingAs($user)->json($method, $this->link($server1, '/databases/' . $hashids->encode($database3->id) . $endpoint))->assertNotFound();54$this->actingAs($user)->json($method, $this->link($server2, '/databases/' . $hashids->encode($database3->id) . $endpoint))->assertNotFound();55$this->actingAs($user)->json($method, $this->link($server3, '/databases/' . $hashids->encode($database3->id) . $endpoint))->assertNotFound();56}5758public static function methodDataProvider(): array59{60return [61['POST', '/rotate-password'],62['DELETE', ''],63];64}65}666768