PasswordPolicyChecks.php 7.1 KB

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