Debian Codegraph @

includes/session/SessionManager.php

raw symbols SCIP
1 <?php
2 /**
3 * MediaWiki\Session entry point
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Session
22 */
23
24 namespace MediaWiki\Session;
25
26 use InvalidArgumentException;
27 use LogicException;
28 use MediaWiki\Config\Config;
29 use MediaWiki\Context\RequestContext;
30 use MediaWiki\HookContainer\HookContainer;
31 use MediaWiki\HookContainer\HookRunner;
32 use MediaWiki\MainConfigNames;
33 use MediaWiki\MediaWikiServices;
34 use MediaWiki\Request\FauxRequest;
35 use MediaWiki\Request\WebRequest;
36 use MediaWiki\User\User;
37 use MediaWiki\User\UserNameUtils;
38 use MWException;
39 use Psr\Log\LoggerInterface;
40 use Psr\Log\LogLevel;
41 use Wikimedia\ObjectCache\BagOStuff;
42 use Wikimedia\ObjectCache\CachedBagOStuff;
43
44 /**
45 * This serves as the entry point to the MediaWiki session handling system.
46 *
47 * Most methods here are for internal use by session handling code. Other callers
48 * should only use getGlobalSession and the methods of SessionManagerInterface;
49 * the rest of the functionality is exposed via MediaWiki\Session\Session methods.
50 *
51 * To provide custom session handling, implement a MediaWiki\Session\SessionProvider.
52 *
53 * @anchor SessionManager-storage-expectations
54 *
55 * ## Storage expectations
56 *
57 * The SessionManager should be configured with a very fast storage system that is
58 * optimized for holding key-value pairs. It expects:
59 *
60 * - Low latencies. Session data is read or written to during nearly all web requests from
61 * people that have contributed to or otherwise engaged with the site, including those not
62 * logged in with a registered account.
63 *
64 * - Locally writable data. The data must be writable from both primary and secondary
65 * data centres.
66 *
67 * - Locally latest reads. Writes must by default be immediately consistent within
68 * the local data centre, and visible to other reads from web servers in that data centre.
69 *
70 * - Replication. The data must be eventually consistent across all data centres. Writes
71 * are either synced to all remote data centres, or locally overwritten by another write
72 * that is.
73 *
74 * The SessionManager uses `set()` and `delete()` for write operations, which should be
75 * synchronous in the local data centre, and replicate asynchronously to any others.
76 *
77 * @ingroup Session
78 * @since 1.27
79 * @see https://www.mediawiki.org/wiki/Manual:SessionManager_and_AuthManager
80 */
81 class SessionManager implements SessionManagerInterface {
82 /** @var SessionManager|null */
83 private static $instance = null;
84
85 /** @var Session|null */
86 private static $globalSession = null;
87
88 /** @var WebRequest|null */
89 private static $globalSessionRequest = null;
90
91 /** @var LoggerInterface */
92 private $logger;
93
94 /** @var HookContainer */
95 private $hookContainer;
96
97 /** @var HookRunner */
98 private $hookRunner;
99
100 /** @var Config */
101 private $config;
102
103 /** @var UserNameUtils */
104 private $userNameUtils;
105
106 /** @var CachedBagOStuff|null */
107 private $store;
108
109 /** @var SessionProvider[] */
110 private $sessionProviders = null;
111
112 /** @var string[] */
113 private $varyCookies = null;
114
115 /** @var array */
116 private $varyHeaders = null;
117
118 /** @var SessionBackend[] */
119 private $allSessionBackends = [];
120
121 /** @var SessionId[] */
122 private $allSessionIds = [];
123
124 /** @var true[] */
125 private $preventUsers = [];
126
127 /**
128 * Get the global SessionManager
129 * @return self
130 */
131 public static function singleton() {
132 if ( self::$instance === null ) {
133 self::$instance = new self();
134 }
135 return self::$instance;
136 }
137
138 /**
139 * If PHP's session_id() has been set, returns that session. Otherwise
140 * returns the session for RequestContext::getMain()->getRequest().
141 *
142 * @return Session
143 */
144 public static function getGlobalSession(): Session {
145 if ( !PHPSessionHandler::isEnabled() ) {
146 $id = '';
147 } else {
148 $id = session_id();
149 }
150
151 $request = RequestContext::getMain()->getRequest();
152 if (
153 !self::$globalSession // No global session is set up yet
154 || self::$globalSessionRequest !== $request // The global WebRequest changed
155 || ( $id !== '' && self::$globalSession->getId() !== $id ) // Someone messed with session_id()
156 ) {
157 self::$globalSessionRequest = $request;
158 if ( $id === '' ) {
159 // session_id() wasn't used, so fetch the Session from the WebRequest.
160 // We use $request->getSession() instead of $singleton->getSessionForRequest()
161 // because doing the latter would require a public
162 // "$request->getSessionId()" method that would confuse end
163 // users by returning SessionId|null where they'd expect it to
164 // be short for $request->getSession()->getId(), and would
165 // wind up being a duplicate of the code in
166 // $request->getSession() anyway.
167 self::$globalSession = $request->getSession();
168 } else {
169 // Someone used session_id(), so we need to follow suit.
170 // Note this overwrites whatever session might already be
171 // associated with $request with the one for $id.
172 self::$globalSession = self::singleton()->getSessionById( $id, true, $request )
173 ?: $request->getSession();
174 }
175 }
176 return self::$globalSession;
177 }
178
179 /**
180 * @param array $options
181 * - config: Config to fetch configuration from. Defaults to the default 'main' config.
182 * - logger: LoggerInterface to use for logging. Defaults to the 'session' channel.
183 * - store: BagOStuff to store session data in.
184 */
185 public function __construct( $options = [] ) {
186 $services = MediaWikiServices::getInstance();
187 if ( isset( $options['config'] ) ) {
188 $this->config = $options['config'];
189 if ( !$this->config instanceof Config ) {
190 throw new InvalidArgumentException(
191 '$options[\'config\'] must be an instance of Config'
192 );
193 }
194 } else {
195 $this->config = $services->getMainConfig();
196 }
197
198 if ( isset( $options['logger'] ) ) {
199 if ( !$options['logger'] instanceof LoggerInterface ) {
200 throw new InvalidArgumentException(
201 '$options[\'logger\'] must be an instance of LoggerInterface'
202 );
203 }
204 $this->setLogger( $options['logger'] );
205 } else {
206 $this->setLogger( \MediaWiki\Logger\LoggerFactory::getInstance( 'session' ) );
207 }
208
209 if ( isset( $options['hookContainer'] ) ) {
210 $this->setHookContainer( $options['hookContainer'] );
211 } else {
212 $this->setHookContainer( $services->getHookContainer() );
213 }
214
215 if ( isset( $options['store'] ) ) {
216 if ( !$options['store'] instanceof BagOStuff ) {
217 throw new InvalidArgumentException(
218 '$options[\'store\'] must be an instance of BagOStuff'
219 );
220 }
221 $store = $options['store'];
222 } else {
223 $store = $services->getObjectCacheFactory()
224 ->getInstance( $this->config->get( MainConfigNames::SessionCacheType ) );
225 }
226
227 $this->logger->debug( 'SessionManager using store ' . get_class( $store ) );
228 $this->store = $store instanceof CachedBagOStuff ? $store : new CachedBagOStuff( $store );
229 $this->userNameUtils = $services->getUserNameUtils();
230
231 register_shutdown_function( [ $this, 'shutdown' ] );
232 }
233
234 public function setLogger( LoggerInterface $logger ) {
235 $this->logger = $logger;
236 }
237
238 /**
239 * @internal
240 * @param HookContainer $hookContainer
241 */
242 public function setHookContainer( HookContainer $hookContainer ) {
243 $this->hookContainer = $hookContainer;
244 $this->hookRunner = new HookRunner( $hookContainer );
245 }
246
247 public function getSessionForRequest( WebRequest $request ) {
248 $info = $this->getSessionInfoForRequest( $request );
249
250 if ( !$info ) {
251 $session = $this->getInitialSession( $request );
252 } else {
253 $session = $this->getSessionFromInfo( $info, $request );
254 }
255 return $session;
256 }
257
258 public function getSessionById( $id, $create = false, ?WebRequest $request = null ) {
259 if ( !self::validateSessionId( $id ) ) {
260 throw new InvalidArgumentException( 'Invalid session ID' );
261 }
262 if ( !$request ) {
263 $request = new FauxRequest;
264 }
265
266 $session = null;
267 $info = new SessionInfo( SessionInfo::MIN_PRIORITY, [ 'id' => $id, 'idIsSafe' => true ] );
268
269 // If we already have the backend loaded, use it directly
270 if ( isset( $this->allSessionBackends[$id] ) ) {
271 return $this->getSessionFromInfo( $info, $request );
272 }
273
274 // Test if the session is in storage, and if so try to load it.
275 $key = $this->store->makeKey( 'MWSession', $id );
276 if ( is_array( $this->store->get( $key ) ) ) {
277 $create = false; // If loading fails, don't bother creating because it probably will fail too.
278 if ( $this->loadSessionInfoFromStore( $info, $request ) ) {
279 $session = $this->getSessionFromInfo( $info, $request );
280 }
281 }
282
283 if ( $create && $session === null ) {
284 try {
285 $session = $this->getEmptySessionInternal( $request, $id );
286 } catch ( \Exception $ex ) {
287 $this->logger->error( 'Failed to create empty session: {exception}',
288 [
289 'method' => __METHOD__,
290 'exception' => $ex,
291 ] );
292 $session = null;
293 }
294 }
295
296 return $session;
297 }
298
299 public function getEmptySession( ?WebRequest $request = null ) {
300 return $this->getEmptySessionInternal( $request );
301 }
302
303 /**
304 * @see SessionManagerInterface::getEmptySession
305 * @param WebRequest|null $request
306 * @param string|null $id ID to force on the new session
307 * @return Session
308 */
309 private function getEmptySessionInternal( ?WebRequest $request = null, $id = null ) {
310 if ( $id !== null ) {
311 if ( !self::validateSessionId( $id ) ) {
312 throw new InvalidArgumentException( 'Invalid session ID' );
313 }
314
315 $key = $this->store->makeKey( 'MWSession', $id );
316 if ( is_array( $this->store->get( $key ) ) ) {
317 throw new InvalidArgumentException( 'Session ID already exists' );
318 }
319 }
320 if ( !$request ) {
321 $request = new FauxRequest;
322 }
323
324 $infos = [];
325 foreach ( $this->getProviders() as $provider ) {
326 $info = $provider->newSessionInfo( $id );
327 if ( !$info ) {
328 continue;
329 }
330 if ( $info->getProvider() !== $provider ) {
331 throw new \UnexpectedValueException(
332 "$provider returned an empty session info for a different provider: $info"
333 );
334 }
335 if ( $id !== null && $info->getId() !== $id ) {
336 throw new \UnexpectedValueException(
337 "$provider returned empty session info with a wrong id: " .
338 $info->getId() . ' != ' . $id
339 );
340 }
341 if ( !$info->isIdSafe() ) {
342 throw new \UnexpectedValueException(
343 "$provider returned empty session info with id flagged unsafe"
344 );
345 }
346 $compare = $infos ? SessionInfo::compare( $infos[0], $info ) : -1;
347 if ( $compare > 0 ) {
348 continue;
349 }
350 if ( $compare === 0 ) {
351 $infos[] = $info;
352 } else {
353 $infos = [ $info ];
354 }
355 }
356
357 // Make sure there's exactly one
358 if ( count( $infos ) > 1 ) {
359 throw new \UnexpectedValueException(
360 'Multiple empty sessions tied for top priority: ' . implode( ', ', $infos )
361 );
362 } elseif ( count( $infos ) < 1 ) {
363 throw new \UnexpectedValueException( 'No provider could provide an empty session!' );
364 }
365
366 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
367 return $this->getSessionFromInfo( $infos[0], $request );
368 }
369
370 /**
371 * Create a new session. Populate it with a default secret token, to avoid
372 * a session replication race on a subsequent edit/save cycle (e.g. in
373 * a multi-dc setup, ref https://phabricator.wikimedia.org/T279664#8139533).
374 *
375 * @param WebRequest|null $request Corresponding request. Any existing
376 * session associated with this WebRequest object will be overwritten.
377 * @return Session
378 */
379 private function getInitialSession( ?WebRequest $request = null ) {
380 $session = $this->getEmptySession( $request );
381 $session->getToken();
382 return $session;
383 }
384
385 public function invalidateSessionsForUser( User $user ) {
386 $user->setToken();
387 $user->saveSettings();
388
389 foreach ( $this->getProviders() as $provider ) {
390 $provider->invalidateSessionsForUser( $user );
391 }
392 }
393
394 /**
395 * @return array<string,null>
396 */
397 public function getVaryHeaders() {
398 // @codeCoverageIgnoreStart
399 if ( defined( 'MW_NO_SESSION' ) && MW_NO_SESSION !== 'warn' ) {
400 return [];
401 }
402 // @codeCoverageIgnoreEnd
403 if ( $this->varyHeaders === null ) {
404 $headers = [];
405 foreach ( $this->getProviders() as $provider ) {
406 foreach ( $provider->getVaryHeaders() as $header => $_ ) {
407 $headers[$header] = null;
408 }
409 }
410 $this->varyHeaders = $headers;
411 }
412 return $this->varyHeaders;
413 }
414
415 public function getVaryCookies() {
416 // @codeCoverageIgnoreStart
417 if ( defined( 'MW_NO_SESSION' ) && MW_NO_SESSION !== 'warn' ) {
418 return [];
419 }
420 // @codeCoverageIgnoreEnd
421 if ( $this->varyCookies === null ) {
422 $cookies = [];
423 foreach ( $this->getProviders() as $provider ) {
424 $cookies = array_merge( $cookies, $provider->getVaryCookies() );
425 }
426 $this->varyCookies = array_values( array_unique( $cookies ) );
427 }
428 return $this->varyCookies;
429 }
430
431 /**
432 * Validate a session ID
433 * @param string $id
434 * @return bool
435 */
436 public static function validateSessionId( $id ) {
437 return is_string( $id ) && preg_match( '/^[a-zA-Z0-9_-]{32,}$/', $id );
438 }
439
440 /***************************************************************************/
441 // region Internal methods
442 /** @name Internal methods */
443
444 /**
445 * Prevent future sessions for the user
446 *
447 * The intention is that the named account will never again be usable for
448 * normal login (i.e. there is no way to undo the prevention of access).
449 *
450 * @internal For use from \MediaWiki\User\User::newSystemUser only
451 * @param string $username
452 */
453 public function preventSessionsForUser( $username ) {
454 $this->preventUsers[$username] = true;
455
456 // Instruct the session providers to kill any other sessions too.
457 foreach ( $this->getProviders() as $provider ) {
458 $provider->preventSessionsForUser( $username );
459 }
460 }
461
462 /**
463 * Test if a user is prevented
464 * @internal For use from SessionBackend only
465 * @param string $username
466 * @return bool
467 */
468 public function isUserSessionPrevented( $username ) {
469 return !empty( $this->preventUsers[$username] );
470 }
471
472 /**
473 * Get the available SessionProviders
474 * @return SessionProvider[]
475 */
476 protected function getProviders() {
477 if ( $this->sessionProviders === null ) {
478 $this->sessionProviders = [];
479 $objectFactory = MediaWikiServices::getInstance()->getObjectFactory();
480 foreach ( $this->config->get( MainConfigNames::SessionProviders ) as $spec ) {
481 /** @var SessionProvider $provider */
482 $provider = $objectFactory->createObject( $spec );
483 $provider->init(
484 $this->logger,
485 $this->config,
486 $this,
487 $this->hookContainer,
488 $this->userNameUtils
489 );
490 if ( isset( $this->sessionProviders[(string)$provider] ) ) {
491 // @phan-suppress-next-line PhanTypeSuspiciousStringExpression
492 throw new \UnexpectedValueException( "Duplicate provider name \"$provider\"" );
493 }
494 $this->sessionProviders[(string)$provider] = $provider;
495 }
496 }
497 return $this->sessionProviders;
498 }
499
500 /**
501 * Get a session provider by name
502 *
503 * Generally, this will only be used by internal implementation of some
504 * special session-providing mechanism. General purpose code, if it needs
505 * to access a SessionProvider at all, will use Session::getProvider().
506 *
507 * @param string $name
508 * @return SessionProvider|null
509 */
510 public function getProvider( $name ) {
511 $providers = $this->getProviders();
512 return $providers[$name] ?? null;
513 }
514
515 /**
516 * Save all active sessions on shutdown
517 * @internal For internal use with register_shutdown_function()
518 */
519 public function shutdown() {
520 if ( $this->allSessionBackends ) {
521 $this->logger->debug( 'Saving all sessions on shutdown' );
522 if ( session_id() !== '' ) {
523 // @codeCoverageIgnoreStart
524 session_write_close();
525 }
526 // @codeCoverageIgnoreEnd
527 foreach ( $this->allSessionBackends as $backend ) {
528 $backend->shutdown();
529 }
530 }
531 }
532
533 /**
534 * Fetch the SessionInfo(s) for a request
535 * @param WebRequest $request
536 * @return SessionInfo|null
537 */
538 private function getSessionInfoForRequest( WebRequest $request ) {
539 // Call all providers to fetch "the" session
540 $infos = [];
541 foreach ( $this->getProviders() as $provider ) {
542 $info = $provider->provideSessionInfo( $request );
543 if ( !$info ) {
544 continue;
545 }
546 if ( $info->getProvider() !== $provider ) {
547 throw new \UnexpectedValueException(
548 "$provider returned session info for a different provider: $info"
549 );
550 }
551 $infos[] = $info;
552 }
553
554 // Sort the SessionInfos. Then find the first one that can be
555 // successfully loaded, and then all the ones after it with the same
556 // priority.
557 usort( $infos, [ SessionInfo::class, 'compare' ] );
558 $retInfos = [];
559 while ( $infos ) {
560 $info = array_pop( $infos );
561 if ( $this->loadSessionInfoFromStore( $info, $request ) ) {
562 $retInfos[] = $info;
563 while ( $infos ) {
564 /** @var SessionInfo $info */
565 $info = array_pop( $infos );
566 if ( SessionInfo::compare( $retInfos[0], $info ) ) {
567 // We hit a lower priority, stop checking.
568 break;
569 }
570 if ( $this->loadSessionInfoFromStore( $info, $request ) ) {
571 // This is going to error out below, but we want to
572 // provide a complete list.
573 $retInfos[] = $info;
574 } else {
575 // Session load failed, so unpersist it from this request
576 $this->logUnpersist( $info, $request );
577 $info->getProvider()->unpersistSession( $request );
578 }
579 }
580 } else {
581 // Session load failed, so unpersist it from this request
582 $this->logUnpersist( $info, $request );
583 $info->getProvider()->unpersistSession( $request );
584 }
585 }
586
587 if ( count( $retInfos ) > 1 ) {
588 throw new SessionOverflowException(
589 $retInfos,
590 'Multiple sessions for this request tied for top priority: ' . implode( ', ', $retInfos )
591 );
592 }
593
594 return $retInfos[0] ?? null;
595 }
596
597 /**
598 * Load and verify the session info against the store
599 *
600 * @param SessionInfo &$info Will likely be replaced with an updated SessionInfo instance
601 * @param WebRequest $request
602 * @return bool Whether the session info matches the stored data (if any)
603 */
604 private function loadSessionInfoFromStore( SessionInfo &$info, WebRequest $request ) {
605 $key = $this->store->makeKey( 'MWSession', $info->getId() );
606 $blob = $this->store->get( $key );
607
608 // If we got data from the store and the SessionInfo says to force use,
609 // "fail" means to delete the data from the store and retry. Otherwise,
610 // "fail" is just return false.
611 if ( $info->forceUse() && $blob !== false ) {
612 $failHandler = function () use ( $key, &$info, $request ) {
613 $this->store->delete( $key );
614 return $this->loadSessionInfoFromStore( $info, $request );
615 };
616 } else {
617 $failHandler = static function () {
618 return false;
619 };
620 }
621
622 $newParams = [];
623
624 if ( $blob !== false ) {
625 // Double check: blob must be an array, if it's saved at all
626 if ( !is_array( $blob ) ) {
627 $this->logger->warning( 'Session "{session}": Bad data', [
628 'session' => $info->__toString(),
629 ] );
630 $this->store->delete( $key );
631 return $failHandler();
632 }
633
634 // Double check: blob has data and metadata arrays
635 if ( !isset( $blob['data'] ) || !is_array( $blob['data'] ) ||
636 !isset( $blob['metadata'] ) || !is_array( $blob['metadata'] )
637 ) {
638 $this->logger->warning( 'Session "{session}": Bad data structure', [
639 'session' => $info->__toString(),
640 ] );
641 $this->store->delete( $key );
642 return $failHandler();
643 }
644
645 $data = $blob['data'];
646 $metadata = $blob['metadata'];
647
648 // Double check: metadata must be an array and must contain certain
649 // keys, if it's saved at all
650 if ( !array_key_exists( 'userId', $metadata ) ||
651 !array_key_exists( 'userName', $metadata ) ||
652 !array_key_exists( 'userToken', $metadata ) ||
653 !array_key_exists( 'provider', $metadata )
654 ) {
655 $this->logger->warning( 'Session "{session}": Bad metadata', [
656 'session' => $info->__toString(),
657 ] );
658 $this->store->delete( $key );
659 return $failHandler();
660 }
661
662 // First, load the provider from metadata, or validate it against the metadata.
663 $provider = $info->getProvider();
664 if ( $provider === null ) {
665 $newParams['provider'] = $provider = $this->getProvider( $metadata['provider'] );
666 if ( !$provider ) {
667 $this->logger->warning(
668 'Session "{session}": Unknown provider ' . $metadata['provider'],
669 [
670 'session' => $info->__toString(),
671 ]
672 );
673 $this->store->delete( $key );
674 return $failHandler();
675 }
676 } elseif ( $metadata['provider'] !== (string)$provider ) {
677 $this->logger->warning( 'Session "{session}": Wrong provider ' .
678 $metadata['provider'] . ' !== ' . $provider,
679 [
680 'session' => $info->__toString(),
681 ] );
682 return $failHandler();
683 }
684
685 // Load provider metadata from metadata, or validate it against the metadata
686 $providerMetadata = $info->getProviderMetadata();
687 if ( isset( $metadata['providerMetadata'] ) ) {
688 if ( $providerMetadata === null ) {
689 $newParams['metadata'] = $metadata['providerMetadata'];
690 } else {
691 try {
692 $newProviderMetadata = $provider->mergeMetadata(
693 $metadata['providerMetadata'], $providerMetadata
694 );
695 if ( $newProviderMetadata !== $providerMetadata ) {
696 $newParams['metadata'] = $newProviderMetadata;
697 }
698 } catch ( MetadataMergeException $ex ) {
699 $this->logger->warning(
700 'Session "{session}": Metadata merge failed: {exception}',
701 [
702 'session' => $info->__toString(),
703 'exception' => $ex,
704 ] + $ex->getContext()
705 );
706 return $failHandler();
707 }
708 }
709 }
710
711 // Next, load the user from metadata, or validate it against the metadata.
712 $userInfo = $info->getUserInfo();
713 if ( !$userInfo ) {
714 // For loading, id is preferred to name.
715 try {
716 if ( $metadata['userId'] ) {
717 $userInfo = UserInfo::newFromId( $metadata['userId'] );
718 } elseif ( $metadata['userName'] !== null ) { // Shouldn't happen, but just in case
719 $userInfo = UserInfo::newFromName( $metadata['userName'] );
720 } else {
721 $userInfo = UserInfo::newAnonymous();
722 }
723 } catch ( InvalidArgumentException $ex ) {
724 $this->logger->error( 'Session "{session}": {exception}', [
725 'session' => $info->__toString(),
726 'exception' => $ex,
727 ] );
728 return $failHandler();
729 }
730 $newParams['userInfo'] = $userInfo;
731 } else {
732 // User validation passes if user ID matches, or if there
733 // is no saved ID and the names match.
734 if ( $metadata['userId'] ) {
735 if ( $metadata['userId'] !== $userInfo->getId() ) {
736 // Maybe something like UserMerge changed the user ID. Or it's manual tampering.
737 $this->logger->warning(
738 'Session "{session}": User ID mismatch, {uid_a} !== {uid_b}',
739 [
740 'session' => $info->__toString(),
741 'uid_a' => $metadata['userId'],
742 'uid_b' => $userInfo->getId(),
743 'uname_a' => $metadata['userName'] ?? '<null>',
744 'uname_b' => $userInfo->getName() ?? '<null>',
745 ] );
746 return $failHandler();
747 }
748
749 // If the user was renamed, probably best to fail here.
750 if ( $metadata['userName'] !== null &&
751 $userInfo->getName() !== $metadata['userName']
752 ) {
753 $this->logger->warning(
754 'Session "{session}": User ID matched but name didn\'t (rename?), {uname_a} !== {uname_b}',
755 [
756 'session' => $info->__toString(),
757 'uname_a' => $metadata['userName'],
758 'uname_b' => $userInfo->getName(),
759 ] );
760 return $failHandler();
761 }
762
763 } elseif ( $metadata['userName'] !== null ) { // Shouldn't happen, but just in case
764 if ( $metadata['userName'] !== $userInfo->getName() ) {
765 $this->logger->warning(
766 'Session "{session}": User name mismatch, {uname_a} !== {uname_b}',
767 [
768 'session' => $info->__toString(),
769 'uname_a' => $metadata['userName'],
770 'uname_b' => $userInfo->getName(),
771 ] );
772 return $failHandler();
773 }
774 } elseif ( !$userInfo->isAnon() ) {
775 // The metadata in the session store entry indicates this is an anonymous session,
776 // but the request metadata (e.g. the username cookie) says otherwise. Maybe the
777 // user logged out but unsetting the cookies failed?
778 $this->logger->warning(
779 'Session "{session}": the session store entry is for an anonymous user, '
780 . 'but the session metadata indicates a non-anonynmous user',
781 [
782 'session' => $info->__toString(),
783 ] );
784 return $failHandler();
785 }
786 }
787
788 // And if we have a token in the metadata, it must match the loaded/provided user.
789 // A mismatch probably means the session was invalidated.
790 if ( $metadata['userToken'] !== null &&
791 $userInfo->getToken() !== $metadata['userToken']
792 ) {
793 $this->logger->warning( 'Session "{session}": User token mismatch', [
794 'session' => $info->__toString(),
795 ] );
796 return $failHandler();
797 }
798 if ( !$userInfo->isVerified() ) {
799 $newParams['userInfo'] = $userInfo->verified();
800 }
801
802 if ( !empty( $metadata['remember'] ) && !$info->wasRemembered() ) {
803 $newParams['remembered'] = true;
804 }
805 if ( !empty( $metadata['forceHTTPS'] ) && !$info->forceHTTPS() ) {
806 $newParams['forceHTTPS'] = true;
807 }
808 if ( !empty( $metadata['persisted'] ) && !$info->wasPersisted() ) {
809 $newParams['persisted'] = true;
810 }
811
812 if ( !$info->isIdSafe() ) {
813 $newParams['idIsSafe'] = true;
814 }
815 } else {
816 // No metadata, so we can't load the provider if one wasn't given.
817 if ( $info->getProvider() === null ) {
818 $this->logger->warning(
819 'Session "{session}": Null provider and no metadata',
820 [
821 'session' => $info->__toString(),
822 ] );
823 return $failHandler();
824 }
825
826 // If no user was provided and no metadata, it must be anon.
827 if ( !$info->getUserInfo() ) {
828 if ( $info->getProvider()->canChangeUser() ) {
829 $newParams['userInfo'] = UserInfo::newAnonymous();
830 } else {
831 // This is a session provider bug - providers with canChangeUser() === false
832 // should never return an anonymous SessionInfo.
833 $this->logger->info(
834 'Session "{session}": No user provided and provider cannot set user',
835 [
836 'session' => $info->__toString(),
837 ] );
838 return $failHandler();
839 }
840 } elseif ( !$info->getUserInfo()->isVerified() ) {
841 // The session was not found in the session store, and the request contains no
842 // information beyond the session ID that could be used to verify it.
843 // Probably just a session timeout.
844 $this->logger->info(
845 'Session "{session}": Unverified user provided and no metadata to auth it',
846 [
847 'session' => $info->__toString(),
848 ] );
849 return $failHandler();
850 }
851
852 $data = false;
853 $metadata = false;
854
855 if ( !$info->getProvider()->persistsSessionId() && !$info->isIdSafe() ) {
856 // The ID doesn't come from the user, so it should be safe
857 // (and if not, nothing we can do about it anyway)
858 $newParams['idIsSafe'] = true;
859 }
860 }
861
862 // Construct the replacement SessionInfo, if necessary
863 if ( $newParams ) {
864 $newParams['copyFrom'] = $info;
865 $info = new SessionInfo( $info->getPriority(), $newParams );
866 }
867
868 // Allow the provider to check the loaded SessionInfo
869 $providerMetadata = $info->getProviderMetadata();
870 if ( !$info->getProvider()->refreshSessionInfo( $info, $request, $providerMetadata ) ) {
871 return $failHandler();
872 }
873 if ( $providerMetadata !== $info->getProviderMetadata() ) {
874 $info = new SessionInfo( $info->getPriority(), [
875 'metadata' => $providerMetadata,
876 'copyFrom' => $info,
877 ] );
878 }
879
880 // Give hooks a chance to abort. Combined with the SessionMetadata
881 // hook, this can allow for tying a session to an IP address or the
882 // like.
883 $reason = 'Hook aborted';
884 if ( !$this->hookRunner->onSessionCheckInfo(
885 $reason, $info, $request, $metadata, $data )
886 ) {
887 $this->logger->warning( 'Session "{session}": ' . $reason, [
888 'session' => $info->__toString(),
889 ] );
890 return $failHandler();
891 }
892
893 return true;
894 }
895
896 /**
897 * Create a Session corresponding to the passed SessionInfo
898 * @internal For use by a SessionProvider that needs to specially create its
899 * own Session. Most session providers won't need this.
900 * @param SessionInfo $info
901 * @param WebRequest $request
902 * @return Session
903 */
904 public function getSessionFromInfo( SessionInfo $info, WebRequest $request ) {
905 // @codeCoverageIgnoreStart
906 if ( defined( 'MW_NO_SESSION' ) ) {
907 $ep = defined( 'MW_ENTRY_POINT' ) ? MW_ENTRY_POINT : 'this';
908
909 if ( MW_NO_SESSION === 'warn' ) {
910 // Undocumented safety case for converting existing entry points
911 $this->logger->error( 'Sessions are supposed to be disabled for this entry point', [
912 'exception' => new \BadMethodCallException( "Sessions are disabled for $ep entry point" ),
913 ] );
914 } else {
915 throw new \BadMethodCallException( "Sessions are disabled for $ep entry point" );
916 }
917 }
918 // @codeCoverageIgnoreEnd
919
920 $id = $info->getId();
921
922 if ( !isset( $this->allSessionBackends[$id] ) ) {
923 if ( !isset( $this->allSessionIds[$id] ) ) {
924 $this->allSessionIds[$id] = new SessionId( $id );
925 }
926 $backend = new SessionBackend(
927 $this->allSessionIds[$id],
928 $info,
929 $this->store,
930 $this->logger,
931 $this->hookContainer,
932 $this->config->get( MainConfigNames::ObjectCacheSessionExpiry )
933 );
934 $this->allSessionBackends[$id] = $backend;
935 $delay = $backend->delaySave();
936 } else {
937 $backend = $this->allSessionBackends[$id];
938 $delay = $backend->delaySave();
939 if ( $info->wasPersisted() ) {
940 $backend->persist();
941 }
942 if ( $info->wasRemembered() ) {
943 $backend->setRememberUser( true );
944 }
945 }
946
947 $request->setSessionId( $backend->getSessionId() );
948 $session = $backend->getSession( $request );
949
950 if ( !$info->isIdSafe() ) {
951 $session->resetId();
952 }
953
954 \Wikimedia\ScopedCallback::consume( $delay );
955 return $session;
956 }
957
958 /**
959 * Deregister a SessionBackend
960 * @internal For use from \MediaWiki\Session\SessionBackend only
961 * @param SessionBackend $backend
962 */
963 public function deregisterSessionBackend( SessionBackend $backend ) {
964 $id = $backend->getId();
965 if ( !isset( $this->allSessionBackends[$id] ) || !isset( $this->allSessionIds[$id] ) ||
966 $this->allSessionBackends[$id] !== $backend ||
967 $this->allSessionIds[$id] !== $backend->getSessionId()
968 ) {
969 throw new InvalidArgumentException( 'Backend was not registered with this SessionManager' );
970 }
971
972 unset( $this->allSessionBackends[$id] );
973 // Explicitly do not unset $this->allSessionIds[$id]
974 }
975
976 /**
977 * Change a SessionBackend's ID
978 * @internal For use from \MediaWiki\Session\SessionBackend only
979 * @param SessionBackend $backend
980 */
981 public function changeBackendId( SessionBackend $backend ) {
982 $sessionId = $backend->getSessionId();
983 $oldId = (string)$sessionId;
984 if ( !isset( $this->allSessionBackends[$oldId] ) || !isset( $this->allSessionIds[$oldId] ) ||
985 $this->allSessionBackends[$oldId] !== $backend ||
986 $this->allSessionIds[$oldId] !== $sessionId
987 ) {
988 throw new InvalidArgumentException( 'Backend was not registered with this SessionManager' );
989 }
990
991 $newId = $this->generateSessionId();
992
993 unset( $this->allSessionBackends[$oldId], $this->allSessionIds[$oldId] );
994 $sessionId->setId( $newId );
995 $this->allSessionBackends[$newId] = $backend;
996 $this->allSessionIds[$newId] = $sessionId;
997 }
998
999 /**
1000 * Generate a new random session ID
1001 * @return string
1002 */
1003 public function generateSessionId() {
1004 $id = \Wikimedia\base_convert( \MWCryptRand::generateHex( 40 ), 16, 32, 32 );
1005 // Cache non-existence to avoid a later fetch
1006 $key = $this->store->makeKey( 'MWSession', $id );
1007 $this->store->set( $key, false, 0, BagOStuff::WRITE_CACHE_ONLY );
1008 return $id;
1009 }
1010
1011 /**
1012 * Call setters on a PHPSessionHandler
1013 * @internal Use PhpSessionHandler::install()
1014 * @param PHPSessionHandler $handler
1015 */
1016 public function setupPHPSessionHandler( PHPSessionHandler $handler ) {
1017 $handler->setManager( $this, $this->store, $this->logger );
1018 }
1019
1020 /**
1021 * Reset the internal caching for unit testing
1022 * @note Unit tests only
1023 * @internal
1024 */
1025 public static function resetCache() {
1026 if ( !defined( 'MW_PHPUNIT_TEST' ) && !defined( 'MW_PARSER_TEST' ) ) {
1027 // @codeCoverageIgnoreStart
1028 throw new LogicException( __METHOD__ . ' may only be called from unit tests!' );
1029 // @codeCoverageIgnoreEnd
1030 }
1031
1032 self::$globalSession = null;
1033 self::$globalSessionRequest = null;
1034 }
1035
1036 private function logUnpersist( SessionInfo $info, WebRequest $request ) {
1037 $logData = [
1038 'id' => $info->getId(),
1039 'provider' => get_class( $info->getProvider() ),
1040 'user' => '<anon>',
1041 'clientip' => $request->getIP(),
1042 'userAgent' => $request->getHeader( 'user-agent' ),
1043 ];
1044 if ( $info->getUserInfo() ) {
1045 if ( !$info->getUserInfo()->isAnon() ) {
1046 $logData['user'] = $info->getUserInfo()->getName();
1047 }
1048 $logData['userVerified'] = $info->getUserInfo()->isVerified();
1049 }
1050 $this->logger->info( 'Failed to load session, unpersisting', $logData );
1051 }
1052
1053 /**
1054 * If the same session is suddenly used from a different IP, that's potentially due
1055 * to a session leak, so log it. In the vast majority of cases it is a false positive
1056 * due to a user switching connections, but we are interested in an audit track where
1057 * we can look up a specific username, so a noisy log is fine.
1058 * Also log changes to the mwuser cookie, an analytics cookie set by mediawiki.user.js
1059 * which should be a little less noisy.
1060 * @private For use in Setup.php only
1061 * @param Session|null $session For testing only
1062 */
1063 public function logPotentialSessionLeakage( ?Session $session = null ) {
1064 $proxyLookup = MediaWikiServices::getInstance()->getProxyLookup();
1065 $session = $session ?: self::getGlobalSession();
1066 $suspiciousIpExpiry = $this->config->get( MainConfigNames::SuspiciousIpExpiry );
1067
1068 if ( $suspiciousIpExpiry === false
1069 // We only care about logged-in users.
1070 || !$session->isPersistent() || $session->getUser()->isAnon()
1071 // We only care about cookie-based sessions.
1072 || !( $session->getProvider() instanceof CookieSessionProvider )
1073 ) {
1074 return;
1075 }
1076 try {
1077 $ip = $session->getRequest()->getIP();
1078 } catch ( MWException $e ) {
1079 return;
1080 }
1081 if ( $ip === '127.0.0.1' || $proxyLookup->isConfiguredProxy( $ip ) ) {
1082 return;
1083 }
1084 $mwuser = $session->getRequest()->getCookie( 'mwuser-sessionId' );
1085 $now = (int)\MediaWiki\Utils\MWTimestamp::now( TS_UNIX );
1086
1087 // Record (and possibly log) that the IP is using the current session.
1088 // Don't touch the stored data unless we are changing the IP or re-adding an expired one.
1089 // This is slightly inaccurate (when an existing IP is seen again, the expiry is not
1090 // extended) but that shouldn't make much difference and limits the session write frequency.
1091 $data = $session->get( 'SessionManager-logPotentialSessionLeakage', [] )
1092 + [ 'ip' => null, 'mwuser' => null, 'timestamp' => 0 ];
1093 // Ignore old IP records; users change networks over time. mwuser is a session cookie and the
1094 // SessionManager session id is also a session cookie so there shouldn't be any problem there.
1095 if ( $data['ip'] &&
1096 ( $now - $data['timestamp'] > $suspiciousIpExpiry )
1097 ) {
1098 $data['ip'] = $data['timestamp'] = null;
1099 }
1100
1101 if ( $data['ip'] !== $ip || $data['mwuser'] !== $mwuser ) {
1102 $session->set( 'SessionManager-logPotentialSessionLeakage',
1103 [ 'ip' => $ip, 'mwuser' => $mwuser, 'timestamp' => $now ] );
1104 }
1105
1106 $ipChanged = ( $data['ip'] && $data['ip'] !== $ip );
1107 $mwuserChanged = ( $data['mwuser'] && $data['mwuser'] !== $mwuser );
1108 $logLevel = $message = null;
1109 $logData = [];
1110 // IPs change all the time. mwuser is a session cookie that's only set when missing,
1111 // so it should only change when the browser session ends which ends the SessionManager
1112 // session as well. Unless we are dealing with a very weird client, such as a bot that
1113 //manipulates cookies and can run Javascript, it should not change.
1114 // IP and mwuser changing at the same time would be *very* suspicious.
1115 if ( $ipChanged ) {
1116 $logLevel = LogLevel::INFO;
1117 $message = 'IP change within the same session';
1118 $logData += [
1119 'oldIp' => $data['ip'],
1120 'oldIpRecorded' => $data['timestamp'],
1121 ];
1122 }
1123 if ( $mwuserChanged ) {
1124 $logLevel = LogLevel::NOTICE;
1125 $message = 'mwuser change within the same session';
1126 $logData += [
1127 'oldMwuser' => $data['mwuser'],
1128 'newMwuser' => $mwuser,
1129 ];
1130 }
1131 if ( $ipChanged && $mwuserChanged ) {
1132 $logLevel = LogLevel::WARNING;
1133 $message = 'IP and mwuser change within the same session';
1134 }
1135 if ( $logLevel ) {
1136 $logData += [
1137 'session' => $session->getId(),
1138 'user' => $session->getUser()->getName(),
1139 'clientip' => $ip,
1140 'userAgent' => $session->getRequest()->getHeader( 'user-agent' ),
1141 ];
1142 $logger = \MediaWiki\Logger\LoggerFactory::getInstance( 'session-ip' );
1143 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable message is set when used here
1144 $logger->log( $logLevel, $message, $logData );
1145 }
1146 }
1147
1148 // endregion -- end of Internal methods
1149
1150 }