Path: blob/1.0-develop/tests/Integration/Api/Client/Server/Subuser/SubuserAuthorizationTest.php
7461 views
<?php12namespace Pterodactyl\Tests\Integration\Api\Client\Server\Subuser;34use Pterodactyl\Models\User;5use Pterodactyl\Models\Subuser;6use Pterodactyl\Repositories\Wings\DaemonServerRepository;7use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;89class SubuserAuthorizationTest extends ClientApiIntegrationTestCase10{11/**12* Test that mismatched subusers are not accessible to a server.13*/14#[\PHPUnit\Framework\Attributes\DataProvider('methodDataProvider')]15public function testUserCannotAccessResourceBelongingToOtherServers(string $method)16{17// Generic subuser, the specific resource we're trying to access.18/** @var User $internal */19$internal = User::factory()->create();2021// The API $user is the owner of $server1.22[$user, $server1] = $this->generateTestAccount();23// Will be a subuser of $server2.24$server2 = $this->createServerModel();25// And as no access to $server3.26$server3 = $this->createServerModel();2728// Set the API $user as a subuser of server 2, but with no permissions29// to do anything with the subusers for that server.30Subuser::factory()->create(['server_id' => $server2->id, 'user_id' => $user->id]);3132Subuser::factory()->create(['server_id' => $server1->id, 'user_id' => $internal->id]);33Subuser::factory()->create(['server_id' => $server2->id, 'user_id' => $internal->id]);34Subuser::factory()->create(['server_id' => $server3->id, 'user_id' => $internal->id]);3536$this->instance(DaemonServerRepository::class, $mock = \Mockery::mock(DaemonServerRepository::class));37if ($method === 'DELETE') {38$mock->expects('setServer->revokeUserJTI')->with($internal->id)->andReturnUndefined();39}4041// This route is acceptable since they're accessing a subuser on their own server.42$this->actingAs($user)->json($method, $this->link($server1, '/users/' . $internal->uuid))->assertStatus($method === 'POST' ? 422 : ($method === 'DELETE' ? 204 : 200));4344// This route can be revealed since the subuser belongs to the correct server, but45// errors out with a 403 since $user does not have the right permissions for this.46$this->actingAs($user)->json($method, $this->link($server2, '/users/' . $internal->uuid))->assertForbidden();47$this->actingAs($user)->json($method, $this->link($server3, '/users/' . $internal->uuid))->assertNotFound();48}4950public static function methodDataProvider(): array51{52return [['GET'], ['POST'], ['DELETE']];53}54}555657