From bf06820c1dc9a84c259885b7dd77b422e7716f68 Mon Sep 17 00:00:00 2001 From: neznaika0 Date: Fri, 13 Nov 2026 19:01:21 +0300 Subject: [PATCH 1/3] fix: Unicode support for `NothingPersonalValidator` --- .../Passwords/NothingPersonalValidator.php | 30 +-- tests/Unit/NothingPersonalValidatorTest.php | 203 ++++++++++++++++++ 2 files changed, 218 insertions(+), 15 deletions(-) diff --git a/src/Authentication/Passwords/NothingPersonalValidator.php b/src/Authentication/Passwords/NothingPersonalValidator.php index 312157ba5..6e1e344b5 100644 --- a/src/Authentication/Passwords/NothingPersonalValidator.php +++ b/src/Authentication/Passwords/NothingPersonalValidator.php @@ -32,7 +32,7 @@ class NothingPersonalValidator extends BaseValidator implements ValidatorInterfa */ public function check(string $password, ?User $user = null): Result { - $password = strtolower($password); + $password = mb_strtolower($password); if ($valid = $this->isNotPersonal($password, $user)) { $valid = $this->isNotSimilar($password, $user); @@ -46,8 +46,6 @@ public function check(string $password, ?User $user = null): Result } /** - * isNotPersonal() - * * Looks for personal information in a password. The personal info used * comes from CodeIgniter\Shield\Entities\User properties username and email. * @@ -59,13 +57,12 @@ public function check(string $password, ?User $user = null): Result * * public $personalFields = ['firstname', 'lastname']; * - * isNotPersonal() returns true if no personal information can be found, or false - * if such info is found. + * @return bool `true` if no personal information can be found, or `false` if such info is found. */ protected function isNotPersonal(string $password, ?User $user): bool { - $userName = strtolower($user->username ?? ''); - $email = strtolower($user->email); + $userName = mb_strtolower($user->username ?? ''); + $email = mb_strtolower($user->email ?? ''); $valid = true; // The most obvious transgressions @@ -112,6 +109,7 @@ protected function isNotPersonal(string $password, ?User $user): bool } } + $needles = array_unique($needles, SORT_LOCALE_STRING); $trivial = [ 'a', 'an', @@ -134,12 +132,12 @@ protected function isNotPersonal(string $password, ?User $user): bool $haystacks = $this->strip_explode($password); foreach ($haystacks as $haystack) { - if (in_array($haystack, $trivial, true) || mb_strlen($haystack, 'UTF-8') < 3) { + if (in_array($haystack, $trivial, true) || mb_strlen($haystack) < 3) { continue; // ignore trivial words } foreach ($needles as $needle) { - if (in_array($needle, $trivial, true) || mb_strlen($needle, 'UTF-8') < 3) { + if (in_array($needle, $trivial, true) || mb_strlen($needle) < 3) { continue; } @@ -152,6 +150,7 @@ protected function isNotPersonal(string $password, ?User $user): bool } } } + if ($valid) { return true; } @@ -163,17 +162,19 @@ protected function isNotPersonal(string $password, ?User $user): bool } /** - * notSimilar() uses $password and $userName to calculate a similarity value. + * Method uses password and username to calculate a similarity value. * Similarity values equal to, or greater than CodeIgniter\Shield\Config::maxSimilarity * are rejected for being too much alike and false is returned. * Otherwise, true is returned, * * A $maxSimilarity value of 0 (zero) returns true without making a comparison. * In other words, 0 (zero) turns off similarity testing. + * + * @return bool `true` if a similarity is found, or `false` if such info is found. */ protected function isNotSimilar(string $password, ?User $user): bool { - if ($user->username === null) { + if (! $user instanceof User || $user->username === null) { return true; } @@ -186,9 +187,10 @@ protected function isNotSimilar(string $password, ?User $user): bool } if ($maxSimilarity > 0) { - $userName = strtolower($user->username); + $userName = mb_strtolower($user->username); similar_text($password, $userName, $similarity); + if ($similarity >= $maxSimilarity) { $this->error = lang('Auth.errorPasswordTooSimilar'); $this->suggestion = lang('Auth.suggestPasswordTooSimilar'); @@ -201,8 +203,6 @@ protected function isNotSimilar(string $password, ?User $user): bool } /** - * strip_explode($str) - * * Replaces all non-word characters and underscores in $str with a space. * Then it explodes that result using the space for a delimiter. * @@ -210,7 +210,7 @@ protected function isNotSimilar(string $password, ?User $user): bool */ protected function strip_explode(string $str): array { - $stripped = preg_replace('/[\W_]+/', ' ', $str); + $stripped = preg_replace('/[\W_]+/u', ' ', $str); $parts = explode(' ', trim((string) $stripped)); // If it's not already there put the untouched input at the top of the array diff --git a/tests/Unit/NothingPersonalValidatorTest.php b/tests/Unit/NothingPersonalValidatorTest.php index 9fb8e3389..91a300afc 100644 --- a/tests/Unit/NothingPersonalValidatorTest.php +++ b/tests/Unit/NothingPersonalValidatorTest.php @@ -376,4 +376,207 @@ public static function provideCheckPasswordWithBadEmail(): iterable ], ]; } + + #[DataProvider('provideIsolatedMethodIsNotPersonal')] + public function testIsolatedMethodIsNotPersonal(mixed ...$input): void + { + $user = null; + $fields = []; + // Password removes spaces inside `check()`, so we’ll duplicate it here + $password = mb_strtolower($input[0]); + + if (is_array($input[1])) { + $user = new User($input[1]); + $fields = array_keys($input[1]); + } + + $config = new Auth(); + $config->personalFields = array_filter($fields, static fn ($val): bool => $val !== 'email' && $val !== 'username'); + $validator = new NothingPersonalValidator($config); + + $isNotPersonal = self::getPrivateMethodInvoker($validator, 'isNotPersonal'); + + /** @var bool */ + $result = $isNotPersonal($password, $user); + + $this->assertSame($input[2], $result); + } + + public static function provideIsolatedMethodIsNotPersonal(): iterable + { + // @todo: strrev() doesn’t work with Unicode + // yield 'Password is equal to reversed username' => [ + // 'ПеТьКа', + // [ + // 'email' => 'аДмИн@СаЙт.Ру', + // 'username' => 'аКьТеП' + // ], + // false + // ]; + + yield 'Empty password and username' => [ + '', + [ + 'email' => 'админ@сайт.ру', + ], + false, + ]; + + yield 'Password is equal to username' => [ + 'ПаРоЛь', + [ + 'email' => 'админ@сайт.ру', + 'username' => 'пАрОлЬ', + ], + false, + ]; + + yield 'Password is equal to email' => [ + 'аДмИн@СаЙт.Ру', + [ + 'email' => 'АдМиН@сАйТ.рУ', + 'username' => 'Петя', + ], + false, + ]; + + yield 'Password contains the username' => [ + 'ПеТя-9000', + [ + 'email' => 'админ@сайт.ру', + 'username' => 'пЕтЯ', + ], + false, + ]; + + yield 'Password contains the email' => [ + 'ПеТя-9000', + [ + 'email' => 'админ@петя.ру', + 'username' => 'Пётр', + ], + false, + ]; + + yield 'Password contains the personal data' => [ + '_ПеТр_9000', + [ + 'email' => 'админ@caйт.ру', + 'username' => 'Пётр', + 'lastname' => 'Петров', + ], + false, + ]; + + yield 'Empty password' => [ + '', + [ + 'email' => 'админ@сайт.ру', + 'username' => 'Петя', + ], + true, + ]; + + yield 'Only spaces in password' => [ + ' ', + [ + 'email' => 'админ@сайт.ру', + 'username' => 'Петя', + ], + true, + ]; + + yield 'Password without user' => [ + '_ПеТр_9000', + null, + true, + ]; + } + + #[DataProvider('provideIsolatedMethodIsNotSimilar')] + public function testIsolatedMethodIsNotSimilar(mixed ...$input): void + { + $user = null; + // Password removes spaces inside `check()`, so we’ll duplicate it here + $password = mb_strtolower($input[0]); + + if (is_array($input[1])) { + $user = new User($input[1]); + } + + $config = new Auth(); + $config->maxSimilarity = $input[2]; + $validator = new NothingPersonalValidator($config); + + $isNotSimilar = self::getPrivateMethodInvoker($validator, 'isNotSimilar'); + + /** @var bool */ + $result = $isNotSimilar($password, $user); + + $this->assertSame($input[3], $result); + } + + public static function provideIsolatedMethodIsNotSimilar(): iterable + { + yield 'Same password and username (similar > 1%)' => [ + 'ПёТр', + [ + 'username' => 'ПёТр', + ], + 1, + false, + ]; + + yield 'Same password and username (similar > 100%)' => [ + 'ПёТр', + [ + 'username' => 'ПёТр', + ], + 150, + false, + ]; + + yield 'Empty password and username (similar < 1%)' => [ + '', + [ + 'username' => '', + ], + 0, + true, + ]; + + yield 'Empty password and username (similar > 1%)' => [ + '', + [ + 'username' => '', + ], + 1, + true, + ]; + + yield 'Empty password and specified username (similar > 1%)' => [ + '', + [ + 'username' => 'ПёТр', + ], + 1, + true, + ]; + + yield 'Empty username and specified password (similar > 1%)' => [ + 'ПаРоЛь', + [ + 'username' => '', + ], + 1, + true, + ]; + + yield 'Empty User and specified password (similar > 1%)' => [ + 'ПаРоЛь', + null, + 1, + true, + ]; + } } From 0fb864c4c414cb507cc8d8b48a9d8dd416e5cb60 Mon Sep 17 00:00:00 2001 From: neznaika0 Date: Sat, 14 Nov 2026 20:02:16 +0300 Subject: [PATCH 2/3] fix: Fix Infection errors --- .../Passwords/NothingPersonalValidator.php | 1 - tests/Unit/NothingPersonalValidatorTest.php | 34 +++++++++++++++++-- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/Authentication/Passwords/NothingPersonalValidator.php b/src/Authentication/Passwords/NothingPersonalValidator.php index 6e1e344b5..ec1a134f2 100644 --- a/src/Authentication/Passwords/NothingPersonalValidator.php +++ b/src/Authentication/Passwords/NothingPersonalValidator.php @@ -109,7 +109,6 @@ protected function isNotPersonal(string $password, ?User $user): bool } } - $needles = array_unique($needles, SORT_LOCALE_STRING); $trivial = [ 'a', 'an', diff --git a/tests/Unit/NothingPersonalValidatorTest.php b/tests/Unit/NothingPersonalValidatorTest.php index 91a300afc..554c1978d 100644 --- a/tests/Unit/NothingPersonalValidatorTest.php +++ b/tests/Unit/NothingPersonalValidatorTest.php @@ -402,6 +402,26 @@ public function testIsolatedMethodIsNotPersonal(mixed ...$input): void $this->assertSame($input[2], $result); } + #[DataProvider('provideIsolatedMethodIsNotPersonal')] + public function testCheckMultibyteMethodIsNotPersonal(mixed ...$input): void + { + $user = null; + $fields = []; + + if (is_array($input[1])) { + $user = new User($input[1]); + $fields = array_keys($input[1]); + } + + $config = new Auth(); + $config->personalFields = array_filter($fields, static fn ($val): bool => $val !== 'email' && $val !== 'username'); + $validator = new NothingPersonalValidator($config); + + $result = $validator->check($input[0], $user); + + $this->assertSame($input[2], $result->isOK()); + } + public static function provideIsolatedMethodIsNotPersonal(): iterable { // @todo: strrev() doesn’t work with Unicode @@ -414,10 +434,12 @@ public static function provideIsolatedMethodIsNotPersonal(): iterable // false // ]; - yield 'Empty password and username' => [ - '', + yield 'Repeated words in the personal data' => [ + 'Никола', [ - 'email' => 'админ@сайт.ру', + 'email' => 'Никола@никола.ру', + 'username' => 'Никола Никола', + 'lastname' => 'Никола Никола', ], false, ]; @@ -521,6 +543,7 @@ public static function provideIsolatedMethodIsNotSimilar(): iterable yield 'Same password and username (similar > 1%)' => [ 'ПёТр', [ + 'email' => 'петя@caйт.ру', 'username' => 'ПёТр', ], 1, @@ -530,6 +553,7 @@ public static function provideIsolatedMethodIsNotSimilar(): iterable yield 'Same password and username (similar > 100%)' => [ 'ПёТр', [ + 'email' => 'петя@caйт.ру', 'username' => 'ПёТр', ], 150, @@ -539,6 +563,7 @@ public static function provideIsolatedMethodIsNotSimilar(): iterable yield 'Empty password and username (similar < 1%)' => [ '', [ + 'email' => 'петя@caйт.ру', 'username' => '', ], 0, @@ -548,6 +573,7 @@ public static function provideIsolatedMethodIsNotSimilar(): iterable yield 'Empty password and username (similar > 1%)' => [ '', [ + 'email' => 'петя@caйт.ру', 'username' => '', ], 1, @@ -557,6 +583,7 @@ public static function provideIsolatedMethodIsNotSimilar(): iterable yield 'Empty password and specified username (similar > 1%)' => [ '', [ + 'email' => 'петя@caйт.ру', 'username' => 'ПёТр', ], 1, @@ -566,6 +593,7 @@ public static function provideIsolatedMethodIsNotSimilar(): iterable yield 'Empty username and specified password (similar > 1%)' => [ 'ПаРоЛь', [ + 'email' => 'петя@caйт.ру', 'username' => '', ], 1, From 74d3f430fc3eb216a5e1a1ff2f4e7337d768a7cb Mon Sep 17 00:00:00 2001 From: neznaika0 Date: Sat, 14 Nov 2026 20:55:30 +0300 Subject: [PATCH 3/3] test: Add covers `IsNotRepsonal()` --- tests/Unit/NothingPersonalValidatorTest.php | 32 +++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/Unit/NothingPersonalValidatorTest.php b/tests/Unit/NothingPersonalValidatorTest.php index 554c1978d..1b82a66af 100644 --- a/tests/Unit/NothingPersonalValidatorTest.php +++ b/tests/Unit/NothingPersonalValidatorTest.php @@ -607,4 +607,36 @@ public static function provideIsolatedMethodIsNotSimilar(): iterable true, ]; } + + public function testIsNotPersonalAcceptsWrongData(): void + { + $config = new Auth(); + $config->personalFields = ['altName', 'job']; + $validator = new NothingPersonalValidator($config); + + $isNotPersonal = self::getPrivateMethodInvoker($validator, 'isNotPersonal'); + + /** @var bool */ + $result = $isNotPersonal( + 'Pa55w0rd', + new User([ + 'email' => 'user@email.local', + 'username' => 'John', + ]), + ); + + $this->assertTrue($result); + + $result = $isNotPersonal( + 'Pa55w0rd', + new User([ + 'email' => 'user@email.local', + 'username' => 'John', + 'altName' => [], + 'job' => new Auth(), + ]), + ); + + $this->assertTrue($result); + } }