Path: blob/1.0-develop/tests/Integration/Api/Client/Server/Subuser/SubuserAuthorizationTest.php
14052 views
<?php12namespace Pterodactyl\Tests\Integration\Api\Client\Server\Subuser;34use Pterodactyl\Models\User;5use Pterodactyl\Models\Subuser;6use Illuminate\Support\Facades\Bus;7use Pterodactyl\Jobs\RevokeSftpAccessJob;8use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;910class SubuserAuthorizationTest extends ClientApiIntegrationTestCase11{12/**13* Test that mismatched subusers are not accessible to a server.14*/15#[\PHPUnit\Framework\Attributes\DataProvider('methodDataProvider')]16public function testUserCannotAccessResourceBelongingToOtherServers(string $method)17{18Bus::fake([RevokeSftpAccessJob::class]);1920// Generic subuser, the specific resource we're trying to access.21/** @var User $internal */22$internal = User::factory()->create();2324// The API $user is the owner of $server1.25[$user, $server1] = $this->generateTestAccount();26// Will be a subuser of $server2.27$server2 = $this->createServerModel();28// And as no access to $server3.29$server3 = $this->createServerModel();3031// Set the API $user as a subuser of server 2, but with no permissions32// to do anything with the subusers for that server.33Subuser::factory()->create(['server_id' => $server2->id, 'user_id' => $user->id]);3435Subuser::factory()->create(['server_id' => $server1->id, 'user_id' => $internal->id]);36Subuser::factory()->create(['server_id' => $server2->id, 'user_id' => $internal->id]);37Subuser::factory()->create(['server_id' => $server3->id, 'user_id' => $internal->id]);3839// This route is acceptable since they're accessing a subuser on their own server.40$this->actingAs($user)->json($method, $this->link($server1, '/users/' . $internal->uuid))->assertStatus($method === 'POST' ? 422 : ($method === 'DELETE' ? 204 : 200));4142// This route can be revealed since the subuser belongs to the correct server, but43// errors out with a 403 since $user does not have the right permissions for this.44$this->actingAs($user)->json($method, $this->link($server2, '/users/' . $internal->uuid))->assertForbidden();45$this->actingAs($user)->json($method, $this->link($server3, '/users/' . $internal->uuid))->assertNotFound();4647if ($method === 'DELETE') {48Bus::assertDispatchedTimes(function (RevokeSftpAccessJob $job) use ($server1, $internal) {49return $job->user === $internal->uuid && $job->target->is($server1);50});51} else {52Bus::assertNotDispatched(RevokeSftpAccessJob::class);53}54}5556public static function methodDataProvider(): array57{58return [['GET'], ['POST'], ['DELETE']];59}60}616263