MediaWiki  master
PasswordPolicyChecks.php
Go to the documentation of this file.
1 <?php
23 use Cdb\Reader as CdbReader;
25 use Wikimedia\PasswordBlacklist;
26 
39 
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 
64  public static function checkMinimumPasswordLengthToLogin( $policyVal, User $user, $password ) {
65  $status = Status::newGood();
66  if ( $policyVal > strlen( $password ) ) {
67  $status->fatal( 'passwordtooshort', $policyVal );
68  }
69  return $status;
70  }
71 
80  public static function checkMaximalPasswordLength( $policyVal, User $user, $password ) {
81  $status = Status::newGood();
82  if ( $policyVal < strlen( $password ) ) {
83  $status->fatal( 'passwordtoolong', $policyVal );
84  }
85  return $status;
86  }
87 
95  public static function checkPasswordCannotMatchUsername( $policyVal, User $user, $password ) {
96  $status = Status::newGood();
97  $username = $user->getName();
98  $contLang = MediaWikiServices::getInstance()->getContentLanguage();
99  if (
100  $policyVal && hash_equals( $contLang->lc( $username ), $contLang->lc( $password ) )
101  ) {
102  $status->error( 'password-name-match' );
103  }
104  return $status;
105  }
106 
115  $policyVal,
116  User $user,
117  $password
118  ) {
119  $status = Status::newGood();
120  $username = $user->getName();
121  if ( $policyVal && stripos( $username, $password ) !== false ) {
122  $status->error( 'password-substring-username-match' );
123  }
124  return $status;
125  }
126 
134  public static function checkPasswordCannotMatchBlacklist( $policyVal, User $user, $password ) {
135  static $blockedLogins = [
136  'Useruser' => 'Passpass', 'Useruser1' => 'Passpass1', # r75589
137  'Apitestsysop' => 'testpass', 'Apitestuser' => 'testpass' # r75605
138  ];
139 
140  $status = Status::newGood();
141  $username = $user->getName();
142  if ( $policyVal ) {
143  if (
144  isset( $blockedLogins[$username] ) &&
145  hash_equals( $blockedLogins[$username], $password )
146  ) {
147  $status->error( 'password-login-forbidden' );
148  }
149 
150  // Example from ApiChangeAuthenticationRequest
151  if ( hash_equals( 'ExamplePassword', $password ) ) {
152  $status->error( 'password-login-forbidden' );
153  }
154  }
155  return $status;
156  }
157 
172  public static function checkPopularPasswordBlacklist( $policyVal, User $user, $password ) {
174  $status = Status::newGood();
175  if ( $policyVal > 0 ) {
176  $langEn = MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( 'en' );
177  $passwordKey = $langEn->lc( trim( $password ) );
178 
179  // People often use the name of the current site, which won't be
180  // in the common password file. Also check '' for people who use
181  // just whitespace.
182  $sitename = $langEn->lc( trim( $wgSitename ) );
183  $hardcodedCommonPasswords = [ '', 'wiki', 'mediawiki', $sitename ];
184  if ( in_array( $passwordKey, $hardcodedCommonPasswords ) ) {
185  $status->error( 'passwordtoopopular' );
186  return $status;
187  }
188 
189  // This could throw an exception, but there's not a good way
190  // of failing gracefully, if say the file is missing, so just
191  // let the exception fall through.
192  // Format of cdb file is mapping password => popularity rank.
193  // See maintenance/createCommonPasswordCdb.php
194  $db = CdbReader::open( $wgPopularPasswordFile );
195 
196  $res = $db->get( $passwordKey );
197  if ( $res && (int)$res <= $policyVal ) {
198  // Note: If you want to find the true number of common
199  // passwords stored (for reporting the error), you have to take
200  // the max of the policyVal and $db->get( '_TOTALENTRIES' ).
201  $status->error( 'passwordtoopopular' );
202  }
203  }
204  return $status;
205  }
206 
221  public static function checkPasswordNotInLargeBlacklist( $policyVal, User $user, $password ) {
222  $status = Status::newGood();
223  if ( $policyVal && PasswordBlacklist\PasswordBlacklist::isBlacklisted( $password ) ) {
224  $status->error( 'passwordinlargeblacklist' );
225  }
226 
227  return $status;
228  }
229 
230 }
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:130
PasswordPolicyChecks\checkMinimalPasswordLength
static checkMinimalPasswordLength( $policyVal, User $user, $password)
Check password is longer than minimum, not fatal.
Definition: PasswordPolicyChecks.php:47
PasswordPolicyChecks\checkPasswordCannotBeSubstringInUsername
static checkPasswordCannotBeSubstringInUsername( $policyVal, User $user, $password)
Check if password is a (case-insensitive) substring within the username.
Definition: PasswordPolicyChecks.php:114
PasswordPolicyChecks\checkPasswordCannotMatchBlacklist
static checkPasswordCannotMatchBlacklist( $policyVal, User $user, $password)
Check if username and password are on a blacklist of past MediaWiki default passwords.
Definition: PasswordPolicyChecks.php:134
$res
$res
Definition: testCompression.php:54
PasswordPolicyChecks\checkPasswordCannotMatchUsername
static checkPasswordCannotMatchUsername( $policyVal, User $user, $password)
Check if username and password are a (case-insensitive) match.
Definition: PasswordPolicyChecks.php:95
PasswordPolicyChecks\checkMinimumPasswordLengthToLogin
static checkMinimumPasswordLengthToLogin( $policyVal, User $user, $password)
Check password is longer than minimum, fatal.
Definition: PasswordPolicyChecks.php:64
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:81
PasswordPolicyChecks\checkPasswordNotInLargeBlacklist
static checkPasswordNotInLargeBlacklist( $policyVal, User $user, $password)
Ensure the password isn't in the list of passwords blacklisted by the wikimedia/password-blacklist li...
Definition: PasswordPolicyChecks.php:221
$wgSitename
$wgSitename
Name of the site.
Definition: DefaultSettings.php:80
PasswordPolicyChecks\checkMaximalPasswordLength
static checkMaximalPasswordLength( $policyVal, User $user, $password)
Check password is shorter than maximum, fatal.
Definition: PasswordPolicyChecks.php:80
$wgPopularPasswordFile
string $wgPopularPasswordFile
Where popular password file is located.
Definition: DefaultSettings.php:8769
PasswordPolicyChecks\checkPopularPasswordBlacklist
static checkPopularPasswordBlacklist( $policyVal, User $user, $password)
Ensure that password isn't in top X most popular passwords, as defined by $wgPopularPasswordFile.
Definition: PasswordPolicyChecks.php:172
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:52
User\getName
getName()
Get the user name, or the IP of an anonymous user.
Definition: User.php:2284
PasswordPolicyChecks
Functions to check passwords against a policy requirement.
Definition: PasswordPolicyChecks.php:38