814171f5d155f64e34803de141a0640d8c7d3267
[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 DeferredUpdates;
25 use IP;
26 use MediaWiki\User\UserIdentity;
27 use MWCryptHash;
28 use User;
29 use WebRequest;
30 use WebResponse;
31 use Wikimedia\IPSet;
32
33 /**
34 * A service class for checking blocks.
35 * To obtain an instance, use MediaWikiServices::getInstance()->getBlockManager().
36 *
37 * @since 1.34 Refactored from User and Block.
38 */
39 class BlockManager {
40 // TODO: This should be UserIdentity instead of User
41 /** @var User */
42 private $currentUser;
43
44 /** @var WebRequest */
45 private $currentRequest;
46
47 /** @var bool */
48 private $applyIpBlocksToXff;
49
50 /** @var bool */
51 private $cookieSetOnAutoblock;
52
53 /** @var bool */
54 private $cookieSetOnIpBlock;
55
56 /** @var array */
57 private $dnsBlacklistUrls;
58
59 /** @var bool */
60 private $enableDnsBlacklist;
61
62 /** @var array */
63 private $proxyList;
64
65 /** @var array */
66 private $proxyWhitelist;
67
68 /** @var string|bool */
69 private $secretKey;
70
71 /** @var array */
72 private $softBlockRanges;
73
74 /**
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
86 */
87 public function __construct(
88 User $currentUser,
89 WebRequest $currentRequest,
90 $applyIpBlocksToXff,
91 $cookieSetOnAutoblock,
92 $cookieSetOnIpBlock,
93 array $dnsBlacklistUrls,
94 $enableDnsBlacklist,
95 array $proxyList,
96 array $proxyWhitelist,
97 $secretKey,
98 $softBlockRanges
99 ) {
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;
111 }
112
113 /**
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
116 * blocks.
117 *
118 * TODO: $user should be UserIdentity instead of User
119 *
120 * @internal This should only be called by User::getBlockedStatus
121 * @param User $user
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.
126 */
127 public function getUserBlock( User $user, $fromReplica ) {
128 $isAnon = $user->getId() === 0;
129
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();
133
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
137 $ip = null;
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();
146 }
147
148 // User/IP blocking
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 );
152
153 // Cookie blocking
154 $cookieBlock = $this->getBlockFromCookieValue( $user, $request );
155 if ( $cookieBlock instanceof AbstractBlock ) {
156 $blocks[] = $cookieBlock;
157 }
158
159 // Proxy blocking
160 if ( $ip !== null && !in_array( $ip, $this->proxyWhitelist ) ) {
161 // Local list
162 if ( $this->isLocallyBlockedProxy( $ip ) ) {
163 $blocks[] = new SystemBlock( [
164 'byText' => wfMessage( 'proxyblocker' )->text(),
165 'reason' => wfMessage( 'proxyblockreason' )->plain(),
166 'address' => $ip,
167 'systemBlock' => 'proxy',
168 ] );
169 } elseif ( $isAnon && $this->isDnsBlacklisted( $ip ) ) {
170 $blocks[] = new SystemBlock( [
171 'byText' => wfMessage( 'sorbs' )->text(),
172 'reason' => wfMessage( 'sorbsreason' )->plain(),
173 'address' => $ip,
174 'systemBlock' => 'dnsbl',
175 ] );
176 }
177 }
178
179 // (T25343) Apply IP blocks to the contents of XFF headers, if enabled
180 if ( $this->applyIpBlocksToXff
181 && $ip !== null
182 && !in_array( $ip, $this->proxyWhitelist )
183 ) {
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 );
190 }
191
192 // Soft blocking
193 if ( $ip !== null
194 && $isAnon
195 && IP::isInRanges( $ip, $this->softBlockRanges )
196 ) {
197 $blocks[] = new SystemBlock( [
198 'address' => $ip,
199 'byText' => 'MediaWiki default',
200 'reason' => wfMessage( 'softblockrangesreason', $ip )->plain(),
201 'anonOnly' => true,
202 'systemBlock' => 'wgSoftBlockRanges',
203 ] );
204 }
205
206 // Filter out any duplicated blocks, e.g. from the cookie
207 $blocks = $this->getUniqueBlocks( $blocks );
208
209 if ( count( $blocks ) > 0 ) {
210 if ( count( $blocks ) === 1 ) {
211 $block = $blocks[ 0 ];
212 } else {
213 $block = new CompositeBlock( [
214 'address' => $ip,
215 'byText' => 'MediaWiki default',
216 'reason' => wfMessage( 'blockedtext-composite-reason' )->plain(),
217 'originalBlocks' => $blocks,
218 ] );
219 }
220 return $block;
221 }
222
223 return null;
224 }
225
226 /**
227 * Given a list of blocks, return a list of unique blocks.
228 *
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.
232 *
233 * @param AbstractBlock[] $blocks
234 * @return AbstractBlock[]
235 */
236 private function getUniqueBlocks( array $blocks ) {
237 $systemBlocks = [];
238 $databaseBlocks = [];
239
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;
246 }
247 } else {
248 $databaseBlocks[$block->getId()] = $block;
249 }
250 }
251
252 return array_merge( $systemBlocks, $databaseBlocks );
253 }
254
255 /**
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.
258 *
259 * @param UserIdentity $user
260 * @param WebRequest $request
261 * @return DatabaseBlock|bool The block object, or false if none could be loaded.
262 */
263 private function getBlockFromCookieValue(
264 UserIdentity $user,
265 WebRequest $request
266 ) {
267 $blockCookieId = $this->getIdFromCookieValue( $request->getCookie( 'BlockID' ) );
268
269 if ( $blockCookieId !== null ) {
270 // TODO: remove dependency on DatabaseBlock
271 $block = DatabaseBlock::newFromID( $blockCookieId );
272 if (
273 $block instanceof DatabaseBlock &&
274 $this->shouldApplyCookieBlock( $block, $user->isAnon() )
275 ) {
276 return $block;
277 }
278 $this->clearBlockCookie( $request->response() );
279 }
280
281 return false;
282 }
283
284 /**
285 * Check if the block loaded from the cookie should be applied.
286 *
287 * @param DatabaseBlock $block
288 * @param bool $isAnon The user is logged out
289 * @return bool The block sould be applied
290 */
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();
301 default:
302 return false;
303 }
304 }
305 return false;
306 }
307
308 /**
309 * Check if an IP address is in the local proxy list
310 *
311 * @param string $ip
312 * @return bool
313 */
314 private function isLocallyBlockedProxy( $ip ) {
315 if ( !$this->proxyList ) {
316 return false;
317 }
318
319 if ( !is_array( $this->proxyList ) ) {
320 // Load values from the specified file
321 $this->proxyList = array_map( 'trim', file( $this->proxyList ) );
322 }
323
324 $resultProxyList = [];
325 $deprecatedIPEntries = [];
326
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;
338 } else {
339 $resultProxyList[] = $value;
340 }
341 }
342
343 if ( $deprecatedIPEntries ) {
344 wfDeprecated(
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' );
347 }
348
349 $proxyListIPSet = new IPSet( $resultProxyList );
350 return $proxyListIPSet->match( $ip );
351 }
352
353 /**
354 * Whether the given IP is in a DNS blacklist.
355 *
356 * @param string $ip IP to check
357 * @param bool $checkWhitelist Whether to check the whitelist first
358 * @return bool True if blacklisted.
359 */
360 public function isDnsBlacklisted( $ip, $checkWhitelist = false ) {
361 if ( !$this->enableDnsBlacklist ||
362 ( $checkWhitelist && in_array( $ip, $this->proxyWhitelist ) )
363 ) {
364 return false;
365 }
366
367 return $this->inDnsBlacklist( $ip, $this->dnsBlacklistUrls );
368 }
369
370 /**
371 * Whether the given IP is in a given DNS blacklist.
372 *
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.
376 */
377 private function inDnsBlacklist( $ip, array $bases ) {
378 $found = false;
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 ) ) );
383
384 foreach ( $bases as $base ) {
385 // Make hostname
386 // If we have an access key, use that too (ProjectHoneypot, etc.)
387 $basename = $base;
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]}";
392 } else {
393 $hostname = "$ipReversed.{$base[0]}";
394 }
395 $basename = $base[0];
396 } else {
397 $hostname = "$ipReversed.$base";
398 }
399
400 // Send query
401 $ipList = $this->checkHost( $hostname );
402
403 if ( $ipList ) {
404 wfDebugLog(
405 'dnsblacklist',
406 "Hostname $hostname is {$ipList[0]}, it's a proxy says $basename!"
407 );
408 $found = true;
409 break;
410 }
411
412 wfDebugLog( 'dnsblacklist', "Requested $hostname, not found in $basename." );
413 }
414 }
415
416 return $found;
417 }
418
419 /**
420 * Wrapper for mocking in tests.
421 *
422 * @param string $hostname DNSBL query
423 * @return string[]|bool IPv4 array, or false if the IP is not blacklisted
424 */
425 protected function checkHost( $hostname ) {
426 return gethostbynamel( $hostname );
427 }
428
429 /**
430 * Set the 'BlockID' cookie depending on block type and user authentication status.
431 *
432 * @since 1.34
433 * @param User $user
434 */
435 public function trackBlockWithCookie( User $user ) {
436 $request = $user->getRequest();
437 if ( $request->getCookie( 'BlockID' ) !== null ) {
438 // User already has a block cookie
439 return;
440 }
441
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();
449
450 if ( $block ) {
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 );
456 return;
457 }
458 }
459 } else {
460 if ( $this->shouldTrackBlockWithCookie( $block, $isAnon ) ) {
461 $this->setBlockCookie( $block, $response );
462 }
463 }
464 }
465 },
466 DeferredUpdates::PRESEND
467 );
468 }
469
470 /**
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.
473 *
474 * @since 1.34
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.
479 */
480 public function setBlockCookie( DatabaseBlock $block, WebResponse $response ) {
481 // Calculate the default expiry time.
482 $maxExpiryTime = wfTimestamp( TS_MW, wfTimestamp() + ( 24 * 60 * 60 ) );
483
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;
488 }
489
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 );
495 }
496
497 /**
498 * Check if the block should be tracked with a cookie.
499 *
500 * @param AbstractBlock $block
501 * @param bool $isAnon The user is logged out
502 * @return bool The block sould be tracked with a cookie
503 */
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();
512 default:
513 return false;
514 }
515 }
516 return false;
517 }
518
519 /**
520 * Unset the 'BlockID' cookie.
521 *
522 * @since 1.34
523 * @param WebResponse $response
524 */
525 public static function clearBlockCookie( WebResponse $response ) {
526 $response->clearCookie( 'BlockID', [ 'httpOnly' => false ] );
527 }
528
529 /**
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.
532 *
533 * @since 1.34
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.
538 */
539 public function getIdFromCookieValue( $cookieValue ) {
540 // The cookie value must start with a number
541 if ( !is_numeric( substr( $cookieValue, 0, 1 ) ) ) {
542 return null;
543 }
544
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.
550 return $id;
551 }
552 $storedHmac = substr( $cookieValue, $bangPos + 1 );
553 $calculatedHmac = MWCryptHash::hmac( $id, $this->secretKey, false );
554 if ( $calculatedHmac === $storedHmac ) {
555 return $id;
556 } else {
557 return null;
558 }
559 }
560
561 /**
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
564 * be the block ID.
565 *
566 * @since 1.34
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.
571 */
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.
576 return $id;
577 }
578 $hmac = MWCryptHash::hmac( $id, $this->secretKey, false );
579 $cookieValue = $id . '!' . $hmac;
580 return $cookieValue;
581 }
582
583 }