Debian Codegraph @

tests/phpunit/includes/block/BlockManagerTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Block\AbstractBlock;
4 use MediaWiki\Block\Block;
5 use MediaWiki\Block\BlockManager;
6 use MediaWiki\Block\CompositeBlock;
7 use MediaWiki\Block\DatabaseBlock;
8 use MediaWiki\Block\SystemBlock;
9 use MediaWiki\MainConfigNames;
10 use MediaWiki\Request\FauxRequest;
11 use MediaWiki\Request\FauxResponse;
12 use MediaWiki\User\User;
13 use Psr\Log\NullLogger;
14 use Wikimedia\TestingAccessWrapper;
15
16 /**
17 * @group Blocking
18 * @group Database
19 * @covers \MediaWiki\Block\BlockManager
20 */
21 class BlockManagerTest extends MediaWikiIntegrationTestCase {
22 use TestAllServiceOptionsUsed;
23
24 protected User $user;
25 protected User $sysopUser;
26 private array $blockManagerConfig;
27
28 protected function setUp(): void {
29 parent::setUp();
30
31 $this->user = $this->getTestUser()->getUser();
32 $this->sysopUser = $this->getTestSysop()->getUser();
33 $this->blockManagerConfig = [
34 MainConfigNames::ApplyIpBlocksToXff => true,
35 MainConfigNames::CookieSetOnAutoblock => true,
36 MainConfigNames::CookieSetOnIpBlock => true,
37 MainConfigNames::DnsBlacklistUrls => [],
38 MainConfigNames::EnableDnsBlacklist => true,
39 MainConfigNames::ProxyList => [],
40 MainConfigNames::ProxyWhitelist => [],
41 MainConfigNames::SecretKey => false,
42 MainConfigNames::SoftBlockRanges => [],
43 ];
44 }
45
46 private function getBlockManager( $overrideConfig ) {
47 return new BlockManager(
48 ...$this->getBlockManagerConstructorArgs( $overrideConfig )
49 );
50 }
51
52 private function getBlockManagerConstructorArgs( $overrideConfig ) {
53 $blockManagerConfig = array_merge( $this->blockManagerConfig, $overrideConfig );
54 $this->overrideConfigValues( $blockManagerConfig );
55 $services = $this->getServiceContainer();
56 return [
57 new LoggedServiceOptions(
58 self::$serviceOptionsAccessLog,
59 BlockManager::CONSTRUCTOR_OPTIONS,
60 $services->getMainConfig()
61 ),
62 $services->getUserFactory(),
63 $services->getUserIdentityUtils(),
64 new NullLogger(),
65 $services->getHookContainer(),
66 $services->getDatabaseBlockStore(),
67 $services->getProxyLookup()
68 ];
69 }
70
71 public function testGetBlock() {
72 // Reset so that hooks are called
73 $permissionManager = $this->getServiceContainer()->getPermissionManager();
74 $permissionManager->invalidateUsersRightsCache();
75
76 $onGetUserBlockCalled = false;
77 $onGetUserBlockIP = false;
78 $this->setTemporaryHook(
79 'GetUserBlock',
80 static function ( $user, $ip, &$block ) use ( &$onGetUserBlockCalled, &$onGetUserBlockIP ) {
81 $onGetUserBlockCalled = true;
82 $onGetUserBlockIP = $ip;
83 return true;
84 }
85 );
86
87 $blockManager = $this->getBlockManager( [] );
88 $block = $blockManager->getBlock(
89 $this->user,
90 null,
91 false
92 );
93
94 // We don't actually care about the block, just whether or not the right hooks were called
95 $this->assertTrue(
96 $onGetUserBlockCalled,
97 'Check that HookRunner::onGetUserBlock was called'
98 );
99 $this->assertNull(
100 $onGetUserBlockIP,
101 'The `GetUserBlock` hook should have been called with null since we ' .
102 'didn\'t pass a request'
103 );
104 }
105
106 /**
107 * @dataProvider provideBlocksForShouldApplyCookieBlock
108 */
109 public function testGetBlockFromCookieValue( $options, $expected ) {
110 /** @var BlockManager $blockManager */
111 $blockManager = TestingAccessWrapper::newFromObject(
112 $this->getBlockManager( [
113 MainConfigNames::CookieSetOnAutoblock => true,
114 MainConfigNames::CookieSetOnIpBlock => true,
115 ] )
116 );
117
118 $blockStore = $this->getServiceContainer()->getDatabaseBlockStore();
119 $block = new DatabaseBlock( array_merge( [
120 'address' => $options['target'] ?: $this->user,
121 'by' => $this->sysopUser,
122 ], $options['blockOptions'] ) );
123 $blockStore->insertBlock( $block );
124
125 $user = $options['registered'] ? $this->user : new User();
126 $user->getRequest()->setCookie( 'BlockID', $blockManager->getCookieValue( $block ) );
127
128 $this->assertSame( $expected, (bool)$blockManager->getBlockFromCookieValue(
129 $user,
130 $user->getRequest()
131 ) );
132 }
133
134 /**
135 * @dataProvider provideBlocksForShouldApplyCookieBlock
136 */
137 public function testTrackBlockWithCookieRemovesBlocks( $options, $expectKeepCookie ) {
138 /** @var BlockManager $blockManager */
139 $blockManager = TestingAccessWrapper::newFromObject(
140 $this->getBlockManager( [
141 MainConfigNames::CookieSetOnAutoblock => true,
142 MainConfigNames::CookieSetOnIpBlock => true,
143 ] )
144 );
145
146 $blockStore = $this->getServiceContainer()->getDatabaseBlockStore();
147 $block = new DatabaseBlock( array_merge( [
148 'address' => $options['target'] ?: $this->user,
149 'by' => $this->sysopUser,
150 ], $options['blockOptions'] ) );
151 $blockStore->insertBlock( $block );
152
153 $user = $options['registered'] ? $this->user : new User();
154 $user->getRequest()->setCookie( 'BlockID', $blockManager->getCookieValue( $block ) );
155
156 $response = new FauxResponse;
157
158 $blockManager->trackBlockWithCookie(
159 $user,
160 $response
161 );
162
163 $this->assertCount(
164 $expectKeepCookie ? 0 : 1,
165 $response->getCookies()
166 );
167 }
168
169 public static function provideBlocksForShouldApplyCookieBlock() {
170 return [
171 'Autoblocking user block' => [
172 [
173 'target' => '',
174 'registered' => true,
175 'blockOptions' => [
176 'enableAutoblock' => true
177 ],
178 ],
179 true,
180 ],
181 'Autoblocking user block for anonymous user' => [
182 [
183 'target' => '',
184 'registered' => false,
185 'blockOptions' => [
186 'enableAutoblock' => true
187 ],
188 ],
189 true,
190 ],
191 'Non-autoblocking user block' => [
192 [
193 'target' => '',
194 'registered' => true,
195 'blockOptions' => [],
196 ],
197 false,
198 ],
199 'IP block for anonymous user' => [
200 [
201 'target' => '127.0.0.1',
202 'registered' => false,
203 'blockOptions' => [],
204 ],
205 true,
206 ],
207 'IP block for logged in user' => [
208 [
209 'target' => '127.0.0.1',
210 'registered' => true,
211 'blockOptions' => [],
212 ],
213 false,
214 ],
215 'IP range block for anonymous user' => [
216 [
217 'target' => '127.0.0.0/8',
218 'registered' => false,
219 'blockOptions' => [],
220 ],
221 true,
222 ],
223 ];
224 }
225
226 /**
227 * @dataProvider provideIsLocallyBlockedProxy
228 */
229 public function testIsLocallyBlockedProxy( $proxyList, $expected ) {
230 /** @var BlockManager $blockManager */
231 $blockManager = TestingAccessWrapper::newFromObject(
232 $this->getBlockManager( [
233 MainConfigNames::ProxyList => $proxyList
234 ] )
235 );
236
237 $ip = '1.2.3.4';
238 $this->assertSame( $expected, $blockManager->isLocallyBlockedProxy( $ip ) );
239 }
240
241 public static function provideIsLocallyBlockedProxy() {
242 return [
243 'Proxy list is empty' => [ [], false ],
244 'Proxy list contains IP' => [ [ '1.2.3.4' ], true ],
245 'Proxy list contains IP as value' => [ [ 'test' => '1.2.3.4' ], true ],
246 'Proxy list contains range that covers IP' => [ [ '1.2.3.0/16' ], true ],
247 ];
248 }
249
250 /**
251 * @dataProvider provideIsDnsBlacklisted
252 */
253 public function testIsDnsBlacklisted( $options, $expected ) {
254 $blockManagerConfig = [
255 MainConfigNames::EnableDnsBlacklist => true,
256 MainConfigNames::DnsBlacklistUrls => $options['blacklist'],
257 MainConfigNames::ProxyWhitelist => $options['whitelist'],
258 ];
259
260 $blockManager = $this->getMockBuilder( BlockManager::class )
261 ->setConstructorArgs( $this->getBlockManagerConstructorArgs( $blockManagerConfig ) )
262 ->onlyMethods( [ 'checkHost' ] )
263 ->getMock();
264 $blockManager->method( 'checkHost' )
265 ->willReturnMap( [ [
266 $options['dnsblQuery'],
267 $options['dnsblResponse'],
268 ] ] );
269
270 $this->assertSame(
271 $expected,
272 $blockManager->isDnsBlacklisted( $options['ip'], $options['checkWhitelist'] )
273 );
274 }
275
276 public static function provideIsDnsBlacklisted() {
277 $dnsblFound = [ '127.0.0.2' ];
278 $dnsblNotFound = false;
279 return [
280 'IP is blacklisted' => [
281 [
282 'blacklist' => [ 'dnsbl.test' ],
283 'ip' => '127.0.0.1',
284 'dnsblQuery' => '1.0.0.127.dnsbl.test',
285 'dnsblResponse' => $dnsblFound,
286 'whitelist' => [],
287 'checkWhitelist' => false,
288 ],
289 true,
290 ],
291 'IP is blacklisted; blacklist has key' => [
292 [
293 'blacklist' => [ [ 'dnsbl.test', 'key' ] ],
294 'ip' => '127.0.0.1',
295 'dnsblQuery' => 'key.1.0.0.127.dnsbl.test',
296 'dnsblResponse' => $dnsblFound,
297 'whitelist' => [],
298 'checkWhitelist' => false,
299 ],
300 true,
301 ],
302 'IP is blacklisted; blacklist is array' => [
303 [
304 'blacklist' => [ [ 'dnsbl.test' ] ],
305 'ip' => '127.0.0.1',
306 'dnsblQuery' => '1.0.0.127.dnsbl.test',
307 'dnsblResponse' => $dnsblFound,
308 'whitelist' => [],
309 'checkWhitelist' => false,
310 ],
311 true,
312 ],
313 'IP is not blacklisted' => [
314 [
315 'blacklist' => [ 'dnsbl.test' ],
316 'ip' => '1.2.3.4',
317 'dnsblQuery' => '4.3.2.1.dnsbl.test',
318 'dnsblResponse' => $dnsblNotFound,
319 'whitelist' => [],
320 'checkWhitelist' => false,
321 ],
322 false,
323 ],
324 'Blacklist is empty' => [
325 [
326 'blacklist' => [],
327 'ip' => '127.0.0.1',
328 'dnsblQuery' => '1.0.0.127.dnsbl.test',
329 'dnsblResponse' => $dnsblFound,
330 'whitelist' => [],
331 'checkWhitelist' => false,
332 ],
333 false,
334 ],
335 'IP is blacklisted and whitelisted; whitelist is not checked' => [
336 [
337 'blacklist' => [ 'dnsbl.test' ],
338 'ip' => '127.0.0.1',
339 'dnsblQuery' => '1.0.0.127.dnsbl.test',
340 'dnsblResponse' => $dnsblFound,
341 'whitelist' => [ '127.0.0.1' ],
342 'checkWhitelist' => false,
343 ],
344 true,
345 ],
346 'IP is blacklisted and whitelisted; whitelist is checked' => [
347 [
348 'blacklist' => [ 'dnsbl.test' ],
349 'ip' => '127.0.0.1',
350 'dnsblQuery' => '1.0.0.127.dnsbl.test',
351 'dnsblResponse' => $dnsblFound,
352 'whitelist' => [ '127.0.0.1' ],
353 'checkWhitelist' => true,
354 ],
355 false,
356 ],
357 ];
358 }
359
360 public function testGetUniqueBlocks() {
361 $blockId = 100;
362
363 $block = $this->getMockBuilder( DatabaseBlock::class )
364 ->onlyMethods( [ 'getId' ] )
365 ->getMock();
366 $block->method( 'getId' )
367 ->willReturn( $blockId );
368
369 $autoblock = $this->getMockBuilder( DatabaseBlock::class )
370 ->onlyMethods( [ 'getParentBlockId', 'getType' ] )
371 ->getMock();
372 $autoblock->method( 'getParentBlockId' )
373 ->willReturn( $blockId );
374 $autoblock->method( 'getType' )
375 ->willReturn( Block::TYPE_AUTO );
376
377 $autoblockWithoutParentIdMethod = $this->getMockBuilder( AbstractBlock::class )
378 ->onlyMethods( [ 'getType' ] )
379 ->getMockForAbstractClass();
380 $autoblockWithoutParentIdMethod->method( 'getType' )
381 ->willReturn( Block::TYPE_AUTO );
382
383 $systemBlock = new SystemBlock();
384 $blocks = [ $block, $block, $autoblock, $systemBlock, $autoblockWithoutParentIdMethod ];
385
386 $blockManager = TestingAccessWrapper::newFromObject( $this->getBlockManager( [] ) );
387 $this->assertArrayEquals(
388 [ $block, $systemBlock, $autoblockWithoutParentIdMethod ],
389 $blockManager->getUniqueBlocks( $blocks )
390 );
391 }
392
393 /**
394 * @dataProvider provideTrackBlockWithCookie
395 */
396 public function testTrackBlockWithCookie( $options, $expected ) {
397 $this->overrideConfigValue( MainConfigNames::CookiePrefix, '' );
398
399 $request = new FauxRequest();
400 if ( $options['cookieSet'] ) {
401 $request->setCookie( 'BlockID', 'the value does not matter' );
402 }
403 /** @var FauxResponse $response */
404 $response = $request->response();
405
406 $user = $this->getMockBuilder( User::class )
407 ->onlyMethods( [ 'getBlock', 'getRequest' ] )
408 ->getMock();
409 $user->method( 'getBlock' )
410 ->willReturn( $options['block'] );
411 $user->method( 'getRequest' )
412 ->willReturn( $request );
413
414 // Although the block cookie is set via DeferredUpdates, in command line mode updates are
415 // processed immediately
416 $blockManager = $this->getBlockManager( [
417 MainConfigNames::SecretKey => '',
418 MainConfigNames::CookieSetOnIpBlock => true,
419 ] );
420 $blockManager->trackBlockWithCookie( $user, $response );
421
422 $this->assertCount( $expected['count'], $response->getCookies() );
423 $this->assertEquals( $expected['value'], $response->getCookie( 'BlockID' ) );
424 }
425
426 public function provideTrackBlockWithCookie() {
427 $blockId = 123;
428 return [
429 'Block cookie is already set; there is a trackable block' => [
430 [
431 'cookieSet' => true,
432 'block' => $this->getTrackableBlock( $blockId ),
433 ],
434 [
435 'count' => 1,
436 'value' => $blockId,
437 ]
438 ],
439 'Block cookie is already set; there is no block' => [
440 [
441 'cookieSet' => true,
442 'block' => null,
443 ],
444 [
445 // Cookie is cleared by setting it to empty value
446 'count' => 1,
447 'value' => '',
448 ]
449 ],
450 'Block cookie is not yet set; there is no block' => [
451 [
452 'cookieSet' => false,
453 'block' => null,
454 ],
455 [
456 'count' => 0,
457 'value' => null,
458 ]
459 ],
460 'Block cookie is not yet set; there is a trackable block' => [
461 [
462 'cookieSet' => false,
463 'block' => $this->getTrackableBlock( $blockId ),
464 ],
465 [
466 'count' => 1,
467 'value' => $blockId,
468 ]
469 ],
470 'Block cookie is not yet set; there is a composite block with a trackable block' => [
471 [
472 'cookieSet' => false,
473 'block' => new CompositeBlock( [
474 'originalBlocks' => [
475 new SystemBlock(),
476 $this->getTrackableBlock( $blockId ),
477 ]
478 ] ),
479 ],
480 [
481 'count' => 1,
482 'value' => $blockId,
483 ]
484 ],
485 'Block cookie is not yet set; there is a composite block but no trackable block' => [
486 [
487 'cookieSet' => false,
488 'block' => new CompositeBlock( [
489 'originalBlocks' => [
490 new SystemBlock(),
491 new SystemBlock(),
492 ]
493 ] ),
494 ],
495 [
496 'count' => 0,
497 'value' => null,
498 ]
499 ],
500 ];
501 }
502
503 private function getTrackableBlock( $blockId ) {
504 $block = $this->getMockBuilder( DatabaseBlock::class )
505 ->onlyMethods( [ 'getType', 'getId' ] )
506 ->getMock();
507 $block->method( 'getType' )
508 ->willReturn( DatabaseBlock::TYPE_IP );
509 $block->method( 'getId' )
510 ->willReturn( $blockId );
511 return $block;
512 }
513
514 /**
515 * @dataProvider provideSetBlockCookie
516 */
517 public function testSetBlockCookie( $expiryDelta, $expectedExpiryDelta ) {
518 $this->overrideConfigValue( MainConfigNames::CookiePrefix, '' );
519
520 $request = new FauxRequest();
521 $response = $request->response();
522
523 /** @var BlockManager $blockManager */
524 $blockManager = TestingAccessWrapper::newFromObject(
525 $this->getBlockManager( [
526 MainConfigNames::SecretKey => '',
527 MainConfigNames::CookieSetOnIpBlock => true,
528 ] )
529 );
530
531 $now = wfTimestamp();
532
533 $block = new DatabaseBlock( [
534 'expiry' => $expiryDelta === '' ? '' : $now + $expiryDelta
535 ] );
536 $blockManager->setBlockCookie( $block, $response );
537 $cookies = $response->getCookies();
538
539 $this->assertEqualsWithDelta(
540 $now + $expectedExpiryDelta,
541 $cookies['BlockID']['expire'],
542 60 // Allow actual to be up to 60 seconds later than expected
543 );
544 }
545
546 public static function provideSetBlockCookie() {
547 // Maximum length of a block cookie, defined in BlockManager::setBlockCookie
548 $maxExpiryDelta = ( 24 * 60 * 60 );
549
550 $longExpiryDelta = ( 48 * 60 * 60 );
551 $shortExpiryDelta = ( 12 * 60 * 60 );
552
553 return [
554 'Block has indefinite expiry' => [
555 '',
556 $maxExpiryDelta,
557 ],
558 'Block expiry is later than maximum cookie block expiry' => [
559 $longExpiryDelta,
560 $maxExpiryDelta,
561 ],
562 'Block expiry is sooner than maximum cookie block expiry' => [
563 $shortExpiryDelta,
564 $shortExpiryDelta,
565 ],
566 ];
567 }
568
569 /**
570 * @dataProvider provideShouldTrackBlockWithCookie
571 */
572 public function testShouldTrackBlockWithCookie( $options, $expected ) {
573 $block = $this->getMockBuilder( DatabaseBlock::class )
574 ->onlyMethods( [ 'getType', 'isAutoblocking' ] )
575 ->getMock();
576 $block->method( 'getType' )
577 ->willReturn( $options['type'] );
578 if ( isset( $options['autoblocking'] ) ) {
579 $block->method( 'isAutoblocking' )
580 ->willReturn( $options['autoblocking'] );
581 }
582
583 /** @var BlockManager $blockManager */
584 $blockManager = TestingAccessWrapper::newFromObject(
585 $this->getBlockManager( $options['blockManagerConfig'] )
586 );
587
588 $this->assertSame(
589 $expected,
590 $blockManager->shouldTrackBlockWithCookie( $block, $options['isAnon'] )
591 );
592 }
593
594 public static function provideShouldTrackBlockWithCookie() {
595 return [
596 'IP block, anonymous user, IP block cookies enabled' => [
597 [
598 'type' => DatabaseBlock::TYPE_IP,
599 'isAnon' => true,
600 'blockManagerConfig' => [ MainConfigNames::CookieSetOnIpBlock => true ],
601 ],
602 true
603 ],
604 'IP range block, anonymous user, IP block cookies enabled' => [
605 [
606 'type' => DatabaseBlock::TYPE_RANGE,
607 'isAnon' => true,
608 'blockManagerConfig' => [ MainConfigNames::CookieSetOnIpBlock => true ],
609 ],
610 true
611 ],
612 'IP block, anonymous user, IP block cookies disabled' => [
613 [
614 'type' => DatabaseBlock::TYPE_IP,
615 'isAnon' => true,
616 'blockManagerConfig' => [ MainConfigNames::CookieSetOnIpBlock => false ],
617 ],
618 false
619 ],
620 'IP block, logged in user, IP block cookies enabled' => [
621 [
622 'type' => DatabaseBlock::TYPE_IP,
623 'isAnon' => false,
624 'blockManagerConfig' => [ MainConfigNames::CookieSetOnIpBlock => true ],
625 ],
626 false
627 ],
628 'User block, anonymous, autoblock cookies enabled, block is autoblocking' => [
629 [
630 'type' => DatabaseBlock::TYPE_USER,
631 'isAnon' => true,
632 'blockManagerConfig' => [ MainConfigNames::CookieSetOnAutoblock => true ],
633 'autoblocking' => true,
634 ],
635 false
636 ],
637 'User block, logged in, autoblock cookies enabled, block is autoblocking' => [
638 [
639 'type' => DatabaseBlock::TYPE_USER,
640 'isAnon' => false,
641 'blockManagerConfig' => [ MainConfigNames::CookieSetOnAutoblock => true ],
642 'autoblocking' => true,
643 ],
644 true
645 ],
646 'User block, logged in, autoblock cookies disabled, block is autoblocking' => [
647 [
648 'type' => DatabaseBlock::TYPE_USER,
649 'isAnon' => false,
650 'blockManagerConfig' => [ MainConfigNames::CookieSetOnAutoblock => false ],
651 'autoblocking' => true,
652 ],
653 false
654 ],
655 'User block, logged in, autoblock cookies enabled, block is not autoblocking' => [
656 [
657 'type' => DatabaseBlock::TYPE_USER,
658 'isAnon' => false,
659 'blockManagerConfig' => [ MainConfigNames::CookieSetOnAutoblock => true ],
660 'autoblocking' => false,
661 ],
662 false
663 ],
664 'Block type is autoblock' => [
665 [
666 'type' => DatabaseBlock::TYPE_AUTO,
667 'isAnon' => true,
668 'blockManagerConfig' => [],
669 ],
670 false
671 ]
672 ];
673 }
674
675 public function testClearBlockCookie() {
676 $this->overrideConfigValue( MainConfigNames::CookiePrefix, '' );
677
678 $request = new FauxRequest();
679 $response = $request->response();
680 $response->setCookie( 'BlockID', '100' );
681 $this->assertSame( '100', $response->getCookie( 'BlockID' ) );
682
683 BlockManager::clearBlockCookie( $response );
684 $this->assertSame( '', $response->getCookie( 'BlockID' ) );
685 }
686
687 /**
688 * @dataProvider provideGetIdFromCookieValue
689 */
690 public function testGetIdFromCookieValue( $options, $expected ) {
691 /** @var BlockManager $blockManager */
692 $blockManager = TestingAccessWrapper::newFromObject(
693 $this->getBlockManager( [ MainConfigNames::SecretKey => $options['secretKey'] ] ) );
694 $this->assertEquals(
695 $expected,
696 $blockManager->getIdFromCookieValue( $options['cookieValue'] )
697 );
698 }
699
700 public static function provideGetIdFromCookieValue() {
701 $blockId = 100;
702 $secretKey = '123';
703 $hmac = MWCryptHash::hmac( $blockId, $secretKey, false );
704 return [
705 'No secret key is set' => [
706 [
707 'secretKey' => '',
708 'cookieValue' => $blockId,
709 'calculatedHmac' => MWCryptHash::hmac( $blockId, '', false ),
710 ],
711 $blockId,
712 ],
713 'Secret key is set and stored hmac is correct' => [
714 [
715 'secretKey' => $secretKey,
716 'cookieValue' => $blockId . '!' . $hmac,
717 'calculatedHmac' => $hmac,
718 ],
719 $blockId,
720 ],
721 'Secret key is set and stored hmac is incorrect' => [
722 [
723 'secretKey' => $secretKey,
724 'cookieValue' => $blockId . '!xyz',
725 'calculatedHmac' => $hmac,
726 ],
727 null,
728 ],
729 ];
730 }
731
732 /**
733 * @dataProvider provideGetCookieValue
734 */
735 public function testGetCookieValue( $options, $expected ) {
736 /** @var BlockManager $blockManager */
737 $blockManager = TestingAccessWrapper::newFromObject( $this->getBlockManager( [
738 MainConfigNames::SecretKey => $options['secretKey']
739 ] ) );
740
741 $block = $this->getMockBuilder( DatabaseBlock::class )
742 ->onlyMethods( [ 'getId' ] )
743 ->getMock();
744 $block->method( 'getId' )
745 ->willReturn( $options['blockId'] );
746
747 $this->assertEquals(
748 $expected,
749 $blockManager->getCookieValue( $block )
750 );
751 }
752
753 public static function provideGetCookieValue() {
754 $blockId = 100;
755 return [
756 'Secret key not set' => [
757 [
758 'secretKey' => '',
759 'blockId' => $blockId,
760 'hmac' => MWCryptHash::hmac( $blockId, '', false ),
761 ],
762 $blockId,
763 ],
764 'Secret key set' => [
765 [
766 'secretKey' => '123',
767 'blockId' => $blockId,
768 'hmac' => MWCryptHash::hmac( $blockId, '123', false ),
769 ],
770 $blockId . '!' . MWCryptHash::hmac( $blockId, '123', false ) ],
771 ];
772 }
773
774 /**
775 * @dataProvider provideGetXffBlocks
776 */
777 public function testGetXffBlocks(
778 $applyIpBlocksToXff,
779 $proxyWhiteList,
780 $isAnon,
781 $expected
782 ) {
783 $xff = '1.2.3.4, 5.6.7.8, 9.10.11.12';
784 $ip = '1.2.3.4';
785
786 $blockManagerConfig = [
787 MainConfigNames::ApplyIpBlocksToXff => $applyIpBlocksToXff,
788 MainConfigNames::ProxyWhitelist => $proxyWhiteList,
789 ];
790
791 $blockManagerMock = $this->getMockBuilder( BlockManager::class )
792 ->setConstructorArgs( $this->getBlockManagerConstructorArgs( $blockManagerConfig ) )
793 ->onlyMethods( [ 'getBlocksForIPList' ] )
794 ->getMock();
795 $blockManagerMock->method( 'getBlocksForIPList' )
796 ->willReturnCallback( function () use ( $isAnon ) {
797 if ( $isAnon ) {
798 return [ $this->createMock( DatabaseBlock::class ) ];
799 } else {
800 return [];
801 }
802 } );
803
804 /** @var BlockManager $blockManager */
805 $blockManager = TestingAccessWrapper::newFromObject( $blockManagerMock );
806
807 $this->assertSame(
808 $expected,
809 (bool)$blockManager->getXffBlocks( $ip, $xff, $isAnon, false )
810 );
811 }
812
813 public static function provideGetXffBlocks() {
814 return [
815 'ApplyIpBlocksToXff config is false' => [
816 'applyIpBlocksToXff' => false,
817 'proxyWhiteList' => [],
818 'isAnon' => true,
819 false,
820 ],
821 'IP is in ProxyWhiteList' => [
822 'applyIpBlocksToXff' => true,
823 'proxyWhiteList' => [ '1.2.3.4' ],
824 'isAnon' => true,
825 false,
826 ],
827 'User is logged in' => [
828 'applyIpBlocksToXff' => true,
829 'proxyWhiteList' => [],
830 'isAnon' => false,
831 false,
832 ],
833 'IP is in XFF list but not in ProxyWhiteList' => [
834 'applyIpBlocksToXff' => true,
835 'proxyWhiteList' => [],
836 'isAnon' => true,
837 true,
838 ],
839 ];
840 }
841
842 /**
843 * @dataProvider provideGetSystemIpBlocks
844 */
845 public function testGetSystemIpBlocks(
846 $proxyWhitelist,
847 $softBlockRanges,
848 $isLocallyBlockedProxy,
849 $isDnsBlacklisted,
850 $isAnon,
851 $expected
852 ) {
853 $ip = '1.2.3.4';
854
855 $blockManagerConfig = [
856 MainConfigNames::ProxyWhitelist => $proxyWhitelist,
857 MainConfigNames::SoftBlockRanges => $softBlockRanges,
858 MainConfigNames::ProxyList => ( $isLocallyBlockedProxy ? [ $ip ] : [] ),
859 ];
860
861 $blockManagerMock = $this->getMockBuilder( BlockManager::class )
862 ->setConstructorArgs( $this->getBlockManagerConstructorArgs( $blockManagerConfig ) )
863 ->onlyMethods( [ 'isDnsBlacklisted' ] )
864 ->getMock();
865 $blockManagerMock->method( 'isDnsBlacklisted' )
866 ->willReturn( $isDnsBlacklisted );
867
868 /** @var BlockManager $blockManager */
869 $blockManager = TestingAccessWrapper::newFromObject( $blockManagerMock );
870
871 $this->assertSame(
872 $expected,
873 (bool)$blockManager->getSystemIpBlocks( $ip, $isAnon )
874 );
875 }
876
877 public static function provideGetSystemIpBlocks() {
878 return [
879 'IP is in ProxyWhiteList' => [
880 'proxyWhitelist' => [ '1.2.3.4' ],
881 'softBlockRanges' => [],
882 'isLocallyBlockedProxy' => true,
883 'isDnsBlacklisted' => true,
884 'isAnon' => true,
885 false,
886 ],
887 'IP is locally blocked proxy only' => [
888 'proxyWhitelist' => [],
889 'softBlockRanges' => [],
890 'isLocallyBlockedProxy' => true,
891 'isDnsBlacklisted' => false,
892 'isAnon' => false,
893 true,
894 ],
895 'IP is DNS blacklisted only, anon' => [
896 'proxyWhitelist' => [],
897 'softBlockRanges' => [],
898 'isLocallyBlockedProxy' => false,
899 'isDnsBlacklisted' => true,
900 'isAnon' => true,
901 true,
902 ],
903 'IP is DNS blacklisted only, logged in' => [
904 'proxyWhitelist' => [],
905 'softBlockRanges' => [],
906 'isLocallyBlockedProxy' => false,
907 'isDnsBlacklisted' => true,
908 'isAnon' => false,
909 false,
910 ],
911 'IP is in SoftBlockRanges and ProxyWhiteList, anon' => [
912 'proxyWhitelist' => [ '1.2.3.4' ],
913 'softBlockRanges' => [ '1.2.3.4' ],
914 'isLocallyBlockedProxy' => false,
915 'isDnsBlacklisted' => false,
916 'isAnon' => true,
917 true,
918 ],
919 'IP is in SoftBlockRanges and ProxyWhiteList, logged in' => [
920 'proxyWhitelist' => [ '1.2.3.4' ],
921 'softBlockRanges' => [ '1.2.3.4' ],
922 'isLocallyBlockedProxy' => false,
923 'isDnsBlacklisted' => false,
924 'isAnon' => false,
925 false,
926 ],
927 ];
928 }
929
930 public function testGetBlocksForIPList() {
931 $blockManager = $this->getBlockManager( [] );
932 $block = new DatabaseBlock( [
933 'address' => '1.2.3.4',
934 'by' => $this->getTestSysop()->getUser(),
935 ] );
936 $inserted = $this->getServiceContainer()
937 ->getDatabaseBlockStore()
938 ->insertBlock( $block );
939 $this->assertTrue(
940 (bool)$inserted['id'],
941 'Check that the block was inserted correctly'
942 );
943
944 // Early return of empty array if no ips in the list
945 $list = $blockManager->getBlocksForIPList( [], true, false );
946 $this->assertCount(
947 0,
948 $list,
949 'No blocks retrieved if no ips listed'
950 );
951
952 // Early return of empty array if all ips are either invalid or trusted proxies,
953 // '192.168.1.1' is set to trusted in setUp();
954 $list = $blockManager->getBlocksForIPList(
955 [ '300.300.300.300', '192.168.1.1' ],
956 true,
957 false
958 );
959 $this->assertCount(
960 0,
961 $list,
962 'No blocks retrieved if all ips are invalid or trusted proxies'
963 );
964
965 // Actually fetching, block was inserted above
966 $list = $blockManager->getBlocksForIPList( [ '1.2.3.4' ], true, false );
967 $this->assertCount(
968 1,
969 $list,
970 'Block retrieved for the blocked ip'
971 );
972 $this->assertInstanceOf(
973 DatabaseBlock::class,
974 $list[0],
975 'DatabaseBlock returned'
976 );
977 $this->assertSame(
978 $inserted['id'],
979 $list[0]->getId(),
980 'Block returned is the correct one'
981 );
982 }
983
984 /**
985 * @coversNothing
986 */
987 public function testAllServiceOptionsUsed() {
988 $this->assertAllServiceOptionsUsed();
989 }
990 }