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
21 namespace MediaWiki\Block
;
28 use MediaWiki\Config\ServiceOptions
;
29 use MediaWiki\Permissions\PermissionManager
;
30 use MediaWiki\User\UserIdentity
;
38 * A service class for checking blocks.
39 * To obtain an instance, use MediaWikiServices::getInstance()->getBlockManager().
41 * @since 1.34 Refactored from User and Block.
44 // TODO: This should be UserIdentity instead of User
48 /** @var WebRequest */
49 private $currentRequest;
51 /** @var PermissionManager */
52 private $permissionManager;
55 * TODO Make this a const when HHVM support is dropped (T192166)
60 public static $constructorOptions = [
62 'CookieSetOnAutoblock',
73 * @param ServiceOptions $options
74 * @param User $currentUser
75 * @param WebRequest $currentRequest
76 * @param PermissionManager $permissionManager
78 public function __construct(
79 ServiceOptions
$options,
81 WebRequest
$currentRequest,
82 PermissionManager
$permissionManager
84 $options->assertRequiredOptions( self
::$constructorOptions );
85 $this->options
= $options;
86 $this->currentUser
= $currentUser;
87 $this->currentRequest
= $currentRequest;
88 $this->permissionManager
= $permissionManager;
92 * Get the blocks that apply to a user. If there is only one, return that, otherwise
93 * return a composite block that combines the strictest features of the applicable
96 * TODO: $user should be UserIdentity instead of User
98 * @internal This should only be called by User::getBlockedStatus
100 * @param bool $fromReplica Whether to check the replica DB first.
101 * To improve performance, non-critical checks are done against replica DBs.
102 * Check when actually saving should be done against master.
103 * @return AbstractBlock|null The most relevant block, or null if there is no block.
105 public function getUserBlock( User
$user, $fromReplica ) {
106 $isAnon = $user->getId() === 0;
107 $fromMaster = !$fromReplica;
109 // TODO: If $user is the current user, we should use the current request. Otherwise,
110 // we should not look for XFF or cookie blocks.
111 $request = $user->getRequest();
113 # We only need to worry about passing the IP address to the block generator if the
114 # user is not immune to autoblocks/hardblocks, and they are the current user so we
115 # know which IP address they're actually coming from
117 $sessionUser = $this->currentUser
;
118 // the session user is set up towards the end of Setup.php. Until then,
119 // assume it's a logged-out user.
120 $globalUserName = $sessionUser->isSafeToLoad()
121 ?
$sessionUser->getName()
122 : IP
::sanitizeIP( $this->currentRequest
->getIP() );
123 if ( $user->getName() === $globalUserName &&
124 !$this->permissionManager
->userHasRight( $user, 'ipblock-exempt' ) ) {
125 $ip = $this->currentRequest
->getIP();
129 // After this, $blocks is an array of blocks or an empty array
130 // TODO: remove dependency on DatabaseBlock
131 $blocks = DatabaseBlock
::newListFromTarget( $user, $ip, $fromMaster );
134 $cookieBlock = $this->getBlockFromCookieValue( $user, $request );
135 if ( $cookieBlock instanceof AbstractBlock
) {
136 $blocks[] = $cookieBlock;
140 if ( $ip !== null && !in_array( $ip, $this->options
->get( 'ProxyWhitelist' ) ) ) {
142 if ( $this->isLocallyBlockedProxy( $ip ) ) {
143 $blocks[] = new SystemBlock( [
144 'byText' => wfMessage( 'proxyblocker' )->text(),
145 'reason' => wfMessage( 'proxyblockreason' )->plain(),
147 'systemBlock' => 'proxy',
149 } elseif ( $isAnon && $this->isDnsBlacklisted( $ip ) ) {
150 $blocks[] = new SystemBlock( [
151 'byText' => wfMessage( 'sorbs' )->text(),
152 'reason' => wfMessage( 'sorbsreason' )->plain(),
154 'systemBlock' => 'dnsbl',
159 // (T25343) Apply IP blocks to the contents of XFF headers, if enabled
160 if ( $this->options
->get( 'ApplyIpBlocksToXff' )
162 && !in_array( $ip, $this->options
->get( 'ProxyWhitelist' ) )
164 $xff = $request->getHeader( 'X-Forwarded-For' );
165 $xff = array_map( 'trim', explode( ',', $xff ) );
166 $xff = array_diff( $xff, [ $ip ] );
167 // TODO: remove dependency on DatabaseBlock
168 $xffblocks = DatabaseBlock
::getBlocksForIPList( $xff, $isAnon, $fromMaster );
169 $blocks = array_merge( $blocks, $xffblocks );
175 && IP
::isInRanges( $ip, $this->options
->get( 'SoftBlockRanges' ) )
177 $blocks[] = new SystemBlock( [
179 'byText' => 'MediaWiki default',
180 'reason' => wfMessage( 'softblockrangesreason', $ip )->plain(),
182 'systemBlock' => 'wgSoftBlockRanges',
186 // Filter out any duplicated blocks, e.g. from the cookie
187 $blocks = $this->getUniqueBlocks( $blocks );
190 if ( count( $blocks ) > 0 ) {
191 if ( count( $blocks ) === 1 ) {
192 $block = $blocks[ 0 ];
194 $block = new CompositeBlock( [
196 'byText' => 'MediaWiki default',
197 'reason' => wfMessage( 'blockedtext-composite-reason' )->plain(),
198 'originalBlocks' => $blocks,
203 Hooks
::run( 'GetUserBlock', [ clone $user, $ip, &$block ] );
209 * Given a list of blocks, return a list of unique blocks.
211 * This usually means that each block has a unique ID. For a block with ID null,
212 * if it's an autoblock, it will be filtered out if the parent block is present;
213 * if not, it is assumed to be a unique system block, and kept.
215 * @param AbstractBlock[] $blocks
216 * @return AbstractBlock[]
218 private function getUniqueBlocks( array $blocks ) {
220 $databaseBlocks = [];
222 foreach ( $blocks as $block ) {
223 if ( $block instanceof SystemBlock
) {
224 $systemBlocks[] = $block;
225 } elseif ( $block->getType() === DatabaseBlock
::TYPE_AUTO
) {
226 if ( !isset( $databaseBlocks[$block->getParentBlockId()] ) ) {
227 $databaseBlocks[$block->getParentBlockId()] = $block;
230 $databaseBlocks[$block->getId()] = $block;
234 return array_values( array_merge( $systemBlocks, $databaseBlocks ) );
238 * Try to load a block from an ID given in a cookie value. If the block is invalid
239 * doesn't exist, or the cookie value is malformed, remove the cookie.
241 * @param UserIdentity $user
242 * @param WebRequest $request
243 * @return DatabaseBlock|bool The block object, or false if none could be loaded.
245 private function getBlockFromCookieValue(
249 $cookieValue = $request->getCookie( 'BlockID' );
250 if ( is_null( $cookieValue ) ) {
254 $blockCookieId = $this->getIdFromCookieValue( $cookieValue );
255 if ( !is_null( $blockCookieId ) ) {
256 // TODO: remove dependency on DatabaseBlock
257 $block = DatabaseBlock
::newFromID( $blockCookieId );
259 $block instanceof DatabaseBlock
&&
260 $this->shouldApplyCookieBlock( $block, $user->isAnon() )
266 $this->clearBlockCookie( $request->response() );
272 * Check if the block loaded from the cookie should be applied.
274 * @param DatabaseBlock $block
275 * @param bool $isAnon The user is logged out
276 * @return bool The block sould be applied
278 private function shouldApplyCookieBlock( DatabaseBlock
$block, $isAnon ) {
279 if ( !$block->isExpired() ) {
280 switch ( $block->getType() ) {
281 case DatabaseBlock
::TYPE_IP
:
282 case DatabaseBlock
::TYPE_RANGE
:
283 // If block is type IP or IP range, load only
284 // if user is not logged in (T152462)
286 $this->options
->get( 'CookieSetOnIpBlock' );
287 case DatabaseBlock
::TYPE_USER
:
288 return $block->isAutoblocking() &&
289 $this->options
->get( 'CookieSetOnAutoblock' );
298 * Check if an IP address is in the local proxy list
303 private function isLocallyBlockedProxy( $ip ) {
304 $proxyList = $this->options
->get( 'ProxyList' );
309 if ( !is_array( $proxyList ) ) {
310 // Load values from the specified file
311 $proxyList = array_map( 'trim', file( $proxyList ) );
314 $proxyListIPSet = new IPSet( $proxyList );
315 return $proxyListIPSet->match( $ip );
319 * Whether the given IP is in a DNS blacklist.
321 * @param string $ip IP to check
322 * @param bool $checkWhitelist Whether to check the whitelist first
323 * @return bool True if blacklisted.
325 public function isDnsBlacklisted( $ip, $checkWhitelist = false ) {
326 if ( !$this->options
->get( 'EnableDnsBlacklist' ) ||
327 ( $checkWhitelist && in_array( $ip, $this->options
->get( 'ProxyWhitelist' ) ) )
332 return $this->inDnsBlacklist( $ip, $this->options
->get( 'DnsBlacklistUrls' ) );
336 * Whether the given IP is in a given DNS blacklist.
338 * @param string $ip IP to check
339 * @param array $bases Array of Strings: URL of the DNS blacklist
340 * @return bool True if blacklisted.
342 private function inDnsBlacklist( $ip, array $bases ) {
344 // @todo FIXME: IPv6 ??? (https://bugs.php.net/bug.php?id=33170)
345 if ( IP
::isIPv4( $ip ) ) {
346 // Reverse IP, T23255
347 $ipReversed = implode( '.', array_reverse( explode( '.', $ip ) ) );
349 foreach ( $bases as $base ) {
351 // If we have an access key, use that too (ProjectHoneypot, etc.)
353 if ( is_array( $base ) ) {
354 if ( count( $base ) >= 2 ) {
355 // Access key is 1, base URL is 0
356 $hostname = "{$base[1]}.$ipReversed.{$base[0]}";
358 $hostname = "$ipReversed.{$base[0]}";
360 $basename = $base[0];
362 $hostname = "$ipReversed.$base";
366 $ipList = $this->checkHost( $hostname );
371 "Hostname $hostname is {$ipList[0]}, it's a proxy says $basename!"
377 wfDebugLog( 'dnsblacklist', "Requested $hostname, not found in $basename." );
385 * Wrapper for mocking in tests.
387 * @param string $hostname DNSBL query
388 * @return string[]|bool IPv4 array, or false if the IP is not blacklisted
390 protected function checkHost( $hostname ) {
391 return gethostbynamel( $hostname );
395 * Set the 'BlockID' cookie depending on block type and user authentication status.
400 public function trackBlockWithCookie( User
$user ) {
401 $request = $user->getRequest();
402 if ( $request->getCookie( 'BlockID' ) !== null ) {
403 // User already has a block cookie
407 // Defer checks until the user has been fully loaded to avoid circular dependency
408 // of User on itself (T180050 and T226777)
409 DeferredUpdates
::addCallableUpdate(
410 function () use ( $user, $request ) {
411 $block = $user->getBlock();
412 $response = $request->response();
413 $isAnon = $user->isAnon();
416 if ( $block instanceof CompositeBlock
) {
417 // TODO: Improve on simply tracking the first trackable block (T225654)
418 foreach ( $block->getOriginalBlocks() as $originalBlock ) {
419 if ( $this->shouldTrackBlockWithCookie( $originalBlock, $isAnon ) ) {
420 $this->setBlockCookie( $originalBlock, $response );
425 if ( $this->shouldTrackBlockWithCookie( $block, $isAnon ) ) {
426 $this->setBlockCookie( $block, $response );
431 DeferredUpdates
::PRESEND
436 * Set the 'BlockID' cookie to this block's ID and expiry time. The cookie's expiry will be
437 * the same as the block's, to a maximum of 24 hours.
440 * @internal Should be private.
441 * Left public for backwards compatibility, until DatabaseBlock::setCookie is removed.
442 * @param DatabaseBlock $block
443 * @param WebResponse $response The response on which to set the cookie.
445 public function setBlockCookie( DatabaseBlock
$block, WebResponse
$response ) {
446 // Calculate the default expiry time.
447 $maxExpiryTime = wfTimestamp( TS_MW
, wfTimestamp() +
( 24 * 60 * 60 ) );
449 // Use the block's expiry time only if it's less than the default.
450 $expiryTime = $block->getExpiry();
451 if ( $expiryTime === 'infinity' ||
$expiryTime > $maxExpiryTime ) {
452 $expiryTime = $maxExpiryTime;
455 // Set the cookie. Reformat the MediaWiki datetime as a Unix timestamp for the cookie.
456 $expiryValue = DateTime
::createFromFormat(
459 new DateTimeZone( 'UTC' )
461 $cookieOptions = [ 'httpOnly' => false ];
462 $cookieValue = $this->getCookieValue( $block );
463 $response->setCookie( 'BlockID', $cookieValue, $expiryValue, $cookieOptions );
467 * Check if the block should be tracked with a cookie.
469 * @param AbstractBlock $block
470 * @param bool $isAnon The user is logged out
471 * @return bool The block sould be tracked with a cookie
473 private function shouldTrackBlockWithCookie( AbstractBlock
$block, $isAnon ) {
474 if ( $block instanceof DatabaseBlock
) {
475 switch ( $block->getType() ) {
476 case DatabaseBlock
::TYPE_IP
:
477 case DatabaseBlock
::TYPE_RANGE
:
478 return $isAnon && $this->options
->get( 'CookieSetOnIpBlock' );
479 case DatabaseBlock
::TYPE_USER
:
481 $this->options
->get( 'CookieSetOnAutoblock' ) &&
482 $block->isAutoblocking();
491 * Unset the 'BlockID' cookie.
494 * @param WebResponse $response
496 public static function clearBlockCookie( WebResponse
$response ) {
497 $response->clearCookie( 'BlockID', [ 'httpOnly' => false ] );
501 * Get the stored ID from the 'BlockID' cookie. The cookie's value is usually a combination of
502 * the ID and a HMAC (see DatabaseBlock::setCookie), but will sometimes only be the ID.
505 * @internal Should be private.
506 * Left public for backwards compatibility, until DatabaseBlock::getIdFromCookieValue is removed.
507 * @param string $cookieValue The string in which to find the ID.
508 * @return int|null The block ID, or null if the HMAC is present and invalid.
510 public function getIdFromCookieValue( $cookieValue ) {
511 // The cookie value must start with a number
512 if ( !is_numeric( substr( $cookieValue, 0, 1 ) ) ) {
516 // Extract the ID prefix from the cookie value (may be the whole value, if no bang found).
517 $bangPos = strpos( $cookieValue, '!' );
518 $id = ( $bangPos === false ) ?
$cookieValue : substr( $cookieValue, 0, $bangPos );
519 if ( !$this->options
->get( 'SecretKey' ) ) {
520 // If there's no secret key, just use the ID as given.
523 $storedHmac = substr( $cookieValue, $bangPos +
1 );
524 $calculatedHmac = MWCryptHash
::hmac( $id, $this->options
->get( 'SecretKey' ), false );
525 if ( $calculatedHmac === $storedHmac ) {
533 * Get the BlockID cookie's value for this block. This is usually the block ID concatenated
534 * with an HMAC in order to avoid spoofing (T152951), but if wgSecretKey is not set will just
538 * @internal Should be private.
539 * Left public for backwards compatibility, until DatabaseBlock::getCookieValue is removed.
540 * @param DatabaseBlock $block
541 * @return string The block ID, probably concatenated with "!" and the HMAC.
543 public function getCookieValue( DatabaseBlock
$block ) {
544 $id = $block->getId();
545 if ( !$this->options
->get( 'SecretKey' ) ) {
546 // If there's no secret key, don't append a HMAC.
549 $hmac = MWCryptHash
::hmac( $id, $this->options
->get( 'SecretKey' ), false );
550 $cookieValue = $id . '!' . $hmac;