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