Debian Codegraph @

includes/Permissions/UserAuthority.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 namespace MediaWiki\Permissions;
22
23 use InvalidArgumentException;
24 use MediaWiki\Block\Block;
25 use MediaWiki\Block\BlockErrorFormatter;
26 use MediaWiki\Context\IContextSource;
27 use MediaWiki\Linker\LinkTarget;
28 use MediaWiki\Page\PageIdentity;
29 use MediaWiki\Request\WebRequest;
30 use MediaWiki\Title\TitleValue;
31 use MediaWiki\User\User;
32 use MediaWiki\User\UserIdentity;
33 use Wikimedia\Assert\Assert;
34 use Wikimedia\DebugInfo\DebugInfoTrait;
35 use Wikimedia\Rdbms\IDBAccessObject;
36
37 /**
38 * Represents the authority of a given User. For anonymous visitors, this will typically
39 * allow only basic permissions. For logged in users, permissions are generally based on group
40 * membership, but may be adjusted based on things like IP range blocks, OAuth grants, or
41 * rate limits.
42 *
43 * @note This is intended as an intermediate step towards an implementation of Authority that
44 * contains much of the logic currently in PermissionManager, and is based directly on
45 * WebRequest and Session, rather than a User object. However, for now, code that needs an
46 * Authority that reflects the current user and web request should use a User object directly.
47 *
48 * @unstable
49 * @since 1.36
50 */
51 class UserAuthority implements Authority {
52
53 use DebugInfoTrait;
54
55 /**
56 * @var PermissionManager
57 * @noVarDump
58 */
59 private $permissionManager;
60
61 /**
62 * @var RateLimiter
63 * @noVarDump
64 */
65 private $rateLimiter;
66
67 /**
68 * @var User
69 * @noVarDump
70 */
71 private $actor;
72
73 /**
74 * Local cache for user block information. False is used to indicate that there is no block,
75 * while null indicates that we don't know and have to check.
76 * @var Block|false|null
77 */
78 private $userBlock = null;
79
80 /**
81 * Cache for the outcomes of rate limit checks.
82 * We cache the outcomes primarily so we don't bump the counter multiple times
83 * per request.
84 * @var array<string,array> Map of actions to [ int, bool ] pairs.
85 * The first element is the increment performed so far (typically 1).
86 * The second element is the cached outcome of the check (whether the limit was reached)
87 */
88 private $limitCache = [];
89
90 /**
91 * Whether the limit cache should be used. Generally, the limit cache should be used in web
92 * requests, since we don't want to bump the same limit more than once per request. It
93 * should not be used during testing, so limits can easily be tested without knowledge
94 * about the caching mechanism.
95 *
96 * @var bool
97 */
98 private bool $useLimitCache;
99
100 private WebRequest $request;
101 private IContextSource $uiContext;
102 private BlockErrorFormatter $blockErrorFormatter;
103
104 /**
105 * @param User $user
106 * @param WebRequest $request
107 * @param IContextSource $uiContext
108 * @param PermissionManager $permissionManager
109 * @param RateLimiter $rateLimiter
110 * @param BlockErrorFormatter $blockErrorFormatter
111 */
112 public function __construct(
113 User $user,
114 WebRequest $request,
115 IContextSource $uiContext,
116 PermissionManager $permissionManager,
117 RateLimiter $rateLimiter,
118 BlockErrorFormatter $blockErrorFormatter
119 ) {
120 $this->actor = $user;
121 $this->request = $request;
122 $this->uiContext = $uiContext;
123 $this->permissionManager = $permissionManager;
124 $this->rateLimiter = $rateLimiter;
125 $this->blockErrorFormatter = $blockErrorFormatter;
126 $this->useLimitCache = !defined( 'MW_PHPUNIT_TEST' );
127 }
128
129 /**
130 * @internal
131 * @param bool $useLimitCache
132 */
133 public function setUseLimitCache( bool $useLimitCache ) {
134 $this->useLimitCache = $useLimitCache;
135 }
136
137 /** @inheritDoc */
138 public function getUser(): UserIdentity {
139 return $this->actor;
140 }
141
142 /** @inheritDoc */
143 public function isAllowed( string $permission, ?PermissionStatus $status = null ): bool {
144 return $this->internalAllowed( $permission, $status, false, null );
145 }
146
147 /** @inheritDoc */
148 public function isAllowedAny( ...$permissions ): bool {
149 if ( !$permissions ) {
150 throw new InvalidArgumentException( 'At least one permission must be specified' );
151 }
152
153 return $this->permissionManager->userHasAnyRight( $this->actor, ...$permissions );
154 }
155
156 /** @inheritDoc */
157 public function isAllowedAll( ...$permissions ): bool {
158 if ( !$permissions ) {
159 throw new InvalidArgumentException( 'At least one permission must be specified' );
160 }
161
162 return $this->permissionManager->userHasAllRights( $this->actor, ...$permissions );
163 }
164
165 /** @inheritDoc */
166 public function probablyCan(
167 string $action,
168 PageIdentity $target,
169 ?PermissionStatus $status = null
170 ): bool {
171 return $this->internalCan(
172 PermissionManager::RIGOR_QUICK,
173 $action,
174 $target,
175 $status,
176 false // do not check the rate limit
177 );
178 }
179
180 /** @inheritDoc */
181 public function definitelyCan(
182 string $action,
183 PageIdentity $target,
184 ?PermissionStatus $status = null
185 ): bool {
186 // Note that we do not use RIGOR_SECURE to avoid hitting the primary
187 // database for read operations. RIGOR_FULL performs the same checks,
188 // but is subject to replication lag.
189 return $this->internalCan(
190 PermissionManager::RIGOR_FULL,
191 $action,
192 $target,
193 $status,
194 0 // only check the rate limit, don't count it as a hit
195 );
196 }
197
198 /** @inheritDoc */
199 public function isDefinitelyAllowed( string $action, ?PermissionStatus $status = null ): bool {
200 $userBlock = $this->getApplicableBlock( PermissionManager::RIGOR_FULL, $action );
201 return $this->internalAllowed( $action, $status, 0, $userBlock );
202 }
203
204 /** @inheritDoc */
205 public function authorizeAction(
206 string $action,
207 ?PermissionStatus $status = null
208 ): bool {
209 // Any side-effects can be added here.
210
211 $userBlock = $this->getApplicableBlock( PermissionManager::RIGOR_SECURE, $action );
212
213 return $this->internalAllowed(
214 $action,
215 $status,
216 1,
217 $userBlock
218 );
219 }
220
221 /** @inheritDoc */
222 public function authorizeRead(
223 string $action,
224 PageIdentity $target,
225 ?PermissionStatus $status = null
226 ): bool {
227 // Any side-effects can be added here.
228
229 // Note that we do not use RIGOR_SECURE to avoid hitting the primary
230 // database for read operations. RIGOR_FULL performs the same checks,
231 // but is subject to replication lag.
232 return $this->internalCan(
233 PermissionManager::RIGOR_FULL,
234 $action,
235 $target,
236 $status,
237 1 // count a hit towards the rate limit
238 );
239 }
240
241 /** @inheritDoc */
242 public function authorizeWrite(
243 string $action,
244 PageIdentity $target,
245 ?PermissionStatus $status = null
246 ): bool {
247 // Any side-effects can be added here.
248
249 // Note that we need to use RIGOR_SECURE here to ensure that we do not
250 // miss a user block or page protection due to replication lag.
251 return $this->internalCan(
252 PermissionManager::RIGOR_SECURE,
253 $action,
254 $target,
255 $status,
256 1 // count a hit towards the rate limit
257 );
258 }
259
260 /**
261 * Check whether the user is allowed to perform the action, taking into account
262 * the user's block status as well as any rate limits.
263 *
264 * @param string $action
265 * @param PermissionStatus|null $status
266 * @param int|false $limitRate False means no check, 0 means check only,
267 * and 1 means check and increment
268 * @param ?Block $userBlock
269 *
270 * @return bool
271 */
272 private function internalAllowed(
273 string $action,
274 ?PermissionStatus $status,
275 $limitRate,
276 ?Block $userBlock
277 ): bool {
278 if ( $status ) {
279 Assert::precondition(
280 $status->isGood(),
281 'The PermissionStatus passed as $status parameter must still be good'
282 );
283 }
284
285 if ( !$this->permissionManager->userHasRight( $this->actor, $action ) ) {
286 if ( !$status ) {
287 return false;
288 }
289
290 $status->setPermission( $action );
291 $status->merge(
292 $this->permissionManager->newFatalPermissionDeniedStatus(
293 $action,
294 $this->uiContext
295 )
296 );
297 }
298
299 if ( $userBlock ) {
300 if ( !$status ) {
301 return false;
302 }
303
304 $messages = $this->blockErrorFormatter->getMessages(
305 $userBlock,
306 $this->actor,
307 $this->request->getIP()
308 );
309
310 $status->setPermission( $action );
311 foreach ( $messages as $message ) {
312 $status->fatal( $message );
313 }
314 }
315
316 // Check and bump the rate limit.
317 if ( $limitRate !== false ) {
318 $isLimited = $this->limit( $action, $limitRate, $status );
319 if ( $isLimited && !$status ) {
320 return false;
321 }
322 }
323
324 return !$status || $status->isOK();
325 }
326
327 // See ApiBase::BLOCK_CODE_MAP
328 private const BLOCK_CODES = [
329 'blockedtext',
330 'blockedtext-partial',
331 'autoblockedtext',
332 'systemblockedtext',
333 'blockedtext-composite',
334 'blockedtext-tempuser',
335 'autoblockedtext-tempuser',
336 ];
337
338 /**
339 * @param string $rigor
340 * @param string $action
341 * @param PageIdentity $target
342 * @param ?PermissionStatus $status
343 * @param int|false $limitRate False means no check, 0 means check only,
344 * a non-zero values means check and increment
345 *
346 * @return bool
347 */
348 private function internalCan(
349 string $rigor,
350 string $action,
351 PageIdentity $target,
352 ?PermissionStatus $status,
353 $limitRate
354 ): bool {
355 // Check and bump the rate limit.
356 if ( $limitRate !== false ) {
357 $isLimited = $this->limit( $action, $limitRate, $status );
358 if ( $isLimited && !$status ) {
359 // bail early if we don't have a status object
360 return false;
361 }
362 }
363
364 if ( !( $target instanceof LinkTarget ) ) {
365 // TODO: PermissionManager should accept PageIdentity!
366 $target = TitleValue::newFromPage( $target );
367 }
368
369 if ( $status ) {
370 $status->setPermission( $action );
371
372 $tempStatus = $this->permissionManager->getPermissionStatus(
373 $action,
374 $this->actor,
375 $target,
376 $rigor
377 );
378
379 if ( $tempStatus->isGood() ) {
380 // Nothing to merge, return early
381 return $status->isOK();
382 }
383
384 // Instead of `$status->merge( $tempStatus )`, process the messages like this to ensure that
385 // the resulting status contains Message objects instead of strings+arrays, and thus does not
386 // trigger wikitext escaping in a legacy code path. See T368821 for more information about
387 // that behavior, and see T306494 for the specific bug this fixes.
388 foreach ( $tempStatus->getMessages() as $msg ) {
389 $status->fatal( $msg );
390 }
391
392 foreach ( self::BLOCK_CODES as $code ) {
393 // HACK: Detect whether the permission was denied because the user is blocked.
394 // A similar hack exists in ApiBase::BLOCK_CODE_MAP.
395 // When permission checking logic is moved out of PermissionManager,
396 // we can record the block info directly when first checking the block,
397 // rather than doing that here.
398 if ( $tempStatus->hasMessage( $code ) ) {
399 $block = $this->getBlock();
400 if ( $block ) {
401 $status->setBlock( $block );
402 }
403 break;
404 }
405 }
406
407 return $status->isOK();
408 } else {
409 // allow PermissionManager to short-circuit
410 return $this->permissionManager->userCan(
411 $action,
412 $this->actor,
413 $target,
414 $rigor
415 );
416 }
417 }
418
419 /**
420 * Check whether a rate limit has been exceeded for the given action.
421 *
422 * @see RateLimiter::limit
423 * @internal For use by User::pingLimiter only.
424 *
425 * @param string $action
426 * @param int $incrBy
427 * @param PermissionStatus|null $status
428 *
429 * @return bool
430 */
431 public function limit( string $action, int $incrBy, ?PermissionStatus $status ): bool {
432 $isLimited = null;
433
434 if ( $this->useLimitCache && isset( $this->limitCache[ $action ] ) ) {
435 // subtract the increment that was already applied earlier
436 $incrRemaining = $incrBy - $this->limitCache[ $action ][ 0 ];
437
438 // if no increment is left to apply, return the cached outcome
439 if ( $incrRemaining < 1 ) {
440 $isLimited = $this->limitCache[ $action ][ 1 ];
441 }
442 } else {
443 $incrRemaining = $incrBy;
444 }
445
446 if ( $isLimited === null ) {
447 // NOTE: Avoid toRateLimitSubject() if possible, for performance
448 if ( $this->rateLimiter->isLimitable( $action ) ) {
449 $isLimited = $this->rateLimiter->limit(
450 $this->actor->toRateLimitSubject(),
451 $action,
452 $incrRemaining
453 );
454 } else {
455 $isLimited = false;
456 }
457
458 // Cache the outcome, so we don't bump the counter twice during the same request.
459 $this->limitCache[ $action ] = [ $incrBy, $isLimited ];
460 }
461
462 if ( $isLimited && $status ) {
463 $status->setRateLimitExceeded();
464 }
465
466 return $isLimited;
467 }
468
469 /** @inheritDoc */
470 public function getBlock( int $freshness = IDBAccessObject::READ_NORMAL ): ?Block {
471 // Cache block info, so we don't have to fetch it again unnecessarily.
472 if ( $this->userBlock === null || $freshness === IDBAccessObject::READ_LATEST ) {
473 $this->userBlock = $this->actor->getBlock( $freshness );
474
475 // if we got null back, remember this as "false"
476 $this->userBlock = $this->userBlock ?: false;
477 }
478
479 // if we remembered "false", return null
480 return $this->userBlock ?: null;
481 }
482
483 private function getApplicableBlock(
484 string $rigor,
485 string $action,
486 ?PageIdentity $target = null
487 ): ?Block {
488 // NOTE: We follow the parameter order of internalCan here.
489 // It doesn't match the one in PermissionManager.
490 return $this->permissionManager->getApplicableBlock(
491 $action,
492 $this->actor,
493 $rigor,
494 $target,
495 $this->request
496 );
497 }
498
499 public function isRegistered(): bool {
500 return $this->actor->isRegistered();
501 }
502
503 public function isTemp(): bool {
504 return $this->actor->isTemp();
505 }
506
507 public function isNamed(): bool {
508 return $this->actor->isNamed();
509 }
510 }