-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathCrawlerProtectionIntegrationTest.php
More file actions
772 lines (679 loc) · 27.6 KB
/
Copy pathCrawlerProtectionIntegrationTest.php
File metadata and controls
772 lines (679 loc) · 27.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
<?php
/**
* Copyright (c) 2025-2026 MyWikis
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file CrawlerProtectionIntegrationTest.php
*/
namespace MediaWiki\Extension\CrawlerProtection\Tests\Integration;
use MediaWiki\Config\ServiceOptions;
use MediaWiki\Extension\CrawlerProtection\CrawlerProtectionService;
use MediaWiki\Extension\CrawlerProtection\HookRunner;
use MediaWiki\Extension\CrawlerProtection\ResponseFactory;
use MediaWiki\Logger\LoggerFactory;
use MediaWikiIntegrationTestCase;
/**
* Integration tests for the CrawlerProtection extension.
*
* These tests run under the real MediaWiki service container and exercise the
* parts of the extension that unit tests with hand-written stubs cannot reach:
*
* - Service-container wiring: confirms that the service names declared in
* extension.json resolve to the expected PHP objects.
* - Hook registration: confirms that the hooks listed in extension.json are
* actually registered with MediaWiki's HookContainer.
* - Real OutputPage state: confirms that the denial path sets HTTP 403 on a
* genuine OutputPage instance, exercising both the setPageTitle() (MW < 1.41)
* and setPageTitleMsg() (MW >= 1.41) branches through method_exists().
* - End-to-end service behaviour: confirms that checkPerformAction() and
* checkSpecialPage() block anonymous users as configured.
*
* @group CrawlerProtection
* @group Database
* @coversDefaultClass \MediaWiki\Extension\CrawlerProtection\CrawlerProtectionService
*/
class CrawlerProtectionIntegrationTest extends MediaWikiIntegrationTestCase {
// ---------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------
/**
* Override every CrawlerProtection config key so that tests are not
* sensitive to wiki-local defaults.
*
* @param array $overrides Values that differ from the all-defaults baseline.
*/
private function overrideCrawlerProtectionConfig( array $overrides = [] ): void {
$defaults = [
'CrawlerProtectedActions' => [ 'history' ],
'CrawlerProtectedApiModules' => [],
'CrawlerProtectedRestPaths' => [],
'CrawlerProtectedSpecialPages' => [ 'whatlinkshere', 'recentchangeslinked' ],
'CrawlerProtectedQueryParams' => [ 'target' ],
'CrawlerProtectionAllowedIPs' => [],
'CrawlerProtectionProtectRevisions' => true,
'CrawlerProtectionTreatTempUsersAsAnon' => false,
'CrawlerProtectionTrustXForwardedFor' => false,
'CrawlerProtectionRawDenial' => false,
'CrawlerProtectionUse418' => false,
'CrawlerProtectionRawDenialHeader' => 'HTTP/1.0 403 Forbidden',
'CrawlerProtectionRawDenialText' => '',
];
$this->overrideConfigValues( array_merge( $defaults, $overrides ) );
}
/**
* Return a fresh OutputPage attached to a minimal RequestContext.
*
* A Title is required so that OutputPage::addWikiTextAsInterface() can
* invoke the parser without an "Invalid title" error. TitleFactory is
* used instead of the Title class so that the test works both on releases
* that still provide the unnamespaced Title alias and on those that do not.
*
* @return \MediaWiki\Output\OutputPage|\OutputPage
*/
private function makeOutputPage() {
$context = new \RequestContext();
$context->setTitle(
$this->getServiceContainer()->getTitleFactory()->makeTitle( NS_MAIN, 'Test' )
);
return $context->getOutput();
}
/**
* Build a FauxRequest carrying the given query parameters.
*
* FauxRequest lives in the MediaWiki\Request namespace on newer releases
* and in the global namespace on older ones, so the class is resolved at
* run time rather than referenced directly.
*
* @param array $params Query parameters
* @return \MediaWiki\Request\FauxRequest|\FauxRequest
*/
private function makeRequest( array $params ) {
$class = class_exists( \MediaWiki\Request\FauxRequest::class )
? \MediaWiki\Request\FauxRequest::class
: 'FauxRequest';
return new $class( $params );
}
/**
* Return a real anonymous user object for the given IP address.
*
* A real user is used rather than a mock because the User class has moved
* between namespaces across supported releases, and mocking it by name
* fails on releases where the chosen name is only an alias.
*
* @param string $ip
* @return \MediaWiki\User\User|\User
*/
private function makeAnonUser( string $ip = '1.2.3.4' ) {
return $this->getServiceContainer()->getUserFactory()->newAnonymous( $ip );
}
/**
* Build a CrawlerProtectionService wired against the real container but
* with cliMode forced to false.
*
* PHPUnit runs PHP as a CLI process, so ServiceWiring.php detects
* MW_ENTRY_POINT === 'cli' and constructs the container service with
* cliMode = true, which bypasses all protection. Tests that exercise
* the blocking path must use this helper instead of pulling the service
* directly from the container.
*
* @return CrawlerProtectionService
*/
private function makeWebModeService(): CrawlerProtectionService {
return new CrawlerProtectionService(
new ServiceOptions(
CrawlerProtectionService::CONSTRUCTOR_OPTIONS,
$this->getServiceContainer()->getMainConfig()
),
$this->getServiceContainer()->get( 'CrawlerProtection.ResponseFactory' ),
new HookRunner( $this->getServiceContainer()->getHookContainer() ),
// false = web-request mode — not CLI
false,
LoggerFactory::getInstance( 'CrawlerProtection' ),
$this->getServiceContainer()->getSpecialPageFactory(),
$this->getServiceContainer()->getContentLanguage()
);
}
// ---------------------------------------------------------------
// Service-container wiring
// ---------------------------------------------------------------
/**
* Verify that CrawlerProtection.CrawlerProtectionService resolves from
* the real service container and is of the correct type.
*
* This catches typos in extension.json service names and constructor
* mis-wiring in ServiceWiring.php.
*
* @covers \MediaWiki\Extension\CrawlerProtection\CrawlerProtectionService::__construct
*/
public function testCrawlerProtectionServiceResolvesFromContainer(): void {
$service = $this->getServiceContainer()->get( 'CrawlerProtection.CrawlerProtectionService' );
$this->assertInstanceOf( CrawlerProtectionService::class, $service );
}
/**
* Verify that CrawlerProtection.ResponseFactory resolves from the real
* service container and is of the correct type.
*
* @covers \MediaWiki\Extension\CrawlerProtection\ResponseFactory::__construct
*/
public function testResponseFactoryResolvesFromContainer(): void {
$factory = $this->getServiceContainer()->get( 'CrawlerProtection.ResponseFactory' );
$this->assertInstanceOf( ResponseFactory::class, $factory );
}
// ---------------------------------------------------------------
// Hook registration
// ---------------------------------------------------------------
/**
* Verify that the MediaWikiPerformAction hook handler is registered.
*
* This catches mistakes in the "Hooks" section of extension.json.
*
* @covers \MediaWiki\Extension\CrawlerProtection\Hooks::__construct
*/
public function testMediaWikiPerformActionHookIsRegistered(): void {
$this->assertTrue(
$this->getServiceContainer()->getHookContainer()->isRegistered( 'MediaWikiPerformAction' ),
'MediaWikiPerformAction hook must be registered by the extension'
);
}
/**
* Verify that the SpecialPageBeforeExecute hook handler is registered.
*
* This catches mistakes in the "Hooks" section of extension.json.
*
* @covers \MediaWiki\Extension\CrawlerProtection\Hooks::__construct
*/
public function testSpecialPageBeforeExecuteHookIsRegistered(): void {
$this->assertTrue(
$this->getServiceContainer()->getHookContainer()->isRegistered( 'SpecialPageBeforeExecute' ),
'SpecialPageBeforeExecute hook must be registered by the extension'
);
}
/**
* Verify that the ApiCheckCanExecute hook handler is registered.
*
* @covers \MediaWiki\Extension\CrawlerProtection\Hooks::onApiCheckCanExecute
*/
public function testApiCheckCanExecuteHookIsRegistered(): void {
$this->assertTrue(
$this->getServiceContainer()->getHookContainer()->isRegistered( 'ApiCheckCanExecute' ),
'ApiCheckCanExecute hook must be registered by the extension'
);
}
/**
* Verify that the RestCheckCanExecute hook handler is registered.
*
* The hook itself only fires on MediaWiki 1.44 and later, but the handler
* is registered on every supported version.
*
* @covers \MediaWiki\Extension\CrawlerProtection\Hooks::onRestCheckCanExecute
*/
public function testRestCheckCanExecuteHookIsRegistered(): void {
$this->assertTrue(
$this->getServiceContainer()->getHookContainer()->isRegistered( 'RestCheckCanExecute' ),
'RestCheckCanExecute hook must be registered by the extension'
);
}
/**
* Hooks deliberately does not implement core's ApiCheckCanExecuteHook and
* RestCheckCanExecuteHook interfaces, so PHP cannot catch signature drift
* against core. Compare the handler signatures with the core interfaces
* wherever those interfaces exist.
*
* @dataProvider provideCoreHookInterfaces
* @covers \MediaWiki\Extension\CrawlerProtection\Hooks::onApiCheckCanExecute
* @covers \MediaWiki\Extension\CrawlerProtection\Hooks::onRestCheckCanExecute
* @param string $interface Core hook interface name
* @param string $method Handler method name
*/
public function testHandlerSignatureMatchesCoreInterface( string $interface, string $method ): void {
if ( !interface_exists( $interface ) ) {
$this->markTestSkipped( "$interface is not available on this MediaWiki version" );
}
$expected = new \ReflectionMethod( $interface, $method );
$actual = new \ReflectionMethod( \MediaWiki\Extension\CrawlerProtection\Hooks::class, $method );
$this->assertSame(
$expected->getNumberOfParameters(),
$actual->getNumberOfParameters(),
"$method must take the same number of parameters as $interface"
);
foreach ( $expected->getParameters() as $index => $parameter ) {
$this->assertSame(
$parameter->isPassedByReference(),
$actual->getParameters()[$index]->isPassedByReference(),
"Parameter #$index of $method must match the by-reference mode of $interface"
);
// The handlers are deliberately untyped, so a reordering of
// same-arity parameters in core would otherwise go unnoticed:
// the parameter names are what pins the order.
$this->assertSame(
$parameter->getName(),
$actual->getParameters()[$index]->getName(),
"Parameter #$index of $method must have the same name as in $interface"
);
}
}
/**
* @return array[]
*/
public static function provideCoreHookInterfaces(): array {
return [
'ApiCheckCanExecute' => [
'MediaWiki\\Api\\Hook\\ApiCheckCanExecuteHook',
'onApiCheckCanExecute',
],
'RestCheckCanExecute' => [
'MediaWiki\\Rest\\Hook\\RestCheckCanExecuteHook',
'onRestCheckCanExecute',
],
];
}
// ---------------------------------------------------------------
// ResponseFactory::denyAccessPretty() with real OutputPage
// ---------------------------------------------------------------
/**
* Confirm that the "pretty" denial path sets HTTP 403 on a real OutputPage.
*
* This exercises the method_exists()-based branch in denyAccessPretty():
* on MW < 1.41 the method uses setPageTitle(); on MW >= 1.41 it uses
* setPageTitleMsg(). Both paths must end with a 403 status code, and the
* CI matrix (REL1_39 and REL1_43+) naturally covers both branches.
*
* OutputPage::getStatusCode() was added in MW 1.45. On earlier versions
* the test verifies the call does not throw; the status-code assertion is
* skipped when the getter is absent.
*
* @covers \MediaWiki\Extension\CrawlerProtection\ResponseFactory::denyAccessPretty
* @covers \MediaWiki\Extension\CrawlerProtection\ResponseFactory::denyAccess
*/
public function testDenyAccessPrettySetsStatusCode403OnRealOutputPage(): void {
$this->overrideCrawlerProtectionConfig( [
'CrawlerProtectionRawDenial' => false,
'CrawlerProtectionUse418' => false,
] );
/** @var ResponseFactory $factory */
$factory = $this->getServiceContainer()->get( 'CrawlerProtection.ResponseFactory' );
$output = $this->makeOutputPage();
$factory->denyAccess( $output );
if ( method_exists( $output, 'getStatusCode' ) ) {
$this->assertSame( 403, $output->getStatusCode() );
} else {
// On MW < 1.45, getStatusCode() does not exist; verify only that
// denyAccess() completed without throwing.
$this->addToAssertionCount( 1 );
}
}
// ---------------------------------------------------------------
// CrawlerProtectionService::checkPerformAction()
// ---------------------------------------------------------------
/**
* An anonymous user requesting a protected action must be blocked (return
* false) and the OutputPage must receive HTTP 403.
*
* @covers \MediaWiki\Extension\CrawlerProtection\CrawlerProtectionService::checkPerformAction
*/
public function testAnonymousUserWithProtectedActionIsBlocked(): void {
$this->overrideCrawlerProtectionConfig( [
'CrawlerProtectedActions' => [ 'history' ],
'CrawlerProtectionProtectRevisions' => false,
] );
$service = $this->makeWebModeService();
$user = $this->makeAnonUser();
$request = $this->makeRequest( [ 'action' => 'history' ] );
$output = $this->makeOutputPage();
$result = $service->checkPerformAction( $output, $user, $request );
$this->assertFalse( $result, 'checkPerformAction must return false to abort the request' );
if ( method_exists( $output, 'getStatusCode' ) ) {
$this->assertSame( 403, $output->getStatusCode() );
}
$this->assertSame(
'Special:WhatLinksHere',
$output->getRequest()->response()->getHeader( 'X-CrawlerProtection-Special-Page-Aliases' )
);
}
/**
* A registered (logged-in) user requesting a protected action must not
* be blocked — the service must return true.
*
* @covers \MediaWiki\Extension\CrawlerProtection\CrawlerProtectionService::checkPerformAction
*/
public function testRegisteredUserWithProtectedActionIsNotBlocked(): void {
$this->overrideCrawlerProtectionConfig( [
'CrawlerProtectedActions' => [ 'history' ],
] );
$service = $this->makeWebModeService();
// getMutableTestUser() returns a real registered user object.
$user = $this->getMutableTestUser()->getUser();
$request = $this->makeRequest( [ 'action' => 'history' ] );
$output = $this->makeOutputPage();
$result = $service->checkPerformAction( $output, $user, $request );
$this->assertTrue( $result, 'checkPerformAction must return true for registered users' );
}
// ---------------------------------------------------------------
// X-Forwarded-For allowlist
// ---------------------------------------------------------------
/**
* Build a request that reaches the wiki through a reverse proxy: the
* connecting address is the proxy, and the client address is reported in
* the X-Forwarded-For header.
*
* A real WebRequest is used so that the header lookup exercises
* MediaWiki's own getHeader() implementation.
*
* @param array $params Query parameters
* @param string $forwardedFor X-Forwarded-For header value
* @param string $proxyIP Connecting (proxy) address
* @return \MediaWiki\Request\FauxRequest|\FauxRequest
*/
private function makeProxiedRequest(
array $params,
string $forwardedFor,
string $proxyIP = '10.0.0.1'
) {
$request = $this->makeRequest( $params );
$request->setIP( $proxyIP );
$request->setHeaders( [ 'X-Forwarded-For' => $forwardedFor ] );
return $request;
}
/**
* Behind a reverse proxy that is not registered in $wgCdnServersNoPurge,
* WebRequest::getIP() reports the proxy address, so an allowlisted client
* is still blocked while CrawlerProtectionTrustXForwardedFor is off.
*
* @covers \MediaWiki\Extension\CrawlerProtection\CrawlerProtectionService::checkPerformAction
*/
public function testForwardedIPIsIgnoredWhenTrustDisabled(): void {
$this->overrideCrawlerProtectionConfig( [
'CrawlerProtectedActions' => [ 'history' ],
'CrawlerProtectionAllowedIPs' => [ '1.2.3.4' ],
] );
$service = $this->makeWebModeService();
$request = $this->makeProxiedRequest( [ 'action' => 'history' ], '1.2.3.4' );
$this->assertFalse(
$service->checkPerformAction( $this->makeOutputPage(), $this->makeAnonUser(), $request )
);
}
/**
* With CrawlerProtectionTrustXForwardedFor enabled, the address reported
* by the proxy is matched against the allowlist.
*
* @covers \MediaWiki\Extension\CrawlerProtection\CrawlerProtectionService::checkPerformAction
*/
public function testForwardedIPIsAllowedWhenTrustEnabled(): void {
$this->overrideCrawlerProtectionConfig( [
'CrawlerProtectedActions' => [ 'history' ],
'CrawlerProtectionAllowedIPs' => [ '1.2.3.4' ],
'CrawlerProtectionTrustXForwardedFor' => true,
] );
$service = $this->makeWebModeService();
$request = $this->makeProxiedRequest( [ 'action' => 'history' ], '1.2.3.4' );
$this->assertTrue(
$service->checkPerformAction( $this->makeOutputPage(), $this->makeAnonUser(), $request )
);
}
/**
* Entries a client prepends to the header must never be trusted: only the
* address appended by the proxy (the last entry) is matched.
*
* @covers \MediaWiki\Extension\CrawlerProtection\CrawlerProtectionService::checkPerformAction
*/
public function testSpoofedForwardedChainIsRejected(): void {
$this->overrideCrawlerProtectionConfig( [
'CrawlerProtectedActions' => [ 'history' ],
'CrawlerProtectionAllowedIPs' => [ '1.2.3.4' ],
'CrawlerProtectionTrustXForwardedFor' => true,
] );
$service = $this->makeWebModeService();
$request = $this->makeProxiedRequest( [ 'action' => 'history' ], '1.2.3.4, 203.0.113.9' );
$this->assertFalse(
$service->checkPerformAction( $this->makeOutputPage(), $this->makeAnonUser(), $request )
);
}
// ---------------------------------------------------------------
// CrawlerProtectionService::checkSpecialPage()
// ---------------------------------------------------------------
/**
* An anonymous user visiting a protected special page must be blocked.
*
* @covers \MediaWiki\Extension\CrawlerProtection\CrawlerProtectionService::checkSpecialPage
*/
public function testAnonymousUserOnProtectedSpecialPageIsBlocked(): void {
$this->overrideCrawlerProtectionConfig( [
'CrawlerProtectedSpecialPages' => [ 'WhatLinksHere' ],
] );
$service = $this->makeWebModeService();
$user = $this->makeAnonUser();
$output = $this->makeOutputPage();
$request = $this->makeRequest( [] );
$result = $service->checkSpecialPage( 'WhatLinksHere', $output, $user, $request );
$this->assertFalse( $result, 'checkSpecialPage must return false to abort the request' );
if ( method_exists( $output, 'getStatusCode' ) ) {
$this->assertSame( 403, $output->getStatusCode() );
}
}
/**
* A registered user visiting a protected special page must not be blocked.
*
* @covers \MediaWiki\Extension\CrawlerProtection\CrawlerProtectionService::checkSpecialPage
*/
public function testRegisteredUserOnProtectedSpecialPageIsNotBlocked(): void {
$this->overrideCrawlerProtectionConfig( [
'CrawlerProtectedSpecialPages' => [ 'WhatLinksHere' ],
] );
$service = $this->makeWebModeService();
$user = $this->getMutableTestUser()->getUser();
$output = $this->makeOutputPage();
$request = $this->makeRequest( [] );
$result = $service->checkSpecialPage( 'WhatLinksHere', $output, $user, $request );
$this->assertTrue( $result, 'checkSpecialPage must return true for registered users' );
}
// ---------------------------------------------------------------
// Action API and REST entry points
// ---------------------------------------------------------------
/**
* An anonymous Action API request naming a protected module must be
* denied.
*
* @covers \MediaWiki\Extension\CrawlerProtection\CrawlerProtectionService::checkApiModules
*/
public function testAnonymousApiRequestForProtectedModuleIsBlocked(): void {
$this->overrideCrawlerProtectionConfig( [
'CrawlerProtectedApiModules' => [ 'revisions' ],
] );
$service = $this->makeWebModeService();
$this->assertFalse(
$service->checkApiModules(
[ 'query', 'revisions' ],
$this->makeAnonUser(),
$this->makeRequest( [] )
)
);
}
/**
* A registered user must not be blocked on the Action API.
*
* @covers \MediaWiki\Extension\CrawlerProtection\CrawlerProtectionService::checkApiModules
*/
public function testRegisteredApiRequestForProtectedModuleIsNotBlocked(): void {
$this->overrideCrawlerProtectionConfig( [
'CrawlerProtectedApiModules' => [ 'revisions' ],
] );
$service = $this->makeWebModeService();
$this->assertTrue(
$service->checkApiModules(
[ 'query', 'revisions' ],
$this->getMutableTestUser()->getUser(),
$this->makeRequest( [] )
)
);
}
/**
* An anonymous REST request matching a protected path pattern must be
* denied, and a non-matching path must not be.
*
* @covers \MediaWiki\Extension\CrawlerProtection\CrawlerProtectionService::checkRestPath
*/
public function testAnonymousRestRequestForProtectedPathIsBlocked(): void {
$this->overrideCrawlerProtectionConfig( [
'CrawlerProtectedRestPaths' => [ '/page/*/history' ],
] );
$service = $this->makeWebModeService();
$user = $this->makeAnonUser();
$request = $this->makeRequest( [] );
$this->assertFalse( $service->checkRestPath( '/page/Main_Page/history', $user, $request ) );
$this->assertTrue( $service->checkRestPath( '/page/Main_Page/bare', $user, $request ) );
}
// ---------------------------------------------------------------
// Hooks::onApiCheckCanExecute through a real ApiMain
// ---------------------------------------------------------------
/**
* Build a real ApiMain around a FauxRequest carrying the given parameters.
*
* A FauxRequest puts ApiMain into internal mode, where execute() calls
* executeAction() directly: checkExecutePermissions() - and therefore the
* ApiCheckCanExecute hook - still runs, but an ApiUsageException
* propagates to the caller instead of being formatted into a response
* body. ApiMain moved into the MediaWiki\Api namespace after MW 1.39,
* which keeps a class alias under the old global name on every supported
* release, so the unqualified name is used here.
*
* @param array $params Request parameters
* @param \MediaWiki\User\User|\User $user User making the request
* @return \ApiMain
*/
private function makeApiMain( array $params, $user ) {
$context = new \RequestContext();
$context->setRequest( $this->makeRequest( $params ) );
$context->setUser( $user );
$context->setTitle(
$this->getServiceContainer()->getTitleFactory()->makeTitle( NS_MAIN, 'Test' )
);
return new \ApiMain( $context );
}
/**
* Install a web-mode CrawlerProtectionService in the container so that the
* hook handler built by ObjectFactory uses it instead of the CLI-mode
* service that ServiceWiring.php produces under PHPUnit.
*/
private function useWebModeServiceInContainer(): void {
$this->setService(
'CrawlerProtection.CrawlerProtectionService',
$this->makeWebModeService()
);
}
/**
* The hook handler must deny an anonymous action=query request naming a
* protected sub-module, and must itself set HTTP 403 on the response
* rather than leaving core's default "200 OK with an error body".
*
* This exercises the adapter itself - sub-module parsing and the $message
* contract with core - which the service-level tests above cannot reach.
* The status and header assertions pin what the extension sets; whether
* core preserves them while printing the error response is out of scope
* here, since internal mode does not reach the printer.
*
* @covers \MediaWiki\Extension\CrawlerProtection\Hooks::onApiCheckCanExecute
*/
public function testApiCheckCanExecuteDeniesAnonymousProtectedSubModule(): void {
$this->overrideCrawlerProtectionConfig( [
'CrawlerProtectedApiModules' => [ 'revisions' ],
] );
$this->useWebModeServiceInContainer();
$api = $this->makeApiMain(
[ 'action' => 'query', 'prop' => 'revisions', 'titles' => 'Main Page' ],
$this->makeAnonUser()
);
$denied = false;
try {
$api->execute();
} catch ( \Exception $e ) {
// ApiUsageException moved into the MediaWiki\Api namespace after
// MW 1.39, so it is recognised by the method it exposes rather
// than by class name.
$this->assertTrue(
method_exists( $e, 'getStatusValue' ),
'Expected an ApiUsageException, got ' . get_class( $e ) . ': ' . $e->getMessage()
);
$denied = true;
$this->assertTrue(
// @phan-suppress-next-line PhanUndeclaredMethod ApiUsageException only
$e->getStatusValue()->hasMessage( 'crawlerprotection-accessdenied-text' ),
'The denial must use the extension error message'
);
}
$this->assertTrue( $denied, 'An anonymous request for a protected sub-module must be denied' );
$response = $api->getRequest()->response();
$this->assertSame(
403,
// @phan-suppress-next-line PhanUndeclaredMethod FauxResponse only
$response->getStatusCode(),
'The extension must set HTTP 403 on a denied API request'
);
$this->assertSame(
'noindex,nofollow',
// @phan-suppress-next-line PhanUndeclaredMethod FauxResponse only
$response->getHeader( 'X-Robots-Tag' ),
'The extension must tell crawlers not to re-request a denied URL'
);
}
/**
* The hook handler must let an anonymous request through when no
* configured module is involved.
*
* @covers \MediaWiki\Extension\CrawlerProtection\Hooks::onApiCheckCanExecute
*/
public function testApiCheckCanExecuteAllowsUnprotectedModule(): void {
$this->overrideCrawlerProtectionConfig( [
'CrawlerProtectedApiModules' => [ 'revisions' ],
] );
$this->useWebModeServiceInContainer();
$api = $this->makeApiMain(
[ 'action' => 'query', 'meta' => 'siteinfo' ],
$this->makeAnonUser()
);
$api->execute();
$this->assertArrayHasKey(
'query',
$api->getResult()->getResultData( [], [ 'Strip' => 'all' ] ),
'An unprotected module must execute normally'
);
}
/**
* A registered user must not be denied by the hook handler even when the
* module is protected.
*
* @covers \MediaWiki\Extension\CrawlerProtection\Hooks::onApiCheckCanExecute
*/
public function testApiCheckCanExecuteAllowsRegisteredUserOnProtectedModule(): void {
$this->overrideCrawlerProtectionConfig( [
'CrawlerProtectedApiModules' => [ 'siteinfo' ],
] );
$this->useWebModeServiceInContainer();
$api = $this->makeApiMain(
[ 'action' => 'query', 'meta' => 'siteinfo' ],
$this->getMutableTestUser()->getUser()
);
$api->execute();
$this->assertArrayHasKey(
'query',
$api->getResult()->getResultData( [], [ 'Strip' => 'all' ] ),
'A registered user must not be denied'
);
}
}