From 8b0d7d5851b88326c9f2a7a1121fdefc70317524 Mon Sep 17 00:00:00 2001 From: Jeremy Angele Date: Fri, 19 Jan 2024 21:00:28 +0100 Subject: [PATCH] Add default max length for Password rule object --- src/Illuminate/Validation/Rules/Password.php | 10 +++------- tests/Validation/ValidationPasswordRuleTest.php | 6 +++++- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Illuminate/Validation/Rules/Password.php b/src/Illuminate/Validation/Rules/Password.php index 588f357334cc..6e94d4136518 100644 --- a/src/Illuminate/Validation/Rules/Password.php +++ b/src/Illuminate/Validation/Rules/Password.php @@ -42,7 +42,7 @@ class Password implements Rule, DataAwareRule, ValidatorAwareRule * * @var int */ - protected $max; + protected $max = 72; /** * If the password requires at least one uppercase and one lowercase letter. @@ -218,7 +218,7 @@ public static function min($size) */ public function max($size) { - $this->max = $size; + $this->max = (int) $size; return $this; } @@ -312,7 +312,7 @@ public function passes($attribute, $value) $validator = Validator::make( $this->data, - [$attribute => array_merge(['string', 'min:'.$this->min], $this->customRules)], + [$attribute => array_merge(['string', 'min:'.$this->min, 'max:'.$this->max], $this->customRules)], $this->validator->customMessages, $this->validator->customAttributes )->after(function ($validator) use ($attribute, $value) { @@ -320,10 +320,6 @@ public function passes($attribute, $value) return; } - if ($this->max && mb_strlen($value) > $this->max) { - $validator->addFailure($attribute, 'max.string'); - } - if ($this->mixedCase && ! preg_match('/(\p{Ll}+.*\p{Lu})|(\p{Lu}+.*\p{Ll})/u', $value)) { $validator->addFailure($attribute, 'password.mixed'); } diff --git a/tests/Validation/ValidationPasswordRuleTest.php b/tests/Validation/ValidationPasswordRuleTest.php index 8fb9673ebff9..f07b7b759413 100644 --- a/tests/Validation/ValidationPasswordRuleTest.php +++ b/tests/Validation/ValidationPasswordRuleTest.php @@ -44,6 +44,10 @@ public function testMin() public function testMax() { + $this->fails(Password::min(2), [str_repeat('a', 73)], [ + 'validation.max.string', + ]); + $this->fails(Password::min(2)->max(4), ['aaaaa', '11111111'], [ 'validation.max.string', ]); @@ -139,7 +143,7 @@ public function testUncompromised() '%HurHUnw7zM!', 'rundeliekend', '7Z^k5EvqQ9g%c!Jt9$ufnNpQy#Kf', - 'NRs*Gz2@hSmB$vVBSPDfqbRtEzk4nF7ZAbM29VMW$BPD%b2U%3VmJAcrY5eZGVxP%z%apnwSX', + 'NRs*Gz2@hSmB$vVBSPDfqbRtEzk4nF7ZAbM29VMW$BPD%b2U%3VmJAcrY5eZGVxP%z%apnwS', ]); }