mirror of
https://github.com/pterodactyl/panel.git
synced 2026-12-02 20:35:50 -05:00
password rule code
This commit is contained in:
@@ -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<array<string, ApiScalar>> */
|
||||
private static function cleanTrace(Throwable $exception): array
|
||||
{
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 */
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user