2022-05-24 20:54:25 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Permissions;
|
|
|
|
|
|
2023-06-18 12:31:18 +00:00
|
|
|
use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
|
2022-05-24 20:54:25 +00:00
|
|
|
use MediaWiki\Config\ServiceOptions;
|
|
|
|
|
use MediaWiki\HookContainer\HookContainer;
|
|
|
|
|
use MediaWiki\HookContainer\HookRunner;
|
|
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
|
|
|
|
use MediaWiki\MainConfigNames;
|
2023-09-19 16:42:44 +00:00
|
|
|
use MediaWiki\User\CentralId\CentralIdLookup;
|
2022-05-24 20:54:25 +00:00
|
|
|
use MediaWiki\User\UserFactory;
|
|
|
|
|
use MediaWiki\User\UserGroupManager;
|
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
use Wikimedia\IPUtils;
|
2024-09-27 18:21:33 +00:00
|
|
|
use Wikimedia\Stats\NullStatsdDataFactory;
|
2022-07-01 06:07:07 +00:00
|
|
|
use Wikimedia\WRStats\LimitCondition;
|
|
|
|
|
use Wikimedia\WRStats\WRStatsFactory;
|
2022-05-24 20:54:25 +00:00
|
|
|
|
|
|
|
|
/**
|
2022-06-29 23:41:05 +00:00
|
|
|
* Provides rate limiting for a set of actions based on several counter
|
2022-05-24 20:54:25 +00:00
|
|
|
* buckets.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.39
|
|
|
|
|
*/
|
|
|
|
|
class RateLimiter {
|
|
|
|
|
|
2024-07-30 22:04:51 +00:00
|
|
|
private LoggerInterface $logger;
|
|
|
|
|
private StatsdDataFactoryInterface $stats;
|
2022-05-24 20:54:25 +00:00
|
|
|
|
2024-07-30 22:04:51 +00:00
|
|
|
private ServiceOptions $options;
|
|
|
|
|
private WRStatsFactory $wrstatsFactory;
|
|
|
|
|
private ?CentralIdLookup $centralIdLookup;
|
|
|
|
|
private UserFactory $userFactory;
|
|
|
|
|
private UserGroupManager $userGroupManager;
|
|
|
|
|
private HookContainer $hookContainer;
|
|
|
|
|
private HookRunner $hookRunner;
|
2022-05-24 20:54:25 +00:00
|
|
|
|
|
|
|
|
/** @var array */
|
|
|
|
|
private $rateLimits;
|
|
|
|
|
|
2022-06-28 21:28:54 +00:00
|
|
|
/**
|
|
|
|
|
* Actions that are exempt from all rate limiting.
|
|
|
|
|
*
|
|
|
|
|
* Actions listed here will bypass all rate limiting,
|
|
|
|
|
* including limits implemented in hooks.
|
|
|
|
|
*
|
|
|
|
|
* This serves as a performance optimization, to avoid overhead for actions
|
|
|
|
|
* that are performed a lot and have no need to be limited.
|
|
|
|
|
*
|
|
|
|
|
* @note This is currently hard-coded to contain just the 'read' action.
|
|
|
|
|
* It can be made configurable to extended to include more actions if needed.
|
|
|
|
|
*
|
|
|
|
|
* @var array<string,bool>
|
|
|
|
|
*/
|
|
|
|
|
private array $nonLimitableActions = [
|
|
|
|
|
'read' => true,
|
|
|
|
|
];
|
|
|
|
|
|
2022-05-24 20:54:25 +00:00
|
|
|
/**
|
|
|
|
|
* @internal
|
|
|
|
|
*/
|
|
|
|
|
public const CONSTRUCTOR_OPTIONS = [
|
|
|
|
|
MainConfigNames::RateLimits,
|
|
|
|
|
MainConfigNames::RateLimitsExcludedIPs,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
|
ServiceOptions $options,
|
2022-07-01 06:07:07 +00:00
|
|
|
WRStatsFactory $wrstatsFactory,
|
2022-05-24 20:54:25 +00:00
|
|
|
?CentralIdLookup $centralIdLookup,
|
|
|
|
|
UserFactory $userFactory,
|
|
|
|
|
UserGroupManager $userGroupManager,
|
|
|
|
|
HookContainer $hookContainer
|
|
|
|
|
) {
|
|
|
|
|
$this->logger = LoggerFactory::getInstance( 'ratelimit' );
|
2023-06-18 12:31:18 +00:00
|
|
|
$this->stats = new NullStatsdDataFactory();
|
2022-05-24 20:54:25 +00:00
|
|
|
|
|
|
|
|
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
|
|
|
|
|
$this->options = $options;
|
2022-07-01 06:07:07 +00:00
|
|
|
$this->wrstatsFactory = $wrstatsFactory;
|
2022-05-24 20:54:25 +00:00
|
|
|
$this->centralIdLookup = $centralIdLookup;
|
|
|
|
|
$this->userFactory = $userFactory;
|
|
|
|
|
$this->userGroupManager = $userGroupManager;
|
2022-06-28 21:28:54 +00:00
|
|
|
$this->hookContainer = $hookContainer;
|
2022-05-24 20:54:25 +00:00
|
|
|
$this->hookRunner = new HookRunner( $hookContainer );
|
|
|
|
|
|
|
|
|
|
$this->rateLimits = $this->options->get( MainConfigNames::RateLimits );
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-18 12:31:18 +00:00
|
|
|
public function setStats( StatsdDataFactoryInterface $stats ) {
|
|
|
|
|
$this->stats = $stats;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function incrementStats( $name ) {
|
|
|
|
|
$this->stats->increment( "RateLimiter.$name" );
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-24 20:54:25 +00:00
|
|
|
/**
|
|
|
|
|
* Is this user exempt from rate limiting?
|
|
|
|
|
*
|
|
|
|
|
* @param RateLimitSubject $subject The subject of the rate limit, representing the
|
|
|
|
|
* client performing the action.
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function isExempt( RateLimitSubject $subject ) {
|
|
|
|
|
$rateLimitsExcludedIPs = $this->options->get( MainConfigNames::RateLimitsExcludedIPs );
|
|
|
|
|
|
|
|
|
|
$ip = $subject->getIP();
|
|
|
|
|
if ( $ip && IPUtils::isInRanges( $ip, $rateLimitsExcludedIPs ) ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NOTE: To avoid circular dependencies, we rely on a flag here rather than using an
|
|
|
|
|
// Authority instance to check the permission. Using PermissionManager might work,
|
|
|
|
|
// but keeping cross-dependencies to a minimum seems best. The code that constructs
|
|
|
|
|
// the RateLimitSubject should know where to get the relevant info.
|
|
|
|
|
return $subject->is( RateLimitSubject::EXEMPT );
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-28 21:28:54 +00:00
|
|
|
/**
|
|
|
|
|
* Checks whether the given action may be limited.
|
|
|
|
|
* Can be used for optimization, to avoid calling limit() if we can know in advance that no limit will apply.
|
|
|
|
|
*
|
|
|
|
|
* @param string $action
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function isLimitable( $action ) {
|
|
|
|
|
// Bypass limit checks for actions that are defined to be non-limitable.
|
|
|
|
|
// This is a performance optimization.
|
|
|
|
|
if ( $this->nonLimitableActions[$action] ?? false ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( isset( $this->rateLimits[$action] ) ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $this->hookContainer->isRegistered( 'PingLimiter' ) ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-24 20:54:25 +00:00
|
|
|
/**
|
|
|
|
|
* Implements simple rate limits: enforce maximum actions per time period
|
|
|
|
|
* to put a brake on flooding.
|
|
|
|
|
*
|
2022-06-28 21:28:54 +00:00
|
|
|
* @note This method will always return false for any action listed in
|
|
|
|
|
* $this->nonLimitableActions. This allows rate limit checks to
|
|
|
|
|
* be bypassed for certain actions to avoid overhead and improve
|
|
|
|
|
* performance.
|
|
|
|
|
*
|
2022-05-24 20:54:25 +00:00
|
|
|
* @param RateLimitSubject $subject The subject of the rate limit, representing the
|
|
|
|
|
* client performing the action.
|
|
|
|
|
* @param string $action Action to enforce
|
Replace all instances of "per default" with "by default"
According to the dictionary, "per" (or more conventionally "as per")
means "according to". Refer OED "per" sense II.3.a. For example:
"No value was passed, so return null, as per default".
In this sentence, we are not specifying the default, we are referring
to the default. This correct usage of "per default" was used nowhere
in MediaWiki core as far as I can see.
Instead we have "per default" being used to mean "by default", that is,
giving the value to use when no explicit value was specified.
In OED, the phrase "by default" is blessed with its own section just
for computing usage:
"P.1.e. Computing. As an option or setting adopted automatically by a
computer program whenever an alternative is not specified by the user
or programmer. Cf. sense I.7a."
There are highly similar pre-computing usages of the same phrase,
whereas the phrase "per default" is not mentioned.
As a matter of style, I think "per default" should not be used even
when it is strictly correct, since the common incorrect usage makes it
ambiguous and misleading.
Change-Id: Ibcccc65ead864d082677b472b34ff32ff41c60ae
2024-04-29 00:13:28 +00:00
|
|
|
* @param int $incrBy Positive amount to increment counter by, 1 by default.
|
2022-05-24 20:54:25 +00:00
|
|
|
* Use 0 to check the limit without bumping the counter.
|
|
|
|
|
*
|
2022-07-01 06:07:07 +00:00
|
|
|
* @return bool True if a rate limit was exceeded.
|
2022-05-24 20:54:25 +00:00
|
|
|
*/
|
|
|
|
|
public function limit( RateLimitSubject $subject, string $action, int $incrBy = 1 ) {
|
2022-06-28 21:28:54 +00:00
|
|
|
// Bypass limit checks for actions that are defined to be non-limitable.
|
|
|
|
|
// This is a performance optimization.
|
|
|
|
|
if ( $this->nonLimitableActions[$action] ?? false ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-24 20:54:25 +00:00
|
|
|
$user = $subject->getUser();
|
|
|
|
|
$ip = $subject->getIP();
|
|
|
|
|
|
|
|
|
|
// Call the 'PingLimiter' hook
|
|
|
|
|
$result = false;
|
|
|
|
|
$legacyUser = $this->userFactory->newFromUserIdentity( $user );
|
|
|
|
|
if ( !$this->hookRunner->onPingLimiter( $legacyUser, $action, $result, $incrBy ) ) {
|
2023-06-18 12:31:18 +00:00
|
|
|
$this->incrementStats( "limit.$action.result." . ( $result ? 'tripped_by_hook' : 'passed_by_hook' ) );
|
2022-05-24 20:54:25 +00:00
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !isset( $this->rateLimits[$action] ) ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Some groups shouldn't trigger the ping limiter, ever
|
2022-07-01 06:07:07 +00:00
|
|
|
if ( $this->canBypass( $action ) && $this->isExempt( $subject ) ) {
|
2023-06-18 12:31:18 +00:00
|
|
|
$this->incrementStats( "limit.$action.result.exempt" );
|
2022-05-24 20:54:25 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-01 06:07:07 +00:00
|
|
|
$conds = $this->getConditions( $action );
|
|
|
|
|
$limiter = $this->wrstatsFactory->createRateLimiter( $conds, [ 'limiter', $action ] );
|
2025-02-28 18:12:30 +00:00
|
|
|
$peekMode = $incrBy === 0;
|
|
|
|
|
$limitBatch = $limiter->createBatch( $incrBy ?: 1 );
|
2022-05-24 20:54:25 +00:00
|
|
|
$this->logger->debug( __METHOD__ . ": limiting $action rate for {$user->getName()}" );
|
|
|
|
|
|
|
|
|
|
$id = $user->getId();
|
|
|
|
|
$isNewbie = $subject->is( RateLimitSubject::NEWBIE );
|
|
|
|
|
|
|
|
|
|
if ( $id == 0 ) {
|
|
|
|
|
// "shared anon" limit, for all anons combined
|
2022-07-01 06:07:07 +00:00
|
|
|
if ( isset( $conds['anon'] ) ) {
|
|
|
|
|
$limitBatch->localOp( 'anon', [] );
|
2022-05-24 20:54:25 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// "global per name" limit, across sites
|
2022-07-01 06:07:07 +00:00
|
|
|
if ( isset( $conds['user-global'] ) ) {
|
2022-05-24 20:54:25 +00:00
|
|
|
|
|
|
|
|
$centralId = $this->centralIdLookup
|
|
|
|
|
? $this->centralIdLookup->centralIdFromLocalUser( $user, CentralIdLookup::AUDIENCE_RAW )
|
|
|
|
|
: 0;
|
|
|
|
|
|
|
|
|
|
if ( $centralId ) {
|
|
|
|
|
// We don't have proper realms, use provider ID.
|
|
|
|
|
$realm = $this->centralIdLookup->getProviderId();
|
2022-07-01 06:07:07 +00:00
|
|
|
$limitBatch->globalOp( 'user-global', [ $realm, $centralId ] );
|
2022-05-24 20:54:25 +00:00
|
|
|
} else {
|
|
|
|
|
// Fall back to a local key for a local ID
|
2022-07-01 06:07:07 +00:00
|
|
|
$limitBatch->localOp( 'user-global', [ 'local', $id ] );
|
2022-05-24 20:54:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $isNewbie && $ip ) {
|
|
|
|
|
// "per ip" limit for anons and newbie users
|
2022-07-01 06:07:07 +00:00
|
|
|
if ( isset( $conds['ip'] ) ) {
|
|
|
|
|
$limitBatch->globalOp( 'ip', $ip );
|
2022-05-24 20:54:25 +00:00
|
|
|
}
|
|
|
|
|
// "per subnet" limit for anons and newbie users
|
2022-07-01 06:07:07 +00:00
|
|
|
if ( isset( $conds['subnet'] ) ) {
|
2022-05-24 20:54:25 +00:00
|
|
|
$subnet = IPUtils::getSubnet( $ip );
|
|
|
|
|
if ( $subnet !== false ) {
|
2022-07-01 06:07:07 +00:00
|
|
|
$limitBatch->globalOp( 'subnet', $subnet );
|
2022-05-24 20:54:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// determine the "per user account" limit
|
2022-07-01 06:07:07 +00:00
|
|
|
$userEntityType = false;
|
|
|
|
|
if ( $id !== 0 && isset( $conds['user'] ) ) {
|
2022-05-24 20:54:25 +00:00
|
|
|
// default limit for logged-in users
|
2022-07-01 06:07:07 +00:00
|
|
|
$userEntityType = 'user';
|
2022-05-24 20:54:25 +00:00
|
|
|
}
|
|
|
|
|
// limits for newbie logged-in users (overrides all the normal user limits)
|
2022-07-01 06:07:07 +00:00
|
|
|
if ( $id !== 0 && $isNewbie && isset( $conds['newbie'] ) ) {
|
|
|
|
|
$userEntityType = 'newbie';
|
2022-05-24 20:54:25 +00:00
|
|
|
} else {
|
|
|
|
|
// Check for group-specific limits
|
|
|
|
|
// If more than one group applies, use the highest allowance (if higher than the default)
|
|
|
|
|
$userGroups = $this->userGroupManager->getUserGroups( $user );
|
|
|
|
|
foreach ( $userGroups as $group ) {
|
2022-07-01 06:07:07 +00:00
|
|
|
if ( isset( $conds[$group] ) ) {
|
|
|
|
|
if ( $userEntityType === false
|
|
|
|
|
|| $conds[$group]->perSecond() > $conds[$userEntityType]->perSecond()
|
2022-05-24 20:54:25 +00:00
|
|
|
) {
|
2022-07-01 06:07:07 +00:00
|
|
|
$userEntityType = $group;
|
2022-05-24 20:54:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set the user limit key
|
2022-07-01 06:07:07 +00:00
|
|
|
if ( $userEntityType !== false ) {
|
|
|
|
|
$limitBatch->localOp( $userEntityType, $id );
|
2022-05-24 20:54:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ip-based limits for all ping-limitable users
|
2022-07-01 06:07:07 +00:00
|
|
|
if ( isset( $conds['ip-all'] ) && $ip ) {
|
2022-05-24 20:54:25 +00:00
|
|
|
// ignore if user limit is more permissive
|
2022-07-01 06:07:07 +00:00
|
|
|
if ( $isNewbie || $userEntityType === false
|
|
|
|
|
|| $conds['ip-all']->perSecond() > $conds[$userEntityType]->perSecond()
|
|
|
|
|
) {
|
|
|
|
|
$limitBatch->globalOp( 'ip-all', $ip );
|
2022-05-24 20:54:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// subnet-based limits for all ping-limitable users
|
2022-07-01 06:07:07 +00:00
|
|
|
if ( isset( $conds['subnet-all'] ) && $ip ) {
|
2022-05-24 20:54:25 +00:00
|
|
|
$subnet = IPUtils::getSubnet( $ip );
|
|
|
|
|
if ( $subnet !== false ) {
|
|
|
|
|
// ignore if user limit is more permissive
|
2022-07-01 06:07:07 +00:00
|
|
|
if ( $isNewbie || $userEntityType === false
|
|
|
|
|
|| $conds['ip-all']->perSecond() > $conds[$userEntityType]->perSecond()
|
|
|
|
|
) {
|
|
|
|
|
$limitBatch->globalOp( 'subnet-all', $subnet );
|
2022-05-24 20:54:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$loggerInfo = [
|
|
|
|
|
'name' => $user->getName(),
|
|
|
|
|
'ip' => $ip,
|
|
|
|
|
];
|
|
|
|
|
|
2025-02-28 18:12:30 +00:00
|
|
|
$batchResult = $peekMode ? $limitBatch->peek() : $limitBatch->tryIncr();
|
2022-07-01 06:07:07 +00:00
|
|
|
foreach ( $batchResult->getFailedResults() as $type => $result ) {
|
|
|
|
|
$this->logger->info(
|
|
|
|
|
'User::pingLimiter: User tripped rate limit',
|
|
|
|
|
[
|
|
|
|
|
'action' => $action,
|
|
|
|
|
'limit' => $result->condition->limit,
|
|
|
|
|
'period' => $result->condition->window,
|
|
|
|
|
'count' => $result->prevTotal,
|
|
|
|
|
'key' => $type
|
|
|
|
|
] + $loggerInfo
|
|
|
|
|
);
|
2023-06-18 12:31:18 +00:00
|
|
|
|
|
|
|
|
$this->incrementStats( "limit.$action.tripped_by.$type" );
|
2022-07-01 06:07:07 +00:00
|
|
|
}
|
2022-05-24 20:54:25 +00:00
|
|
|
|
2023-06-18 12:31:18 +00:00
|
|
|
$allowed = $batchResult->isAllowed();
|
|
|
|
|
|
|
|
|
|
$this->incrementStats( "limit.$action.result." . ( $allowed ? 'passed' : 'tripped' ) );
|
|
|
|
|
|
|
|
|
|
return !$allowed;
|
2022-07-01 06:07:07 +00:00
|
|
|
}
|
2022-05-24 20:54:25 +00:00
|
|
|
|
2022-07-01 06:07:07 +00:00
|
|
|
private function canBypass( string $action ) {
|
|
|
|
|
return $this->rateLimits[$action]['&can-bypass'] ?? true;
|
|
|
|
|
}
|
2022-05-24 20:54:25 +00:00
|
|
|
|
2022-07-01 06:07:07 +00:00
|
|
|
/**
|
|
|
|
|
* @param string $action
|
|
|
|
|
* @return LimitCondition[]
|
|
|
|
|
*/
|
|
|
|
|
private function getConditions( $action ) {
|
|
|
|
|
if ( !isset( $this->rateLimits[$action] ) ) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
$conds = [];
|
|
|
|
|
foreach ( $this->rateLimits[$action] as $entityType => $limitInfo ) {
|
|
|
|
|
if ( $entityType[0] === '&' ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
[ $limit, $window ] = $limitInfo;
|
|
|
|
|
$conds[$entityType] = new LimitCondition(
|
|
|
|
|
$limit,
|
|
|
|
|
$window
|
2022-05-24 20:54:25 +00:00
|
|
|
);
|
|
|
|
|
}
|
2022-07-01 06:07:07 +00:00
|
|
|
return $conds;
|
2022-05-24 20:54:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|