| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Integration\Permissions; |
| 4 |
|
| 5 |
use Liuggio\StatsdClient\Entity\StatsdData; |
| 6 |
use Liuggio\StatsdClient\Entity\StatsdDataInterface; |
| 7 |
use MediaWiki\Config\ServiceOptions; |
| 8 |
use MediaWiki\MainConfigNames; |
| 9 |
use MediaWiki\Permissions\RateLimiter; |
| 10 |
use MediaWiki\Permissions\RateLimitSubject; |
| 11 |
use MediaWiki\User\CentralId\CentralIdLookup; |
| 12 |
use MediaWiki\User\UserIdentity; |
| 13 |
use MediaWiki\User\UserIdentityValue; |
| 14 |
use MediaWikiIntegrationTestCase; |
| 15 |
use PHPUnit\Framework\MockObject\MockObject; |
| 16 |
use Wikimedia\ObjectCache\HashBagOStuff; |
| 17 |
use Wikimedia\Stats\BufferingStatsdDataFactory; |
| 18 |
use Wikimedia\TestingAccessWrapper; |
| 19 |
use Wikimedia\WRStats\BagOStuffStatsStore; |
| 20 |
use Wikimedia\WRStats\WRStatsFactory; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
class RateLimiterTest extends MediaWikiIntegrationTestCase { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
private function getMockContralIdProvider() { |
| 32 |
$mockCentralIdLookup = $this->createNoOpMock( |
| 33 |
CentralIdLookup::class, |
| 34 |
[ 'centralIdFromLocalUser', 'getProviderId' ] |
| 35 |
); |
| 36 |
|
| 37 |
$mockCentralIdLookup->method( 'centralIdFromLocalUser' ) |
| 38 |
->willReturnCallback( static function ( UserIdentity $user ) { |
| 39 |
return $user->getId() % 100; |
| 40 |
} ); |
| 41 |
$mockCentralIdLookup->method( 'getProviderId' ) |
| 42 |
->willReturn( 'test' ); |
| 43 |
|
| 44 |
return $mockCentralIdLookup; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public function testPingLimiterGlobal() { |
| 52 |
$limits = [ |
| 53 |
'read' => [ |
| 54 |
'anon' => [ 1, 60 ], |
| 55 |
], |
| 56 |
'edit' => [ |
| 57 |
'anon' => [ 1, 60 ], |
| 58 |
], |
| 59 |
'purge' => [ |
| 60 |
'ip' => [ 1, 60 ], |
| 61 |
'subnet' => [ 1, 60 ], |
| 62 |
], |
| 63 |
'rollback' => [ |
| 64 |
'user' => [ 1, 60 ], |
| 65 |
], |
| 66 |
'move' => [ |
| 67 |
'user-global' => [ 1, 60 ], |
| 68 |
], |
| 69 |
'delete' => [ |
| 70 |
'ip-all' => [ 1, 60 ], |
| 71 |
'subnet-all' => [ 1, 60 ], |
| 72 |
], |
| 73 |
]; |
| 74 |
|
| 75 |
|
| 76 |
$cache = new HashBagOStuff( [ 'keyspace' => 'xwiki' ] ); |
| 77 |
$cacheAccess = TestingAccessWrapper::newFromObject( $cache ); |
| 78 |
$cacheAccess->keyspace = 'xwiki'; |
| 79 |
|
| 80 |
$statsFactory = new WRStatsFactory( new BagOStuffStatsStore( $cache ) ); |
| 81 |
|
| 82 |
$stats = new BufferingStatsdDataFactory( 'test.' ); |
| 83 |
$limiter = $this->newRateLimiter( $limits, [], $statsFactory ); |
| 84 |
$limiter->setStats( $stats ); |
| 85 |
|
| 86 |
|
| 87 |
$anon1 = $this->newFakeAnon( '1.2.3.4' ); |
| 88 |
$anon2 = $this->newFakeAnon( '1.2.3.8' ); |
| 89 |
$anon3 = $this->newFakeAnon( '6.7.8.9' ); |
| 90 |
$anon4 = $this->newFakeAnon( '6.7.8.1' ); |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
$frankX1 = $this->newFakeUser( 'Frank', '1.2.3.4', 111 ); |
| 96 |
$frankX2 = $this->newFakeUser( 'Frank', '1.2.3.8', 111 ); |
| 97 |
$frankY1 = $this->newFakeUser( 'Frank', '1.2.3.4', 211 ); |
| 98 |
$janeX1 = $this->newFakeUser( 'Jane', '1.2.3.4', 456 ); |
| 99 |
$janeX3 = $this->newFakeUser( 'Jane', '6.7.8.9', 456 ); |
| 100 |
$janeY1 = $this->newFakeUser( 'Jane', '1.2.3.4', 756 ); |
| 101 |
$karaX1 = $this->newFakeUser( 'Kara', '5.5.5.5', 100 ); |
| 102 |
$karaY1 = $this->newFakeUser( 'Kara', '5.5.5.5', 200 ); |
| 103 |
|
| 104 |
|
| 105 |
$this->assertFalse( $limiter->limit( $anon1, 'read' ), 'First anon read' ); |
| 106 |
$this->assertFalse( $limiter->limit( $anon2, 'read' ), 'Second anon read (should ignore any limits)' ); |
| 107 |
|
| 108 |
$this->assertFalse( $limiter->limit( $anon1, 'edit' ), 'First anon edit' ); |
| 109 |
$this->assertTrue( $limiter->limit( $anon2, 'edit' ), 'Second anon edit' ); |
| 110 |
|
| 111 |
$this->assertFalse( $limiter->limit( $anon1, 'purge' ), 'Anon purge' ); |
| 112 |
$this->assertTrue( $limiter->limit( $anon1, 'purge' ), 'Anon purge via same IP' ); |
| 113 |
|
| 114 |
$this->assertFalse( $limiter->limit( $anon3, 'purge' ), 'Anon purge via different subnet' ); |
| 115 |
$this->assertTrue( $limiter->limit( $anon2, 'purge' ), 'Anon purge via same subnet' ); |
| 116 |
|
| 117 |
$this->assertFalse( $limiter->limit( $frankX1, 'rollback' ), 'First rollback' ); |
| 118 |
$this->assertTrue( $limiter->limit( $frankX2, 'rollback' ), 'Second rollback via different IP' ); |
| 119 |
$this->assertFalse( $limiter->limit( $janeX1, 'rollback' ), 'Rlbk by different user, same IP' ); |
| 120 |
|
| 121 |
$this->assertFalse( $limiter->limit( $frankX1, 'move' ), 'First move' ); |
| 122 |
$this->assertTrue( $limiter->limit( $frankX2, 'move' ), 'Second move via different IP' ); |
| 123 |
$this->assertFalse( $limiter->limit( $janeX1, 'move' ), 'Move by different user, same IP' ); |
| 124 |
$this->assertFalse( $limiter->limit( $karaX1, 'move' ), 'Move by another user' ); |
| 125 |
$this->assertTrue( $limiter->limit( $karaX1, 'move' ), 'Second move by another user' ); |
| 126 |
|
| 127 |
$this->assertFalse( $limiter->limit( $frankX1, 'delete' ), 'First delete' ); |
| 128 |
$this->assertTrue( $limiter->limit( $janeX1, 'delete' ), 'Delete via same IP' ); |
| 129 |
|
| 130 |
$this->assertTrue( $limiter->limit( $frankX2, 'delete' ), 'Delete via same subnet' ); |
| 131 |
$this->assertFalse( $limiter->limit( $janeX3, 'delete' ), 'Delete via different subnet' ); |
| 132 |
|
| 133 |
|
| 134 |
$cacheAccess->keyspace = 'ywiki'; |
| 135 |
|
| 136 |
$this->assertFalse( $limiter->limit( $anon3, 'edit' ), 'Anon edit on wiki Y' ); |
| 137 |
$this->assertTrue( $limiter->limit( $anon4, 'purge' ), 'Anon purge on wiki Y, same subnet' ); |
| 138 |
$this->assertFalse( $limiter->limit( $frankY1, 'rollback' ), 'Rollback on wiki Y, same name' ); |
| 139 |
$this->assertTrue( $limiter->limit( $frankY1, 'move' ), 'Move on wiki Y, same name' ); |
| 140 |
$this->assertTrue( $limiter->limit( $janeY1, 'move' ), 'Move on wiki Y, different user' ); |
| 141 |
$this->assertTrue( $limiter->limit( $frankY1, 'delete' ), 'Delete on wiki Y, same IP' ); |
| 142 |
|
| 143 |
|
| 144 |
$this->assertFalse( $limiter->limit( $karaY1, 'move' ), 'Move by another user' ); |
| 145 |
$this->assertTrue( $limiter->limit( $karaY1, 'move' ), 'Second move by another user' ); |
| 146 |
|
| 147 |
|
| 148 |
$statsData = $stats->getData(); |
| 149 |
$this->assertStatsHasCount( 'test.RateLimiter.limit.edit.tripped_by.anon', 1, $statsData ); |
| 150 |
$this->assertStatsHasCount( 'test.RateLimiter.limit.purge.tripped_by.ip', 1, $statsData ); |
| 151 |
$this->assertStatsHasCount( 'test.RateLimiter.limit.purge.tripped_by.subnet', 1, $statsData ); |
| 152 |
$this->assertStatsHasCount( 'test.RateLimiter.limit.delete.tripped_by.ip_all', 1, $statsData ); |
| 153 |
$this->assertStatsHasCount( 'test.RateLimiter.limit.delete.tripped_by.subnet_all', 1, $statsData ); |
| 154 |
$this->assertStatsHasCount( 'test.RateLimiter.limit.rollback.tripped_by.user', 1, $statsData ); |
| 155 |
$this->assertStatsHasCount( 'test.RateLimiter.limit.move.tripped_by.user_global', 1, $statsData ); |
| 156 |
} |
| 157 |
|
| 158 |
public function testPingLimiterWithStaleCache() { |
| 159 |
$limits = [ |
| 160 |
'edit' => [ |
| 161 |
'user' => [ 1, 60 ], |
| 162 |
], |
| 163 |
]; |
| 164 |
|
| 165 |
$bagTime = 1600000000.0; |
| 166 |
$appTime = 1600000000; |
| 167 |
$bag = new HashBagOStuff(); |
| 168 |
|
| 169 |
$statsFactory = new WRStatsFactory( new BagOStuffStatsStore( $bag ) ); |
| 170 |
$statsFactory->setCurrentTime( $appTime ); |
| 171 |
|
| 172 |
$bag->setMockTime( $bagTime ); |
| 173 |
|
| 174 |
$user = $this->newFakeUser( 'Frank', '1.2.3.4', 111 ); |
| 175 |
$limiter = $this->newRateLimiter( $limits, [], $statsFactory ); |
| 176 |
|
| 177 |
$this->assertFalse( $limiter->limit( $user, 'edit' ), 'limit not reached' ); |
| 178 |
$this->assertTrue( $limiter->limit( $user, 'edit' ), 'limit reached' ); |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
$bagTime += 10; |
| 184 |
$statsFactory->setCurrentTime( $appTime += 100 ); |
| 185 |
|
| 186 |
$this->assertFalse( $limiter->limit( $user, 'edit' ), 'limit expired' ); |
| 187 |
$this->assertTrue( $limiter->limit( $user, 'edit' ), 'limit functional after expiry' ); |
| 188 |
} |
| 189 |
|
| 190 |
public function testPingLimiterRate() { |
| 191 |
$limits = [ |
| 192 |
'edit' => [ |
| 193 |
'user' => [ 3, 60 ], |
| 194 |
], |
| 195 |
]; |
| 196 |
|
| 197 |
$fakeTime = 1600000000; |
| 198 |
$cache = new HashBagOStuff(); |
| 199 |
|
| 200 |
$cache->setMockTime( $fakeTime ); |
| 201 |
$statsFactory = new WRStatsFactory( new BagOStuffStatsStore( $cache ) ); |
| 202 |
$statsFactory->setCurrentTime( $fakeTime ); |
| 203 |
|
| 204 |
$user = $this->newFakeUser( 'Frank', '1.2.3.4', 111 ); |
| 205 |
$limiter = $this->newRateLimiter( $limits, [], $statsFactory ); |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
$this->assertFalse( $limiter->limit( $user, 'edit' ), 'first ping should pass' ); |
| 211 |
|
| 212 |
$statsFactory->setCurrentTime( $fakeTime += 50 ); |
| 213 |
$this->assertFalse( $limiter->limit( $user, 'edit' ), 'second ping should pass' ); |
| 214 |
|
| 215 |
$statsFactory->setCurrentTime( $fakeTime += 50 ); |
| 216 |
$this->assertFalse( $limiter->limit( $user, 'edit' ), 'third ping should pass' ); |
| 217 |
|
| 218 |
$statsFactory->setCurrentTime( $fakeTime += 50 ); |
| 219 |
$this->assertFalse( $limiter->limit( $user, 'edit' ), 'fourth ping should pass' ); |
| 220 |
|
| 221 |
$statsFactory->setCurrentTime( $fakeTime += 50 ); |
| 222 |
$this->assertFalse( $limiter->limit( $user, 'edit' ), 'fifth ping should pass' ); |
| 223 |
} |
| 224 |
|
| 225 |
public function testPingLimiterHook() { |
| 226 |
$limits = [ |
| 227 |
'edit' => [ |
| 228 |
'user' => [ 3, 60 ], |
| 229 |
], |
| 230 |
]; |
| 231 |
|
| 232 |
$stats = new BufferingStatsdDataFactory( 'test.' ); |
| 233 |
|
| 234 |
$user = $this->newFakeUser( 'Frank', '1.2.3.4', 111 ); |
| 235 |
$limiter = $this->newRateLimiter( $limits, [] ); |
| 236 |
$limiter->setStats( $stats ); |
| 237 |
|
| 238 |
|
| 239 |
$this->setTemporaryHook( |
| 240 |
'PingLimiter', |
| 241 |
static function ( &$user, $action, &$result, $incrBy ) { |
| 242 |
return false; |
| 243 |
} |
| 244 |
); |
| 245 |
$this->assertFalse( |
| 246 |
$limiter->limit( $user, 'edit' ), |
| 247 |
'Hooks that just return false leave $result false' |
| 248 |
); |
| 249 |
$this->removeTemporaryHook( 'PingLimiter' ); |
| 250 |
|
| 251 |
|
| 252 |
$this->setTemporaryHook( |
| 253 |
'PingLimiter', |
| 254 |
static function ( &$user, $action, &$result, $incrBy ) { |
| 255 |
$result = true; |
| 256 |
return false; |
| 257 |
} |
| 258 |
); |
| 259 |
$this->assertTrue( |
| 260 |
$limiter->limit( $user, 'edit' ), |
| 261 |
'Hooks can set $result to true' |
| 262 |
); |
| 263 |
$this->assertFalse( |
| 264 |
$limiter->limit( $user, 'read' ), |
| 265 |
'The "read" permission will bypass the hook' |
| 266 |
); |
| 267 |
$this->removeTemporaryHook( 'PingLimiter' ); |
| 268 |
|
| 269 |
|
| 270 |
$this->assertFalse( |
| 271 |
$limiter->limit( $user, 'FakeActionWithNoRateLimit' ), |
| 272 |
'Actions with no rate limit set do not trip the rate limiter' |
| 273 |
); |
| 274 |
|
| 275 |
$statsData = $stats->getData(); |
| 276 |
$this->assertStatsHasCount( 'test.RateLimiter.limit.edit.result.passed_by_hook', 1, $statsData ); |
| 277 |
$this->assertStatsHasCount( 'test.RateLimiter.limit.edit.result.tripped_by_hook', 1, $statsData ); |
| 278 |
|
| 279 |
$this->assertStatsNotHasCount( 'test.RateLimiter.limit.edit.result.passed', $statsData ); |
| 280 |
$this->assertStatsNotHasCount( 'test.RateLimiter.limit.edit.result.tripped', $statsData ); |
| 281 |
} |
| 282 |
|
| 283 |
public function testIsLimitableAction() { |
| 284 |
$limits = [ |
| 285 |
'read' => [ |
| 286 |
'user' => [ 3, 60 ], |
| 287 |
], |
| 288 |
'edit' => [ |
| 289 |
'user' => [ 3, 60 ], |
| 290 |
], |
| 291 |
]; |
| 292 |
|
| 293 |
$limiter = $this->newRateLimiter( $limits, [] ); |
| 294 |
|
| 295 |
$this->assertTrue( $limiter->isLimitable( 'edit' ), 'edit should be limitable' ); |
| 296 |
$this->assertFalse( $limiter->isLimitable( 'read' ), 'read should be non-limitable' ); |
| 297 |
$this->assertFalse( $limiter->isLimitable( 'move' ), 'move should not be limited' ); |
| 298 |
|
| 299 |
|
| 300 |
$this->setTemporaryHook( |
| 301 |
'PingLimiter', |
| 302 |
static function () { |
| 303 |
|
| 304 |
} |
| 305 |
); |
| 306 |
|
| 307 |
$this->assertTrue( $limiter->isLimitable( 'edit' ), 'edit should be limitable' ); |
| 308 |
$this->assertFalse( $limiter->isLimitable( 'read' ), 'read should be non-limitable' ); |
| 309 |
$this->assertTrue( $limiter->isLimitable( 'move' ), 'move should be limitable because of hook' ); |
| 310 |
} |
| 311 |
|
| 312 |
public static function provideIsExempt() { |
| 313 |
$user = new UserIdentityValue( 123, 'Foo' ); |
| 314 |
|
| 315 |
yield 'IP not excluded' |
| 316 |
=> [ [], new RateLimitSubject( $user, '1.2.3.4', [] ), false ]; |
| 317 |
|
| 318 |
yield 'IP excluded' |
| 319 |
=> [ [ '1.2.3.4' ], new RateLimitSubject( $user, '1.2.3.4', [] ), true ]; |
| 320 |
|
| 321 |
yield 'IP subnet excluded' |
| 322 |
=> [ [ '1.2.3.0/8' ], new RateLimitSubject( $user, '1.2.3.4', [] ), true ]; |
| 323 |
|
| 324 |
$flags = [ RateLimitSubject::EXEMPT => true ]; |
| 325 |
yield 'noratelimit right' |
| 326 |
=> [ [], new RateLimitSubject( $user, '1.2.3.4', $flags ), true ]; |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
public function testIsExempt( |
| 337 |
array $rateLimitExcludeIps, |
| 338 |
RateLimitSubject $subject, |
| 339 |
bool $expected |
| 340 |
) { |
| 341 |
$limiter = $this->newRateLimiter( [], $rateLimitExcludeIps ); |
| 342 |
|
| 343 |
$this->assertSame( $expected, $limiter->isExempt( $subject ) ); |
| 344 |
} |
| 345 |
|
| 346 |
private function newFakeAnon( string $ip ) { |
| 347 |
return new RateLimitSubject( |
| 348 |
new UserIdentityValue( 0, $ip ), |
| 349 |
$ip, |
| 350 |
[ RateLimitSubject::NEWBIE => true ] |
| 351 |
); |
| 352 |
} |
| 353 |
|
| 354 |
private function newFakeUser( string $name, string $ip, int $id, $newbie = false ) { |
| 355 |
return new RateLimitSubject( |
| 356 |
new UserIdentityValue( $id, $name ), |
| 357 |
$ip, |
| 358 |
[ RateLimitSubject::NEWBIE => $newbie ] |
| 359 |
); |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
protected function newRateLimiter( |
| 371 |
array $limits, |
| 372 |
array $excludedIPs, |
| 373 |
?WRStatsFactory $statsFactory = null |
| 374 |
): RateLimiter { |
| 375 |
$statsFactory ??= new WRStatsFactory( new BagOStuffStatsStore( new HashBagOStuff() ) ); |
| 376 |
|
| 377 |
$services = $this->getServiceContainer(); |
| 378 |
|
| 379 |
$limiter = new RateLimiter( |
| 380 |
new ServiceOptions( RateLimiter::CONSTRUCTOR_OPTIONS, [ |
| 381 |
MainConfigNames::RateLimits => $limits, |
| 382 |
MainConfigNames::RateLimitsExcludedIPs => $excludedIPs, |
| 383 |
] ), |
| 384 |
$statsFactory, |
| 385 |
$this->getMockContralIdProvider(), |
| 386 |
$services->getUserFactory(), |
| 387 |
$services->getUserGroupManager(), |
| 388 |
$services->getHookContainer() |
| 389 |
); |
| 390 |
|
| 391 |
return $limiter; |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
public function testLimitTypes() { |
| 400 |
$limits = [ |
| 401 |
'edit' => [ |
| 402 |
'user' => [ 1, 60 ], |
| 403 |
'ip' => [ 2, 60 ], |
| 404 |
], |
| 405 |
]; |
| 406 |
|
| 407 |
$newbie1 = $this->newFakeUser( 'User1', '127.0.0.1', 1, true ); |
| 408 |
$newbie2 = $this->newFakeUser( 'User2', '127.0.0.1', 2, true ); |
| 409 |
$newbie3 = $this->newFakeUser( 'User3', '127.0.0.1', 3, true ); |
| 410 |
|
| 411 |
$stats = new BufferingStatsdDataFactory( 'test.' ); |
| 412 |
$limiter = $this->newRateLimiter( $limits, [] ); |
| 413 |
$limiter->setStats( $stats ); |
| 414 |
|
| 415 |
$this->assertFalse( $limiter->limit( $newbie1, 'edit' ) ); |
| 416 |
$this->assertFalse( $limiter->limit( $newbie2, 'edit' ) ); |
| 417 |
$this->assertTrue( $limiter->limit( $newbie3, 'edit' ) ); |
| 418 |
|
| 419 |
$statsData = $stats->getData(); |
| 420 |
$this->assertStatsHasCount( 'test.RateLimiter.limit.edit.result.passed', 1, $statsData ); |
| 421 |
$this->assertStatsHasCount( 'test.RateLimiter.limit.edit.result.tripped', 1, $statsData ); |
| 422 |
$this->assertStatsHasCount( 'test.RateLimiter.limit.edit.tripped_by.ip', 1, $statsData ); |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
public function testLimitTypes_newbie() { |
| 431 |
$limits = [ |
| 432 |
'edit' => [ |
| 433 |
'user' => [ 10, 60 ], |
| 434 |
'newbie' => [ 1, 60 ], |
| 435 |
'ip' => [ 1, 60 ], |
| 436 |
], |
| 437 |
]; |
| 438 |
|
| 439 |
$newbie1 = $this->newFakeUser( 'User1', '127.0.0.1', 1, true ); |
| 440 |
$newbie2 = $this->newFakeUser( 'User2', '127.0.0.1', 2, true ); |
| 441 |
|
| 442 |
$limiter = $this->newRateLimiter( $limits, [] ); |
| 443 |
|
| 444 |
$this->assertFalse( $limiter->limit( $newbie1, 'edit' ) ); |
| 445 |
$this->assertTrue( $limiter->limit( $newbie1, 'edit' ) ); |
| 446 |
$this->assertTrue( $limiter->limit( $newbie2, 'edit' ) ); |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
public function testCanBypass() { |
| 454 |
$limits = [ |
| 455 |
'edit' => [ |
| 456 |
'user' => [ 1, 60 ], |
| 457 |
], |
| 458 |
'delete' => [ |
| 459 |
'&can-bypass' => false, |
| 460 |
'user' => [ 1, 60 ], |
| 461 |
], |
| 462 |
]; |
| 463 |
|
| 464 |
$user = new RateLimitSubject( |
| 465 |
new UserIdentityValue( 7, 'Garth' ), |
| 466 |
'127.0.0.1', |
| 467 |
[ RateLimitSubject::EXEMPT => true ] |
| 468 |
); |
| 469 |
|
| 470 |
$stats = new BufferingStatsdDataFactory( 'test.' ); |
| 471 |
$limiter = $this->newRateLimiter( $limits, [] ); |
| 472 |
$limiter->setStats( $stats ); |
| 473 |
|
| 474 |
$this->assertFalse( $limiter->limit( $user, 'edit' ) ); |
| 475 |
$this->assertFalse( $limiter->limit( $user, 'delete' ) ); |
| 476 |
|
| 477 |
$this->assertFalse( $limiter->limit( $user, 'edit' ), 'bypass should be granted' ); |
| 478 |
$this->assertTrue( $limiter->limit( $user, 'delete' ), 'bypass should be denied' ); |
| 479 |
|
| 480 |
$statsData = $stats->getData(); |
| 481 |
$this->assertStatsHasCount( 'test.RateLimiter.limit.edit.result.exempt', 1, $statsData ); |
| 482 |
$this->assertStatsNotHasCount( 'test.RateLimiter.limit.delete.result.exempt', $statsData ); |
| 483 |
$this->assertStatsHasCount( 'test.RateLimiter.limit.delete.result.tripped', 1, $statsData ); |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
public function testPeek() { |
| 493 |
$limits = [ |
| 494 |
'edit' => [ |
| 495 |
'user' => [ 1, 60 ], |
| 496 |
], |
| 497 |
]; |
| 498 |
|
| 499 |
$user = new RateLimitSubject( new UserIdentityValue( 7, 'Garth' ), '127.0.0.1', [] ); |
| 500 |
|
| 501 |
$limiter = $this->newRateLimiter( $limits, [] ); |
| 502 |
|
| 503 |
|
| 504 |
$this->assertFalse( $limiter->limit( $user, 'edit', 0 ) ); |
| 505 |
|
| 506 |
|
| 507 |
$this->assertFalse( $limiter->limit( $user, 'edit', 0 ) ); |
| 508 |
|
| 509 |
|
| 510 |
$this->assertFalse( $limiter->limit( $user, 'edit', 1 ) ); |
| 511 |
|
| 512 |
|
| 513 |
$this->assertTrue( $limiter->limit( $user, 'edit', 0 ) ); |
| 514 |
} |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
public function testGroupLimits() { |
| 521 |
$limits = [ |
| 522 |
'edit' => [ |
| 523 |
'user' => [ 1, 60 ], |
| 524 |
'autoconfirmed' => [ 2, 60 ], |
| 525 |
], |
| 526 |
]; |
| 527 |
|
| 528 |
$user = $this->getTestUser( [ 'autoconfirmed' ] )->getUser(); |
| 529 |
$user = new RateLimitSubject( $user, '127.0.0.1', [] ); |
| 530 |
|
| 531 |
$stats = new BufferingStatsdDataFactory( 'test.' ); |
| 532 |
$limiter = $this->newRateLimiter( $limits, [] ); |
| 533 |
$limiter->setStats( $stats ); |
| 534 |
|
| 535 |
$this->assertFalse( $limiter->limit( $user, 'edit' ) ); |
| 536 |
$this->assertFalse( $limiter->limit( $user, 'edit' ), 'limit for autoconfirmed used' ); |
| 537 |
$this->assertTrue( $limiter->limit( $user, 'edit' ), 'limit for autoconfirmed exceeded' ); |
| 538 |
|
| 539 |
$statsData = $stats->getData(); |
| 540 |
$this->assertStatsHasCount( 'test.RateLimiter.limit.edit.result.passed', 1, $statsData ); |
| 541 |
$this->assertStatsHasCount( 'test.RateLimiter.limit.edit.result.tripped', 1, $statsData ); |
| 542 |
$this->assertStatsHasCount( 'test.RateLimiter.limit.edit.tripped_by.autoconfirmed', 1, $statsData ); |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
private function assertStatsHasCount( string $key, ?int $value, array $statsData ) { |
| 551 |
$metric = StatsdDataInterface::STATSD_METRIC_COUNT; |
| 552 |
|
| 553 |
foreach ( $statsData as $data ) { |
| 554 |
if ( $data->getMetric() === $metric && $data->getValue() === $value && $data->getKey() == $key ) { |
| 555 |
$this->addToAssertionCount( 1 ); |
| 556 |
return; |
| 557 |
} |
| 558 |
} |
| 559 |
|
| 560 |
$this->fail( "Missing metric data entry: $key/$metric/$value" ); |
| 561 |
} |
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
private function assertStatsNotHasCount( string $key, array $statsData ) { |
| 568 |
$metric = StatsdDataInterface::STATSD_METRIC_COUNT; |
| 569 |
|
| 570 |
foreach ( $statsData as $data ) { |
| 571 |
if ( $data->getMetric() === $metric && $data->getKey() == $key ) { |
| 572 |
$this->fail( "Metric data entry was not expected to be present: $key/$metric" ); |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
$this->addToAssertionCount( 1 ); |
| 577 |
} |
| 578 |
|
| 579 |
} |