3 * Password policy checks
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 namespace MediaWiki\Password
;
25 use MediaWiki\Status\Status
;
26 use MediaWiki\User\UserIdentity
;
27 use Wikimedia\CommonPasswords\CommonPasswords
;
30 * Functions to check passwords against a policy requirement.
32 * $policyVal is the value configured in $wgPasswordPolicy. If the return status is fatal,
33 * the user won't be allowed to log in. If the status is not good but not fatal, the user
34 * will not be allowed to set the given password (on registration or password change),
35 * but can still log in after bypassing a warning.
38 * @see $wgPasswordPolicy
40 class PasswordPolicyChecks
{
43 * Check password is longer than the minimum, not fatal.
44 * @param int $policyVal minimal length
45 * @param UserIdentity $user
46 * @param string $password
47 * @return Status error if $password is shorter than $policyVal
49 public static function checkMinimalPasswordLength( $policyVal, UserIdentity
$user, $password ) {
50 $status = Status
::newGood();
51 if ( $policyVal > strlen( $password ) ) {
52 $status->error( 'passwordtooshort', $policyVal );
58 * Check password is longer than the minimum, fatal.
59 * Intended for locking out users with passwords too short to trust, requiring them
60 * to recover their account by some other means.
61 * @param int $policyVal minimal length
62 * @param UserIdentity $user
63 * @param string $password
64 * @return Status fatal if $password is shorter than $policyVal
66 public static function checkMinimumPasswordLengthToLogin( $policyVal, UserIdentity
$user, $password ) {
67 $status = Status
::newGood();
68 if ( $policyVal > strlen( $password ) ) {
69 $status->fatal( 'passwordtooshort', $policyVal );
75 * Check password is shorter than the maximum, fatal.
76 * Intended for preventing DoS attacks when using a more expensive password hash like PBKDF2.
77 * @param int $policyVal maximum length
78 * @param UserIdentity $user
79 * @param string $password
80 * @return Status fatal if $password is shorter than $policyVal
82 public static function checkMaximalPasswordLength( $policyVal, UserIdentity
$user, $password ) {
83 $status = Status
::newGood();
84 if ( $policyVal < strlen( $password ) ) {
85 $status->fatal( 'passwordtoolong', $policyVal );
91 * Check if a password is a (case-insensitive) substring within the username.
92 * @param bool $policyVal true to force compliance.
93 * @param UserIdentity $user
94 * @param string $password
95 * @return Status error if the password is a substring within username, and the policy is true
97 public static function checkPasswordCannotBeSubstringInUsername(
102 $status = Status
::newGood();
103 $username = $user->getName();
104 if ( $policyVal && stripos( $username, $password ) !== false ) {
105 $status->error( 'password-substring-username-match' );
111 * Check if username and password are on a list of past MediaWiki default passwords.
112 * @param bool $policyVal true to force compliance.
113 * @param UserIdentity $user
114 * @param string $password
115 * @return Status error if the username and password match, and policy is true
117 public static function checkPasswordCannotMatchDefaults( $policyVal, UserIdentity
$user, $password ) {
118 static $blockedLogins = [
120 'Useruser' => 'Passpass',
121 'Useruser1' => 'Passpass1',
123 'Apitestsysop' => 'testpass',
124 'Apitestuser' => 'testpass',
127 $status = Status
::newGood();
128 $username = $user->getName();
131 isset( $blockedLogins[$username] ) &&
132 hash_equals( $blockedLogins[$username], $password )
134 $status->error( 'password-login-forbidden' );
137 // Example from ApiChangeAuthenticationRequest
138 if ( hash_equals( 'ExamplePassword', $password ) ) {
139 $status->error( 'password-login-forbidden' );
146 * Ensure the password isn't in the list of common passwords by the
147 * wikimedia/common-passwords library, which contains (as of 0.2.0) the
148 * 100,000 top passwords from SecLists (as a Bloom filter, with an
149 * 0.000001 false positive ratio).
151 * @param bool $policyVal Whether to apply this policy
152 * @param UserIdentity $user
153 * @param string $password
159 public static function checkPasswordNotInCommonList( $policyVal, UserIdentity
$user, $password ) {
160 $status = Status
::newGood();
161 if ( $policyVal && CommonPasswords
::isCommon( $password ) ) {
162 $status->error( 'passwordincommonlist' );