| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\AbuseFilter\Consequences\Consequence; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use MediaWiki\Extension\AbuseFilter\Consequences\ConsequenceNotPrecheckedException; |
| 7 |
use MediaWiki\Extension\AbuseFilter\Consequences\Parameters; |
| 8 |
use MediaWiki\Title\Title; |
| 9 |
use MediaWiki\User\UserEditTracker; |
| 10 |
use MediaWiki\User\UserFactory; |
| 11 |
use Psr\Log\LoggerInterface; |
| 12 |
use Wikimedia\IPUtils; |
| 13 |
use Wikimedia\ObjectCache\BagOStuff; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
class Throttle extends Consequence implements ConsequencesDisablerConsequence { |
| 19 |
|
| 20 |
private $throttleParams; |
| 21 |
|
| 22 |
private $mainStash; |
| 23 |
|
| 24 |
private $userEditTracker; |
| 25 |
|
| 26 |
private $userFactory; |
| 27 |
|
| 28 |
private $logger; |
| 29 |
|
| 30 |
private $filterIsCentral; |
| 31 |
|
| 32 |
private $centralDB; |
| 33 |
|
| 34 |
|
| 35 |
private $hitThrottle; |
| 36 |
|
| 37 |
private const IPV4_RANGE = '16'; |
| 38 |
private const IPV6_RANGE = '64'; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public function __construct( |
| 52 |
Parameters $parameters, |
| 53 |
array $throttleParams, |
| 54 |
BagOStuff $mainStash, |
| 55 |
UserEditTracker $userEditTracker, |
| 56 |
UserFactory $userFactory, |
| 57 |
LoggerInterface $logger, |
| 58 |
bool $filterIsCentral, |
| 59 |
?string $centralDB |
| 60 |
) { |
| 61 |
parent::__construct( $parameters ); |
| 62 |
$this->throttleParams = $throttleParams; |
| 63 |
$this->mainStash = $mainStash; |
| 64 |
$this->userEditTracker = $userEditTracker; |
| 65 |
$this->userFactory = $userFactory; |
| 66 |
$this->logger = $logger; |
| 67 |
$this->filterIsCentral = $filterIsCentral; |
| 68 |
$this->centralDB = $centralDB; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public function execute(): bool { |
| 76 |
if ( $this->hitThrottle === null ) { |
| 77 |
throw new ConsequenceNotPrecheckedException(); |
| 78 |
} |
| 79 |
foreach ( $this->throttleParams['groups'] as $throttleType ) { |
| 80 |
$this->setThrottled( $throttleType ); |
| 81 |
} |
| 82 |
return !$this->hitThrottle; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public function shouldDisableOtherConsequences(): bool { |
| 89 |
$this->hitThrottle = false; |
| 90 |
foreach ( $this->throttleParams['groups'] as $throttleType ) { |
| 91 |
$this->hitThrottle = $this->isThrottled( $throttleType ) || $this->hitThrottle; |
| 92 |
} |
| 93 |
return !$this->hitThrottle; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public function getSort(): int { |
| 100 |
return 0; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
private function isThrottled( string $types ): bool { |
| 112 |
$key = $this->throttleKey( $types ); |
| 113 |
$newCount = (int)$this->mainStash->get( $key ) + 1; |
| 114 |
|
| 115 |
$this->logger->debug( |
| 116 |
'New value is {newCount} for throttle key {key}. Maximum is {rateCount}.', |
| 117 |
[ |
| 118 |
'newCount' => $newCount, |
| 119 |
'key' => $key, |
| 120 |
'rateCount' => $this->throttleParams['count'], |
| 121 |
] |
| 122 |
); |
| 123 |
|
| 124 |
return $newCount > $this->throttleParams['count']; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
private function setThrottled( string $types ): void { |
| 133 |
$key = $this->throttleKey( $types ); |
| 134 |
$this->logger->debug( |
| 135 |
'Increasing throttle key {key}', |
| 136 |
[ 'key' => $key ] |
| 137 |
); |
| 138 |
$this->mainStash->incrWithInit( $key, $this->throttleParams['period'] ); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
private function throttleKey( string $type ): string { |
| 146 |
$types = explode( ',', $type ); |
| 147 |
|
| 148 |
$identifiers = []; |
| 149 |
|
| 150 |
foreach ( $types as $subtype ) { |
| 151 |
$identifiers[] = $this->throttleIdentifier( $subtype ); |
| 152 |
} |
| 153 |
|
| 154 |
$identifier = sha1( implode( ':', $identifiers ) ); |
| 155 |
|
| 156 |
if ( $this->parameters->getIsGlobalFilter() && !$this->filterIsCentral ) { |
| 157 |
return $this->mainStash->makeGlobalKey( |
| 158 |
'abusefilter', 'throttle', $this->centralDB, $this->throttleParams['id'], $identifier |
| 159 |
); |
| 160 |
} |
| 161 |
|
| 162 |
return $this->mainStash->makeKey( 'abusefilter', 'throttle', $this->throttleParams['id'], $identifier ); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
private function throttleIdentifier( string $type ): string { |
| 170 |
$user = $this->parameters->getUser(); |
| 171 |
switch ( $type ) { |
| 172 |
case 'ip': |
| 173 |
$identifier = $this->parameters->getActionSpecifier()->getIP(); |
| 174 |
break; |
| 175 |
case 'user': |
| 176 |
|
| 177 |
$identifier = $user->getId(); |
| 178 |
break; |
| 179 |
case 'range': |
| 180 |
$requestIP = $this->parameters->getActionSpecifier()->getIP(); |
| 181 |
$range = IPUtils::isIPv6( $requestIP ) ? self::IPV6_RANGE : self::IPV4_RANGE; |
| 182 |
$identifier = IPUtils::sanitizeRange( "{$requestIP}/$range" ); |
| 183 |
break; |
| 184 |
case 'creationdate': |
| 185 |
|
| 186 |
$reg = (int)$this->userFactory->newFromUserIdentity( $user )->getRegistration(); |
| 187 |
$identifier = $reg - ( $reg % 86400 ); |
| 188 |
break; |
| 189 |
case 'editcount': |
| 190 |
|
| 191 |
$identifier = $this->userEditTracker->getUserEditCount( $user ) ?: 0; |
| 192 |
break; |
| 193 |
case 'site': |
| 194 |
$identifier = 1; |
| 195 |
break; |
| 196 |
case 'page': |
| 197 |
$title = Title::castFromLinkTarget( $this->parameters->getTarget() ); |
| 198 |
|
| 199 |
$identifier = $title->getPrefixedText(); |
| 200 |
break; |
| 201 |
default: |
| 202 |
|
| 203 |
throw new InvalidArgumentException( "Invalid throttle type $type." ); |
| 204 |
} |
| 205 |
|
| 206 |
return "$type-$identifier"; |
| 207 |
} |
| 208 |
} |