| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
namespace MediaWiki\Session; |
| 25 |
|
| 26 |
use BadMethodCallException; |
| 27 |
use LogicException; |
| 28 |
use MediaWiki\MainConfigNames; |
| 29 |
use MediaWiki\MediaWikiServices; |
| 30 |
use MediaWiki\Request\WebRequest; |
| 31 |
use MediaWiki\User\User; |
| 32 |
use MWRestrictions; |
| 33 |
use Psr\Log\LoggerInterface; |
| 34 |
use RuntimeException; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
class Session implements \Countable, \Iterator, \ArrayAccess { |
| 55 |
|
| 56 |
private static $encryptionAlgorithm = null; |
| 57 |
|
| 58 |
|
| 59 |
private $backend; |
| 60 |
|
| 61 |
|
| 62 |
private $index; |
| 63 |
|
| 64 |
|
| 65 |
private $logger; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public function __construct( SessionBackend $backend, $index, LoggerInterface $logger ) { |
| 73 |
$this->backend = $backend; |
| 74 |
$this->index = $index; |
| 75 |
$this->logger = $logger; |
| 76 |
} |
| 77 |
|
| 78 |
public function __destruct() { |
| 79 |
$this->backend->deregisterSession( $this->index ); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public function getId() { |
| 87 |
return $this->backend->getId(); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public function getSessionId() { |
| 96 |
return $this->backend->getSessionId(); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public function resetId() { |
| 104 |
return $this->backend->resetId(); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public function getProvider() { |
| 112 |
return $this->backend->getProvider(); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public function isPersistent() { |
| 123 |
return $this->backend->isPersistent(); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public function persist() { |
| 133 |
$this->backend->persist(); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public function unpersist() { |
| 145 |
$this->backend->unpersist(); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public function shouldRememberUser() { |
| 154 |
return $this->backend->shouldRememberUser(); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public function setRememberUser( $remember ) { |
| 163 |
$this->backend->setRememberUser( $remember ); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
public function getRequest() { |
| 171 |
return $this->backend->getRequest( $this->index ); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
public function getUser(): User { |
| 179 |
return $this->backend->getUser(); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
public function getAllowedUserRights() { |
| 187 |
return $this->backend->getAllowedUserRights(); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
public function getRestrictions(): ?MWRestrictions { |
| 196 |
return $this->backend->getRestrictions(); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
public function canSetUser() { |
| 204 |
return $this->backend->canSetUser(); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
public function setUser( $user ) { |
| 215 |
$this->backend->setUser( $user ); |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
public function suggestLoginUsername() { |
| 223 |
return $this->backend->suggestLoginUsername( $this->index ); |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
public function shouldForceHTTPS() { |
| 234 |
return $this->backend->shouldForceHTTPS(); |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
public function setForceHTTPS( $force ) { |
| 245 |
$this->backend->setForceHTTPS( $force ); |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
public function getLoggedOutTimestamp() { |
| 253 |
return $this->backend->getLoggedOutTimestamp(); |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
public function setLoggedOutTimestamp( $ts ) { |
| 260 |
$this->backend->setLoggedOutTimestamp( $ts ); |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
public function getProviderMetadata() { |
| 269 |
return $this->backend->getProviderMetadata(); |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
public function clear() { |
| 276 |
$data = &$this->backend->getData(); |
| 277 |
if ( $data ) { |
| 278 |
$data = []; |
| 279 |
$this->backend->dirty(); |
| 280 |
} |
| 281 |
if ( $this->backend->canSetUser() ) { |
| 282 |
$this->backend->setUser( MediaWikiServices::getInstance()->getUserFactory()->newAnonymous() ); |
| 283 |
} |
| 284 |
$this->backend->save(); |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
public function renew() { |
| 292 |
$this->backend->renew(); |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
public function sessionWithRequest( WebRequest $request ) { |
| 305 |
$request->setSessionId( $this->backend->getSessionId() ); |
| 306 |
return $this->backend->getSession( $request ); |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
public function get( $key, $default = null ) { |
| 316 |
$data = &$this->backend->getData(); |
| 317 |
return array_key_exists( $key, $data ) ? $data[$key] : $default; |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
public function exists( $key ) { |
| 327 |
$data = &$this->backend->getData(); |
| 328 |
return array_key_exists( $key, $data ); |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
public function set( $key, $value ) { |
| 337 |
$data = &$this->backend->getData(); |
| 338 |
if ( !array_key_exists( $key, $data ) || $data[$key] !== $value ) { |
| 339 |
$data[$key] = $value; |
| 340 |
$this->backend->dirty(); |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
public function remove( $key ) { |
| 349 |
$data = &$this->backend->getData(); |
| 350 |
if ( array_key_exists( $key, $data ) ) { |
| 351 |
unset( $data[$key] ); |
| 352 |
$this->backend->dirty(); |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
public function hasToken( string $key = 'default' ): bool { |
| 364 |
$secrets = $this->get( 'wsTokenSecrets' ); |
| 365 |
if ( !is_array( $secrets ) ) { |
| 366 |
return false; |
| 367 |
} |
| 368 |
return isset( $secrets[$key] ) && is_string( $secrets[$key] ); |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
public function getToken( $salt = '', $key = 'default' ) { |
| 382 |
$new = false; |
| 383 |
$secrets = $this->get( 'wsTokenSecrets' ); |
| 384 |
if ( !is_array( $secrets ) ) { |
| 385 |
$secrets = []; |
| 386 |
} |
| 387 |
if ( isset( $secrets[$key] ) && is_string( $secrets[$key] ) ) { |
| 388 |
$secret = $secrets[$key]; |
| 389 |
} else { |
| 390 |
$secret = \MWCryptRand::generateHex( 32 ); |
| 391 |
$secrets[$key] = $secret; |
| 392 |
$this->set( 'wsTokenSecrets', $secrets ); |
| 393 |
$new = true; |
| 394 |
} |
| 395 |
if ( is_array( $salt ) ) { |
| 396 |
$salt = implode( '|', $salt ); |
| 397 |
} |
| 398 |
return new Token( $secret, (string)$salt, $new ); |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
public function resetToken( $key = 'default' ) { |
| 409 |
$secrets = $this->get( 'wsTokenSecrets' ); |
| 410 |
if ( is_array( $secrets ) && isset( $secrets[$key] ) ) { |
| 411 |
unset( $secrets[$key] ); |
| 412 |
$this->set( 'wsTokenSecrets', $secrets ); |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
public function resetAllTokens() { |
| 420 |
$this->remove( 'wsTokenSecrets' ); |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
private function getSecretKeys() { |
| 428 |
$mainConfig = MediaWikiServices::getInstance()->getMainConfig(); |
| 429 |
$sessionSecret = $mainConfig->get( MainConfigNames::SessionSecret ); |
| 430 |
$secretKey = $mainConfig->get( MainConfigNames::SecretKey ); |
| 431 |
$sessionPbkdf2Iterations = $mainConfig->get( MainConfigNames::SessionPbkdf2Iterations ); |
| 432 |
$wikiSecret = $sessionSecret ?: $secretKey; |
| 433 |
$userSecret = $this->get( 'wsSessionSecret', null ); |
| 434 |
if ( $userSecret === null ) { |
| 435 |
$userSecret = \MWCryptRand::generateHex( 32 ); |
| 436 |
$this->set( 'wsSessionSecret', $userSecret ); |
| 437 |
} |
| 438 |
$iterations = $this->get( 'wsSessionPbkdf2Iterations', null ); |
| 439 |
if ( $iterations === null ) { |
| 440 |
$iterations = $sessionPbkdf2Iterations; |
| 441 |
$this->set( 'wsSessionPbkdf2Iterations', $iterations ); |
| 442 |
} |
| 443 |
|
| 444 |
$keymats = openssl_pbkdf2( $wikiSecret, $userSecret, 64, $iterations, 'sha256' ); |
| 445 |
return [ |
| 446 |
substr( $keymats, 0, 32 ), |
| 447 |
substr( $keymats, 32, 32 ), |
| 448 |
]; |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
private static function getEncryptionAlgorithm() { |
| 456 |
if ( self::$encryptionAlgorithm === null ) { |
| 457 |
if ( function_exists( 'openssl_encrypt' ) ) { |
| 458 |
$methods = openssl_get_cipher_methods(); |
| 459 |
if ( in_array( 'aes-256-ctr', $methods, true ) ) { |
| 460 |
self::$encryptionAlgorithm = [ 'openssl', 'aes-256-ctr' ]; |
| 461 |
return self::$encryptionAlgorithm; |
| 462 |
} |
| 463 |
if ( in_array( 'aes-256-cbc', $methods, true ) ) { |
| 464 |
self::$encryptionAlgorithm = [ 'openssl', 'aes-256-cbc' ]; |
| 465 |
return self::$encryptionAlgorithm; |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
throw new BadMethodCallException( |
| 470 |
'Encryption is not available. You need to install the PHP OpenSSL extension.' |
| 471 |
); |
| 472 |
} |
| 473 |
|
| 474 |
return self::$encryptionAlgorithm; |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
public function setSecret( $key, $value ) { |
| 486 |
[ $encKey, $hmacKey ] = $this->getSecretKeys(); |
| 487 |
$serialized = serialize( $value ); |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
$iv = random_bytes( 16 ); |
| 494 |
$algorithm = self::getEncryptionAlgorithm(); |
| 495 |
switch ( $algorithm[0] ) { |
| 496 |
case 'openssl': |
| 497 |
$ciphertext = openssl_encrypt( $serialized, $algorithm[1], $encKey, OPENSSL_RAW_DATA, $iv ); |
| 498 |
if ( $ciphertext === false ) { |
| 499 |
throw new \UnexpectedValueException( 'Encryption failed: ' . openssl_error_string() ); |
| 500 |
} |
| 501 |
break; |
| 502 |
default: |
| 503 |
throw new LogicException( 'invalid algorithm' ); |
| 504 |
} |
| 505 |
|
| 506 |
|
| 507 |
$sealed = base64_encode( $iv ) . '.' . base64_encode( $ciphertext ); |
| 508 |
$hmac = hash_hmac( 'sha256', $sealed, $hmacKey, true ); |
| 509 |
$encrypted = base64_encode( $hmac ) . '.' . $sealed; |
| 510 |
|
| 511 |
|
| 512 |
$this->set( $key, $encrypted ); |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
public function getSecret( $key, $default = null ) { |
| 522 |
|
| 523 |
$encrypted = $this->get( $key, null ); |
| 524 |
if ( $encrypted === null ) { |
| 525 |
return $default; |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
$pieces = explode( '.', $encrypted, 4 ); |
| 534 |
if ( count( $pieces ) !== 3 ) { |
| 535 |
$ex = new RuntimeException( 'Invalid sealed-secret format' ); |
| 536 |
$this->logger->warning( $ex->getMessage(), [ 'exception' => $ex ] ); |
| 537 |
return $default; |
| 538 |
} |
| 539 |
[ $hmac, $iv, $ciphertext ] = $pieces; |
| 540 |
[ $encKey, $hmacKey ] = $this->getSecretKeys(); |
| 541 |
$integCalc = hash_hmac( 'sha256', $iv . '.' . $ciphertext, $hmacKey, true ); |
| 542 |
if ( !hash_equals( $integCalc, base64_decode( $hmac ) ) ) { |
| 543 |
$ex = new RuntimeException( 'Sealed secret has been tampered with, aborting.' ); |
| 544 |
$this->logger->warning( $ex->getMessage(), [ 'exception' => $ex ] ); |
| 545 |
return $default; |
| 546 |
} |
| 547 |
|
| 548 |
|
| 549 |
$algorithm = self::getEncryptionAlgorithm(); |
| 550 |
switch ( $algorithm[0] ) { |
| 551 |
case 'openssl': |
| 552 |
$serialized = openssl_decrypt( base64_decode( $ciphertext ), $algorithm[1], $encKey, |
| 553 |
OPENSSL_RAW_DATA, base64_decode( $iv ) ); |
| 554 |
if ( $serialized === false ) { |
| 555 |
$ex = new RuntimeException( 'Decyption failed: ' . openssl_error_string() ); |
| 556 |
$this->logger->debug( $ex->getMessage(), [ 'exception' => $ex ] ); |
| 557 |
return $default; |
| 558 |
} |
| 559 |
break; |
| 560 |
default: |
| 561 |
throw new \LogicException( 'invalid algorithm' ); |
| 562 |
} |
| 563 |
|
| 564 |
$value = unserialize( $serialized ); |
| 565 |
if ( $value === false && $serialized !== serialize( false ) ) { |
| 566 |
$value = $default; |
| 567 |
} |
| 568 |
return $value; |
| 569 |
} |
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
public function delaySave() { |
| 579 |
return $this->backend->delaySave(); |
| 580 |
} |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
public function save() { |
| 587 |
$this->backend->save(); |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
public function count(): int { |
| 597 |
$data = &$this->backend->getData(); |
| 598 |
return count( $data ); |
| 599 |
} |
| 600 |
|
| 601 |
|
| 602 |
#[\ReturnTypeWillChange] |
| 603 |
public function current() { |
| 604 |
$data = &$this->backend->getData(); |
| 605 |
return current( $data ); |
| 606 |
} |
| 607 |
|
| 608 |
|
| 609 |
#[\ReturnTypeWillChange] |
| 610 |
public function key() { |
| 611 |
$data = &$this->backend->getData(); |
| 612 |
return key( $data ); |
| 613 |
} |
| 614 |
|
| 615 |
|
| 616 |
public function next(): void { |
| 617 |
$data = &$this->backend->getData(); |
| 618 |
next( $data ); |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
public function rewind(): void { |
| 623 |
$data = &$this->backend->getData(); |
| 624 |
reset( $data ); |
| 625 |
} |
| 626 |
|
| 627 |
|
| 628 |
public function valid(): bool { |
| 629 |
$data = &$this->backend->getData(); |
| 630 |
return key( $data ) !== null; |
| 631 |
} |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
public function offsetExists( $offset ): bool { |
| 639 |
$data = &$this->backend->getData(); |
| 640 |
return isset( $data[$offset] ); |
| 641 |
} |
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
#[\ReturnTypeWillChange] |
| 652 |
public function &offsetGet( $offset ) { |
| 653 |
$data = &$this->backend->getData(); |
| 654 |
if ( !array_key_exists( $offset, $data ) ) { |
| 655 |
$ex = new LogicException( "Undefined index (auto-adds to session with a null value): $offset" ); |
| 656 |
$this->logger->debug( $ex->getMessage(), [ 'exception' => $ex ] ); |
| 657 |
} |
| 658 |
return $data[$offset]; |
| 659 |
} |
| 660 |
|
| 661 |
|
| 662 |
public function offsetSet( $offset, $value ): void { |
| 663 |
$this->set( $offset, $value ); |
| 664 |
} |
| 665 |
|
| 666 |
|
| 667 |
public function offsetUnset( $offset ): void { |
| 668 |
$this->remove( $offset ); |
| 669 |
} |
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
} |