Skip to content

Commit 1f7fe09

Browse files
committed
Correctly validate description for API keys to match model expectations; closes pterodactyl#2457
1 parent 1f28fb9 commit 1f7fe09

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

app/Http/Requests/Api/Client/Account/StoreApiKeyRequest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Pterodactyl\Http\Requests\Api\Client\Account;
44

5+
use Pterodactyl\Models\ApiKey;
56
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
67

78
class StoreApiKeyRequest extends ClientApiRequest
@@ -11,9 +12,11 @@ class StoreApiKeyRequest extends ClientApiRequest
1112
*/
1213
public function rules(): array
1314
{
15+
$rules = ApiKey::getRules();
16+
1417
return [
15-
'description' => 'required|string|min:4',
16-
'allowed_ips' => 'array',
18+
'description' => $rules['memo'],
19+
'allowed_ips' => $rules['allowed_ips'],
1720
'allowed_ips.*' => 'ip',
1821
];
1922
}

tests/Integration/Api/Client/ApiKeyControllerTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ public function testNoMoreThanFiveApiKeysCanBeCreatedForAnAccount()
121121

122122
/**
123123
* Test that a bad request results in a validation error being returned by the API.
124+
*
125+
* @see https://github.com/pterodactyl/panel/issues/2457
124126
*/
125127
public function testValidationErrorIsReturnedForBadRequests()
126128
{
@@ -135,6 +137,15 @@ public function testValidationErrorIsReturnedForBadRequests()
135137
$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
136138
$response->assertJsonPath('errors.0.meta.rule', 'required');
137139
$response->assertJsonPath('errors.0.detail', 'The description field is required.');
140+
141+
$response = $this->actingAs($user)->postJson('/api/client/account/api-keys', [
142+
'description' => str_repeat('a', 501),
143+
'allowed_ips' => ['127.0.0.1'],
144+
]);
145+
146+
$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
147+
$response->assertJsonPath('errors.0.meta.rule', 'max');
148+
$response->assertJsonPath('errors.0.detail', 'The description may not be greater than 500 characters.');
138149
}
139150

140151
/**

0 commit comments

Comments
 (0)