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 use Cdb\Reader
as CdbReader
;
24 use MediaWiki\MediaWikiServices
;
25 use Wikimedia\PasswordBlacklist
;
28 * Functions to check passwords against a policy requirement
31 class PasswordPolicyChecks
{
34 * Check password is longer than minimum, not fatal
35 * @param int $policyVal minimal length
37 * @param string $password
38 * @return Status error if $password is shorter than $policyVal
40 public static function checkMinimalPasswordLength( $policyVal, User
$user, $password ) {
41 $status = Status
::newGood();
42 if ( $policyVal > strlen( $password ) ) {
43 $status->error( 'passwordtooshort', $policyVal );
49 * Check password is longer than minimum, fatal
50 * @param int $policyVal minimal length
52 * @param string $password
53 * @return Status fatal if $password is shorter than $policyVal
55 public static function checkMinimumPasswordLengthToLogin( $policyVal, User
$user, $password ) {
56 $status = Status
::newGood();
57 if ( $policyVal > strlen( $password ) ) {
58 $status->fatal( 'passwordtooshort', $policyVal );
64 * Check password is shorter than maximum, fatal
65 * @param int $policyVal maximum length
67 * @param string $password
68 * @return Status fatal if $password is shorter than $policyVal
70 public static function checkMaximalPasswordLength( $policyVal, User
$user, $password ) {
71 $status = Status
::newGood();
72 if ( $policyVal < strlen( $password ) ) {
73 $status->fatal( 'passwordtoolong', $policyVal );
79 * Check if username and password match
80 * @param bool $policyVal true to force compliance.
82 * @param string $password
83 * @return Status error if username and password match, and policy is true
85 public static function checkPasswordCannotMatchUsername( $policyVal, User
$user, $password ) {
86 $status = Status
::newGood();
87 $username = $user->getName();
88 $contLang = MediaWikiServices
::getInstance()->getContentLanguage();
90 $policyVal && $contLang->lc( $password ) === $contLang->lc( $username )
92 $status->error( 'password-name-match' );
98 * Check if username and password are on a blacklist
99 * @param bool $policyVal true to force compliance.
101 * @param string $password
102 * @return Status error if username and password match, and policy is true
104 public static function checkPasswordCannotMatchBlacklist( $policyVal, User
$user, $password ) {
105 static $blockedLogins = [
106 'Useruser' => 'Passpass', 'Useruser1' => 'Passpass1', # r75589
107 'Apitestsysop' => 'testpass', 'Apitestuser' => 'testpass' # r75605
110 $status = Status
::newGood();
111 $username = $user->getName();
113 if ( isset( $blockedLogins[$username] ) && $password == $blockedLogins[$username] ) {
114 $status->error( 'password-login-forbidden' );
117 // Example from ApiChangeAuthenticationRequest
118 if ( $password === 'ExamplePassword' ) {
119 $status->error( 'password-login-forbidden' );
126 * Ensure that password isn't in top X most popular passwords
128 * @param int $policyVal Cut off to use. Will automatically shrink to the max
129 * supported for error messages if set to more than max number of passwords on file,
130 * so you can use the PHP_INT_MAX constant here safely.
132 * @param string $password
136 public static function checkPopularPasswordBlacklist( $policyVal, User
$user, $password ) {
137 global $wgPopularPasswordFile, $wgSitename;
138 $status = Status
::newGood();
139 if ( $policyVal > 0 ) {
140 $langEn = Language
::factory( 'en' );
141 $passwordKey = $langEn->lc( trim( $password ) );
143 // People often use the name of the current site, which won't be
144 // in the common password file. Also check '' for people who use
146 $sitename = $langEn->lc( trim( $wgSitename ) );
147 $hardcodedCommonPasswords = [ '', 'wiki', 'mediawiki', $sitename ];
148 if ( in_array( $passwordKey, $hardcodedCommonPasswords ) ) {
149 $status->error( 'passwordtoopopular' );
153 // This could throw an exception, but there's not a good way
154 // of failing gracefully, if say the file is missing, so just
155 // let the exception fall through.
156 // Format of cdb file is mapping password => popularity rank.
157 // See maintenance/createCommonPasswordCdb.php
158 $db = CdbReader
::open( $wgPopularPasswordFile );
160 $res = $db->get( $passwordKey );
161 if ( $res && (int)$res <= $policyVal ) {
162 // Note: If you want to find the true number of common
163 // passwords stored (for reporting the error), you have to take
164 // the max of the policyVal and $db->get( '_TOTALENTRIES' ).
165 $status->error( 'passwordtoopopular' );
172 * Ensure the password isn't in the list of passwords blacklisted by the
173 * wikimedia/password-blacklist library
175 * @param bool $policyVal Whether to apply this policy
177 * @param string $password
183 public static function checkPasswordNotInLargeBlacklist( $policyVal, User
$user, $password ) {
184 $status = Status
::newGood();
185 if ( $policyVal && PasswordBlacklist\PasswordBlacklist
::isBlacklisted( $password ) ) {
186 $status->error( 'passwordinlargeblacklist' );