Deprecate PasswordPolicyChecks::checkPopularPasswordBlacklist
[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 * @deprecated since 1.33
147 * @return Status
148 * @see $wgPopularPasswordFile
149 */
150 public static function checkPopularPasswordBlacklist( $policyVal, User $user, $password ) {
151 global $wgPopularPasswordFile, $wgSitename;
152 $status = Status::newGood();
153 if ( $policyVal > 0 ) {
154 wfDeprecated( __METHOD__, '1.33' );
155
156 $langEn = Language::factory( 'en' );
157 $passwordKey = $langEn->lc( trim( $password ) );
158
159 // People often use the name of the current site, which won't be
160 // in the common password file. Also check '' for people who use
161 // just whitespace.
162 $sitename = $langEn->lc( trim( $wgSitename ) );
163 $hardcodedCommonPasswords = [ '', 'wiki', 'mediawiki', $sitename ];
164 if ( in_array( $passwordKey, $hardcodedCommonPasswords ) ) {
165 $status->error( 'passwordtoopopular' );
166 return $status;
167 }
168
169 // This could throw an exception, but there's not a good way
170 // of failing gracefully, if say the file is missing, so just
171 // let the exception fall through.
172 // Format of cdb file is mapping password => popularity rank.
173 // See maintenance/createCommonPasswordCdb.php
174 $db = CdbReader::open( $wgPopularPasswordFile );
175
176 $res = $db->get( $passwordKey );
177 if ( $res && (int)$res <= $policyVal ) {
178 // Note: If you want to find the true number of common
179 // passwords stored (for reporting the error), you have to take
180 // the max of the policyVal and $db->get( '_TOTALENTRIES' ).
181 $status->error( 'passwordtoopopular' );
182 }
183 }
184 return $status;
185 }
186
187 /**
188 * Ensure the password isn't in the list of passwords blacklisted by the
189 * wikimedia/password-blacklist library, which contains (as of 0.1.4) the
190 * 100.000 top passwords from SecLists (as a Bloom filter, with an
191 * 0.000001 false positive ratio).
192 *
193 * @param bool $policyVal Whether to apply this policy
194 * @param User $user
195 * @param string $password
196 *
197 * @since 1.33
198 *
199 * @return Status
200 */
201 public static function checkPasswordNotInLargeBlacklist( $policyVal, User $user, $password ) {
202 $status = Status::newGood();
203 if ( $policyVal && PasswordBlacklist\PasswordBlacklist::isBlacklisted( $password ) ) {
204 $status->error( 'passwordinlargeblacklist' );
205 }
206
207 return $status;
208 }
209
210 }