Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / includes / auth / Throttler.php
blob6d3b2f46e64a2e9c2e1992c4faa3cf4494836b12
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.
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.
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
18 * @file
19 * @ingroup Auth
22 namespace MediaWiki\Auth;
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;
31 /**
32 * A helper class for throttling authentication attempts.
33 * @package MediaWiki\Auth
34 * @ingroup Auth
35 * @since 1.27
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
46 protected $conditions;
47 /** @var BagOStuff */
48 protected $cache;
49 /** @var LoggerInterface */
50 protected $logger;
51 /** @var int|float */
52 protected $warningLimit;
54 /**
55 * @param array $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.
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 ) ) );
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,
79 } else {
80 $params += [
81 'type' => 'custom',
82 'cache' => \ObjectCache::getLocalClusterInstance(),
83 'warningLimit' => INF,
87 $this->type = $params['type'];
88 $this->conditions = static::normalizeThrottleConditions( $conditions );
89 $this->cache = $params['cache'];
90 $this->warningLimit = $params['warningLimit'];
92 $this->setLogger( LoggerFactory::getInstance( 'throttler' ) );
95 public function setLogger( LoggerInterface $logger ) {
96 $this->logger = $logger;
99 /**
100 * Increase the throttle counter and return whether the attempt should be throttled.
102 * Should be called before an authentication attempt.
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
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' );
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'];
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;
130 $throttleKey = wfGlobalCacheKey( 'throttler', $this->type, $index, $ipKey, $userKey );
131 $throttleCount = $this->cache->get( $throttleKey );
133 if ( !$throttleCount ) { // counter not started yet
134 $this->cache->add( $throttleKey, 1, $expiry );
135 } elseif ( $throttleCount < $count ) { // throttle limited not yet reached
136 $this->cache->incr( $throttleKey );
137 } else { // throttled
138 $this->logRejection( [
139 'throttle' => $this->type,
140 'index' => $index,
141 'ip' => $ipKey,
142 'username' => $username,
143 'count' => $count,
144 'expiry' => $expiry,
145 // @codeCoverageIgnoreStart
146 'method' => $caller ?: __METHOD__,
147 // @codeCoverageIgnoreEnd
148 ] );
150 return [
151 'throttleIndex' => $index,
152 'count' => $count,
153 'wait' => $expiry,
157 return false;
161 * Clear the throttle counter.
163 * Should be called after a successful authentication attempt.
165 * @param string|null $username
166 * @param string|null $ip
167 * @throws \MWException
169 public function clear( $username = null, $ip = null ) {
170 $userKey = $username ? md5( $username ) : null;
171 foreach ( $this->conditions as $index => $specificThrottle ) {
172 $ipKey = isset( $specificThrottle['allIPs'] ) ? null : $ip;
173 $throttleKey = wfGlobalCacheKey( 'throttler', $this->type, $index, $ipKey, $userKey );
174 $this->cache->delete( $throttleKey );
179 * Handles B/C for $wgPasswordAttemptThrottle.
180 * @param array $throttleConditions
181 * @return array
182 * @see $wgPasswordAttemptThrottle for structure
184 protected static function normalizeThrottleConditions( $throttleConditions ) {
185 if ( !is_array( $throttleConditions ) ) {
186 return [];
188 if ( isset( $throttleConditions['count'] ) ) { // old style
189 $throttleConditions = [ $throttleConditions ];
191 return $throttleConditions;
194 protected function logRejection( array $context ) {
195 $logMsg = 'Throttle {throttle} hit, throttled for {expiry} seconds due to {count} attempts '
196 . 'from username {username} and IP {ip}';
198 // If we are hitting a throttle for >= warningLimit attempts, it is much more likely to be
199 // an attack than someone simply forgetting their password, so log it at a higher level.
200 $level = $context['count'] >= $this->warningLimit ? LogLevel::WARNING : LogLevel::INFO;
202 // It should be noted that once the throttle is hit, every attempt to login will
203 // generate the log message until the throttle expires, not just the attempt that
204 // puts the throttle over the top.
205 $this->logger->log( $level, $logMsg, $context );