Skip to content

Commit 1bf3fd5

Browse files
authored
Version 1.2.0
1.2.0 (Aug 13, 2025) * Fix 403 handling and add login links to prevent nginx 404 fallthrough
1 parent 64998bb commit 1bf3fd5

File tree

6 files changed

+36
-5
lines changed

6 files changed

+36
-5
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,7 @@ intensive.
3030

3131
* add: ProtectionUse418
3232
* 1.43 compatibility
33+
34+
1.2.0 (Aug 13, 2025)
35+
36+
* Fix 403 handling and add login links to prevent nginx 404 fallthrough

extension.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "CrawlerProtection",
33
"author": "MyWikis LLC",
4-
"version": "1.1.0",
4+
"version": "1.2.0",
55
"description": "Suite of protective measures to protect wikis from crawlers.",
66
"type": "hook",
77
"requires": {

i18n/de.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
"authors": [ "MyWikis LLC" ]
44
},
55
"crawlerprotection-accessdenied-title": "Zugriff verweigert",
6-
"crawlerprotection-accessdenied-text": "Anmeldung erforderlich, um diese Aktion auzuführen oder Spezialseite anzusehen."
6+
"crawlerprotection-accessdenied-text": "Anmeldung erforderlich, um diese Aktion auzuführen oder Spezialseite anzusehen. Bitte [[Special:UserLogin|melden Sie sich an]], um fortzufahren."
77
}

i18n/en.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
"authors": [ "MyWikis LLC" ]
44
},
55
"crawlerprotection-accessdenied-title": "Access denied",
6-
"crawlerprotection-accessdenied-text": "You must be logged in to perform this action or view this special page."
6+
"crawlerprotection-accessdenied-text": "You must be logged in to perform this action or view this special page. Please [[Special:UserLogin|log in]] to continue."
77
}

includes/Hooks.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ public function onMediaWikiPerformAction(
7070
$diffId = (int)$request->getVal( 'diff' );
7171
$oldId = (int)$request->getVal( 'oldid' );
7272

73+
$config = MediaWikiServices::getInstance()->getMainConfig();
74+
$protectedActions = $config->get( 'CrawlerProtectedActions' );
75+
$denyFast = $config->get( 'CrawlerProtectionUse418' );
76+
7377
if (
7478
!$user->isRegistered()
7579
&& (
@@ -79,6 +83,9 @@ public function onMediaWikiPerformAction(
7983
|| $oldId > 0
8084
)
8185
) {
86+
if ( $denyFast ) {
87+
$this->denyAccessWith418();
88+
}
8289
$this->denyAccess( $output );
8390
return false;
8491
}
@@ -114,10 +121,10 @@ public function onSpecialPageBeforeExecute( $special, $subPage ) {
114121

115122
$name = strtolower( $special->getName() );
116123
if ( in_array( $name, $normalizedProtectedPages, true ) ) {
117-
$out = $special->getContext()->getOutput();
118124
if ( $denyFast ) {
119125
$this->denyAccessWith418();
120126
}
127+
$out = $special->getContext()->getOutput();
121128
$this->denyAccess( $out );
122129
return false;
123130
}
@@ -143,14 +150,17 @@ protected function denyAccessWith418() {
143150
* @return void
144151
*/
145152
protected function denyAccess( $output ): void {
153+
$output->clearHTML();
146154
$output->setStatusCode( 403 );
147-
$output->addWikiTextAsInterface( wfMessage( 'crawlerprotection-accessdenied-text' )->plain() );
148155

149156
if ( version_compare( MW_VERSION, '1.41', '<' ) ) {
150157
$output->setPageTitle( wfMessage( 'crawlerprotection-accessdenied-title' ) );
151158
} else {
152159
// @phan-suppress-next-line PhanUndeclaredMethod Exists in 1.41+
153160
$output->setPageTitleMsg( wfMessage( 'crawlerprotection-accessdenied-title' ) );
154161
}
162+
163+
$output->addWikiTextAsInterface( wfMessage( 'crawlerprotection-accessdenied-text' )->plain() );
164+
$output->returnToMain();
155165
}
156166
}

tests/phpunit/unit/HooksTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,23 @@ public function testSpecialPageCallsDenyAccessWith418WhenConfigured() {
333333
$this->assertFalse( $result );
334334
}
335335

336+
/**
337+
* @covers ::denyAccess
338+
*/
339+
public function testDenyAccessSetsCorrectStatusAndContent() {
340+
$output = $this->createMock( self::$outputPageClassName );
341+
$output->expects( $this->once() )->method( 'clearHTML' );
342+
$output->expects( $this->once() )->method( 'setStatusCode' )->with( 403 );
343+
$output->expects( $this->once() )->method( 'addWikiTextAsInterface' );
344+
$output->expects( $this->once() )->method( 'returnToMain' );
345+
346+
$hooks = new Hooks();
347+
$reflection = new \ReflectionClass( $hooks );
348+
$method = $reflection->getMethod( 'denyAccess' );
349+
$method->setAccessible( true );
350+
$method->invoke( $hooks, $output );
351+
}
352+
336353
/**
337354
* Data provider for blocked special pages.
338355
*

0 commit comments

Comments
 (0)