Fix use of GenderCache in ApiPageSet::processTitlesArray
[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 User;
33 use WebRequest;
34 use WebResponse;
35 use Wikimedia\IPSet;
36
37 /**
38 * A service class for checking blocks.
39 * To obtain an instance, use MediaWikiServices::getInstance()->getBlockManager().
40 *
41 * @since 1.34 Refactored from User and Block.
42 */
43 class BlockManager {
44 // TODO: This should be UserIdentity instead of User
45 /** @var User */
46 private $currentUser;
47
48 /** @var WebRequest */
49 private $currentRequest;
50
51 /** @var PermissionManager */
52 private $permissionManager;
53
54 /**
55 * TODO Make this a const when HHVM support is dropped (T192166)
56 *
57 * @var array
58 * @since 1.34
59 */
60 public static $constructorOptions = [
61 'ApplyIpBlocksToXff',
62 'CookieSetOnAutoblock',
63 'CookieSetOnIpBlock',
64 'DnsBlacklistUrls',
65 'EnableDnsBlacklist',
66 'ProxyList',
67 'ProxyWhitelist',
68 'SecretKey',
69 'SoftBlockRanges',
70 ];
71
72 /**
73 * @param ServiceOptions $options
74 * @param User $currentUser
75 * @param WebRequest $currentRequest
76 * @param PermissionManager $permissionManager
77 */
78 public function __construct(
79 ServiceOptions $options,
80 User $currentUser,
81 WebRequest $currentRequest,
82 PermissionManager $permissionManager
83 ) {
84 $options->assertRequiredOptions( self::$constructorOptions );
85 $this->options = $options;
86 $this->currentUser = $currentUser;
87 $this->currentRequest = $currentRequest;
88 $this->permissionManager = $permissionManager;
89 }
90
91 /**
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
94 * blocks.
95 *
96 * TODO: $user should be UserIdentity instead of User
97 *
98 * @internal This should only be called by User::getBlockedStatus
99 * @param User $user
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.
104 */
105 public function getUserBlock( User $user, $fromReplica ) {
106 $isAnon = $user->getId() === 0;
107 $fromMaster = !$fromReplica;
108
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();
112
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
116 $ip = null;
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();
126 }
127
128 // User/IP blocking
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 );
132
133 // Cookie blocking
134 $cookieBlock = $this->getBlockFromCookieValue( $user, $request );
135 if ( $cookieBlock instanceof AbstractBlock ) {
136 $blocks[] = $cookieBlock;
137 }
138
139 // Proxy blocking
140 if ( $ip !== null && !in_array( $ip, $this->options->get( 'ProxyWhitelist' ) ) ) {
141 // Local list
142 if ( $this->isLocallyBlockedProxy( $ip ) ) {
143 $blocks[] = new SystemBlock( [
144 'byText' => wfMessage( 'proxyblocker' )->text(),
145 'reason' => wfMessage( 'proxyblockreason' )->plain(),
146 'address' => $ip,
147 'systemBlock' => 'proxy',
148 ] );
149 } elseif ( $isAnon && $this->isDnsBlacklisted( $ip ) ) {
150 $blocks[] = new SystemBlock( [
151 'byText' => wfMessage( 'sorbs' )->text(),
152 'reason' => wfMessage( 'sorbsreason' )->plain(),
153 'address' => $ip,
154 'systemBlock' => 'dnsbl',
155 ] );
156 }
157 }
158
159 // (T25343) Apply IP blocks to the contents of XFF headers, if enabled
160 if ( $this->options->get( 'ApplyIpBlocksToXff' )
161 && $ip !== null
162 && !in_array( $ip, $this->options->get( 'ProxyWhitelist' ) )
163 ) {
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 );
170 }
171
172 // Soft blocking
173 if ( $ip !== null
174 && $isAnon
175 && IP::isInRanges( $ip, $this->options->get( 'SoftBlockRanges' ) )
176 ) {
177 $blocks[] = new SystemBlock( [
178 'address' => $ip,
179 'byText' => 'MediaWiki default',
180 'reason' => wfMessage( 'softblockrangesreason', $ip )->plain(),
181 'anonOnly' => true,
182 'systemBlock' => 'wgSoftBlockRanges',
183 ] );
184 }
185
186 // Filter out any duplicated blocks, e.g. from the cookie
187 $blocks = $this->getUniqueBlocks( $blocks );
188
189 $block = null;
190 if ( count( $blocks ) > 0 ) {
191 if ( count( $blocks ) === 1 ) {
192 $block = $blocks[ 0 ];
193 } else {
194 $block = new CompositeBlock( [
195 'address' => $ip,
196 'byText' => 'MediaWiki default',
197 'reason' => wfMessage( 'blockedtext-composite-reason' )->plain(),
198 'originalBlocks' => $blocks,
199 ] );
200 }
201 }
202
203 Hooks::run( 'GetUserBlock', [ clone $user, $ip, &$block ] );
204
205 return $block;
206 }
207
208 /**
209 * Given a list of blocks, return a list of unique blocks.
210 *
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.
214 *
215 * @param AbstractBlock[] $blocks
216 * @return AbstractBlock[]
217 */
218 private function getUniqueBlocks( array $blocks ) {
219 $systemBlocks = [];
220 $databaseBlocks = [];
221
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;
228 }
229 } else {
230 $databaseBlocks[$block->getId()] = $block;
231 }
232 }
233
234 return array_values( array_merge( $systemBlocks, $databaseBlocks ) );
235 }
236
237 /**
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.
240 *
241 * @param UserIdentity $user
242 * @param WebRequest $request
243 * @return DatabaseBlock|bool The block object, or false if none could be loaded.
244 */
245 private function getBlockFromCookieValue(
246 UserIdentity $user,
247 WebRequest $request
248 ) {
249 $cookieValue = $request->getCookie( 'BlockID' );
250 if ( is_null( $cookieValue ) ) {
251 return false;
252 }
253
254 $blockCookieId = $this->getIdFromCookieValue( $cookieValue );
255 if ( !is_null( $blockCookieId ) ) {
256 // TODO: remove dependency on DatabaseBlock
257 $block = DatabaseBlock::newFromID( $blockCookieId );
258 if (
259 $block instanceof DatabaseBlock &&
260 $this->shouldApplyCookieBlock( $block, !$user->isRegistered() )
261 ) {
262 return $block;
263 }
264 }
265
266 $this->clearBlockCookie( $request->response() );
267
268 return false;
269 }
270
271 /**
272 * Check if the block loaded from the cookie should be applied.
273 *
274 * @param DatabaseBlock $block
275 * @param bool $isAnon The user is logged out
276 * @return bool The block sould be applied
277 */
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)
285 return $isAnon &&
286 $this->options->get( 'CookieSetOnIpBlock' );
287 case DatabaseBlock::TYPE_USER:
288 return $block->isAutoblocking() &&
289 $this->options->get( 'CookieSetOnAutoblock' );
290 default:
291 return false;
292 }
293 }
294 return false;
295 }
296
297 /**
298 * Check if an IP address is in the local proxy list
299 *
300 * @param string $ip
301 * @return bool
302 */
303 private function isLocallyBlockedProxy( $ip ) {
304 $proxyList = $this->options->get( 'ProxyList' );
305 if ( !$proxyList ) {
306 return false;
307 }
308
309 if ( !is_array( $proxyList ) ) {
310 // Load values from the specified file
311 $proxyList = array_map( 'trim', file( $proxyList ) );
312 }
313
314 $proxyListIPSet = new IPSet( $proxyList );
315 return $proxyListIPSet->match( $ip );
316 }
317
318 /**
319 * Whether the given IP is in a DNS blacklist.
320 *
321 * @param string $ip IP to check
322 * @param bool $checkWhitelist Whether to check the whitelist first
323 * @return bool True if blacklisted.
324 */
325 public function isDnsBlacklisted( $ip, $checkWhitelist = false ) {
326 if ( !$this->options->get( 'EnableDnsBlacklist' ) ||
327 ( $checkWhitelist && in_array( $ip, $this->options->get( 'ProxyWhitelist' ) ) )
328 ) {
329 return false;
330 }
331
332 return $this->inDnsBlacklist( $ip, $this->options->get( 'DnsBlacklistUrls' ) );
333 }
334
335 /**
336 * Whether the given IP is in a given DNS blacklist.
337 *
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.
341 */
342 private function inDnsBlacklist( $ip, array $bases ) {
343 $found = false;
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 ) ) );
348
349 foreach ( $bases as $base ) {
350 // Make hostname
351 // If we have an access key, use that too (ProjectHoneypot, etc.)
352 $basename = $base;
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]}";
357 } else {
358 $hostname = "$ipReversed.{$base[0]}";
359 }
360 $basename = $base[0];
361 } else {
362 $hostname = "$ipReversed.$base";
363 }
364
365 // Send query
366 $ipList = $this->checkHost( $hostname );
367
368 if ( $ipList ) {
369 wfDebugLog(
370 'dnsblacklist',
371 "Hostname $hostname is {$ipList[0]}, it's a proxy says $basename!"
372 );
373 $found = true;
374 break;
375 }
376
377 wfDebugLog( 'dnsblacklist', "Requested $hostname, not found in $basename." );
378 }
379 }
380
381 return $found;
382 }
383
384 /**
385 * Wrapper for mocking in tests.
386 *
387 * @param string $hostname DNSBL query
388 * @return string[]|bool IPv4 array, or false if the IP is not blacklisted
389 */
390 protected function checkHost( $hostname ) {
391 return gethostbynamel( $hostname );
392 }
393
394 /**
395 * Set the 'BlockID' cookie depending on block type and user authentication status.
396 *
397 * @since 1.34
398 * @param User $user
399 */
400 public function trackBlockWithCookie( User $user ) {
401 $request = $user->getRequest();
402 if ( $request->getCookie( 'BlockID' ) !== null ) {
403 // User already has a block cookie
404 return;
405 }
406
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();
414
415 if ( $block ) {
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 );
421 return;
422 }
423 }
424 } else {
425 if ( $this->shouldTrackBlockWithCookie( $block, $isAnon ) ) {
426 $this->setBlockCookie( $block, $response );
427 }
428 }
429 }
430 },
431 DeferredUpdates::PRESEND
432 );
433 }
434
435 /**
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.
438 *
439 * @since 1.34
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.
444 */
445 public function setBlockCookie( DatabaseBlock $block, WebResponse $response ) {
446 // Calculate the default expiry time.
447 $maxExpiryTime = wfTimestamp( TS_MW, wfTimestamp() + ( 24 * 60 * 60 ) );
448
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;
453 }
454
455 // Set the cookie. Reformat the MediaWiki datetime as a Unix timestamp for the cookie.
456 $expiryValue = DateTime::createFromFormat(
457 'YmdHis',
458 $expiryTime,
459 new DateTimeZone( 'UTC' )
460 )->format( 'U' );
461 $cookieOptions = [ 'httpOnly' => false ];
462 $cookieValue = $this->getCookieValue( $block );
463 $response->setCookie( 'BlockID', $cookieValue, $expiryValue, $cookieOptions );
464 }
465
466 /**
467 * Check if the block should be tracked with a cookie.
468 *
469 * @param AbstractBlock $block
470 * @param bool $isAnon The user is logged out
471 * @return bool The block sould be tracked with a cookie
472 */
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:
480 return !$isAnon &&
481 $this->options->get( 'CookieSetOnAutoblock' ) &&
482 $block->isAutoblocking();
483 default:
484 return false;
485 }
486 }
487 return false;
488 }
489
490 /**
491 * Unset the 'BlockID' cookie.
492 *
493 * @since 1.34
494 * @param WebResponse $response
495 */
496 public static function clearBlockCookie( WebResponse $response ) {
497 $response->clearCookie( 'BlockID', [ 'httpOnly' => false ] );
498 }
499
500 /**
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.
503 *
504 * @since 1.34
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.
509 */
510 public function getIdFromCookieValue( $cookieValue ) {
511 // The cookie value must start with a number
512 if ( !is_numeric( substr( $cookieValue, 0, 1 ) ) ) {
513 return null;
514 }
515
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.
521 return $id;
522 }
523 $storedHmac = substr( $cookieValue, $bangPos + 1 );
524 $calculatedHmac = MWCryptHash::hmac( $id, $this->options->get( 'SecretKey' ), false );
525 if ( $calculatedHmac === $storedHmac ) {
526 return $id;
527 } else {
528 return null;
529 }
530 }
531
532 /**
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
535 * be the block ID.
536 *
537 * @since 1.34
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.
542 */
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.
547 return $id;
548 }
549 $hmac = MWCryptHash::hmac( $id, $this->options->get( 'SecretKey' ), false );
550 $cookieValue = $id . '!' . $hmac;
551 return $cookieValue;
552 }
553
554 }