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
10283 views
1
<?php
2
3
namespace Pterodactyl\Tests\Integration\Api\Client\Server\Subuser;
4
5
use Mockery\MockInterface;
6
use Pterodactyl\Models\User;
7
use Pterodactyl\Models\Subuser;
8
use Pterodactyl\Repositories\Wings\DaemonRevocationRepository;
9
use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;
10
11
class SubuserAuthorizationTest extends ClientApiIntegrationTestCase
12
{
13
/**
14
* Test that mismatched subusers are not accessible to a server.
15
*/
16
#[\PHPUnit\Framework\Attributes\DataProvider('methodDataProvider')]
17
public function testUserCannotAccessResourceBelongingToOtherServers(string $method)
18
{
19
// Generic subuser, the specific resource we're trying to access.
20
/** @var User $internal */
21
$internal = User::factory()->create();
22
23
// The API $user is the owner of $server1.
24
[$user, $server1] = $this->generateTestAccount();
25
// Will be a subuser of $server2.
26
$server2 = $this->createServerModel();
27
// And as no access to $server3.
28
$server3 = $this->createServerModel();
29
30
// Set the API $user as a subuser of server 2, but with no permissions
31
// to do anything with the subusers for that server.
32
Subuser::factory()->create(['server_id' => $server2->id, 'user_id' => $user->id]);
33
34
Subuser::factory()->create(['server_id' => $server1->id, 'user_id' => $internal->id]);
35
Subuser::factory()->create(['server_id' => $server2->id, 'user_id' => $internal->id]);
36
Subuser::factory()->create(['server_id' => $server3->id, 'user_id' => $internal->id]);
37
38
$this->mock(DaemonRevocationRepository::class, function (MockInterface $mock) use ($method) {
39
$mock->expects('setNode->deauthorize')->times($method === 'DELETE' ? 1 : 0)->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