Debian Codegraph @

includes/Permissions/RateLimiter.php

raw symbols SCIP
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 */
20
21 namespace MediaWiki\Permissions;
22
23 use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
24 use MediaWiki\Config\ServiceOptions;
25 use MediaWiki\HookContainer\HookContainer;
26 use MediaWiki\HookContainer\HookRunner;
27 use MediaWiki\Logger\LoggerFactory;
28 use MediaWiki\MainConfigNames;
29 use MediaWiki\User\CentralId\CentralIdLookup;
30 use MediaWiki\User\UserFactory;
31 use MediaWiki\User\UserGroupManager;
32 use Psr\Log\LoggerInterface;
33 use Wikimedia\IPUtils;
34 use Wikimedia\Stats\NullStatsdDataFactory;
35 use Wikimedia\WRStats\LimitCondition;
36 use Wikimedia\WRStats\WRStatsFactory;
37
38 /**
39 * Provides rate limiting for a set of actions based on several counter
40 * buckets.
41 *
42 * @since 1.39
43 */
44 class RateLimiter {
45
46 private LoggerInterface $logger;
47 private StatsdDataFactoryInterface $stats;
48
49 private ServiceOptions $options;
50 private WRStatsFactory $wrstatsFactory;
51 private ?CentralIdLookup $centralIdLookup;
52 private UserFactory $userFactory;
53 private UserGroupManager $userGroupManager;
54 private HookContainer $hookContainer;
55 private HookRunner $hookRunner;
56
57 /** @var array */
58 private $rateLimits;
59
60 /**
61 * Actions that are exempt from all rate limiting.
62 *
63 * Actions listed here will bypass all rate limiting,
64 * including limits implemented in hooks.
65 *
66 * This serves as a performance optimization, to avoid overhead for actions
67 * that are performed a lot and have no need to be limited.
68 *
69 * @note This is currently hard-coded to contain just the 'read' action.
70 * It can be made configurable to extended to include more actions if needed.
71 *
72 * @var array<string,bool>
73 */
74 private array $nonLimitableActions = [
75 'read' => true,
76 ];
77
78 /**
79 * @internal
80 */
81 public const CONSTRUCTOR_OPTIONS = [
82 MainConfigNames::RateLimits,
83 MainConfigNames::RateLimitsExcludedIPs,
84 ];
85
86 public function __construct(
87 ServiceOptions $options,
88 WRStatsFactory $wrstatsFactory,
89 ?CentralIdLookup $centralIdLookup,
90 UserFactory $userFactory,
91 UserGroupManager $userGroupManager,
92 HookContainer $hookContainer
93 ) {
94 $this->logger = LoggerFactory::getInstance( 'ratelimit' );
95 $this->stats = new NullStatsdDataFactory();
96
97 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
98 $this->options = $options;
99 $this->wrstatsFactory = $wrstatsFactory;
100 $this->centralIdLookup = $centralIdLookup;
101 $this->userFactory = $userFactory;
102 $this->userGroupManager = $userGroupManager;
103 $this->hookContainer = $hookContainer;
104 $this->hookRunner = new HookRunner( $hookContainer );
105
106 $this->rateLimits = $this->options->get( MainConfigNames::RateLimits );
107 }
108
109 public function setStats( StatsdDataFactoryInterface $stats ) {
110 $this->stats = $stats;
111 }
112
113 private function incrementStats( $name ) {
114 $this->stats->increment( "RateLimiter.$name" );
115 }
116
117 /**
118 * Is this user exempt from rate limiting?
119 *
120 * @param RateLimitSubject $subject The subject of the rate limit, representing the
121 * client performing the action.
122 *
123 * @return bool
124 */
125 public function isExempt( RateLimitSubject $subject ) {
126 $rateLimitsExcludedIPs = $this->options->get( MainConfigNames::RateLimitsExcludedIPs );
127
128 $ip = $subject->getIP();
129 if ( $ip && IPUtils::isInRanges( $ip, $rateLimitsExcludedIPs ) ) {
130 return true;
131 }
132
133 // NOTE: To avoid circular dependencies, we rely on a flag here rather than using an
134 // Authority instance to check the permission. Using PermissionManager might work,
135 // but keeping cross-dependencies to a minimum seems best. The code that constructs
136 // the RateLimitSubject should know where to get the relevant info.
137 return $subject->is( RateLimitSubject::EXEMPT );
138 }
139
140 /**
141 * Checks whether the given action may be limited.
142 * Can be used for optimization, to avoid calling limit() if we can know in advance that no limit will apply.
143 *
144 * @param string $action
145 *
146 * @return bool
147 */
148 public function isLimitable( $action ) {
149 // Bypass limit checks for actions that are defined to be non-limitable.
150 // This is a performance optimization.
151 if ( $this->nonLimitableActions[$action] ?? false ) {
152 return false;
153 }
154
155 if ( isset( $this->rateLimits[$action] ) ) {
156 return true;
157 }
158
159 if ( $this->hookContainer->isRegistered( 'PingLimiter' ) ) {
160 return true;
161 }
162
163 return false;
164 }
165
166 /**
167 * Implements simple rate limits: enforce maximum actions per time period
168 * to put a brake on flooding.
169 *
170 * @note This method will always return false for any action listed in
171 * $this->nonLimitableActions. This allows rate limit checks to
172 * be bypassed for certain actions to avoid overhead and improve
173 * performance.
174 *
175 * @param RateLimitSubject $subject The subject of the rate limit, representing the
176 * client performing the action.
177 * @param string $action Action to enforce
178 * @param int $incrBy Positive amount to increment counter by, 1 by default.
179 * Use 0 to check the limit without bumping the counter.
180 *
181 * @return bool True if a rate limit was exceeded.
182 */
183 public function limit( RateLimitSubject $subject, string $action, int $incrBy = 1 ) {
184 // Bypass limit checks for actions that are defined to be non-limitable.
185 // This is a performance optimization.
186 if ( $this->nonLimitableActions[$action] ?? false ) {
187 return false;
188 }
189
190 $user = $subject->getUser();
191 $ip = $subject->getIP();
192
193 // Call the 'PingLimiter' hook
194 $result = false;
195 $legacyUser = $this->userFactory->newFromUserIdentity( $user );
196 if ( !$this->hookRunner->onPingLimiter( $legacyUser, $action, $result, $incrBy ) ) {
197 $this->incrementStats( "limit.$action.result." . ( $result ? 'tripped_by_hook' : 'passed_by_hook' ) );
198 return $result;
199 }
200
201 if ( !isset( $this->rateLimits[$action] ) ) {
202 return false;
203 }
204
205 // Some groups shouldn't trigger the ping limiter, ever
206 if ( $this->canBypass( $action ) && $this->isExempt( $subject ) ) {
207 $this->incrementStats( "limit.$action.result.exempt" );
208 return false;
209 }
210
211 $conds = $this->getConditions( $action );
212 $limiter = $this->wrstatsFactory->createRateLimiter( $conds, [ 'limiter', $action ] );
213 $peekMode = $incrBy === 0;
214 $limitBatch = $limiter->createBatch( $incrBy ?: 1 );
215 $this->logger->debug( __METHOD__ . ": limiting $action rate for {$user->getName()}" );
216
217 $id = $user->getId();
218 $isNewbie = $subject->is( RateLimitSubject::NEWBIE );
219
220 if ( $id == 0 ) {
221 // "shared anon" limit, for all anons combined
222 if ( isset( $conds['anon'] ) ) {
223 $limitBatch->localOp( 'anon', [] );
224 }
225 } else {
226 // "global per name" limit, across sites
227 if ( isset( $conds['user-global'] ) ) {
228
229 $centralId = $this->centralIdLookup
230 ? $this->centralIdLookup->centralIdFromLocalUser( $user, CentralIdLookup::AUDIENCE_RAW )
231 : 0;
232
233 if ( $centralId ) {
234 // We don't have proper realms, use provider ID.
235 $realm = $this->centralIdLookup->getProviderId();
236 $limitBatch->globalOp( 'user-global', [ $realm, $centralId ] );
237 } else {
238 // Fall back to a local key for a local ID
239 $limitBatch->localOp( 'user-global', [ 'local', $id ] );
240 }
241 }
242 }
243
244 if ( $isNewbie && $ip ) {
245 // "per ip" limit for anons and newbie users
246 if ( isset( $conds['ip'] ) ) {
247 $limitBatch->globalOp( 'ip', $ip );
248 }
249 // "per subnet" limit for anons and newbie users
250 if ( isset( $conds['subnet'] ) ) {
251 $subnet = IPUtils::getSubnet( $ip );
252 if ( $subnet !== false ) {
253 $limitBatch->globalOp( 'subnet', $subnet );
254 }
255 }
256 }
257
258 // determine the "per user account" limit
259 $userEntityType = false;
260 if ( $id !== 0 && isset( $conds['user'] ) ) {
261 // default limit for logged-in users
262 $userEntityType = 'user';
263 }
264 // limits for newbie logged-in users (overrides all the normal user limits)
265 if ( $id !== 0 && $isNewbie && isset( $conds['newbie'] ) ) {
266 $userEntityType = 'newbie';
267 } else {
268 // Check for group-specific limits
269 // If more than one group applies, use the highest allowance (if higher than the default)
270 $userGroups = $this->userGroupManager->getUserGroups( $user );
271 foreach ( $userGroups as $group ) {
272 if ( isset( $conds[$group] ) ) {
273 if ( $userEntityType === false
274 || $conds[$group]->perSecond() > $conds[$userEntityType]->perSecond()
275 ) {
276 $userEntityType = $group;
277 }
278 }
279 }
280 }
281
282 // Set the user limit key
283 if ( $userEntityType !== false ) {
284 $limitBatch->localOp( $userEntityType, $id );
285 }
286
287 // ip-based limits for all ping-limitable users
288 if ( isset( $conds['ip-all'] ) && $ip ) {
289 // ignore if user limit is more permissive
290 if ( $isNewbie || $userEntityType === false
291 || $conds['ip-all']->perSecond() > $conds[$userEntityType]->perSecond()
292 ) {
293 $limitBatch->globalOp( 'ip-all', $ip );
294 }
295 }
296
297 // subnet-based limits for all ping-limitable users
298 if ( isset( $conds['subnet-all'] ) && $ip ) {
299 $subnet = IPUtils::getSubnet( $ip );
300 if ( $subnet !== false ) {
301 // ignore if user limit is more permissive
302 if ( $isNewbie || $userEntityType === false
303 || $conds['ip-all']->perSecond() > $conds[$userEntityType]->perSecond()
304 ) {
305 $limitBatch->globalOp( 'subnet-all', $subnet );
306 }
307 }
308 }
309
310 $loggerInfo = [
311 'name' => $user->getName(),
312 'ip' => $ip,
313 ];
314
315 $batchResult = $peekMode ? $limitBatch->peek() : $limitBatch->tryIncr();
316 foreach ( $batchResult->getFailedResults() as $type => $result ) {
317 $this->logger->info(
318 'User::pingLimiter: User tripped rate limit',
319 [
320 'action' => $action,
321 'limit' => $result->condition->limit,
322 'period' => $result->condition->window,
323 'count' => $result->prevTotal,
324 'key' => $type
325 ] + $loggerInfo
326 );
327
328 $this->incrementStats( "limit.$action.tripped_by.$type" );
329 }
330
331 $allowed = $batchResult->isAllowed();
332
333 $this->incrementStats( "limit.$action.result." . ( $allowed ? 'passed' : 'tripped' ) );
334
335 return !$allowed;
336 }
337
338 private function canBypass( string $action ) {
339 return $this->rateLimits[$action]['&can-bypass'] ?? true;
340 }
341
342 /**
343 * @param string $action
344 * @return LimitCondition[]
345 */
346 private function getConditions( $action ) {
347 if ( !isset( $this->rateLimits[$action] ) ) {
348 return [];
349 }
350 $conds = [];
351 foreach ( $this->rateLimits[$action] as $entityType => $limitInfo ) {
352 if ( $entityType[0] === '&' ) {
353 continue;
354 }
355 [ $limit, $window ] = $limitInfo;
356 $conds[$entityType] = new LimitCondition(
357 $limit,
358 $window
359 );
360 }
361 return $conds;
362 }
363
364 }