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