| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Auth; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use MediaWiki\Auth\Throttler; |
| 7 |
use MediaWiki\MainConfigNames; |
| 8 |
use MediaWikiIntegrationTestCase; |
| 9 |
use Psr\Log\AbstractLogger; |
| 10 |
use Psr\Log\LoggerInterface; |
| 11 |
use Psr\Log\NullLogger; |
| 12 |
use Wikimedia\ObjectCache\BagOStuff; |
| 13 |
use Wikimedia\ObjectCache\HashBagOStuff; |
| 14 |
use Wikimedia\TestingAccessWrapper; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
class ThrottlerTest extends MediaWikiIntegrationTestCase { |
| 21 |
public function testConstructor() { |
| 22 |
$cache = new HashBagOStuff(); |
| 23 |
$logger = $this->getMockBuilder( AbstractLogger::class ) |
| 24 |
->onlyMethods( [ 'log' ] ) |
| 25 |
->getMockForAbstractClass(); |
| 26 |
|
| 27 |
$throttler = new Throttler( |
| 28 |
[ [ 'count' => 123, 'seconds' => 456 ] ], |
| 29 |
[ 'type' => 'foo', 'cache' => $cache ] |
| 30 |
); |
| 31 |
$throttler->setLogger( $logger ); |
| 32 |
$throttlerPriv = TestingAccessWrapper::newFromObject( $throttler ); |
| 33 |
$this->assertSame( [ [ 'count' => 123, 'seconds' => 456 ] ], $throttlerPriv->conditions ); |
| 34 |
$this->assertSame( 'foo', $throttlerPriv->type ); |
| 35 |
$this->assertSame( $cache, $throttlerPriv->cache ); |
| 36 |
$this->assertSame( $logger, $throttlerPriv->logger ); |
| 37 |
|
| 38 |
$throttler = new Throttler( [ [ 'count' => 123, 'seconds' => 456 ] ] ); |
| 39 |
$throttler->setLogger( new NullLogger() ); |
| 40 |
$throttlerPriv = TestingAccessWrapper::newFromObject( $throttler ); |
| 41 |
$this->assertSame( [ [ 'count' => 123, 'seconds' => 456 ] ], $throttlerPriv->conditions ); |
| 42 |
$this->assertSame( 'custom', $throttlerPriv->type ); |
| 43 |
$this->assertInstanceOf( BagOStuff::class, $throttlerPriv->cache ); |
| 44 |
$this->assertInstanceOf( LoggerInterface::class, $throttlerPriv->logger ); |
| 45 |
|
| 46 |
$this->overrideConfigValue( |
| 47 |
MainConfigNames::PasswordAttemptThrottle, |
| 48 |
[ [ 'count' => 321, 'seconds' => 654 ] ] |
| 49 |
); |
| 50 |
$throttler = new Throttler(); |
| 51 |
$throttler->setLogger( new NullLogger() ); |
| 52 |
$throttlerPriv = TestingAccessWrapper::newFromObject( $throttler ); |
| 53 |
$this->assertSame( [ [ 'count' => 321, 'seconds' => 654 ] ], $throttlerPriv->conditions ); |
| 54 |
$this->assertSame( 'password', $throttlerPriv->type ); |
| 55 |
$this->assertInstanceOf( BagOStuff::class, $throttlerPriv->cache ); |
| 56 |
$this->assertInstanceOf( LoggerInterface::class, $throttlerPriv->logger ); |
| 57 |
|
| 58 |
try { |
| 59 |
new Throttler( [], [ 'foo' => 1, 'bar' => 2, 'baz' => 3 ] ); |
| 60 |
$this->fail( 'Expected exception not thrown' ); |
| 61 |
} catch ( InvalidArgumentException $ex ) { |
| 62 |
$this->assertSame( 'unrecognized parameters: foo, bar, baz', $ex->getMessage() ); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public function testNormalizeThrottleConditions( $condition, $normalized ) { |
| 70 |
$throttler = new Throttler( $condition ); |
| 71 |
$throttler->setLogger( new NullLogger() ); |
| 72 |
$throttlerPriv = TestingAccessWrapper::newFromObject( $throttler ); |
| 73 |
$this->assertSame( $normalized, $throttlerPriv->conditions ); |
| 74 |
} |
| 75 |
|
| 76 |
public static function provideNormalizeThrottleConditions() { |
| 77 |
return [ |
| 78 |
[ |
| 79 |
[], |
| 80 |
[], |
| 81 |
], |
| 82 |
[ |
| 83 |
[ 'count' => 1, 'seconds' => 2 ], |
| 84 |
[ [ 'count' => 1, 'seconds' => 2 ] ], |
| 85 |
], |
| 86 |
[ |
| 87 |
[ [ 'count' => 1, 'seconds' => 2 ], [ 'count' => 2, 'seconds' => 3 ] ], |
| 88 |
[ [ 'count' => 1, 'seconds' => 2 ], [ 'count' => 2, 'seconds' => 3 ] ], |
| 89 |
], |
| 90 |
]; |
| 91 |
} |
| 92 |
|
| 93 |
public function testNormalizeThrottleConditions2() { |
| 94 |
$priv = TestingAccessWrapper::newFromClass( Throttler::class ); |
| 95 |
$this->assertSame( [], $priv->normalizeThrottleConditions( null ) ); |
| 96 |
$this->assertSame( [], $priv->normalizeThrottleConditions( 'bad' ) ); |
| 97 |
} |
| 98 |
|
| 99 |
public function testIncrease() { |
| 100 |
$cache = new HashBagOStuff(); |
| 101 |
$throttler = new Throttler( [ |
| 102 |
[ 'count' => 2, 'seconds' => 10, ], |
| 103 |
[ 'count' => 4, 'seconds' => 15, 'allIPs' => true ], |
| 104 |
], [ 'cache' => $cache ] ); |
| 105 |
$throttler->setLogger( new NullLogger() ); |
| 106 |
|
| 107 |
$result = $throttler->increase( 'SomeUser', '1.2.3.4' ); |
| 108 |
$this->assertFalse( $result, 'should not throttle' ); |
| 109 |
|
| 110 |
$result = $throttler->increase( 'SomeUser', '1.2.3.4' ); |
| 111 |
$this->assertFalse( $result, 'should not throttle' ); |
| 112 |
|
| 113 |
$result = $throttler->increase( 'SomeUser', '1.2.3.4' ); |
| 114 |
$this->assertSame( [ 'throttleIndex' => 0, 'count' => 2, 'wait' => 10 ], $result ); |
| 115 |
|
| 116 |
$result = $throttler->increase( 'OtherUser', '1.2.3.4' ); |
| 117 |
$this->assertFalse( $result, 'should not throttle' ); |
| 118 |
|
| 119 |
$result = $throttler->increase( 'SomeUser', '2.3.4.5' ); |
| 120 |
$this->assertFalse( $result, 'should not throttle' ); |
| 121 |
|
| 122 |
$result = $throttler->increase( 'SomeUser', '3.4.5.6' ); |
| 123 |
$this->assertFalse( $result, 'should not throttle' ); |
| 124 |
|
| 125 |
$result = $throttler->increase( 'SomeUser', '3.4.5.6' ); |
| 126 |
$this->assertSame( [ 'throttleIndex' => 1, 'count' => 4, 'wait' => 15 ], $result ); |
| 127 |
} |
| 128 |
|
| 129 |
public function testZeroCount() { |
| 130 |
$cache = new HashBagOStuff(); |
| 131 |
$throttler = new Throttler( [ [ 'count' => 0, 'seconds' => 10 ] ], [ 'cache' => $cache ] ); |
| 132 |
$throttler->setLogger( new NullLogger() ); |
| 133 |
|
| 134 |
$result = $throttler->increase( 'SomeUser', '1.2.3.4' ); |
| 135 |
$this->assertFalse( $result, 'should not throttle, count=0 is ignored' ); |
| 136 |
|
| 137 |
$result = $throttler->increase( 'SomeUser', '1.2.3.4' ); |
| 138 |
$this->assertFalse( $result, 'should not throttle, count=0 is ignored' ); |
| 139 |
|
| 140 |
$result = $throttler->increase( 'SomeUser', '1.2.3.4' ); |
| 141 |
$this->assertFalse( $result, 'should not throttle, count=0 is ignored' ); |
| 142 |
} |
| 143 |
|
| 144 |
public function testNamespacing() { |
| 145 |
$cache = new HashBagOStuff(); |
| 146 |
$throttler1 = new Throttler( [ [ 'count' => 1, 'seconds' => 10 ] ], |
| 147 |
[ 'cache' => $cache, 'type' => 'foo' ] ); |
| 148 |
$throttler2 = new Throttler( [ [ 'count' => 1, 'seconds' => 10 ] ], |
| 149 |
[ 'cache' => $cache, 'type' => 'foo' ] ); |
| 150 |
$throttler3 = new Throttler( [ [ 'count' => 1, 'seconds' => 10 ] ], |
| 151 |
[ 'cache' => $cache, 'type' => 'bar' ] ); |
| 152 |
$throttler1->setLogger( new NullLogger() ); |
| 153 |
$throttler2->setLogger( new NullLogger() ); |
| 154 |
$throttler3->setLogger( new NullLogger() ); |
| 155 |
|
| 156 |
$throttled = [ 'throttleIndex' => 0, 'count' => 1, 'wait' => 10 ]; |
| 157 |
|
| 158 |
$result = $throttler1->increase( 'SomeUser', '1.2.3.4' ); |
| 159 |
$this->assertFalse( $result, 'should not throttle' ); |
| 160 |
|
| 161 |
$result = $throttler1->increase( 'SomeUser', '1.2.3.4' ); |
| 162 |
$this->assertEquals( $throttled, $result, 'should throttle' ); |
| 163 |
|
| 164 |
$result = $throttler2->increase( 'SomeUser', '1.2.3.4' ); |
| 165 |
$this->assertEquals( $throttled, $result, 'should throttle, same namespace' ); |
| 166 |
|
| 167 |
$result = $throttler3->increase( 'SomeUser', '1.2.3.4' ); |
| 168 |
$this->assertFalse( $result, 'should not throttle, different namespace' ); |
| 169 |
} |
| 170 |
|
| 171 |
public function testExpiration() { |
| 172 |
$cache = $this->getMockBuilder( HashBagOStuff::class ) |
| 173 |
->onlyMethods( [ 'add', 'incrWithInit' ] )->getMock(); |
| 174 |
$throttler = new Throttler( [ [ 'count' => 3, 'seconds' => 10 ] ], [ 'cache' => $cache ] ); |
| 175 |
$throttler->setLogger( new NullLogger() ); |
| 176 |
|
| 177 |
$cache->expects( $this->once() ) |
| 178 |
->method( 'incrWithInit' ) |
| 179 |
->with( $this->anything(), 10, 1 ); |
| 180 |
$throttler->increase( 'SomeUser' ); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
public function testException() { |
| 186 |
$throttler = new Throttler( [ [ 'count' => 3, 'seconds' => 10 ] ] ); |
| 187 |
$throttler->setLogger( new NullLogger() ); |
| 188 |
$this->expectException( InvalidArgumentException::class ); |
| 189 |
$throttler->increase(); |
| 190 |
} |
| 191 |
|
| 192 |
public function testLog() { |
| 193 |
$cache = new HashBagOStuff(); |
| 194 |
$throttler = new Throttler( [ [ 'count' => 1, 'seconds' => 10 ] ], [ 'cache' => $cache ] ); |
| 195 |
|
| 196 |
$logger = $this->getMockBuilder( AbstractLogger::class ) |
| 197 |
->onlyMethods( [ 'log' ] ) |
| 198 |
->getMockForAbstractClass(); |
| 199 |
$logger->expects( $this->never() )->method( 'log' ); |
| 200 |
$throttler->setLogger( $logger ); |
| 201 |
$result = $throttler->increase( 'SomeUser', '1.2.3.4' ); |
| 202 |
$this->assertFalse( $result, 'should not throttle' ); |
| 203 |
|
| 204 |
$logger = $this->getMockBuilder( AbstractLogger::class ) |
| 205 |
->onlyMethods( [ 'log' ] ) |
| 206 |
->getMockForAbstractClass(); |
| 207 |
$logger->expects( $this->once() )->method( 'log' )->with( $this->anything(), $this->anything(), [ |
| 208 |
'throttle' => 'custom', |
| 209 |
'index' => 0, |
| 210 |
'ipKey' => '1.2.3.4', |
| 211 |
'username' => 'SomeUser', |
| 212 |
'count' => 1, |
| 213 |
'expiry' => 10, |
| 214 |
'method' => 'foo', |
| 215 |
] ); |
| 216 |
$throttler->setLogger( $logger ); |
| 217 |
$result = $throttler->increase( 'SomeUser', '1.2.3.4', 'foo' ); |
| 218 |
$this->assertSame( [ 'throttleIndex' => 0, 'count' => 1, 'wait' => 10 ], $result ); |
| 219 |
} |
| 220 |
|
| 221 |
public function testClear() { |
| 222 |
$cache = new HashBagOStuff(); |
| 223 |
$throttler = new Throttler( [ [ 'count' => 1, 'seconds' => 10 ] ], [ 'cache' => $cache ] ); |
| 224 |
$throttler->setLogger( new NullLogger() ); |
| 225 |
|
| 226 |
$result = $throttler->increase( 'SomeUser', '1.2.3.4' ); |
| 227 |
$this->assertFalse( $result, 'should not throttle' ); |
| 228 |
|
| 229 |
$result = $throttler->increase( 'SomeUser', '1.2.3.4' ); |
| 230 |
$this->assertSame( [ 'throttleIndex' => 0, 'count' => 1, 'wait' => 10 ], $result ); |
| 231 |
|
| 232 |
$result = $throttler->increase( 'OtherUser', '1.2.3.4' ); |
| 233 |
$this->assertFalse( $result, 'should not throttle' ); |
| 234 |
|
| 235 |
$result = $throttler->increase( 'OtherUser', '1.2.3.4' ); |
| 236 |
$this->assertSame( [ 'throttleIndex' => 0, 'count' => 1, 'wait' => 10 ], $result ); |
| 237 |
|
| 238 |
$throttler->clear( 'SomeUser', '1.2.3.4' ); |
| 239 |
|
| 240 |
$result = $throttler->increase( 'SomeUser', '1.2.3.4' ); |
| 241 |
$this->assertFalse( $result, 'should not throttle' ); |
| 242 |
|
| 243 |
$result = $throttler->increase( 'OtherUser', '1.2.3.4' ); |
| 244 |
$this->assertSame( [ 'throttleIndex' => 0, 'count' => 1, 'wait' => 10 ], $result ); |
| 245 |
} |
| 246 |
} |