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
;
26 use MediaWiki\User\UserIdentity
;
34 * A service class for checking blocks.
35 * To obtain an instance, use MediaWikiServices::getInstance()->getBlockManager().
37 * @since 1.34 Refactored from User and Block.
40 // TODO: This should be UserIdentity instead of User
44 /** @var WebRequest */
45 private $currentRequest;
48 private $applyIpBlocksToXff;
51 private $cookieSetOnAutoblock;
54 private $cookieSetOnIpBlock;
57 private $dnsBlacklistUrls;
60 private $enableDnsBlacklist;
66 private $proxyWhitelist;
68 /** @var string|bool */
72 private $softBlockRanges;
75 * @param User $currentUser
76 * @param WebRequest $currentRequest
77 * @param bool $applyIpBlocksToXff
78 * @param bool $cookieSetOnAutoblock
79 * @param bool $cookieSetOnIpBlock
80 * @param string[] $dnsBlacklistUrls
81 * @param bool $enableDnsBlacklist
82 * @param string[] $proxyList
83 * @param string[] $proxyWhitelist
84 * @param string $secretKey
85 * @param array $softBlockRanges
87 public function __construct(
89 WebRequest
$currentRequest,
91 $cookieSetOnAutoblock,
93 array $dnsBlacklistUrls,
96 array $proxyWhitelist,
100 $this->currentUser
= $currentUser;
101 $this->currentRequest
= $currentRequest;
102 $this->applyIpBlocksToXff
= $applyIpBlocksToXff;
103 $this->cookieSetOnAutoblock
= $cookieSetOnAutoblock;
104 $this->cookieSetOnIpBlock
= $cookieSetOnIpBlock;
105 $this->dnsBlacklistUrls
= $dnsBlacklistUrls;
106 $this->enableDnsBlacklist
= $enableDnsBlacklist;
107 $this->proxyList
= $proxyList;
108 $this->proxyWhitelist
= $proxyWhitelist;
109 $this->secretKey
= $secretKey;
110 $this->softBlockRanges
= $softBlockRanges;
114 * Get the blocks that apply to a user. If there is only one, return that, otherwise
115 * return a composite block that combines the strictest features of the applicable
118 * TODO: $user should be UserIdentity instead of User
120 * @internal This should only be called by User::getBlockedStatus
122 * @param bool $fromReplica Whether to check the replica DB first.
123 * To improve performance, non-critical checks are done against replica DBs.
124 * Check when actually saving should be done against master.
125 * @return AbstractBlock|null The most relevant block, or null if there is no block.
127 public function getUserBlock( User
$user, $fromReplica ) {
128 $isAnon = $user->getId() === 0;
130 // TODO: If $user is the current user, we should use the current request. Otherwise,
131 // we should not look for XFF or cookie blocks.
132 $request = $user->getRequest();
134 # We only need to worry about passing the IP address to the block generator if the
135 # user is not immune to autoblocks/hardblocks, and they are the current user so we
136 # know which IP address they're actually coming from
138 $sessionUser = $this->currentUser
;
139 // the session user is set up towards the end of Setup.php. Until then,
140 // assume it's a logged-out user.
141 $globalUserName = $sessionUser->isSafeToLoad()
142 ?
$sessionUser->getName()
143 : IP
::sanitizeIP( $this->currentRequest
->getIP() );
144 if ( $user->getName() === $globalUserName && !$user->isAllowed( 'ipblock-exempt' ) ) {
145 $ip = $this->currentRequest
->getIP();
149 // After this, $blocks is an array of blocks or an empty array
150 // TODO: remove dependency on DatabaseBlock
151 $blocks = DatabaseBlock
::newListFromTarget( $user, $ip, !$fromReplica );
154 $cookieBlock = $this->getBlockFromCookieValue( $user, $request );
155 if ( $cookieBlock instanceof AbstractBlock
) {
156 $blocks[] = $cookieBlock;
160 if ( $ip !== null && !in_array( $ip, $this->proxyWhitelist
) ) {
162 if ( $this->isLocallyBlockedProxy( $ip ) ) {
163 $blocks[] = new SystemBlock( [
164 'byText' => wfMessage( 'proxyblocker' )->text(),
165 'reason' => wfMessage( 'proxyblockreason' )->plain(),
167 'systemBlock' => 'proxy',
169 } elseif ( $isAnon && $this->isDnsBlacklisted( $ip ) ) {
170 $blocks[] = new SystemBlock( [
171 'byText' => wfMessage( 'sorbs' )->text(),
172 'reason' => wfMessage( 'sorbsreason' )->plain(),
174 'systemBlock' => 'dnsbl',
179 // (T25343) Apply IP blocks to the contents of XFF headers, if enabled
180 if ( $this->applyIpBlocksToXff
182 && !in_array( $ip, $this->proxyWhitelist
)
184 $xff = $request->getHeader( 'X-Forwarded-For' );
185 $xff = array_map( 'trim', explode( ',', $xff ) );
186 $xff = array_diff( $xff, [ $ip ] );
187 // TODO: remove dependency on DatabaseBlock
188 $xffblocks = DatabaseBlock
::getBlocksForIPList( $xff, $isAnon, !$fromReplica );
189 $blocks = array_merge( $blocks, $xffblocks );
195 && IP
::isInRanges( $ip, $this->softBlockRanges
)
197 $blocks[] = new SystemBlock( [
199 'byText' => 'MediaWiki default',
200 'reason' => wfMessage( 'softblockrangesreason', $ip )->plain(),
202 'systemBlock' => 'wgSoftBlockRanges',
206 // Filter out any duplicated blocks, e.g. from the cookie
207 $blocks = $this->getUniqueBlocks( $blocks );
209 if ( count( $blocks ) > 0 ) {
210 if ( count( $blocks ) === 1 ) {
211 $block = $blocks[ 0 ];
213 $block = new CompositeBlock( [
215 'byText' => 'MediaWiki default',
216 'reason' => wfMessage( 'blockedtext-composite-reason' )->plain(),
217 'originalBlocks' => $blocks,
227 * Given a list of blocks, return a list of unique blocks.
229 * This usually means that each block has a unique ID. For a block with ID null,
230 * if it's an autoblock, it will be filtered out if the parent block is present;
231 * if not, it is assumed to be a unique system block, and kept.
233 * @param AbstractBlock[] $blocks
234 * @return AbstractBlock[]
236 private function getUniqueBlocks( array $blocks ) {
238 $databaseBlocks = [];
240 foreach ( $blocks as $block ) {
241 if ( $block instanceof SystemBlock
) {
242 $systemBlocks[] = $block;
243 } elseif ( $block->getType() === DatabaseBlock
::TYPE_AUTO
) {
244 if ( !isset( $databaseBlocks[$block->getParentBlockId()] ) ) {
245 $databaseBlocks[$block->getParentBlockId()] = $block;
248 $databaseBlocks[$block->getId()] = $block;
252 return array_merge( $systemBlocks, $databaseBlocks );
256 * Try to load a block from an ID given in a cookie value. If the block is invalid
257 * or doesn't exist, remove the cookie.
259 * @param UserIdentity $user
260 * @param WebRequest $request
261 * @return DatabaseBlock|bool The block object, or false if none could be loaded.
263 private function getBlockFromCookieValue(
267 $blockCookieId = $this->getIdFromCookieValue( $request->getCookie( 'BlockID' ) );
269 if ( $blockCookieId !== null ) {
270 // TODO: remove dependency on DatabaseBlock
271 $block = DatabaseBlock
::newFromID( $blockCookieId );
273 $block instanceof DatabaseBlock
&&
274 $this->shouldApplyCookieBlock( $block, $user->isAnon() )
278 $this->clearBlockCookie( $request->response() );
285 * Check if the block loaded from the cookie should be applied.
287 * @param DatabaseBlock $block
288 * @param bool $isAnon The user is logged out
289 * @return bool The block sould be applied
291 private function shouldApplyCookieBlock( DatabaseBlock
$block, $isAnon ) {
292 if ( !$block->isExpired() ) {
293 switch ( $block->getType() ) {
294 case DatabaseBlock
::TYPE_IP
:
295 case DatabaseBlock
::TYPE_RANGE
:
296 // If block is type IP or IP range, load only
297 // if user is not logged in (T152462)
298 return $isAnon && $this->cookieSetOnIpBlock
;
299 case DatabaseBlock
::TYPE_USER
:
300 return $this->cookieSetOnAutoblock
&& $block->isAutoblocking();
309 * Check if an IP address is in the local proxy list
314 private function isLocallyBlockedProxy( $ip ) {
315 if ( !$this->proxyList
) {
319 if ( !is_array( $this->proxyList
) ) {
320 // Load values from the specified file
321 $this->proxyList
= array_map( 'trim', file( $this->proxyList
) );
324 $resultProxyList = [];
325 $deprecatedIPEntries = [];
327 // backward compatibility: move all ip addresses in keys to values
328 foreach ( $this->proxyList
as $key => $value ) {
329 $keyIsIP = IP
::isIPAddress( $key );
330 $valueIsIP = IP
::isIPAddress( $value );
331 if ( $keyIsIP && !$valueIsIP ) {
332 $deprecatedIPEntries[] = $key;
333 $resultProxyList[] = $key;
334 } elseif ( $keyIsIP && $valueIsIP ) {
335 $deprecatedIPEntries[] = $key;
336 $resultProxyList[] = $key;
337 $resultProxyList[] = $value;
339 $resultProxyList[] = $value;
343 if ( $deprecatedIPEntries ) {
345 'IP addresses in the keys of $wgProxyList (found the following IP addresses in keys: ' .
346 implode( ', ', $deprecatedIPEntries ) . ', please move them to values)', '1.30' );
349 $proxyListIPSet = new IPSet( $resultProxyList );
350 return $proxyListIPSet->match( $ip );
354 * Whether the given IP is in a DNS blacklist.
356 * @param string $ip IP to check
357 * @param bool $checkWhitelist Whether to check the whitelist first
358 * @return bool True if blacklisted.
360 public function isDnsBlacklisted( $ip, $checkWhitelist = false ) {
361 if ( !$this->enableDnsBlacklist ||
362 ( $checkWhitelist && in_array( $ip, $this->proxyWhitelist
) )
367 return $this->inDnsBlacklist( $ip, $this->dnsBlacklistUrls
);
371 * Whether the given IP is in a given DNS blacklist.
373 * @param string $ip IP to check
374 * @param array $bases Array of Strings: URL of the DNS blacklist
375 * @return bool True if blacklisted.
377 private function inDnsBlacklist( $ip, array $bases ) {
379 // @todo FIXME: IPv6 ??? (https://bugs.php.net/bug.php?id=33170)
380 if ( IP
::isIPv4( $ip ) ) {
381 // Reverse IP, T23255
382 $ipReversed = implode( '.', array_reverse( explode( '.', $ip ) ) );
384 foreach ( $bases as $base ) {
386 // If we have an access key, use that too (ProjectHoneypot, etc.)
388 if ( is_array( $base ) ) {
389 if ( count( $base ) >= 2 ) {
390 // Access key is 1, base URL is 0
391 $hostname = "{$base[1]}.$ipReversed.{$base[0]}";
393 $hostname = "$ipReversed.{$base[0]}";
395 $basename = $base[0];
397 $hostname = "$ipReversed.$base";
401 $ipList = $this->checkHost( $hostname );
406 "Hostname $hostname is {$ipList[0]}, it's a proxy says $basename!"
412 wfDebugLog( 'dnsblacklist', "Requested $hostname, not found in $basename." );
420 * Wrapper for mocking in tests.
422 * @param string $hostname DNSBL query
423 * @return string[]|bool IPv4 array, or false if the IP is not blacklisted
425 protected function checkHost( $hostname ) {
426 return gethostbynamel( $hostname );
430 * Set the 'BlockID' cookie depending on block type and user authentication status.
435 public function trackBlockWithCookie( User
$user ) {
436 $request = $user->getRequest();
437 if ( $request->getCookie( 'BlockID' ) !== null ) {
438 // User already has a block cookie
442 // Defer checks until the user has been fully loaded to avoid circular dependency
443 // of User on itself (T180050 and T226777)
444 DeferredUpdates
::addCallableUpdate(
445 function () use ( $user, $request ) {
446 $block = $user->getBlock();
447 $response = $request->response();
448 $isAnon = $user->isAnon();
451 if ( $block instanceof CompositeBlock
) {
452 // TODO: Improve on simply tracking the first trackable block (T225654)
453 foreach ( $block->getOriginalBlocks() as $originalBlock ) {
454 if ( $this->shouldTrackBlockWithCookie( $originalBlock, $isAnon ) ) {
455 $this->setBlockCookie( $originalBlock, $response );
460 if ( $this->shouldTrackBlockWithCookie( $block, $isAnon ) ) {
461 $this->setBlockCookie( $block, $response );
466 DeferredUpdates
::PRESEND
471 * Set the 'BlockID' cookie to this block's ID and expiry time. The cookie's expiry will be
472 * the same as the block's, to a maximum of 24 hours.
475 * @internal Should be private.
476 * Left public for backwards compatibility, until DatabaseBlock::setCookie is removed.
477 * @param DatabaseBlock $block
478 * @param WebResponse $response The response on which to set the cookie.
480 public function setBlockCookie( DatabaseBlock
$block, WebResponse
$response ) {
481 // Calculate the default expiry time.
482 $maxExpiryTime = wfTimestamp( TS_MW
, wfTimestamp() +
( 24 * 60 * 60 ) );
484 // Use the block's expiry time only if it's less than the default.
485 $expiryTime = $block->getExpiry();
486 if ( $expiryTime === 'infinity' ||
$expiryTime > $maxExpiryTime ) {
487 $expiryTime = $maxExpiryTime;
490 // Set the cookie. Reformat the MediaWiki datetime as a Unix timestamp for the cookie.
491 $expiryValue = DateTime
::createFromFormat( 'YmdHis', $expiryTime )->format( 'U' );
492 $cookieOptions = [ 'httpOnly' => false ];
493 $cookieValue = $this->getCookieValue( $block );
494 $response->setCookie( 'BlockID', $cookieValue, $expiryValue, $cookieOptions );
498 * Check if the block should be tracked with a cookie.
500 * @param AbstractBlock $block
501 * @param bool $isAnon The user is logged out
502 * @return bool The block sould be tracked with a cookie
504 private function shouldTrackBlockWithCookie( AbstractBlock
$block, $isAnon ) {
505 if ( $block instanceof DatabaseBlock
) {
506 switch ( $block->getType() ) {
507 case DatabaseBlock
::TYPE_IP
:
508 case DatabaseBlock
::TYPE_RANGE
:
509 return $isAnon && $this->cookieSetOnIpBlock
;
510 case DatabaseBlock
::TYPE_USER
:
511 return !$isAnon && $this->cookieSetOnAutoblock
&& $block->isAutoblocking();
520 * Unset the 'BlockID' cookie.
523 * @param WebResponse $response
525 public static function clearBlockCookie( WebResponse
$response ) {
526 $response->clearCookie( 'BlockID', [ 'httpOnly' => false ] );
530 * Get the stored ID from the 'BlockID' cookie. The cookie's value is usually a combination of
531 * the ID and a HMAC (see DatabaseBlock::setCookie), but will sometimes only be the ID.
534 * @internal Should be private.
535 * Left public for backwards compatibility, until DatabaseBlock::getIdFromCookieValue is removed.
536 * @param string $cookieValue The string in which to find the ID.
537 * @return int|null The block ID, or null if the HMAC is present and invalid.
539 public function getIdFromCookieValue( $cookieValue ) {
540 // The cookie value must start with a number
541 if ( !is_numeric( substr( $cookieValue, 0, 1 ) ) ) {
545 // Extract the ID prefix from the cookie value (may be the whole value, if no bang found).
546 $bangPos = strpos( $cookieValue, '!' );
547 $id = ( $bangPos === false ) ?
$cookieValue : substr( $cookieValue, 0, $bangPos );
548 if ( !$this->secretKey
) {
549 // If there's no secret key, just use the ID as given.
552 $storedHmac = substr( $cookieValue, $bangPos +
1 );
553 $calculatedHmac = MWCryptHash
::hmac( $id, $this->secretKey
, false );
554 if ( $calculatedHmac === $storedHmac ) {
562 * Get the BlockID cookie's value for this block. This is usually the block ID concatenated
563 * with an HMAC in order to avoid spoofing (T152951), but if wgSecretKey is not set will just
567 * @internal Should be private.
568 * Left public for backwards compatibility, until DatabaseBlock::getCookieValue is removed.
569 * @param DatabaseBlock $block
570 * @return string The block ID, probably concatenated with "!" and the HMAC.
572 public function getCookieValue( DatabaseBlock
$block ) {
573 $id = $block->getId();
574 if ( !$this->secretKey
) {
575 // If there's no secret key, don't append a HMAC.
578 $hmac = MWCryptHash
::hmac( $id, $this->secretKey
, false );
579 $cookieValue = $id . '!' . $hmac;