diff --git a/app/Exceptions/ApiErrorResponse.php b/app/Exceptions/ApiErrorResponse.php index d783f58b4..403976224 100644 --- a/app/Exceptions/ApiErrorResponse.php +++ b/app/Exceptions/ApiErrorResponse.php @@ -8,8 +8,11 @@ use Illuminate\Auth\Access\AuthorizationException; use Illuminate\Auth\AuthenticationException; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Http\JsonResponse; +use Illuminate\Support\Arr; use Illuminate\Support\Str; +use Illuminate\Validation\Rules\Password; use Illuminate\Validation\ValidationException; +use Illuminate\Validation\Validator; use Pterodactyl\Exceptions\Extensions\InvalidExtensionException; use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; use Throwable; @@ -164,8 +167,12 @@ final class ApiErrorResponse foreach (array_keys($reasons) as $reason) { throw_unless(is_string($reason), UnexpectedValueException::class, 'Laravel returned a malformed validation rule name.'); - // Laravel's own rule objects, such as Password, are named after their class. - $cleaned[] = str_starts_with($reason, 'Illuminate\\') ? Str::snake(class_basename($reason)) : Str::snake($reason); + // Laravel's own rule objects are named after their class. + $cleaned[] = match (true) { + $reason === Password::class => self::passwordRuleCode($exception, $field), + str_starts_with($reason, 'Illuminate\\') => Str::snake(class_basename($reason)), + default => Str::snake($reason), + }; } $codes[str_replace('.', '_', $field)] = $cleaned; @@ -174,6 +181,19 @@ final class ApiErrorResponse return $codes; } + /** + * Laravel's Password rule fails under its own name. A password shorter than the + * policy's minimum has always been reported as the "min" rule, so it keeps that code; + * any other part of the policy reports "password". + */ + private static function passwordRuleCode(ValidationException $exception, string $field): string + { + $min = Password::default()->appliedRules()['min']; + $value = $exception->validator instanceof Validator ? Arr::get($exception->validator->getData(), $field) : null; + + return is_int($min) && is_string($value) && mb_strlen($value) < $min ? 'min' : 'password'; + } + /** @return list> */ private static function cleanTrace(Throwable $exception): array { diff --git a/tests/Integration/Api/Admin/Users/UserControllerTest.php b/tests/Integration/Api/Admin/Users/UserControllerTest.php index 2f525d3c3..82d1d18a0 100644 --- a/tests/Integration/Api/Admin/Users/UserControllerTest.php +++ b/tests/Integration/Api/Admin/Users/UserControllerTest.php @@ -167,7 +167,7 @@ test('non admin forbidden', function (string $method, string $routeName): void { test('passwords shorter than eight characters are rejected', function (string $method, string $route): void { $user = User::factory()->create(); $response = $this->{$method}(route($route, $route === 'api.admin.users.update' ? ['user' => $user->id] : []), ['username' => 'weakuser', 'email' => 'weak@example.com', 'name_first' => 'Weak', 'name_last' => 'Password', 'password' => 'abc']); - $response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY)->assertJsonPath('errors.0.meta.source_field', 'password')->assertJsonPath('errors.0.meta.rule', 'password'); + $response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY)->assertJsonPath('errors.0.meta.source_field', 'password')->assertJsonPath('errors.0.meta.rule', 'min'); $this->assertDatabaseMissing('users', ['username' => 'weakuser']); })->with([['postJson', 'api.admin.users.store'], ['putJson', 'api.admin.users.update']]); test('a blank password leaves the password unchanged', function (): void { diff --git a/tests/Integration/Api/Application/Users/UserControllerTest.php b/tests/Integration/Api/Application/Users/UserControllerTest.php index e2faf1a0b..111d36d67 100644 --- a/tests/Integration/Api/Application/Users/UserControllerTest.php +++ b/tests/Integration/Api/Application/Users/UserControllerTest.php @@ -90,7 +90,7 @@ test('passwords shorter than eight characters are rejected', function (string $m $user = User::factory()->create(); $url = '/api/application/users'.str_replace('{user}', (string) $user->id, $suffix); $response = $this->{$method}($url, ['username' => 'weakuser', 'email' => 'weak@example.com', 'first_name' => 'Weak', 'last_name' => 'Password', 'password' => 'abc']); - $response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY)->assertJsonPath('errors.0.meta.source_field', 'password')->assertJsonPath('errors.0.meta.rule', 'password'); + $response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY)->assertJsonPath('errors.0.meta.source_field', 'password')->assertJsonPath('errors.0.meta.rule', 'min'); $this->assertDatabaseMissing('users', ['username' => 'weakuser']); })->with([['postJson', ''], ['patchJson', '/{user}']]); test('a user can be created with an eight character password', function (): void { diff --git a/tests/Integration/Api/Client/AccountControllerTest.php b/tests/Integration/Api/Client/AccountControllerTest.php index ec5d7f37f..95fe84c28 100644 --- a/tests/Integration/Api/Client/AccountControllerTest.php +++ b/tests/Integration/Api/Client/AccountControllerTest.php @@ -113,7 +113,7 @@ test('password is not updated if current password is invalid', function (): void test('error is returned for invalid request data', function (): void { $user = User::factory()->create(); $this->actingAs($user)->putJson('/api/client/account/password', ['current_password' => 'password'])->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY)->assertJsonPath('errors.0.meta.rule', 'required'); - $this->actingAs($user)->putJson('/api/client/account/password', ['current_password' => 'password', 'password' => 'pass', 'password_confirmation' => 'pass'])->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY)->assertJsonPath('errors.0.meta.rule', 'password'); + $this->actingAs($user)->putJson('/api/client/account/password', ['current_password' => 'password', 'password' => 'pass', 'password_confirmation' => 'pass'])->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY)->assertJsonPath('errors.0.meta.rule', 'min'); }); test('error is returned if password is not confirmed', function (): void { /** @var User $user */ diff --git a/tests/Unit/Exceptions/ApiErrorResponseTest.php b/tests/Unit/Exceptions/ApiErrorResponseTest.php index b36d636ce..b7e197371 100644 --- a/tests/Unit/Exceptions/ApiErrorResponseTest.php +++ b/tests/Unit/Exceptions/ApiErrorResponseTest.php @@ -4,6 +4,9 @@ declare(strict_types=1); namespace Pterodactyl\Tests\Pest\Unit\Exceptions\ApiErrorResponseTest; +use Illuminate\Support\Facades\Validator; +use Illuminate\Validation\Rules\Password; +use Illuminate\Validation\ValidationException; use Pterodactyl\Exceptions\ApiErrorResponse; use Pterodactyl\Tests\TestCase; use RuntimeException; @@ -32,6 +35,16 @@ test('debug traces remove arguments and use each previous exception trace', func $this->assertArrayNotHasKey('object', $frame); } }); +test('a password shorter than the policy reports the min rule, and any other password failure reports password', function () { + $rule = fn (string $password): string => json_decode(ApiErrorResponse::validation( + new ValidationException(Validator::make(['password' => $password], ['password' => ['required', 'string', Password::default()]])) + )->getContent(), true)['errors'][0]['meta']['rule']; + + expect($rule('short'))->toBe('min'); + + Password::defaults(Password::min(8)->mixedCase()); + expect($rule('all-lower-case'))->toBe('password'); +}); function nestedException(): Throwable { try {