Path: blob/1.0-develop/tests/Integration/Api/Client/Server/Subuser/SubuserAuthorizationTest.php
10283 views
<?php12namespace Pterodactyl\Tests\Integration\Api\Client\Server\Subuser;34use Mockery\MockInterface;5use Pterodactyl\Models\User;6use Pterodactyl\Models\Subuser;7use Pterodactyl\Repositories\Wings\DaemonRevocationRepository;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{18// Generic subuser, the specific resource we're trying to access.19/** @var User $internal */20$internal = User::factory()->create();2122// The API $user is the owner of $server1.23[$user, $server1] = $this->generateTestAccount();24// Will be a subuser of $server2.25$server2 = $this->createServerModel();26// And as no access to $server3.27$server3 = $this->createServerModel();2829// Set the API $user as a subuser of server 2, but with no permissions30// to do anything with the subusers for that server.31Subuser::factory()->create(['server_id' => $server2->id, 'user_id' => $user->id]);3233Subuser::factory()->create(['server_id' => $server1->id, 'user_id' => $internal->id]);34Subuser::factory()->create(['server_id' => $server2->id, 'user_id' => $internal->id]);35Subuser::factory()->create(['server_id' => $server3->id, 'user_id' => $internal->id]);3637$this->mock(DaemonRevocationRepository::class, function (MockInterface $mock) use ($method) {38$mock->expects('setNode->deauthorize')->times($method === 'DELETE' ? 1 : 0)->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