| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
namespace MediaWiki\Auth; |
| 23 |
|
| 24 |
use InvalidArgumentException; |
| 25 |
use MediaWiki\Logger\LoggerFactory; |
| 26 |
use MediaWiki\MainConfigNames; |
| 27 |
use MediaWiki\MediaWikiServices; |
| 28 |
use Psr\Log\LoggerAwareInterface; |
| 29 |
use Psr\Log\LoggerInterface; |
| 30 |
use Psr\Log\LogLevel; |
| 31 |
use Wikimedia\ObjectCache\BagOStuff; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class Throttler implements LoggerAwareInterface { |
| 40 |
|
| 41 |
protected $type; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
protected $conditions; |
| 49 |
|
| 50 |
protected $cache; |
| 51 |
|
| 52 |
protected $logger; |
| 53 |
|
| 54 |
protected $warningLimit; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public function __construct( ?array $conditions = null, array $params = [] ) { |
| 66 |
$invalidParams = array_diff_key( $params, |
| 67 |
array_fill_keys( [ 'type', 'cache', 'warningLimit' ], true ) ); |
| 68 |
if ( $invalidParams ) { |
| 69 |
throw new InvalidArgumentException( 'unrecognized parameters: ' |
| 70 |
. implode( ', ', array_keys( $invalidParams ) ) ); |
| 71 |
} |
| 72 |
|
| 73 |
$services = MediaWikiServices::getInstance(); |
| 74 |
$objectCacheFactory = $services->getObjectCacheFactory(); |
| 75 |
|
| 76 |
if ( $conditions === null ) { |
| 77 |
$config = $services->getMainConfig(); |
| 78 |
$conditions = $config->get( MainConfigNames::PasswordAttemptThrottle ); |
| 79 |
$params += [ |
| 80 |
'type' => 'password', |
| 81 |
'cache' => $objectCacheFactory->getLocalClusterInstance(), |
| 82 |
'warningLimit' => 50, |
| 83 |
]; |
| 84 |
} else { |
| 85 |
$params += [ |
| 86 |
'type' => 'custom', |
| 87 |
'cache' => $objectCacheFactory->getLocalClusterInstance(), |
| 88 |
'warningLimit' => INF, |
| 89 |
]; |
| 90 |
} |
| 91 |
|
| 92 |
$this->type = $params['type']; |
| 93 |
$this->conditions = static::normalizeThrottleConditions( $conditions ); |
| 94 |
$this->cache = $params['cache']; |
| 95 |
$this->warningLimit = $params['warningLimit']; |
| 96 |
|
| 97 |
$this->setLogger( LoggerFactory::getInstance( 'throttler' ) ); |
| 98 |
} |
| 99 |
|
| 100 |
public function setLogger( LoggerInterface $logger ) { |
| 101 |
$this->logger = $logger; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public function increase( $username = null, $ip = null, $caller = null ) { |
| 119 |
if ( $username === null && $ip === null ) { |
| 120 |
throw new InvalidArgumentException( 'Either username or IP must be set for throttling' ); |
| 121 |
} |
| 122 |
|
| 123 |
$userKey = $username ? md5( $username ) : null; |
| 124 |
foreach ( $this->conditions as $index => $throttleCondition ) { |
| 125 |
$ipKey = isset( $throttleCondition['allIPs'] ) ? null : $ip; |
| 126 |
$count = $throttleCondition['count']; |
| 127 |
$expiry = $throttleCondition['seconds']; |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
if ( !$count || ( $userKey === null && $ipKey === null ) ) { |
| 132 |
continue; |
| 133 |
} |
| 134 |
|
| 135 |
$throttleKey = $this->getThrottleKey( $this->type, $index, $ipKey, $userKey ); |
| 136 |
$throttleCount = $this->cache->get( $throttleKey ); |
| 137 |
if ( $throttleCount && $throttleCount >= $count ) { |
| 138 |
|
| 139 |
$this->logRejection( [ |
| 140 |
'throttle' => $this->type, |
| 141 |
'index' => $index, |
| 142 |
'ipKey' => $ipKey, |
| 143 |
'username' => $username, |
| 144 |
'count' => $count, |
| 145 |
'expiry' => $expiry, |
| 146 |
|
| 147 |
'method' => $caller ?: __METHOD__, |
| 148 |
|
| 149 |
] ); |
| 150 |
|
| 151 |
return [ 'throttleIndex' => $index, 'count' => $count, 'wait' => $expiry ]; |
| 152 |
} else { |
| 153 |
$this->cache->incrWithInit( $throttleKey, $expiry, 1 ); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
return false; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
public function clear( $username = null, $ip = null ) { |
| 169 |
$userKey = $username ? md5( $username ) : null; |
| 170 |
foreach ( $this->conditions as $index => $specificThrottle ) { |
| 171 |
$ipKey = isset( $specificThrottle['allIPs'] ) ? null : $ip; |
| 172 |
$throttleKey = $this->getThrottleKey( $this->type, $index, $ipKey, $userKey ); |
| 173 |
$this->cache->delete( $throttleKey ); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
private function getThrottleKey( string $type, int $index, ?string $ipKey, ?string $userKey ): string { |
| 186 |
return $this->cache->makeGlobalKey( |
| 187 |
'throttler', |
| 188 |
$type, |
| 189 |
$index, |
| 190 |
$ipKey ?? '', |
| 191 |
$userKey ?? '' |
| 192 |
); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
protected static function normalizeThrottleConditions( $throttleConditions ) { |
| 202 |
if ( !is_array( $throttleConditions ) ) { |
| 203 |
return []; |
| 204 |
} |
| 205 |
if ( isset( $throttleConditions['count'] ) ) { |
| 206 |
$throttleConditions = [ $throttleConditions ]; |
| 207 |
} |
| 208 |
return $throttleConditions; |
| 209 |
} |
| 210 |
|
| 211 |
protected function logRejection( array $context ) { |
| 212 |
$logMsg = 'Throttle {throttle} hit, throttled for {expiry} seconds due to {count} attempts ' |
| 213 |
. 'from username {username} and IP {ipKey}'; |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
$level = $context['count'] >= $this->warningLimit ? LogLevel::WARNING : LogLevel::INFO; |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
$this->logger->log( $level, $logMsg, $context ); |
| 223 |
} |
| 224 |
|
| 225 |
} |