Merge "Avoid expensive array_shift where possible"
[lhc/web/wiklou.git] / includes / password / PasswordPolicyChecks.php
1 <?php
2 /**
3 * Password policy checks
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23 use Cdb\Reader as CdbReader;
24 use MediaWiki\MediaWikiServices;
25 use Wikimedia\PasswordBlacklist;
26
27 /**
28 * Functions to check passwords against a policy requirement.
29 *
30 * $policyVal is the value configured in $wgPasswordPolicy. If the return status is fatal,
31 * the user won't be allowed to login. If the status is not good but not fatal, the user
32 * will not be allowed to set the given password (on registration or password change),
33 * but can still log in after bypassing a warning.
34 *
35 * @since 1.26
36 * @see $wgPasswordPolicy
37 */
38 class PasswordPolicyChecks {
39
40 /**
41 * Check password is longer than minimum, not fatal.
42 * @param int $policyVal minimal length
43 * @param User $user
44 * @param string $password
45 * @return Status error if $password is shorter than $policyVal
46 */
47 public static function checkMinimalPasswordLength( $policyVal, User $user, $password ) {
48 $status = Status::newGood();
49 if ( $policyVal > strlen( $password ) ) {
50 $status->error( 'passwordtooshort', $policyVal );
51 }
52 return $status;
53 }
54
55 /**
56 * Check password is longer than minimum, fatal.
57 * @param int $policyVal minimal length
58 * @param User $user
59 * @param string $password
60 * @return Status fatal if $password is shorter than $policyVal
61 */
62 public static function checkMinimumPasswordLengthToLogin( $policyVal, User $user, $password ) {
63 $status = Status::newGood();
64 if ( $policyVal > strlen( $password ) ) {
65 $status->fatal( 'passwordtooshort', $policyVal );
66 }
67 return $status;
68 }
69
70 /**
71 * Check password is shorter than maximum, fatal.
72 * Intended for preventing DoS attacks when using a more expensive password hash like PBKDF2.
73 * @param int $policyVal maximum length
74 * @param User $user
75 * @param string $password
76 * @return Status fatal if $password is shorter than $policyVal
77 */
78 public static function checkMaximalPasswordLength( $policyVal, User $user, $password ) {
79 $status = Status::newGood();
80 if ( $policyVal < strlen( $password ) ) {
81 $status->fatal( 'passwordtoolong', $policyVal );
82 }
83 return $status;
84 }
85
86 /**
87 * Check if username and password are a (case-insensitive) match.
88 * @param bool $policyVal true to force compliance.
89 * @param User $user
90 * @param string $password
91 * @return Status error if username and password match, and policy is true
92 */
93 public static function checkPasswordCannotMatchUsername( $policyVal, User $user, $password ) {
94 $status = Status::newGood();
95 $username = $user->getName();
96 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
97 if (
98 $policyVal && hash_equals( $contLang->lc( $username ), $contLang->lc( $password ) )
99 ) {
100 $status->error( 'password-name-match' );
101 }
102 return $status;
103 }
104
105 /**
106 * Check if username and password are on a blacklist of past MediaWiki default passwords.
107 * @param bool $policyVal true to force compliance.
108 * @param User $user
109 * @param string $password
110 * @return Status error if username and password match, and policy is true
111 */
112 public static function checkPasswordCannotMatchBlacklist( $policyVal, User $user, $password ) {
113 static $blockedLogins = [
114 'Useruser' => 'Passpass', 'Useruser1' => 'Passpass1', # r75589
115 'Apitestsysop' => 'testpass', 'Apitestuser' => 'testpass' # r75605
116 ];
117
118 $status = Status::newGood();
119 $username = $user->getName();
120 if ( $policyVal ) {
121 if (
122 isset( $blockedLogins[$username] ) &&
123 hash_equals( $blockedLogins[$username], $password )
124 ) {
125 $status->error( 'password-login-forbidden' );
126 }
127
128 // Example from ApiChangeAuthenticationRequest
129 if ( hash_equals( 'ExamplePassword', $password ) ) {
130 $status->error( 'password-login-forbidden' );
131 }
132 }
133 return $status;
134 }
135
136 /**
137 * Ensure that password isn't in top X most popular passwords, as defined by
138 * $wgPopularPasswordFile.
139 *
140 * @param int $policyVal Cut off to use. Will automatically shrink to the max
141 * supported for error messages if set to more than max number of passwords on file,
142 * so you can use the PHP_INT_MAX constant here safely.
143 * @param User $user
144 * @param string $password
145 * @since 1.27
146 * @return Status
147 * @see $wgPopularPasswordFile
148 */
149 public static function checkPopularPasswordBlacklist( $policyVal, User $user, $password ) {
150 global $wgPopularPasswordFile, $wgSitename;
151 $status = Status::newGood();
152 if ( $policyVal > 0 ) {
153 $langEn = Language::factory( 'en' );
154 $passwordKey = $langEn->lc( trim( $password ) );
155
156 // People often use the name of the current site, which won't be
157 // in the common password file. Also check '' for people who use
158 // just whitespace.
159 $sitename = $langEn->lc( trim( $wgSitename ) );
160 $hardcodedCommonPasswords = [ '', 'wiki', 'mediawiki', $sitename ];
161 if ( in_array( $passwordKey, $hardcodedCommonPasswords ) ) {
162 $status->error( 'passwordtoopopular' );
163 return $status;
164 }
165
166 // This could throw an exception, but there's not a good way
167 // of failing gracefully, if say the file is missing, so just
168 // let the exception fall through.
169 // Format of cdb file is mapping password => popularity rank.
170 // See maintenance/createCommonPasswordCdb.php
171 $db = CdbReader::open( $wgPopularPasswordFile );
172
173 $res = $db->get( $passwordKey );
174 if ( $res && (int)$res <= $policyVal ) {
175 // Note: If you want to find the true number of common
176 // passwords stored (for reporting the error), you have to take
177 // the max of the policyVal and $db->get( '_TOTALENTRIES' ).
178 $status->error( 'passwordtoopopular' );
179 }
180 }
181 return $status;
182 }
183
184 /**
185 * Ensure the password isn't in the list of passwords blacklisted by the
186 * wikimedia/password-blacklist library, which contains (as of 0.1.4) the
187 * 100.000 top passwords from SecLists (as a Bloom filter, with an
188 * 0.000001 false positive ratio).
189 *
190 * @param bool $policyVal Whether to apply this policy
191 * @param User $user
192 * @param string $password
193 *
194 * @since 1.33
195 *
196 * @return Status
197 */
198 public static function checkPasswordNotInLargeBlacklist( $policyVal, User $user, $password ) {
199 $status = Status::newGood();
200 if ( $policyVal && PasswordBlacklist\PasswordBlacklist::isBlacklisted( $password ) ) {
201 $status->error( 'passwordinlargeblacklist' );
202 }
203
204 return $status;
205 }
206
207 }