From f31a6d3967b89b7b37ef41d677efd0add73de606 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sun, 27 Sep 2020 10:39:18 -0700 Subject: [PATCH] Fix parameter bindings for client API routes; closes pterodactyl/panel#2359 --- .../Client/SubstituteClientApiBindings.php | 4 +- .../Subuser/CreateServerSubuserTest.php | 2 +- .../Server/Subuser/DeleteSubuserTest.php | 59 +++++++++++++++++++ 3 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 tests/Integration/Api/Client/Server/Subuser/DeleteSubuserTest.php diff --git a/app/Http/Middleware/Api/Client/SubstituteClientApiBindings.php b/app/Http/Middleware/Api/Client/SubstituteClientApiBindings.php index 77879c97f..7ab597b63 100644 --- a/app/Http/Middleware/Api/Client/SubstituteClientApiBindings.php +++ b/app/Http/Middleware/Api/Client/SubstituteClientApiBindings.php @@ -49,11 +49,11 @@ class SubstituteClientApiBindings extends ApiSubstituteBindings return Database::query()->where('id', $id)->firstOrFail(); }); - $this->router->model('backup', Backup::class, function ($value) { + $this->router->bind('backup', function ($value) { return Backup::query()->where('uuid', $value)->firstOrFail(); }); - $this->router->model('user', User::class, function ($value) { + $this->router->bind('user', function ($value) { return User::query()->where('uuid', $value)->firstOrFail(); }); diff --git a/tests/Integration/Api/Client/Server/Subuser/CreateServerSubuserTest.php b/tests/Integration/Api/Client/Server/Subuser/CreateServerSubuserTest.php index 53a299e02..3a1f8bc6b 100644 --- a/tests/Integration/Api/Client/Server/Subuser/CreateServerSubuserTest.php +++ b/tests/Integration/Api/Client/Server/Subuser/CreateServerSubuserTest.php @@ -1,6 +1,6 @@ generateTestAccount(); + + /** @var \Pterodactyl\Models\User $differentUser */ + $differentUser = factory(User::class)->create(); + + // Generate a UUID that lines up with a user in the database if it were to be cast to an int. + $uuid = $differentUser->id . str_repeat('a', strlen((string)$differentUser->id)) . substr(Uuid::uuid4()->toString(), 8); + + /** @var \Pterodactyl\Models\User $subuser */ + $subuser = factory(User::class)->create(['uuid' => $uuid]); + + Subuser::query()->forceCreate([ + 'user_id' => $subuser->id, + 'server_id' => $server->id, + 'permissions' => [ Permission::ACTION_WEBSOCKET_CONNECT ], + ]); + + $this->actingAs($user)->deleteJson($this->link($server) . "/users/{$subuser->uuid}")->assertNoContent(); + + // Try the same test, but this time with a UUID that if cast to an int (shouldn't) line up with + // anything in the database. + $uuid = '18180000' . substr(Uuid::uuid4()->toString(), 8); + /** @var \Pterodactyl\Models\User $subuser */ + $subuser = factory(User::class)->create(['uuid' => $uuid]); + + Subuser::query()->forceCreate([ + 'user_id' => $subuser->id, + 'server_id' => $server->id, + 'permissions' => [ Permission::ACTION_WEBSOCKET_CONNECT ], + ]); + + $this->actingAs($user)->deleteJson($this->link($server) . "/users/{$subuser->uuid}")->assertNoContent(); + } +}