];
/**
- * Array of IPs which should be excluded from rate limits.
+ * Array of IPs / CIDR ranges which should be excluded from rate limits.
* This may be useful for whitelisting NAT gateways for conferences, etc.
*/
$wgRateLimitsExcludedIPs = [];
*/
public function isPingLimitable() {
global $wgRateLimitsExcludedIPs;
- if ( in_array( $this->getRequest()->getIP(), $wgRateLimitsExcludedIPs ) ) {
+ if ( IP::isInRanges( $this->getRequest()->getIP(), $wgRateLimitsExcludedIPs ) ) {
// No other good way currently to disable rate limits
// for specific IPs. :P
// But this is a crappy hack and should die.
// Clean up.
$block->delete();
}
+
+ public function testIsPingLimitable() {
+ $request = new FauxRequest();
+ $request->setIP( '1.2.3.4' );
+ $user = User::newFromSession( $request );
+
+ $this->setMwGlobals( 'wgRateLimitsExcludedIPs', [] );
+ $this->assertTrue( $user->isPingLimitable() );
+
+ $this->setMwGlobals( 'wgRateLimitsExcludedIPs', [ '1.2.3.4' ] );
+ $this->assertFalse( $user->isPingLimitable() );
+
+ $this->setMwGlobals( 'wgRateLimitsExcludedIPs', [ '1.2.3.0/8' ] );
+ $this->assertFalse( $user->isPingLimitable() );
+
+ $this->setMwGlobals( 'wgRateLimitsExcludedIPs', [] );
+ $noRateLimitUser = $this->getMockBuilder( User::class )->disableOriginalConstructor()
+ ->setMethods( [ 'getIP', 'getRights' ] )->getMock();
+ $noRateLimitUser->expects( $this->any() )->method( 'getIP' )->willReturn( '1.2.3.4' );
+ $noRateLimitUser->expects( $this->any() )->method( 'getRights' )->willReturn( [ 'noratelimit' ] );
+ $this->assertFalse( $noRateLimitUser->isPingLimitable() );
+ }
}