Merge "Update searchdisabled message with docs and better link target"
[mediawiki.git] / includes / block / BlockManager.php
blobf98bd304f78e0ddaee34e945c95e3c3b11f1d72a
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.
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.
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
18 * @file
21 namespace MediaWiki\Block;
23 use LogicException;
24 use MediaWiki\Config\ServiceOptions;
25 use MediaWiki\HookContainer\HookContainer;
26 use MediaWiki\HookContainer\HookRunner;
27 use MediaWiki\MainConfigNames;
28 use MediaWiki\MediaWikiServices;
29 use MediaWiki\Message\Message;
30 use MediaWiki\Request\ProxyLookup;
31 use MediaWiki\Request\WebRequest;
32 use MediaWiki\Request\WebResponse;
33 use MediaWiki\User\User;
34 use MediaWiki\User\UserFactory;
35 use MediaWiki\User\UserIdentity;
36 use MediaWiki\User\UserIdentityUtils;
37 use MWCryptHash;
38 use Psr\Log\LoggerInterface;
39 use Wikimedia\IPSet;
40 use Wikimedia\IPUtils;
42 /**
43 * A service class for checking blocks.
44 * To obtain an instance, use MediaWikiServices::getInstance()->getBlockManager().
46 * @since 1.34 Refactored from User and Block.
48 class BlockManager {
49 /**
50 * @internal For use by ServiceWiring
52 public const CONSTRUCTOR_OPTIONS = [
53 MainConfigNames::ApplyIpBlocksToXff,
54 MainConfigNames::CookieSetOnAutoblock,
55 MainConfigNames::CookieSetOnIpBlock,
56 MainConfigNames::DnsBlacklistUrls,
57 MainConfigNames::EnableDnsBlacklist,
58 MainConfigNames::ProxyList,
59 MainConfigNames::ProxyWhitelist,
60 MainConfigNames::SecretKey,
61 MainConfigNames::SoftBlockRanges,
64 private ServiceOptions $options;
65 private UserFactory $userFactory;
66 private UserIdentityUtils $userIdentityUtils;
67 private LoggerInterface $logger;
68 private HookRunner $hookRunner;
69 private DatabaseBlockStore $blockStore;
70 private ProxyLookup $proxyLookup;
72 private BlockCache $userBlockCache;
73 private BlockCache $createAccountBlockCache;
75 public function __construct(
76 ServiceOptions $options,
77 UserFactory $userFactory,
78 UserIdentityUtils $userIdentityUtils,
79 LoggerInterface $logger,
80 HookContainer $hookContainer,
81 DatabaseBlockStore $blockStore,
82 ProxyLookup $proxyLookup
83 ) {
84 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
85 $this->options = $options;
86 $this->userFactory = $userFactory;
87 $this->userIdentityUtils = $userIdentityUtils;
88 $this->logger = $logger;
89 $this->hookRunner = new HookRunner( $hookContainer );
90 $this->blockStore = $blockStore;
91 $this->proxyLookup = $proxyLookup;
93 $this->userBlockCache = new BlockCache;
94 $this->createAccountBlockCache = new BlockCache;
97 /**
98 * Get the blocks that apply to a user. If there is only one, return that, otherwise
99 * return a composite block that combines the strictest features of the applicable
100 * blocks.
102 * Different blocks may be sought, depending on the user and their permissions. The
103 * user may be:
104 * (1) The global user (and can be affected by IP blocks). The global request object
105 * is needed for checking the IP address, the XFF header and the cookies.
106 * (2) The global user (and exempt from IP blocks). The global request object is
107 * available.
108 * (3) Another user (not the global user). No request object is available or needed;
109 * just look for a block against the user account.
111 * Cases #1 and #2 check whether the global user is blocked in practice; the block
112 * may due to their user account being blocked or to an IP address block or cookie
113 * block (or multiple of these). Case #3 simply checks whether a user's account is
114 * blocked, and does not determine whether the person using that account is affected
115 * in practice by any IP address or cookie blocks.
117 * @deprecated since 1.42 Use getBlock(), which is the same except that it expects
118 * the caller to do ipblock-exempt permission checking and to set $request to null
119 * if the user is exempt from IP blocks.
121 * @param UserIdentity $user
122 * @param WebRequest|null $request The global request object if the user is the
123 * global user (cases #1 and #2), otherwise null (case #3). The IP address and
124 * information from the request header are needed to find some types of blocks.
125 * @param bool $fromReplica Whether to check the replica DB first.
126 * To improve performance, non-critical checks are done against replica DBs.
127 * Check when actually saving should be done against primary.
128 * @param bool $disableIpBlockExemptChecking This is used internally to prevent
129 * a infinite recursion with autopromote. See T270145.
130 * @return AbstractBlock|null The most relevant block, or null if there is no block.
132 public function getUserBlock(
133 UserIdentity $user,
134 $request,
135 $fromReplica,
136 $disableIpBlockExemptChecking = false
138 wfDeprecated( __METHOD__, '1.42' );
139 // If this is the global user, they may be affected by IP blocks (case #1),
140 // or they may be exempt (case #2). If affected, look for additional blocks
141 // against the IP address and referenced in a cookie.
142 $checkIpBlocks = $request &&
143 // Because calling getBlock within Autopromote leads back to here,
144 // thus causing a infinite recursion. We fix this by not checking for
145 // ipblock-exempt when calling getBlock within Autopromote.
146 // See T270145.
147 !$disableIpBlockExemptChecking &&
148 !$this->isIpBlockExempt( $user );
150 return $this->getBlock(
151 $user,
152 $checkIpBlocks ? $request : null,
153 $fromReplica
158 * Get the blocks that apply to a user. If there is only one, return that, otherwise
159 * return a composite block that combines the strictest features of the applicable
160 * blocks.
162 * If the user is exempt from IP blocks, the request should be null.
164 * @since 1.42
165 * @param UserIdentity $user The user performing the action
166 * @param WebRequest|null $request The request to use for IP and cookie
167 * blocks, or null to skip checking for such blocks. If the user has the
168 * ipblock-exempt right, the request should be null.
169 * @param bool $fromReplica Whether to check the replica DB first.
170 * To improve performance, non-critical checks are done against replica DBs.
171 * Check when actually saving should be done against primary.
172 * @return AbstractBlock|null
174 public function getBlock(
175 UserIdentity $user,
176 ?WebRequest $request,
177 $fromReplica = true
178 ): ?AbstractBlock {
179 $fromPrimary = !$fromReplica;
180 $ip = null;
182 // TODO: normalise the fromPrimary parameter when replication is not configured.
183 // Maybe DatabaseBlockStore can tell us about the LoadBalancer configuration.
184 $cacheKey = new BlockCacheKey(
185 $request,
186 $user,
187 $fromPrimary
189 $block = $this->userBlockCache->get( $cacheKey );
190 if ( $block !== null ) {
191 $this->logger->debug( "Block cache hit with key {$cacheKey}" );
192 return $block ?: null;
194 $this->logger->debug( "Block cache miss with key {$cacheKey}" );
196 if ( $request ) {
198 // Case #1: checking the global user, including IP blocks
199 $ip = $request->getIP();
200 // For soft blocks, i.e. blocks that don't block logged-in users,
201 // temporary users are treated as anon users, and are blocked.
202 $applySoftBlocks = !$this->userIdentityUtils->isNamed( $user );
204 $xff = $request->getHeader( 'X-Forwarded-For' );
206 $blocks = array_merge(
207 $this->blockStore->newListFromTarget( $user, $ip, $fromPrimary ),
208 $this->getSystemIpBlocks( $ip, $applySoftBlocks ),
209 $this->getXffBlocks( $ip, $xff, $applySoftBlocks, $fromPrimary ),
210 $this->getCookieBlock( $user, $request )
212 } else {
214 // Case #2: checking the global user, but they are exempt from IP blocks
215 // and cookie blocks, so we only check for a user account block.
216 // Case #3: checking whether another user's account is blocked.
217 $blocks = $this->blockStore->newListFromTarget( $user, null, $fromPrimary );
221 $block = $this->createGetBlockResult( $ip, $blocks );
223 $legacyUser = $this->userFactory->newFromUserIdentity( $user );
224 $this->hookRunner->onGetUserBlock( clone $legacyUser, $ip, $block );
226 $this->userBlockCache->set( $cacheKey, $block ?: false );
227 return $block;
231 * Clear the cache of any blocks that refer to the specified user
233 public function clearUserCache( UserIdentity $user ) {
234 $this->userBlockCache->clearUser( $user );
235 $this->createAccountBlockCache->clearUser( $user );
239 * Get the block which applies to a create account action, if there is any
241 * @since 1.42
242 * @param UserIdentity $user
243 * @param WebRequest|null $request The request, or null to omit IP address
244 * and cookie blocks. If the user has the ipblock-exempt right, null
245 * should be passed.
246 * @param bool $fromReplica
247 * @return AbstractBlock|null
249 public function getCreateAccountBlock(
250 UserIdentity $user,
251 ?WebRequest $request,
252 $fromReplica
254 $key = new BlockCacheKey( $request, $user, $fromReplica );
255 $cachedBlock = $this->createAccountBlockCache->get( $key );
256 if ( $cachedBlock !== null ) {
257 $this->logger->debug( "Create account block cache hit with key {$key}" );
258 return $cachedBlock ?: null;
260 $this->logger->debug( "Create account block cache miss with key {$key}" );
262 $applicableBlocks = [];
263 $userBlock = $this->getBlock( $user, $request, $fromReplica );
264 if ( $userBlock ) {
265 $applicableBlocks = $userBlock->toArray();
268 // T15611: if the IP address the user is trying to create an account from is
269 // blocked with createaccount disabled, prevent new account creation there even
270 // when the user is logged in
271 if ( $request ) {
272 $ipBlock = $this->blockStore->newFromTarget(
273 null, $request->getIP()
275 if ( $ipBlock ) {
276 $applicableBlocks = array_merge( $applicableBlocks, $ipBlock->toArray() );
280 foreach ( $applicableBlocks as $i => $block ) {
281 if ( !$block->appliesToRight( 'createaccount' ) ) {
282 unset( $applicableBlocks[$i] );
285 $result = $this->createGetBlockResult(
286 $request ? $request->getIP() : null,
287 $applicableBlocks
289 $this->createAccountBlockCache->set( $key, $result ?: false );
290 return $result;
294 * Remove elements of a block which fail a callback test.
296 * @since 1.42
297 * @param Block|null $block The block, or null to pass in zero blocks.
298 * @param callable $callback The callback, which will be called once for
299 * each non-composite component of the block. The only parameter is the
300 * non-composite Block. It should return true, to keep that component,
301 * or false, to remove that component.
302 * @return Block|null
303 * - If there are zero remaining elements, null will be returned.
304 * - If there is one remaining element, a DatabaseBlock or some other
305 * non-composite block will be returned.
306 * - If there is more than one remaining element, a CompositeBlock will
307 * be returned.
309 public function filter( ?Block $block, $callback ) {
310 if ( !$block ) {
311 return null;
312 } elseif ( $block instanceof CompositeBlock ) {
313 $blocks = $block->getOriginalBlocks();
314 $originalCount = count( $blocks );
315 foreach ( $blocks as $i => $originalBlock ) {
316 if ( !$callback( $originalBlock ) ) {
317 unset( $blocks[$i] );
320 if ( !$blocks ) {
321 return null;
322 } elseif ( count( $blocks ) === 1 ) {
323 return $blocks[ array_key_first( $blocks ) ];
324 } elseif ( count( $blocks ) === $originalCount ) {
325 return $block;
326 } else {
327 return $block->withOriginalBlocks( array_values( $blocks ) );
329 } elseif ( !$callback( $block ) ) {
330 return null;
331 } else {
332 return $block;
337 * Determine if a user is exempt from IP blocks
338 * @param UserIdentity $user
339 * @return bool
341 private function isIpBlockExempt( UserIdentity $user ) {
342 return MediaWikiServices::getInstance()->getPermissionManager()
343 ->userHasRight( $user, 'ipblock-exempt' );
347 * @param string|null $ip
348 * @param AbstractBlock[] $blocks
349 * @return AbstractBlock|null
351 private function createGetBlockResult( ?string $ip, array $blocks ): ?AbstractBlock {
352 // Filter out any duplicated blocks, e.g. from the cookie
353 $blocks = $this->getUniqueBlocks( $blocks );
355 if ( count( $blocks ) === 0 ) {
356 return null;
357 } elseif ( count( $blocks ) === 1 ) {
358 return $blocks[ 0 ];
359 } else {
360 $compositeBlock = CompositeBlock::createFromBlocks( ...$blocks );
361 $compositeBlock->setTarget( $ip );
362 return $compositeBlock;
367 * Get the blocks that apply to an IP address. If there is only one, return that, otherwise
368 * return a composite block that combines the strictest features of the applicable blocks.
370 * @since 1.38
371 * @param string $ip
372 * @param bool $fromReplica
373 * @return AbstractBlock|null
375 public function getIpBlock( string $ip, bool $fromReplica ): ?AbstractBlock {
376 if ( !IPUtils::isValid( $ip ) ) {
377 return null;
380 $blocks = array_merge(
381 $this->blockStore->newListFromTarget( $ip, $ip, !$fromReplica ),
382 $this->getSystemIpBlocks( $ip, true )
385 return $this->createGetBlockResult( $ip, $blocks );
389 * Get the cookie block, if there is one.
391 * @param UserIdentity $user
392 * @param WebRequest $request
393 * @return AbstractBlock[]
395 private function getCookieBlock( UserIdentity $user, WebRequest $request ): array {
396 $cookieBlock = $this->getBlockFromCookieValue( $user, $request );
398 return $cookieBlock instanceof DatabaseBlock ? [ $cookieBlock ] : [];
402 * Get any system blocks against the IP address.
404 * @param string $ip
405 * @param bool $applySoftBlocks
406 * @return AbstractBlock[]
408 private function getSystemIpBlocks( string $ip, bool $applySoftBlocks ): array {
409 $blocks = [];
411 // Proxy blocking
412 if ( !in_array( $ip, $this->options->get( MainConfigNames::ProxyWhitelist ) ) ) {
413 // Local list
414 if ( $this->isLocallyBlockedProxy( $ip ) ) {
415 $blocks[] = new SystemBlock( [
416 'reason' => new Message( 'proxyblockreason' ),
417 'address' => $ip,
418 'systemBlock' => 'proxy',
419 ] );
420 } elseif ( $applySoftBlocks && $this->isDnsBlacklisted( $ip ) ) {
421 $blocks[] = new SystemBlock( [
422 'reason' => new Message( 'sorbsreason' ),
423 'address' => $ip,
424 'anonOnly' => true,
425 'systemBlock' => 'dnsbl',
426 ] );
430 // Soft blocking
431 if ( $applySoftBlocks && IPUtils::isInRanges( $ip, $this->options->get( MainConfigNames::SoftBlockRanges ) ) ) {
432 $blocks[] = new SystemBlock( [
433 'address' => $ip,
434 'reason' => new Message( 'softblockrangesreason', [ $ip ] ),
435 'anonOnly' => true,
436 'systemBlock' => 'wgSoftBlockRanges',
437 ] );
440 return $blocks;
444 * If `$wgApplyIpBlocksToXff` is truthy and the IP that the user is accessing the wiki from is not in
445 * `$wgProxyWhitelist`, then get the blocks that apply to the IP(s) in the X-Forwarded-For HTTP
446 * header.
448 * @param string $ip
449 * @param string $xff
450 * @param bool $applySoftBlocks
451 * @param bool $fromPrimary
452 * @return AbstractBlock[]
454 private function getXffBlocks(
455 string $ip,
456 string $xff,
457 bool $applySoftBlocks,
458 bool $fromPrimary
459 ): array {
460 // (T25343) Apply IP blocks to the contents of XFF headers, if enabled
461 if ( $this->options->get( MainConfigNames::ApplyIpBlocksToXff )
462 && !in_array( $ip, $this->options->get( MainConfigNames::ProxyWhitelist ) )
464 $xff = array_map( 'trim', explode( ',', $xff ) );
465 $xff = array_diff( $xff, [ $ip ] );
466 $xffblocks = $this->getBlocksForIPList( $xff, $applySoftBlocks, $fromPrimary );
468 // (T285159) Exclude autoblocks from XFF headers to prevent spoofed
469 // headers uncovering the IPs of autoblocked users
470 $xffblocks = array_filter( $xffblocks, static function ( $block ) {
471 return $block->getType() !== Block::TYPE_AUTO;
472 } );
474 return $xffblocks;
477 return [];
481 * Get all blocks that match any IP from an array of IP addresses
483 * @internal Public to support deprecated method in DatabaseBlock
485 * @param array $ipChain List of IPs (strings), usually retrieved from the
486 * X-Forwarded-For header of the request
487 * @param bool $applySoftBlocks Include soft blocks (anonymous-only blocks). These
488 * should only block anonymous and temporary users.
489 * @param bool $fromPrimary Whether to query the primary or replica DB
490 * @return DatabaseBlock[]
492 public function getBlocksForIPList( array $ipChain, bool $applySoftBlocks, bool $fromPrimary ) {
493 if ( $ipChain === [] ) {
494 return [];
497 $ips = [];
498 foreach ( array_unique( $ipChain ) as $ipaddr ) {
499 // Discard invalid IP addresses. Since XFF can be spoofed and we do not
500 // necessarily trust the header given to us, make sure that we are only
501 // checking for blocks on well-formatted IP addresses (IPv4 and IPv6).
502 // Do not treat private IP spaces as special as it may be desirable for wikis
503 // to block those IP ranges in order to stop misbehaving proxies that spoof XFF.
504 if ( !IPUtils::isValid( $ipaddr ) ) {
505 continue;
507 // Don't check trusted IPs (includes local CDNs which will be in every request)
508 if ( $this->proxyLookup->isTrustedProxy( $ipaddr ) ) {
509 continue;
511 $ips[] = $ipaddr;
513 return $this->blockStore->newListFromIPs( $ips, $applySoftBlocks, $fromPrimary );
517 * Given a list of blocks, return a list of unique blocks.
519 * This usually means that each block has a unique ID. For a block with ID null,
520 * if it's an autoblock, it will be filtered out if the parent block is present;
521 * if not, it is assumed to be a unique system block, and kept.
523 * @param AbstractBlock[] $blocks
524 * @return AbstractBlock[]
526 private function getUniqueBlocks( array $blocks ) {
527 $systemBlocks = [];
528 $databaseBlocks = [];
530 foreach ( $blocks as $block ) {
531 if ( $block instanceof SystemBlock ) {
532 $systemBlocks[] = $block;
533 } elseif ( $block->getType() === DatabaseBlock::TYPE_AUTO && $block instanceof DatabaseBlock ) {
534 if ( !isset( $databaseBlocks[$block->getParentBlockId()] ) ) {
535 $databaseBlocks[$block->getParentBlockId()] = $block;
537 } else {
538 // @phan-suppress-next-line PhanTypeMismatchDimAssignment getId is not null here
539 $databaseBlocks[$block->getId()] = $block;
543 return array_values( array_merge( $systemBlocks, $databaseBlocks ) );
547 * Try to load a block from an ID given in a cookie value.
549 * If the block is invalid, doesn't exist, or the cookie value is malformed, no
550 * block will be loaded. In these cases the cookie will either (1) be replaced
551 * with a valid cookie or (2) removed, next time trackBlockWithCookie is called.
553 * @param UserIdentity $user
554 * @param WebRequest $request
555 * @return DatabaseBlock|false The block object, or false if none could be loaded.
557 private function getBlockFromCookieValue(
558 UserIdentity $user,
559 WebRequest $request
561 $cookieValue = $request->getCookie( 'BlockID' );
562 if ( $cookieValue === null ) {
563 return false;
566 $blockCookieId = $this->getIdFromCookieValue( $cookieValue );
567 if ( $blockCookieId !== null ) {
568 $block = $this->blockStore->newFromID( $blockCookieId );
569 if (
570 $block instanceof DatabaseBlock &&
571 $this->shouldApplyCookieBlock( $block, !$user->isRegistered() )
573 return $block;
577 return false;
581 * Check if the block loaded from the cookie should be applied.
583 * @param DatabaseBlock $block
584 * @param bool $isAnon The user is logged out
585 * @return bool The block should be applied
587 private function shouldApplyCookieBlock( DatabaseBlock $block, $isAnon ) {
588 if ( !$block->isExpired() ) {
589 switch ( $block->getType() ) {
590 case DatabaseBlock::TYPE_IP:
591 case DatabaseBlock::TYPE_RANGE:
592 // If block is type IP or IP range, load only
593 // if user is not logged in (T152462)
594 return $isAnon &&
595 $this->options->get( MainConfigNames::CookieSetOnIpBlock );
596 case DatabaseBlock::TYPE_USER:
597 return $block->isAutoblocking() &&
598 $this->options->get( MainConfigNames::CookieSetOnAutoblock );
599 default:
600 return false;
603 return false;
607 * Check if an IP address is in the local proxy list
609 * @param string $ip
610 * @return bool
612 private function isLocallyBlockedProxy( $ip ) {
613 $proxyList = $this->options->get( MainConfigNames::ProxyList );
614 if ( !$proxyList ) {
615 return false;
618 if ( !is_array( $proxyList ) ) {
619 // Load values from the specified file
620 $proxyList = array_map( 'trim', file( $proxyList ) );
623 $proxyListIPSet = new IPSet( $proxyList );
624 return $proxyListIPSet->match( $ip );
628 * Whether the given IP is in a DNS blacklist.
630 * @param string $ip IP to check
631 * @param bool $checkAllowed Whether to check $wgProxyWhitelist first
632 * @return bool True if blacklisted.
634 public function isDnsBlacklisted( $ip, $checkAllowed = false ) {
635 if ( !$this->options->get( MainConfigNames::EnableDnsBlacklist ) ||
636 ( $checkAllowed && in_array( $ip, $this->options->get( MainConfigNames::ProxyWhitelist ) ) )
638 return false;
641 return $this->inDnsBlacklist( $ip, $this->options->get( MainConfigNames::DnsBlacklistUrls ) );
645 * Whether the given IP is in a given DNS blacklist.
647 * @param string $ip IP to check
648 * @param string[] $bases URL of the DNS blacklist
649 * @return bool True if blacklisted.
651 private function inDnsBlacklist( $ip, array $bases ) {
652 $found = false;
653 // @todo FIXME: IPv6 ??? (https://bugs.php.net/bug.php?id=33170)
654 if ( IPUtils::isIPv4( $ip ) ) {
655 // Reverse IP, T23255
656 $ipReversed = implode( '.', array_reverse( explode( '.', $ip ) ) );
658 foreach ( $bases as $base ) {
659 // Make hostname
660 // If we have an access key, use that too (ProjectHoneypot, etc.)
661 $basename = $base;
662 if ( is_array( $base ) ) {
663 if ( count( $base ) >= 2 ) {
664 // Access key is 1, base URL is 0
665 $hostname = "{$base[1]}.$ipReversed.{$base[0]}";
666 } else {
667 $hostname = "$ipReversed.{$base[0]}";
669 $basename = $base[0];
670 } else {
671 $hostname = "$ipReversed.$base";
674 // Send query
675 $ipList = $this->checkHost( $hostname );
677 if ( $ipList ) {
678 $this->logger->info(
679 'Hostname {hostname} is {ipList}, it\'s a proxy says {basename}!',
681 'hostname' => $hostname,
682 'ipList' => $ipList[0],
683 'basename' => $basename,
686 $found = true;
687 break;
690 $this->logger->debug( "Requested $hostname, not found in $basename." );
694 return $found;
698 * Wrapper for mocking in tests.
700 * @param string $hostname DNSBL query
701 * @return string[]|false IPv4 array, or false if the IP is not blacklisted
703 protected function checkHost( $hostname ) {
704 return gethostbynamel( $hostname );
708 * Set the 'BlockID' cookie depending on block type and user authentication status.
710 * If a block cookie is already set, this will check the block that the cookie references
711 * and do the following:
712 * - If the block is a valid block that should be applied, do nothing and return early.
713 * This ensures that the cookie's expiry time is based on the time of the first page
714 * load or attempt. (See discussion on T233595.)
715 * - If the block is invalid (e.g. has expired), clear the cookie and continue to check
716 * whether there is another block that should be tracked.
717 * - If the block is a valid block, but should not be tracked by a cookie, clear the
718 * cookie and continue to check whether there is another block that should be tracked.
720 * Must be called after the User object is loaded, and before headers are sent.
722 * @since 1.34
723 * @param User $user
724 * @param WebResponse $response The response on which to set the cookie.
726 public function trackBlockWithCookie( User $user, WebResponse $response ) {
727 if ( !$this->options->get( MainConfigNames::CookieSetOnIpBlock ) &&
728 !$this->options->get( MainConfigNames::CookieSetOnAutoblock ) ) {
729 // Cookie blocks are disabled, return early to prevent executing unnecessary logic.
730 return;
733 $request = $user->getRequest();
735 if ( $request->getCookie( 'BlockID' ) !== null ) {
736 $cookieBlock = $this->getBlockFromCookieValue( $user, $request );
737 if ( $cookieBlock && $this->shouldApplyCookieBlock( $cookieBlock, $user->isAnon() ) ) {
738 return;
740 // The block pointed to by the cookie is invalid or should not be tracked.
741 $this->clearBlockCookie( $response );
744 if ( !$user->isSafeToLoad() ) {
745 // Prevent a circular dependency by not allowing this method to be called
746 // before or while the user is being loaded.
747 // E.g. User > BlockManager > Block > Message > getLanguage > User.
748 // See also T180050 and T226777.
749 throw new LogicException( __METHOD__ . ' requires a loaded User object' );
751 if ( $response->headersSent() ) {
752 throw new LogicException( __METHOD__ . ' must be called pre-send' );
755 $block = $user->getBlock();
756 $isAnon = $user->isAnon();
758 if ( $block ) {
759 foreach ( $block->toArray() as $originalBlock ) {
760 // TODO: Improve on simply tracking the first trackable block (T225654)
761 if ( $originalBlock instanceof DatabaseBlock
762 && $this->shouldTrackBlockWithCookie( $originalBlock, $isAnon )
764 $this->setBlockCookie( $originalBlock, $response );
765 return;
772 * Set the 'BlockID' cookie to this block's ID and expiry time. The cookie's expiry will be
773 * the same as the block's, to a maximum of 24 hours.
775 * @since 1.34
776 * @param DatabaseBlock $block
777 * @param WebResponse $response The response on which to set the cookie.
779 private function setBlockCookie( DatabaseBlock $block, WebResponse $response ) {
780 // Calculate the default expiry time.
781 $maxExpiryTime = wfTimestamp( TS_MW, (int)wfTimestamp() + ( 24 * 60 * 60 ) );
783 // Use the block's expiry time only if it's less than the default.
784 $expiryTime = $block->getExpiry();
785 if ( $expiryTime === 'infinity' || $expiryTime > $maxExpiryTime ) {
786 $expiryTime = $maxExpiryTime;
789 // Set the cookie
790 $expiryValue = (int)wfTimestamp( TS_UNIX, $expiryTime );
791 $cookieOptions = [ 'httpOnly' => false ];
792 $cookieValue = $this->getCookieValue( $block );
793 $response->setCookie( 'BlockID', $cookieValue, $expiryValue, $cookieOptions );
797 * Check if the block should be tracked with a cookie.
799 * @param DatabaseBlock $block
800 * @param bool $isAnon The user is logged out
801 * @return bool The block should be tracked with a cookie
803 private function shouldTrackBlockWithCookie( DatabaseBlock $block, $isAnon ) {
804 switch ( $block->getType() ) {
805 case DatabaseBlock::TYPE_IP:
806 case DatabaseBlock::TYPE_RANGE:
807 return $isAnon && $this->options->get( MainConfigNames::CookieSetOnIpBlock );
808 case DatabaseBlock::TYPE_USER:
809 return !$isAnon &&
810 $this->options->get( MainConfigNames::CookieSetOnAutoblock ) &&
811 $block->isAutoblocking();
812 default:
813 return false;
818 * Unset the 'BlockID' cookie.
820 * @since 1.34
821 * @param WebResponse $response
823 public static function clearBlockCookie( WebResponse $response ) {
824 $response->clearCookie( 'BlockID', [ 'httpOnly' => false ] );
828 * Get the stored ID from the 'BlockID' cookie. The cookie's value is usually a combination of
829 * the ID and a HMAC (see self::getCookieValue), but will sometimes only be the ID.
831 * @since 1.34
832 * @param string $cookieValue The string in which to find the ID.
833 * @return int|null The block ID, or null if the HMAC is present and invalid.
835 private function getIdFromCookieValue( $cookieValue ) {
836 // The cookie value must start with a number
837 if ( !is_numeric( substr( $cookieValue, 0, 1 ) ) ) {
838 return null;
841 // Extract the ID prefix from the cookie value (may be the whole value, if no bang found).
842 $bangPos = strpos( $cookieValue, '!' );
843 $id = ( $bangPos === false ) ? $cookieValue : substr( $cookieValue, 0, $bangPos );
844 if ( !$this->options->get( MainConfigNames::SecretKey ) ) {
845 // If there's no secret key, just use the ID as given.
846 return (int)$id;
848 $storedHmac = substr( $cookieValue, $bangPos + 1 );
849 $calculatedHmac = MWCryptHash::hmac( $id, $this->options->get( MainConfigNames::SecretKey ), false );
850 if ( $calculatedHmac === $storedHmac ) {
851 return (int)$id;
852 } else {
853 return null;
858 * Get the BlockID cookie's value for this block. This is usually the block ID concatenated
859 * with an HMAC in order to avoid spoofing (T152951), but if wgSecretKey is not set will just
860 * be the block ID.
862 * @since 1.34
863 * @param DatabaseBlock $block
864 * @return string The block ID, probably concatenated with "!" and the HMAC.
866 private function getCookieValue( DatabaseBlock $block ) {
867 $id = (string)$block->getId();
868 if ( !$this->options->get( MainConfigNames::SecretKey ) ) {
869 // If there's no secret key, don't append a HMAC.
870 return $id;
872 $hmac = MWCryptHash::hmac( $id, $this->options->get( MainConfigNames::SecretKey ), false );
873 return $id . '!' . $hmac;