Repository navigation
fix: Unicode support for NothingPersonalValidator
#1406
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
neznaika0
wants to merge
3
commits into
codeigniter4:develop
Choose a base branch
from
neznaika0:test/personal-validator
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,7 +32,7 @@ | |
| */ | ||
| 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 @@ | |
| } | ||
|
|
||
| /** | ||
| * 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 $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 | ||
|
|
@@ -134,12 +131,12 @@ | |
| $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 +149,7 @@ | |
| } | ||
| } | ||
| } | ||
|
|
||
| if ($valid) { | ||
| return true; | ||
| } | ||
|
|
@@ -163,17 +161,19 @@ | |
| } | ||
|
|
||
| /** | ||
| * 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 +186,10 @@ | |
| } | ||
|
|
||
| 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,16 +202,14 @@ | |
| } | ||
|
|
||
| /** | ||
| * 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. | ||
| * | ||
| * @return array<int, string> | ||
| */ | ||
| protected function strip_explode(string $str): array | ||
|
Check warning on line 210 in src/Authentication/Passwords/NothingPersonalValidator.php
|
||
| { | ||
| $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 | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.