-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathCrawlerProtectionService.php
More file actions
190 lines (169 loc) · 5.34 KB
/
CrawlerProtectionService.php
File metadata and controls
190 lines (169 loc) · 5.34 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
<?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 CrawlerProtectionService.php
*/
namespace MediaWiki\Extension\CrawlerProtection;
use MediaWiki\Config\ServiceOptions;
use MediaWiki\Output\OutputPage;
use MediaWiki\Request\WebRequest;
use MediaWiki\User\User;
/**
* Core business logic for CrawlerProtection.
*
* Determines whether a given request should be blocked for anonymous
* users and delegates the actual denial to ResponseFactory.
*/
class CrawlerProtectionService {
/** @var string[] List of constructor options this class accepts */
public const CONSTRUCTOR_OPTIONS = [
'CrawlerProtectedActions',
'CrawlerProtectedSpecialPages',
];
/** @var ServiceOptions */
private ServiceOptions $options;
/** @var ResponseFactory */
private ResponseFactory $responseFactory;
/**
* @param ServiceOptions $options
* @param ResponseFactory $responseFactory
*/
public function __construct(
ServiceOptions $options,
ResponseFactory $responseFactory
) {
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
$this->options = $options;
$this->responseFactory = $responseFactory;
}
/**
* Check whether a regular page request should be blocked.
*
* Returns false (= abort further processing) when the request is
* blocked, true otherwise.
*
* @param OutputPage $output
* @param User $user
* @param WebRequest $request
* @return bool
*/
public function checkPerformAction(
$output,
$user,
$request
): bool {
if ( $user->isRegistered() ) {
return true;
}
$type = $request->getVal( 'type' );
$action = $request->getVal( 'action' );
$diffId = (int)$request->getVal( 'diff' );
$oldId = (int)$request->getVal( 'oldid' );
if (
$type === 'revision'
|| $this->isProtectedAction( $action )
|| $diffId > 0
|| $oldId > 0
) {
$this->responseFactory->denyAccess( $output );
return false;
}
return true;
}
/**
* Determine whether the given action name is in the configured
* list of protected actions.
*
* The comparison is case-insensitive so that configured values
* like "History" or "HISTORY" match the action parameter.
*
* @param string|null $action
* @return bool
*/
public function isProtectedAction( ?string $action ): bool {
if ( $action === null ) {
return false;
}
$protectedActions = array_map(
'strtolower',
$this->options->get( 'CrawlerProtectedActions' )
);
return in_array( strtolower( $action ), $protectedActions, true );
}
/**
* Check whether a special page request should be blocked.
*
* Returns false (= abort further processing) when the request is
* blocked, true otherwise.
*
* @param string $specialPageName The canonical special page name
* @param OutputPage $output
* @param User $user
* @return bool
*/
public function checkSpecialPage(
string $specialPageName,
$output,
$user
): bool {
if ( $user->isRegistered() ) {
return true;
}
if ( $this->isProtectedSpecialPage( $specialPageName ) ) {
$this->responseFactory->denyAccess( $output );
return false;
}
return true;
}
/**
* Determine whether the given special page name is in the
* configured list of protected special pages.
*
* Because this method is only called from the SpecialPageBeforeExecute
* hook, any "Foo:" prefix on a configured value is necessarily the
* "Special" namespace in English or its localized equivalent (e.g.
* "Spezial:" in German). We therefore simply strip everything up to
* and including the first colon rather than checking for a specific
* namespace name, which keeps the logic language-agnostic.
*
* @param string $specialPageName
* @return bool
*/
public function isProtectedSpecialPage( string $specialPageName ): bool {
$protectedSpecialPages = $this->options->get( 'CrawlerProtectedSpecialPages' );
// Normalize protected special pages: lowercase and strip any
// namespace prefix (everything up to and including the first ':').
$normalizedProtectedPages = array_map(
static function ( string $p ): string {
$lower = strtolower( $p );
$colonPos = strpos( $lower, ':' );
if ( $colonPos !== false ) {
return substr( $lower, $colonPos + 1 );
}
return $lower;
},
$protectedSpecialPages
);
$name = strtolower( $specialPageName );
return in_array( $name, $normalizedProtectedPages, true );
}
}