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/DeleteSubuserTest.php
7461 views
1
<?php
2
3
namespace Pterodactyl\Tests\Integration\Api\Client\Server\Subuser;
4
5
use Ramsey\Uuid\Uuid;
6
use Pterodactyl\Models\User;
7
use Pterodactyl\Models\Subuser;
8
use Pterodactyl\Models\Permission;
9
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
10
use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;
11
12
class DeleteSubuserTest extends ClientApiIntegrationTestCase
13
{
14
/**
15
* Guards against PHP's exciting behavior where a string can be cast to an int and only
16
* the first numeric digits are returned. This causes UUIDs to be returned as an int when
17
* looking up users, thus returning the wrong subusers (or no subuser at all).
18
*
19
* For example, 12aaaaaa-bbbb-cccc-ddddeeeeffff would be cast to "12" if you tried to cast
20
* it to an integer. Then, in the deep API middlewares you would end up trying to load a user
21
* with an ID of 12, which may or may not exist and be wrongly assigned to the model object.
22
*
23
* @see https://github.com/pterodactyl/panel/issues/2359
24
*/
25
public function testCorrectSubuserIsDeletedFromServer()
26
{
27
$this->swap(DaemonServerRepository::class, $mock = \Mockery::mock(DaemonServerRepository::class));
28
29
[$user, $server] = $this->generateTestAccount();
30
31
/** @var User $differentUser */
32
$differentUser = User::factory()->create();
33
34
$real = Uuid::uuid4()->toString();
35
// Generate a UUID that lines up with a user in the database if it were to be cast to an int.
36
$uuid = $differentUser->id . substr($real, strlen((string) $differentUser->id));
37
38
/** @var User $subuser */
39
$subuser = User::factory()->create(['uuid' => $uuid]);
40
41
Subuser::query()->forceCreate([
42
'user_id' => $subuser->id,
43
'server_id' => $server->id,
44
'permissions' => [Permission::ACTION_WEBSOCKET_CONNECT],
45
]);
46
47
$mock->expects('setServer->revokeUserJTI')->with($subuser->id)->andReturnUndefined();
48
49
$this->actingAs($user)->deleteJson($this->link($server) . "/users/$subuser->uuid")->assertNoContent();
50
51
// Try the same test, but this time with a UUID that if cast to an int (shouldn't) line up with
52
// anything in the database.
53
$uuid = '18180000' . substr(Uuid::uuid4()->toString(), 8);
54
/** @var User $subuser */
55
$subuser = User::factory()->create(['uuid' => $uuid]);
56
57
Subuser::query()->forceCreate([
58
'user_id' => $subuser->id,
59
'server_id' => $server->id,
60
'permissions' => [Permission::ACTION_WEBSOCKET_CONNECT],
61
]);
62
63
$mock->expects('setServer->revokeUserJTI')->with($subuser->id)->andReturnUndefined();
64
65
$this->actingAs($user)->deleteJson($this->link($server) . "/users/$subuser->uuid")->assertNoContent();
66
}
67
}
68
69