Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/tests/Integration/Api/Client/Server/Subuser/SubuserAuthorizationTest.php
7461 views
1
<?php
2
3
namespace Pterodactyl\Tests\Integration\Api\Client\Server\Subuser;
4
5
use Pterodactyl\Models\User;
6
use Pterodactyl\Models\Subuser;
7
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
8
use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;
9
10
class SubuserAuthorizationTest extends ClientApiIntegrationTestCase
11
{
12
/**
13
* Test that mismatched subusers are not accessible to a server.
14
*/
15
#[\PHPUnit\Framework\Attributes\DataProvider('methodDataProvider')]
16
public 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();
21
22
// 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();
28
29
// Set the API $user as a subuser of server 2, but with no permissions
30
// to do anything with the subusers for that server.
31
Subuser::factory()->create(['server_id' => $server2->id, 'user_id' => $user->id]);
32
33
Subuser::factory()->create(['server_id' => $server1->id, 'user_id' => $internal->id]);
34
Subuser::factory()->create(['server_id' => $server2->id, 'user_id' => $internal->id]);
35
Subuser::factory()->create(['server_id' => $server3->id, 'user_id' => $internal->id]);
36
37
$this->instance(DaemonServerRepository::class, $mock = \Mockery::mock(DaemonServerRepository::class));
38
if ($method === 'DELETE') {
39
$mock->expects('setServer->revokeUserJTI')->with($internal->id)->andReturnUndefined();
40
}
41
42
// This route is acceptable since they're accessing a subuser on their own server.
43
$this->actingAs($user)->json($method, $this->link($server1, '/users/' . $internal->uuid))->assertStatus($method === 'POST' ? 422 : ($method === 'DELETE' ? 204 : 200));
44
45
// This route can be revealed since the subuser belongs to the correct server, but
46
// errors out with a 403 since $user does not have the right permissions for this.
47
$this->actingAs($user)->json($method, $this->link($server2, '/users/' . $internal->uuid))->assertForbidden();
48
$this->actingAs($user)->json($method, $this->link($server3, '/users/' . $internal->uuid))->assertNotFound();
49
}
50
51
public static function methodDataProvider(): array
52
{
53
return [['GET'], ['POST'], ['DELETE']];
54
}
55
}
56
57