Merge "Add .pipeline/ with dev image variant"
[lhc/web/wiklou.git] / includes / auth / Throttler.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup Auth
20 */
21
22 namespace MediaWiki\Auth;
23
24 use BagOStuff;
25 use MediaWiki\Logger\LoggerFactory;
26 use MediaWiki\MediaWikiServices;
27 use Psr\Log\LoggerAwareInterface;
28 use Psr\Log\LoggerInterface;
29 use Psr\Log\LogLevel;
30
31 /**
32 * A helper class for throttling authentication attempts.
33 * @package MediaWiki\Auth
34 * @ingroup Auth
35 * @since 1.27
36 */
37 class Throttler implements LoggerAwareInterface {
38 /** @var string */
39 protected $type;
40 /**
41 * See documentation of $wgPasswordAttemptThrottle for format. Old (pre-1.27) format is not
42 * allowed here.
43 * @var array[]
44 * @see https://www.mediawiki.org/wiki/Manual:$wgPasswordAttemptThrottle
45 */
46 protected $conditions;
47 /** @var BagOStuff */
48 protected $cache;
49 /** @var LoggerInterface */
50 protected $logger;
51 /** @var int|float */
52 protected $warningLimit;
53
54 /**
55 * @param array|null $conditions An array of arrays describing throttling conditions.
56 * Defaults to $wgPasswordAttemptThrottle. See documentation of that variable for format.
57 * @param array $params Parameters (all optional):
58 * - type: throttle type, used as a namespace for counters,
59 * - cache: a BagOStuff object where throttle counters are stored.
60 * - warningLimit: the log level will be raised to warning when rejecting an attempt after
61 * no less than this many failures.
62 */
63 public function __construct( array $conditions = null, array $params = [] ) {
64 $invalidParams = array_diff_key( $params,
65 array_fill_keys( [ 'type', 'cache', 'warningLimit' ], true ) );
66 if ( $invalidParams ) {
67 throw new \InvalidArgumentException( 'unrecognized parameters: '
68 . implode( ', ', array_keys( $invalidParams ) ) );
69 }
70
71 if ( $conditions === null ) {
72 $config = MediaWikiServices::getInstance()->getMainConfig();
73 $conditions = $config->get( 'PasswordAttemptThrottle' );
74 $params += [
75 'type' => 'password',
76 'cache' => \ObjectCache::getLocalClusterInstance(),
77 'warningLimit' => 50,
78 ];
79 } else {
80 $params += [
81 'type' => 'custom',
82 'cache' => \ObjectCache::getLocalClusterInstance(),
83 'warningLimit' => INF,
84 ];
85 }
86
87 $this->type = $params['type'];
88 $this->conditions = static::normalizeThrottleConditions( $conditions );
89 $this->cache = $params['cache'];
90 $this->warningLimit = $params['warningLimit'];
91
92 $this->setLogger( LoggerFactory::getInstance( 'throttler' ) );
93 }
94
95 public function setLogger( LoggerInterface $logger ) {
96 $this->logger = $logger;
97 }
98
99 /**
100 * Increase the throttle counter and return whether the attempt should be throttled.
101 *
102 * Should be called before an authentication attempt.
103 *
104 * @param string|null $username
105 * @param string|null $ip
106 * @param string|null $caller The authentication method from which we were called.
107 * @return array|false False if the attempt should not be throttled, an associative array
108 * with three keys otherwise:
109 * - throttleIndex: which throttle condition was met (a key of the conditions array)
110 * - count: throttle count (ie. number of failed attempts)
111 * - wait: time in seconds until authentication can be attempted
112 */
113 public function increase( $username = null, $ip = null, $caller = null ) {
114 if ( $username === null && $ip === null ) {
115 throw new \InvalidArgumentException( 'Either username or IP must be set for throttling' );
116 }
117
118 $userKey = $username ? md5( $username ) : null;
119 foreach ( $this->conditions as $index => $throttleCondition ) {
120 $ipKey = isset( $throttleCondition['allIPs'] ) ? null : $ip;
121 $count = $throttleCondition['count'];
122 $expiry = $throttleCondition['seconds'];
123
124 // a limit of 0 is used as a disable flag in some throttling configuration settings
125 // throttling the whole world is probably a bad idea
126 if ( !$count || $userKey === null && $ipKey === null ) {
127 continue;
128 }
129
130 $throttleKey = $this->cache->makeGlobalKey(
131 'throttler',
132 $this->type,
133 $index,
134 $ipKey,
135 $userKey
136 );
137 $throttleCount = $this->cache->get( $throttleKey );
138 if ( $throttleCount && $throttleCount >= $count ) {
139 // Throttle limited reached
140 $this->logRejection( [
141 'throttle' => $this->type,
142 'index' => $index,
143 'ipKey' => $ipKey,
144 'username' => $username,
145 'count' => $count,
146 'expiry' => $expiry,
147 // @codeCoverageIgnoreStart
148 'method' => $caller ?: __METHOD__,
149 // @codeCoverageIgnoreEnd
150 ] );
151
152 return [ 'throttleIndex' => $index, 'count' => $count, 'wait' => $expiry ];
153 } else {
154 $this->cache->incrWithInit( $throttleKey, $expiry, 1 );
155 }
156 }
157
158 return false;
159 }
160
161 /**
162 * Clear the throttle counter.
163 *
164 * Should be called after a successful authentication attempt.
165 *
166 * @param string|null $username
167 * @param string|null $ip
168 * @throws \MWException
169 */
170 public function clear( $username = null, $ip = null ) {
171 $userKey = $username ? md5( $username ) : null;
172 foreach ( $this->conditions as $index => $specificThrottle ) {
173 $ipKey = isset( $specificThrottle['allIPs'] ) ? null : $ip;
174 $throttleKey = $this->cache->makeGlobalKey( 'throttler', $this->type, $index, $ipKey, $userKey );
175 $this->cache->delete( $throttleKey );
176 }
177 }
178
179 /**
180 * Handles B/C for $wgPasswordAttemptThrottle.
181 * @param array $throttleConditions
182 * @return array[]
183 * @see $wgPasswordAttemptThrottle for structure
184 */
185 protected static function normalizeThrottleConditions( $throttleConditions ) {
186 if ( !is_array( $throttleConditions ) ) {
187 return [];
188 }
189 if ( isset( $throttleConditions['count'] ) ) { // old style
190 $throttleConditions = [ $throttleConditions ];
191 }
192 return $throttleConditions;
193 }
194
195 protected function logRejection( array $context ) {
196 $logMsg = 'Throttle {throttle} hit, throttled for {expiry} seconds due to {count} attempts '
197 . 'from username {username} and IP {ipKey}';
198
199 // If we are hitting a throttle for >= warningLimit attempts, it is much more likely to be
200 // an attack than someone simply forgetting their password, so log it at a higher level.
201 $level = $context['count'] >= $this->warningLimit ? LogLevel::WARNING : LogLevel::INFO;
202
203 // It should be noted that once the throttle is hit, every attempt to login will
204 // generate the log message until the throttle expires, not just the attempt that
205 // puts the throttle over the top.
206 $this->logger->log( $level, $logMsg, $context );
207 }
208
209 }