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
;
25 use MediaWiki\User\UserIdentity
;
33 * A service class for checking blocks.
34 * To obtain an instance, use MediaWikiServices::getInstance()->getBlockManager().
36 * @since 1.34 Refactored from User and Block.
39 // TODO: This should be UserIdentity instead of User
43 /** @var WebRequest */
44 private $currentRequest;
47 private $applyIpBlocksToXff;
50 private $cookieSetOnAutoblock;
53 private $cookieSetOnIpBlock;
56 private $dnsBlacklistUrls;
59 private $enableDnsBlacklist;
65 private $proxyWhitelist;
67 /** @var string|bool */
71 private $softBlockRanges;
74 * @param User $currentUser
75 * @param WebRequest $currentRequest
76 * @param bool $applyIpBlocksToXff
77 * @param bool $cookieSetOnAutoblock
78 * @param bool $cookieSetOnIpBlock
79 * @param array $dnsBlacklistUrls
80 * @param bool $enableDnsBlacklist
81 * @param array $proxyList
82 * @param array $proxyWhitelist
83 * @param string $secretKey
84 * @param array $softBlockRanges
86 public function __construct(
90 $cookieSetOnAutoblock,
99 $this->currentUser
= $currentUser;
100 $this->currentRequest
= $currentRequest;
101 $this->applyIpBlocksToXff
= $applyIpBlocksToXff;
102 $this->cookieSetOnAutoblock
= $cookieSetOnAutoblock;
103 $this->cookieSetOnIpBlock
= $cookieSetOnIpBlock;
104 $this->dnsBlacklistUrls
= $dnsBlacklistUrls;
105 $this->enableDnsBlacklist
= $enableDnsBlacklist;
106 $this->proxyList
= $proxyList;
107 $this->proxyWhitelist
= $proxyWhitelist;
108 $this->secretKey
= $secretKey;
109 $this->softBlockRanges
= $softBlockRanges;
113 * Get the blocks that apply to a user. If there is only one, return that, otherwise
114 * return a composite block that combines the strictest features of the applicable
117 * TODO: $user should be UserIdentity instead of User
119 * @internal This should only be called by User::getBlockedStatus
121 * @param bool $fromReplica Whether to check the replica DB first.
122 * To improve performance, non-critical checks are done against replica DBs.
123 * Check when actually saving should be done against master.
124 * @return AbstractBlock|null The most relevant block, or null if there is no block.
126 public function getUserBlock( User
$user, $fromReplica ) {
127 $isAnon = $user->getId() === 0;
129 // TODO: If $user is the current user, we should use the current request. Otherwise,
130 // we should not look for XFF or cookie blocks.
131 $request = $user->getRequest();
133 # We only need to worry about passing the IP address to the block generator if the
134 # user is not immune to autoblocks/hardblocks, and they are the current user so we
135 # know which IP address they're actually coming from
137 $sessionUser = $this->currentUser
;
138 // the session user is set up towards the end of Setup.php. Until then,
139 // assume it's a logged-out user.
140 $globalUserName = $sessionUser->isSafeToLoad()
141 ?
$sessionUser->getName()
142 : IP
::sanitizeIP( $this->currentRequest
->getIP() );
143 if ( $user->getName() === $globalUserName && !$user->isAllowed( 'ipblock-exempt' ) ) {
144 $ip = $this->currentRequest
->getIP();
148 // After this, $blocks is an array of blocks or an empty array
149 // TODO: remove dependency on DatabaseBlock
150 $blocks = DatabaseBlock
::newListFromTarget( $user, $ip, !$fromReplica );
153 $cookieBlock = $this->getBlockFromCookieValue( $user, $request );
154 if ( $cookieBlock instanceof AbstractBlock
) {
155 $blocks[] = $cookieBlock;
159 if ( $ip !== null && !in_array( $ip, $this->proxyWhitelist
) ) {
161 if ( $this->isLocallyBlockedProxy( $ip ) ) {
162 $blocks[] = new SystemBlock( [
163 'byText' => wfMessage( 'proxyblocker' )->text(),
164 'reason' => wfMessage( 'proxyblockreason' )->plain(),
166 'systemBlock' => 'proxy',
168 } elseif ( $isAnon && $this->isDnsBlacklisted( $ip ) ) {
169 $blocks[] = new SystemBlock( [
170 'byText' => wfMessage( 'sorbs' )->text(),
171 'reason' => wfMessage( 'sorbsreason' )->plain(),
173 'systemBlock' => 'dnsbl',
178 // (T25343) Apply IP blocks to the contents of XFF headers, if enabled
179 if ( $this->applyIpBlocksToXff
181 && !in_array( $ip, $this->proxyWhitelist
)
183 $xff = $request->getHeader( 'X-Forwarded-For' );
184 $xff = array_map( 'trim', explode( ',', $xff ) );
185 $xff = array_diff( $xff, [ $ip ] );
186 // TODO: remove dependency on DatabaseBlock
187 $xffblocks = DatabaseBlock
::getBlocksForIPList( $xff, $isAnon, !$fromReplica );
188 $blocks = array_merge( $blocks, $xffblocks );
194 && IP
::isInRanges( $ip, $this->softBlockRanges
)
196 $blocks[] = new SystemBlock( [
198 'byText' => 'MediaWiki default',
199 'reason' => wfMessage( 'softblockrangesreason', $ip )->plain(),
201 'systemBlock' => 'wgSoftBlockRanges',
205 // Filter out any duplicated blocks, e.g. from the cookie
206 $blocks = $this->getUniqueBlocks( $blocks );
208 if ( count( $blocks ) > 0 ) {
209 if ( count( $blocks ) === 1 ) {
210 $block = $blocks[ 0 ];
212 $block = new CompositeBlock( [
214 'byText' => 'MediaWiki default',
215 'reason' => wfMessage( 'blockedtext-composite-reason' )->plain(),
216 'originalBlocks' => $blocks,
226 * Given a list of blocks, return a list blocks where each block either has a
227 * unique ID or has ID null.
229 * @param AbstractBlock[] $blocks
230 * @return AbstractBlock[]
232 private function getUniqueBlocks( $blocks ) {
235 foreach ( $blocks as $block ) {
236 $id = $block->getId();
237 if ( $id === null ) {
238 $uniqueBlocks[] = $block;
239 } elseif ( !isset( $blockIds[$id] ) ) {
240 $uniqueBlocks[] = $block;
241 $blockIds[$block->getId()] = true;
244 return $uniqueBlocks;
248 * Try to load a block from an ID given in a cookie value.
250 * @param UserIdentity $user
251 * @param WebRequest $request
252 * @return DatabaseBlock|bool The block object, or false if none could be loaded.
254 private function getBlockFromCookieValue(
258 $blockCookieVal = $request->getCookie( 'BlockID' );
259 $response = $request->response();
261 // Make sure there's something to check. The cookie value must start with a number.
262 if ( strlen( $blockCookieVal ) < 1 ||
!is_numeric( substr( $blockCookieVal, 0, 1 ) ) ) {
265 // Load the block from the ID in the cookie.
266 $blockCookieId = $this->getIdFromCookieValue( $blockCookieVal );
267 if ( $blockCookieId !== null ) {
268 // An ID was found in the cookie.
269 // TODO: remove dependency on DatabaseBlock
270 $tmpBlock = DatabaseBlock
::newFromID( $blockCookieId );
271 if ( $tmpBlock instanceof DatabaseBlock
) {
272 switch ( $tmpBlock->getType() ) {
273 case DatabaseBlock
::TYPE_USER
:
274 $blockIsValid = !$tmpBlock->isExpired() && $tmpBlock->isAutoblocking();
275 $useBlockCookie = ( $this->cookieSetOnAutoblock
=== true );
277 case DatabaseBlock
::TYPE_IP
:
278 case DatabaseBlock
::TYPE_RANGE
:
279 // If block is type IP or IP range, load only if user is not logged in (T152462)
280 $blockIsValid = !$tmpBlock->isExpired() && $user->getId() === 0;
281 $useBlockCookie = ( $this->cookieSetOnIpBlock
=== true );
284 $blockIsValid = false;
285 $useBlockCookie = false;
288 if ( $blockIsValid && $useBlockCookie ) {
293 // If the block is invalid or doesn't exist, remove the cookie.
294 $this->clearBlockCookie( $response );
300 * Check if an IP address is in the local proxy list
305 private function isLocallyBlockedProxy( $ip ) {
306 if ( !$this->proxyList
) {
310 if ( !is_array( $this->proxyList
) ) {
311 // Load values from the specified file
312 $this->proxyList
= array_map( 'trim', file( $this->proxyList
) );
315 $resultProxyList = [];
316 $deprecatedIPEntries = [];
318 // backward compatibility: move all ip addresses in keys to values
319 foreach ( $this->proxyList
as $key => $value ) {
320 $keyIsIP = IP
::isIPAddress( $key );
321 $valueIsIP = IP
::isIPAddress( $value );
322 if ( $keyIsIP && !$valueIsIP ) {
323 $deprecatedIPEntries[] = $key;
324 $resultProxyList[] = $key;
325 } elseif ( $keyIsIP && $valueIsIP ) {
326 $deprecatedIPEntries[] = $key;
327 $resultProxyList[] = $key;
328 $resultProxyList[] = $value;
330 $resultProxyList[] = $value;
334 if ( $deprecatedIPEntries ) {
336 'IP addresses in the keys of $wgProxyList (found the following IP addresses in keys: ' .
337 implode( ', ', $deprecatedIPEntries ) . ', please move them to values)', '1.30' );
340 $proxyListIPSet = new IPSet( $resultProxyList );
341 return $proxyListIPSet->match( $ip );
345 * Whether the given IP is in a DNS blacklist.
347 * @param string $ip IP to check
348 * @param bool $checkWhitelist Whether to check the whitelist first
349 * @return bool True if blacklisted.
351 public function isDnsBlacklisted( $ip, $checkWhitelist = false ) {
352 if ( !$this->enableDnsBlacklist ||
353 ( $checkWhitelist && in_array( $ip, $this->proxyWhitelist
) )
358 return $this->inDnsBlacklist( $ip, $this->dnsBlacklistUrls
);
362 * Whether the given IP is in a given DNS blacklist.
364 * @param string $ip IP to check
365 * @param array $bases Array of Strings: URL of the DNS blacklist
366 * @return bool True if blacklisted.
368 private function inDnsBlacklist( $ip, array $bases ) {
370 // @todo FIXME: IPv6 ??? (https://bugs.php.net/bug.php?id=33170)
371 if ( IP
::isIPv4( $ip ) ) {
372 // Reverse IP, T23255
373 $ipReversed = implode( '.', array_reverse( explode( '.', $ip ) ) );
375 foreach ( $bases as $base ) {
377 // If we have an access key, use that too (ProjectHoneypot, etc.)
379 if ( is_array( $base ) ) {
380 if ( count( $base ) >= 2 ) {
381 // Access key is 1, base URL is 0
382 $hostname = "{$base[1]}.$ipReversed.{$base[0]}";
384 $hostname = "$ipReversed.{$base[0]}";
386 $basename = $base[0];
388 $hostname = "$ipReversed.$base";
392 $ipList = $this->checkHost( $hostname );
397 "Hostname $hostname is {$ipList[0]}, it's a proxy says $basename!"
403 wfDebugLog( 'dnsblacklist', "Requested $hostname, not found in $basename." );
411 * Wrapper for mocking in tests.
413 * @param string $hostname DNSBL query
414 * @return string[]|bool IPv4 array, or false if the IP is not blacklisted
416 protected function checkHost( $hostname ) {
417 return gethostbynamel( $hostname );
421 * Set the 'BlockID' cookie depending on block type and user authentication status.
426 public function trackBlockWithCookie( User
$user ) {
427 $block = $user->getBlock();
428 $request = $user->getRequest();
429 $response = $request->response();
430 $isAnon = $user->isAnon();
432 if ( $block && $request->getCookie( 'BlockID' ) === null ) {
433 if ( $block instanceof CompositeBlock
) {
434 // TODO: Improve on simply tracking the first trackable block (T225654)
435 foreach ( $block->getOriginalBlocks() as $originalBlock ) {
436 if ( $this->shouldTrackBlockWithCookie( $originalBlock, $isAnon ) ) {
437 $this->setBlockCookie( $originalBlock, $response );
442 if ( $this->shouldTrackBlockWithCookie( $block, $isAnon ) ) {
443 $this->setBlockCookie( $block, $response );
450 * Set the 'BlockID' cookie to this block's ID and expiry time. The cookie's expiry will be
451 * the same as the block's, to a maximum of 24 hours.
454 * @internal Should be private.
455 * Left public for backwards compatibility, until DatabaseBlock::setCookie is removed.
456 * @param DatabaseBlock $block
457 * @param WebResponse $response The response on which to set the cookie.
459 public function setBlockCookie( DatabaseBlock
$block, WebResponse
$response ) {
460 // Calculate the default expiry time.
461 $maxExpiryTime = wfTimestamp( TS_MW
, wfTimestamp() +
( 24 * 60 * 60 ) );
463 // Use the block's expiry time only if it's less than the default.
464 $expiryTime = $block->getExpiry();
465 if ( $expiryTime === 'infinity' ||
$expiryTime > $maxExpiryTime ) {
466 $expiryTime = $maxExpiryTime;
469 // Set the cookie. Reformat the MediaWiki datetime as a Unix timestamp for the cookie.
470 $expiryValue = DateTime
::createFromFormat( 'YmdHis', $expiryTime )->format( 'U' );
471 $cookieOptions = [ 'httpOnly' => false ];
472 $cookieValue = $this->getCookieValue( $block );
473 $response->setCookie( 'BlockID', $cookieValue, $expiryValue, $cookieOptions );
477 * Check if the block should be tracked with a cookie.
479 * @param AbstractBlock $block
480 * @param bool $isAnon The user is logged out
481 * @return bool The block sould be tracked with a cookie
483 private function shouldTrackBlockWithCookie( AbstractBlock
$block, $isAnon ) {
484 if ( $block instanceof DatabaseBlock
) {
485 switch ( $block->getType() ) {
486 case DatabaseBlock
::TYPE_IP
:
487 case DatabaseBlock
::TYPE_RANGE
:
488 return $isAnon && $this->cookieSetOnIpBlock
;
489 case DatabaseBlock
::TYPE_USER
:
490 return !$isAnon && $this->cookieSetOnAutoblock
&& $block->isAutoblocking();
499 * Unset the 'BlockID' cookie.
502 * @param WebResponse $response
504 public static function clearBlockCookie( WebResponse
$response ) {
505 $response->clearCookie( 'BlockID', [ 'httpOnly' => false ] );
509 * Get the stored ID from the 'BlockID' cookie. The cookie's value is usually a combination of
510 * the ID and a HMAC (see DatabaseBlock::setCookie), but will sometimes only be the ID.
513 * @internal Should be private.
514 * Left public for backwards compatibility, until DatabaseBlock::getIdFromCookieValue is removed.
515 * @param string $cookieValue The string in which to find the ID.
516 * @return int|null The block ID, or null if the HMAC is present and invalid.
518 public function getIdFromCookieValue( $cookieValue ) {
519 // Extract the ID prefix from the cookie value (may be the whole value, if no bang found).
520 $bangPos = strpos( $cookieValue, '!' );
521 $id = ( $bangPos === false ) ?
$cookieValue : substr( $cookieValue, 0, $bangPos );
522 if ( !$this->secretKey
) {
523 // If there's no secret key, just use the ID as given.
526 $storedHmac = substr( $cookieValue, $bangPos +
1 );
527 $calculatedHmac = MWCryptHash
::hmac( $id, $this->secretKey
, false );
528 if ( $calculatedHmac === $storedHmac ) {
536 * Get the BlockID cookie's value for this block. This is usually the block ID concatenated
537 * with an HMAC in order to avoid spoofing (T152951), but if wgSecretKey is not set will just
541 * @internal Should be private.
542 * Left public for backwards compatibility, until DatabaseBlock::getCookieValue is removed.
543 * @param DatabaseBlock $block
544 * @return string The block ID, probably concatenated with "!" and the HMAC.
546 public function getCookieValue( DatabaseBlock
$block ) {
547 $id = $block->getId();
548 if ( !$this->secretKey
) {
549 // If there's no secret key, don't append a HMAC.
552 $hmac = MWCryptHash
::hmac( $id, $this->secretKey
, false );
553 $cookieValue = $id . '!' . $hmac;