077bdca7c856ff67fd82a45111faadf31e580f9a
[lhc/web/wiklou.git] / includes / block / BlockManager.php
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\Block;
22
23 use DateTime;
24 use DateTimeZone;
25 use DeferredUpdates;
26 use Hooks;
27 use IP;
28 use MediaWiki\Config\ServiceOptions;
29 use MediaWiki\Permissions\PermissionManager;
30 use MediaWiki\User\UserIdentity;
31 use MWCryptHash;
32 use Psr\Log\LoggerInterface;
33 use User;
34 use WebRequest;
35 use WebResponse;
36 use Wikimedia\IPSet;
37
38 /**
39 * A service class for checking blocks.
40 * To obtain an instance, use MediaWikiServices::getInstance()->getBlockManager().
41 *
42 * @since 1.34 Refactored from User and Block.
43 */
44 class BlockManager {
45 /** @var PermissionManager */
46 private $permissionManager;
47
48 /** @var ServiceOptions */
49 private $options;
50
51 /**
52 * TODO Make this a const when HHVM support is dropped (T192166)
53 *
54 * @var array
55 * @since 1.34
56 */
57 public static $constructorOptions = [
58 'ApplyIpBlocksToXff',
59 'CookieSetOnAutoblock',
60 'CookieSetOnIpBlock',
61 'DnsBlacklistUrls',
62 'EnableDnsBlacklist',
63 'ProxyList',
64 'ProxyWhitelist',
65 'SecretKey',
66 'SoftBlockRanges',
67 ];
68
69 /** @var LoggerInterface */
70 private $logger;
71
72 /**
73 * @param ServiceOptions $options
74 * @param PermissionManager $permissionManager
75 * @param LoggerInterface $logger
76 */
77 public function __construct(
78 ServiceOptions $options,
79 PermissionManager $permissionManager,
80 LoggerInterface $logger
81 ) {
82 $options->assertRequiredOptions( self::$constructorOptions );
83 $this->options = $options;
84 $this->permissionManager = $permissionManager;
85 $this->logger = $logger;
86 }
87
88 /**
89 * Get the blocks that apply to a user. If there is only one, return that, otherwise
90 * return a composite block that combines the strictest features of the applicable
91 * blocks.
92 *
93 * Different blocks may be sought, depending on the user and their permissions. The
94 * user may be:
95 * (1) The global user (and can be affected by IP blocks). The global request object
96 * is needed for checking the IP address, the XFF header and the cookies.
97 * (2) The global user (and exempt from IP blocks). The global request object is
98 * needed for checking the cookies.
99 * (3) Another user (not the global user). No request object is available or needed;
100 * just look for a block against the user account.
101 *
102 * Cases #1 and #2 check whether the global user is blocked in practice; the block
103 * may due to their user account being blocked or to an IP address block or cookie
104 * block (or multiple of these). Case #3 simply checks whether a user's account is
105 * blocked, and does not determine whether the person using that account is affected
106 * in practice by any IP address or cookie blocks.
107 *
108 * @internal This should only be called by User::getBlockedStatus
109 * @param User $user
110 * @param WebRequest|null $request The global request object if the user is the
111 * global user (cases #1 and #2), otherwise null (case #3). The IP address and
112 * information from the request header are needed to find some types of blocks.
113 * @param bool $fromReplica Whether to check the replica DB first.
114 * To improve performance, non-critical checks are done against replica DBs.
115 * Check when actually saving should be done against master.
116 * @return AbstractBlock|null The most relevant block, or null if there is no block.
117 */
118 public function getUserBlock( User $user, $request, $fromReplica ) {
119 $fromMaster = !$fromReplica;
120 $ip = null;
121
122 // If this is the global user, they may be affected by IP blocks (case #1),
123 // or they may be exempt (case #2). If affected, look for additional blocks
124 // against the IP address.
125 $checkIpBlocks = $request &&
126 !$this->permissionManager->userHasRight( $user, 'ipblock-exempt' );
127
128 if ( $request && $checkIpBlocks ) {
129
130 // Case #1: checking the global user, including IP blocks
131 $ip = $request->getIP();
132 // TODO: remove dependency on DatabaseBlock (T221075)
133 $blocks = DatabaseBlock::newListFromTarget( $user, $ip, $fromMaster );
134 $this->getAdditionalIpBlocks( $blocks, $request, !$user->isRegistered(), $fromMaster );
135 $this->getCookieBlock( $blocks, $user, $request );
136
137 } elseif ( $request ) {
138
139 // Case #2: checking the global user, but they are exempt from IP blocks
140 // TODO: remove dependency on DatabaseBlock (T221075)
141 $blocks = DatabaseBlock::newListFromTarget( $user, null, $fromMaster );
142 $this->getCookieBlock( $blocks, $user, $request );
143
144 } else {
145
146 // Case #3: checking whether a user's account is blocked
147 // TODO: remove dependency on DatabaseBlock (T221075)
148 $blocks = DatabaseBlock::newListFromTarget( $user, null, $fromMaster );
149
150 }
151
152 // Filter out any duplicated blocks, e.g. from the cookie
153 $blocks = $this->getUniqueBlocks( $blocks );
154
155 $block = null;
156 if ( count( $blocks ) > 0 ) {
157 if ( count( $blocks ) === 1 ) {
158 $block = $blocks[ 0 ];
159 } else {
160 $block = new CompositeBlock( [
161 'address' => $ip,
162 'byText' => 'MediaWiki default',
163 'reason' => wfMessage( 'blockedtext-composite-reason' )->plain(),
164 'originalBlocks' => $blocks,
165 ] );
166 }
167 }
168
169 Hooks::run( 'GetUserBlock', [ clone $user, $ip, &$block ] );
170
171 return $block;
172 }
173
174 /**
175 * Get the cookie block, if there is one.
176 *
177 * @param AbstractBlock[] &$blocks
178 * @param UserIdentity $user
179 * @param WebRequest $request
180 * @return void
181 */
182 private function getCookieBlock( &$blocks, UserIdentity $user, WebRequest $request ) {
183 $cookieBlock = $this->getBlockFromCookieValue( $user, $request );
184 if ( $cookieBlock instanceof DatabaseBlock ) {
185 $blocks[] = $cookieBlock;
186 }
187 }
188
189 /**
190 * Check for any additional blocks against the IP address or any IPs in the XFF header.
191 *
192 * @param AbstractBlock[] &$blocks Blocks found so far
193 * @param WebRequest $request
194 * @param bool $isAnon The user is logged out
195 * @param bool $fromMaster
196 * @return void
197 */
198 private function getAdditionalIpBlocks( &$blocks, WebRequest $request, $isAnon, $fromMaster ) {
199 $ip = $request->getIP();
200
201 // Proxy blocking
202 if ( !in_array( $ip, $this->options->get( 'ProxyWhitelist' ) ) ) {
203 // Local list
204 if ( $this->isLocallyBlockedProxy( $ip ) ) {
205 $blocks[] = new SystemBlock( [
206 'byText' => wfMessage( 'proxyblocker' )->text(),
207 'reason' => wfMessage( 'proxyblockreason' )->plain(),
208 'address' => $ip,
209 'systemBlock' => 'proxy',
210 ] );
211 } elseif ( $isAnon && $this->isDnsBlacklisted( $ip ) ) {
212 $blocks[] = new SystemBlock( [
213 'byText' => wfMessage( 'sorbs' )->text(),
214 'reason' => wfMessage( 'sorbsreason' )->plain(),
215 'address' => $ip,
216 'systemBlock' => 'dnsbl',
217 ] );
218 }
219 }
220
221 // Soft blocking
222 if ( $isAnon && IP::isInRanges( $ip, $this->options->get( 'SoftBlockRanges' ) ) ) {
223 $blocks[] = new SystemBlock( [
224 'address' => $ip,
225 'byText' => 'MediaWiki default',
226 'reason' => wfMessage( 'softblockrangesreason', $ip )->plain(),
227 'anonOnly' => true,
228 'systemBlock' => 'wgSoftBlockRanges',
229 ] );
230 }
231
232 // (T25343) Apply IP blocks to the contents of XFF headers, if enabled
233 if ( $this->options->get( 'ApplyIpBlocksToXff' )
234 && !in_array( $ip, $this->options->get( 'ProxyWhitelist' ) )
235 ) {
236 $xff = $request->getHeader( 'X-Forwarded-For' );
237 $xff = array_map( 'trim', explode( ',', $xff ) );
238 $xff = array_diff( $xff, [ $ip ] );
239 // TODO: remove dependency on DatabaseBlock (T221075)
240 $xffblocks = DatabaseBlock::getBlocksForIPList( $xff, $isAnon, $fromMaster );
241 $blocks = array_merge( $blocks, $xffblocks );
242 }
243 }
244
245 /**
246 * Given a list of blocks, return a list of unique blocks.
247 *
248 * This usually means that each block has a unique ID. For a block with ID null,
249 * if it's an autoblock, it will be filtered out if the parent block is present;
250 * if not, it is assumed to be a unique system block, and kept.
251 *
252 * @param AbstractBlock[] $blocks
253 * @return AbstractBlock[]
254 */
255 private function getUniqueBlocks( array $blocks ) {
256 $systemBlocks = [];
257 $databaseBlocks = [];
258
259 foreach ( $blocks as $block ) {
260 if ( $block instanceof SystemBlock ) {
261 $systemBlocks[] = $block;
262 } elseif ( $block->getType() === DatabaseBlock::TYPE_AUTO ) {
263 /** @var DatabaseBlock $block */
264 '@phan-var DatabaseBlock $block';
265 if ( !isset( $databaseBlocks[$block->getParentBlockId()] ) ) {
266 $databaseBlocks[$block->getParentBlockId()] = $block;
267 }
268 } else {
269 $databaseBlocks[$block->getId()] = $block;
270 }
271 }
272
273 return array_values( array_merge( $systemBlocks, $databaseBlocks ) );
274 }
275
276 /**
277 * Try to load a block from an ID given in a cookie value. If the block is invalid
278 * doesn't exist, or the cookie value is malformed, remove the cookie.
279 *
280 * @param UserIdentity $user
281 * @param WebRequest $request
282 * @return DatabaseBlock|bool The block object, or false if none could be loaded.
283 */
284 private function getBlockFromCookieValue(
285 UserIdentity $user,
286 WebRequest $request
287 ) {
288 $cookieValue = $request->getCookie( 'BlockID' );
289 if ( is_null( $cookieValue ) ) {
290 return false;
291 }
292
293 $blockCookieId = $this->getIdFromCookieValue( $cookieValue );
294 if ( !is_null( $blockCookieId ) ) {
295 // TODO: remove dependency on DatabaseBlock (T221075)
296 $block = DatabaseBlock::newFromID( $blockCookieId );
297 if (
298 $block instanceof DatabaseBlock &&
299 $this->shouldApplyCookieBlock( $block, !$user->isRegistered() )
300 ) {
301 return $block;
302 }
303 }
304
305 $this->clearBlockCookie( $request->response() );
306
307 return false;
308 }
309
310 /**
311 * Check if the block loaded from the cookie should be applied.
312 *
313 * @param DatabaseBlock $block
314 * @param bool $isAnon The user is logged out
315 * @return bool The block sould be applied
316 */
317 private function shouldApplyCookieBlock( DatabaseBlock $block, $isAnon ) {
318 if ( !$block->isExpired() ) {
319 switch ( $block->getType() ) {
320 case DatabaseBlock::TYPE_IP:
321 case DatabaseBlock::TYPE_RANGE:
322 // If block is type IP or IP range, load only
323 // if user is not logged in (T152462)
324 return $isAnon &&
325 $this->options->get( 'CookieSetOnIpBlock' );
326 case DatabaseBlock::TYPE_USER:
327 return $block->isAutoblocking() &&
328 $this->options->get( 'CookieSetOnAutoblock' );
329 default:
330 return false;
331 }
332 }
333 return false;
334 }
335
336 /**
337 * Check if an IP address is in the local proxy list
338 *
339 * @param string $ip
340 * @return bool
341 */
342 private function isLocallyBlockedProxy( $ip ) {
343 $proxyList = $this->options->get( 'ProxyList' );
344 if ( !$proxyList ) {
345 return false;
346 }
347
348 if ( !is_array( $proxyList ) ) {
349 // Load values from the specified file
350 $proxyList = array_map( 'trim', file( $proxyList ) );
351 }
352
353 $proxyListIPSet = new IPSet( $proxyList );
354 return $proxyListIPSet->match( $ip );
355 }
356
357 /**
358 * Whether the given IP is in a DNS blacklist.
359 *
360 * @param string $ip IP to check
361 * @param bool $checkWhitelist Whether to check the whitelist first
362 * @return bool True if blacklisted.
363 */
364 public function isDnsBlacklisted( $ip, $checkWhitelist = false ) {
365 if ( !$this->options->get( 'EnableDnsBlacklist' ) ||
366 ( $checkWhitelist && in_array( $ip, $this->options->get( 'ProxyWhitelist' ) ) )
367 ) {
368 return false;
369 }
370
371 return $this->inDnsBlacklist( $ip, $this->options->get( 'DnsBlacklistUrls' ) );
372 }
373
374 /**
375 * Whether the given IP is in a given DNS blacklist.
376 *
377 * @param string $ip IP to check
378 * @param array $bases Array of Strings: URL of the DNS blacklist
379 * @return bool True if blacklisted.
380 */
381 private function inDnsBlacklist( $ip, array $bases ) {
382 $found = false;
383 // @todo FIXME: IPv6 ??? (https://bugs.php.net/bug.php?id=33170)
384 if ( IP::isIPv4( $ip ) ) {
385 // Reverse IP, T23255
386 $ipReversed = implode( '.', array_reverse( explode( '.', $ip ) ) );
387
388 foreach ( $bases as $base ) {
389 // Make hostname
390 // If we have an access key, use that too (ProjectHoneypot, etc.)
391 $basename = $base;
392 if ( is_array( $base ) ) {
393 if ( count( $base ) >= 2 ) {
394 // Access key is 1, base URL is 0
395 $hostname = "{$base[1]}.$ipReversed.{$base[0]}";
396 } else {
397 $hostname = "$ipReversed.{$base[0]}";
398 }
399 $basename = $base[0];
400 } else {
401 $hostname = "$ipReversed.$base";
402 }
403
404 // Send query
405 $ipList = $this->checkHost( $hostname );
406
407 if ( $ipList ) {
408 $this->logger->info(
409 "Hostname $hostname is {$ipList[0]}, it's a proxy says $basename!"
410 );
411 $found = true;
412 break;
413 }
414
415 $this->logger->debug( "Requested $hostname, not found in $basename." );
416 }
417 }
418
419 return $found;
420 }
421
422 /**
423 * Wrapper for mocking in tests.
424 *
425 * @param string $hostname DNSBL query
426 * @return string[]|bool IPv4 array, or false if the IP is not blacklisted
427 */
428 protected function checkHost( $hostname ) {
429 return gethostbynamel( $hostname );
430 }
431
432 /**
433 * Set the 'BlockID' cookie depending on block type and user authentication status.
434 *
435 * @since 1.34
436 * @param User $user
437 */
438 public function trackBlockWithCookie( User $user ) {
439 $request = $user->getRequest();
440 if ( $request->getCookie( 'BlockID' ) !== null ) {
441 // User already has a block cookie
442 return;
443 }
444
445 // Defer checks until the user has been fully loaded to avoid circular dependency
446 // of User on itself (T180050 and T226777)
447 DeferredUpdates::addCallableUpdate(
448 function () use ( $user, $request ) {
449 $block = $user->getBlock();
450 $response = $request->response();
451 $isAnon = $user->isAnon();
452
453 if ( $block ) {
454 if ( $block instanceof CompositeBlock ) {
455 // TODO: Improve on simply tracking the first trackable block (T225654)
456 foreach ( $block->getOriginalBlocks() as $originalBlock ) {
457 if ( $this->shouldTrackBlockWithCookie( $originalBlock, $isAnon ) ) {
458 '@phan-var DatabaseBlock $originalBlock';
459 $this->setBlockCookie( $originalBlock, $response );
460 return;
461 }
462 }
463 } else {
464 if ( $this->shouldTrackBlockWithCookie( $block, $isAnon ) ) {
465 '@phan-var DatabaseBlock $block';
466 $this->setBlockCookie( $block, $response );
467 }
468 }
469 }
470 },
471 DeferredUpdates::PRESEND
472 );
473 }
474
475 /**
476 * Set the 'BlockID' cookie to this block's ID and expiry time. The cookie's expiry will be
477 * the same as the block's, to a maximum of 24 hours.
478 *
479 * @since 1.34
480 * @internal Should be private.
481 * Left public for backwards compatibility, until DatabaseBlock::setCookie is removed.
482 * @param DatabaseBlock $block
483 * @param WebResponse $response The response on which to set the cookie.
484 */
485 public function setBlockCookie( DatabaseBlock $block, WebResponse $response ) {
486 // Calculate the default expiry time.
487 $maxExpiryTime = wfTimestamp( TS_MW, wfTimestamp() + ( 24 * 60 * 60 ) );
488
489 // Use the block's expiry time only if it's less than the default.
490 $expiryTime = $block->getExpiry();
491 if ( $expiryTime === 'infinity' || $expiryTime > $maxExpiryTime ) {
492 $expiryTime = $maxExpiryTime;
493 }
494
495 // Set the cookie. Reformat the MediaWiki datetime as a Unix timestamp for the cookie.
496 $expiryValue = DateTime::createFromFormat(
497 'YmdHis',
498 $expiryTime,
499 new DateTimeZone( 'UTC' )
500 )->format( 'U' );
501 $cookieOptions = [ 'httpOnly' => false ];
502 $cookieValue = $this->getCookieValue( $block );
503 $response->setCookie( 'BlockID', $cookieValue, $expiryValue, $cookieOptions );
504 }
505
506 /**
507 * Check if the block should be tracked with a cookie.
508 *
509 * @param AbstractBlock $block
510 * @param bool $isAnon The user is logged out
511 * @return bool The block sould be tracked with a cookie
512 */
513 private function shouldTrackBlockWithCookie( AbstractBlock $block, $isAnon ) {
514 if ( $block instanceof DatabaseBlock ) {
515 switch ( $block->getType() ) {
516 case DatabaseBlock::TYPE_IP:
517 case DatabaseBlock::TYPE_RANGE:
518 return $isAnon && $this->options->get( 'CookieSetOnIpBlock' );
519 case DatabaseBlock::TYPE_USER:
520 return !$isAnon &&
521 $this->options->get( 'CookieSetOnAutoblock' ) &&
522 $block->isAutoblocking();
523 default:
524 return false;
525 }
526 }
527 return false;
528 }
529
530 /**
531 * Unset the 'BlockID' cookie.
532 *
533 * @since 1.34
534 * @param WebResponse $response
535 */
536 public static function clearBlockCookie( WebResponse $response ) {
537 $response->clearCookie( 'BlockID', [ 'httpOnly' => false ] );
538 }
539
540 /**
541 * Get the stored ID from the 'BlockID' cookie. The cookie's value is usually a combination of
542 * the ID and a HMAC (see DatabaseBlock::setCookie), but will sometimes only be the ID.
543 *
544 * @since 1.34
545 * @internal Should be private.
546 * Left public for backwards compatibility, until DatabaseBlock::getIdFromCookieValue is removed.
547 * @param string $cookieValue The string in which to find the ID.
548 * @return int|null The block ID, or null if the HMAC is present and invalid.
549 */
550 public function getIdFromCookieValue( $cookieValue ) {
551 // The cookie value must start with a number
552 if ( !is_numeric( substr( $cookieValue, 0, 1 ) ) ) {
553 return null;
554 }
555
556 // Extract the ID prefix from the cookie value (may be the whole value, if no bang found).
557 $bangPos = strpos( $cookieValue, '!' );
558 $id = ( $bangPos === false ) ? $cookieValue : substr( $cookieValue, 0, $bangPos );
559 if ( !$this->options->get( 'SecretKey' ) ) {
560 // If there's no secret key, just use the ID as given.
561 return $id;
562 }
563 $storedHmac = substr( $cookieValue, $bangPos + 1 );
564 $calculatedHmac = MWCryptHash::hmac( $id, $this->options->get( 'SecretKey' ), false );
565 if ( $calculatedHmac === $storedHmac ) {
566 return $id;
567 } else {
568 return null;
569 }
570 }
571
572 /**
573 * Get the BlockID cookie's value for this block. This is usually the block ID concatenated
574 * with an HMAC in order to avoid spoofing (T152951), but if wgSecretKey is not set will just
575 * be the block ID.
576 *
577 * @since 1.34
578 * @internal Should be private.
579 * Left public for backwards compatibility, until DatabaseBlock::getCookieValue is removed.
580 * @param DatabaseBlock $block
581 * @return string The block ID, probably concatenated with "!" and the HMAC.
582 */
583 public function getCookieValue( DatabaseBlock $block ) {
584 $id = $block->getId();
585 if ( !$this->options->get( 'SecretKey' ) ) {
586 // If there's no secret key, don't append a HMAC.
587 return $id;
588 }
589 $hmac = MWCryptHash::hmac( $id, $this->options->get( 'SecretKey' ), false );
590 $cookieValue = $id . '!' . $hmac;
591 return $cookieValue;
592 }
593
594 }