Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
36 / 36
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
PasswordPolicyChecks
100.00% covered (success)
100.00%
36 / 36
100.00% covered (success)
100.00%
6 / 6
17
100.00% covered (success)
100.00%
1 / 1
 checkMinimalPasswordLength
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 checkMinimumPasswordLengthToLogin
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 checkMaximalPasswordLength
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 checkPasswordCannotBeSubstringInUsername
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 checkPasswordCannotMatchDefaults
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
5
 checkPasswordNotInCommonList
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
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
23use MediaWiki\Status\Status;
24use MediaWiki\User\UserIdentity;
25use Wikimedia\CommonPasswords\CommonPasswords;
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 */
38class PasswordPolicyChecks {
39
40    /**
41     * Check password is longer than minimum, not fatal.
42     * @param int $policyVal minimal length
43     * @param UserIdentity $user
44     * @param string $password
45     * @return Status error if $password is shorter than $policyVal
46     */
47    public static function checkMinimalPasswordLength( $policyVal, UserIdentity $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     * Intended for locking out users with passwords too short to trust, requiring them
58     * to recover their account by some other means.
59     * @param int $policyVal minimal length
60     * @param UserIdentity $user
61     * @param string $password
62     * @return Status fatal if $password is shorter than $policyVal
63     */
64    public static function checkMinimumPasswordLengthToLogin( $policyVal, UserIdentity $user, $password ) {
65        $status = Status::newGood();
66        if ( $policyVal > strlen( $password ) ) {
67            $status->fatal( 'passwordtooshort', $policyVal );
68        }
69        return $status;
70    }
71
72    /**
73     * Check password is shorter than maximum, fatal.
74     * Intended for preventing DoS attacks when using a more expensive password hash like PBKDF2.
75     * @param int $policyVal maximum length
76     * @param UserIdentity $user
77     * @param string $password
78     * @return Status fatal if $password is shorter than $policyVal
79     */
80    public static function checkMaximalPasswordLength( $policyVal, UserIdentity $user, $password ) {
81        $status = Status::newGood();
82        if ( $policyVal < strlen( $password ) ) {
83            $status->fatal( 'passwordtoolong', $policyVal );
84        }
85        return $status;
86    }
87
88    /**
89     * Check if password is a (case-insensitive) substring within the username.
90     * @param bool $policyVal true to force compliance.
91     * @param UserIdentity $user
92     * @param string $password
93     * @return Status error if password is a substring within username, and policy is true
94     */
95    public static function checkPasswordCannotBeSubstringInUsername(
96        $policyVal,
97        UserIdentity $user,
98        $password
99    ) {
100        $status = Status::newGood();
101        $username = $user->getName();
102        if ( $policyVal && stripos( $username, $password ) !== false ) {
103            $status->error( 'password-substring-username-match' );
104        }
105        return $status;
106    }
107
108    /**
109     * Check if username and password are on a list of past MediaWiki default passwords.
110     * @param bool $policyVal true to force compliance.
111     * @param UserIdentity $user
112     * @param string $password
113     * @return Status error if username and password match, and policy is true
114     */
115    public static function checkPasswordCannotMatchDefaults( $policyVal, UserIdentity $user, $password ) {
116        static $blockedLogins = [
117            // r75589
118            'Useruser' => 'Passpass',
119            'Useruser1' => 'Passpass1',
120            // r75605
121            'Apitestsysop' => 'testpass',
122            'Apitestuser' => 'testpass',
123        ];
124
125        $status = Status::newGood();
126        $username = $user->getName();
127        if ( $policyVal ) {
128            if (
129                isset( $blockedLogins[$username] ) &&
130                hash_equals( $blockedLogins[$username], $password )
131            ) {
132                $status->error( 'password-login-forbidden' );
133            }
134
135            // Example from ApiChangeAuthenticationRequest
136            if ( hash_equals( 'ExamplePassword', $password ) ) {
137                $status->error( 'password-login-forbidden' );
138            }
139        }
140        return $status;
141    }
142
143    /**
144     * Ensure the password isn't in the list of common passwords by the
145     * wikimedia/common-passwords library, which contains (as of 0.2.0) the
146     * 100,000 top passwords from SecLists (as a Bloom filter, with an
147     * 0.000001 false positive ratio).
148     *
149     * @param bool $policyVal Whether to apply this policy
150     * @param UserIdentity $user
151     * @param string $password
152     *
153     * @since 1.33
154     *
155     * @return Status
156     */
157    public static function checkPasswordNotInCommonList( $policyVal, UserIdentity $user, $password ) {
158        $status = Status::newGood();
159        if ( $policyVal && CommonPasswords::isCommon( $password ) ) {
160            $status->error( 'passwordincommonlist' );
161        }
162
163        return $status;
164    }
165
166}