123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\Session;
- use Magento\Framework\Exception\SessionException;
- use Magento\Framework\Phrase;
- /**
- * Session Validator
- */
- class Validator implements ValidatorInterface
- {
- const VALIDATOR_KEY = '_session_validator_data';
- const VALIDATOR_HTTP_USER_AGENT_KEY = 'http_user_agent';
- const VALIDATOR_HTTP_X_FORWARDED_FOR_KEY = 'http_x_forwarded_for';
- const VALIDATOR_HTTP_VIA_KEY = 'http_via';
- const VALIDATOR_REMOTE_ADDR_KEY = 'remote_addr';
- const XML_PATH_USE_REMOTE_ADDR = 'web/session/use_remote_addr';
- const XML_PATH_USE_HTTP_VIA = 'web/session/use_http_via';
- const XML_PATH_USE_X_FORWARDED = 'web/session/use_http_x_forwarded_for';
- const XML_PATH_USE_USER_AGENT = 'web/session/use_http_user_agent';
- /**
- * @var \Magento\Framework\App\Config\ScopeConfigInterface
- */
- protected $_scopeConfig;
- /**
- * @var \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress
- */
- protected $_remoteAddress;
- /**
- * @var array
- */
- protected $_skippedAgentList;
- /**
- * @var string
- */
- protected $_scopeType;
- /**
- * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
- * @param \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteAddress
- * @param string $scopeType
- * @param array $skippedUserAgentList
- */
- public function __construct(
- \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
- \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteAddress,
- $scopeType,
- array $skippedUserAgentList = []
- ) {
- $this->_scopeConfig = $scopeConfig;
- $this->_remoteAddress = $remoteAddress;
- $this->_skippedAgentList = $skippedUserAgentList;
- $this->_scopeType = $scopeType;
- }
- /**
- * Validate session
- *
- * @param SessionManagerInterface $session
- * @return void
- * @throws SessionException
- */
- public function validate(SessionManagerInterface $session)
- {
- if (!isset($_SESSION[self::VALIDATOR_KEY])) {
- $_SESSION[self::VALIDATOR_KEY] = $this->_getSessionEnvironment();
- } else {
- try {
- $this->_validate();
- } catch (SessionException $e) {
- $session->destroy(['clear_storage' => false]);
- // throw core session exception
- throw $e;
- }
- }
- }
- /**
- * Validate data
- *
- * @return bool
- * @throws SessionException
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
- */
- protected function _validate()
- {
- $sessionData = $_SESSION[self::VALIDATOR_KEY];
- $validatorData = $this->_getSessionEnvironment();
- if ($this->_scopeConfig->getValue(
- self::XML_PATH_USE_REMOTE_ADDR,
- $this->_scopeType
- ) && $sessionData[self::VALIDATOR_REMOTE_ADDR_KEY] != $validatorData[self::VALIDATOR_REMOTE_ADDR_KEY]
- ) {
- throw new SessionException(
- new Phrase(
- 'The "%1" session value is invalid. Verify and try again.',
- [self::VALIDATOR_REMOTE_ADDR_KEY]
- )
- );
- }
- if ($this->_scopeConfig->getValue(
- self::XML_PATH_USE_HTTP_VIA,
- $this->_scopeType
- ) && $sessionData[self::VALIDATOR_HTTP_VIA_KEY] != $validatorData[self::VALIDATOR_HTTP_VIA_KEY]
- ) {
- throw new SessionException(
- new Phrase(
- 'The "%1" session value is invalid. Verify and try again.',
- [self::VALIDATOR_HTTP_VIA_KEY]
- )
- );
- }
- $httpXForwardedKey = $sessionData[self::VALIDATOR_HTTP_X_FORWARDED_FOR_KEY];
- $validatorXForwarded = $validatorData[self::VALIDATOR_HTTP_X_FORWARDED_FOR_KEY];
- if ($this->_scopeConfig->getValue(
- self::XML_PATH_USE_X_FORWARDED,
- $this->_scopeType
- ) && $httpXForwardedKey != $validatorXForwarded
- ) {
- throw new SessionException(
- new Phrase(
- 'The "%1" session value is invalid. Verify and try again.',
- [self::VALIDATOR_HTTP_X_FORWARDED_FOR_KEY]
- )
- );
- }
- if ($this->_scopeConfig->getValue(
- self::XML_PATH_USE_USER_AGENT,
- $this->_scopeType
- ) && $sessionData[self::VALIDATOR_HTTP_USER_AGENT_KEY] != $validatorData[self::VALIDATOR_HTTP_USER_AGENT_KEY]
- ) {
- foreach ($this->_skippedAgentList as $agent) {
- if (preg_match('/' . $agent . '/iu', $validatorData[self::VALIDATOR_HTTP_USER_AGENT_KEY])) {
- return true;
- }
- }
- throw new SessionException(
- new Phrase(
- 'The "%1" session value is invalid. Verify and try again.',
- [self::VALIDATOR_HTTP_USER_AGENT_KEY]
- )
- );
- }
- return true;
- }
- /**
- * Prepare session environment data for validation
- *
- * @return array
- */
- protected function _getSessionEnvironment()
- {
- $parts = [
- self::VALIDATOR_REMOTE_ADDR_KEY => '',
- self::VALIDATOR_HTTP_VIA_KEY => '',
- self::VALIDATOR_HTTP_X_FORWARDED_FOR_KEY => '',
- self::VALIDATOR_HTTP_USER_AGENT_KEY => '',
- ];
- // collect ip data
- if ($this->_remoteAddress->getRemoteAddress()) {
- $parts[self::VALIDATOR_REMOTE_ADDR_KEY] = $this->_remoteAddress->getRemoteAddress();
- }
- if (isset($_ENV['HTTP_VIA'])) {
- $parts[self::VALIDATOR_HTTP_VIA_KEY] = (string)$_ENV['HTTP_VIA'];
- }
- if (isset($_ENV['HTTP_X_FORWARDED_FOR'])) {
- $parts[self::VALIDATOR_HTTP_X_FORWARDED_FOR_KEY] = (string)$_ENV['HTTP_X_FORWARDED_FOR'];
- }
- // collect user agent data
- if (isset($_SERVER['HTTP_USER_AGENT'])) {
- $parts[self::VALIDATOR_HTTP_USER_AGENT_KEY] = (string)$_SERVER['HTTP_USER_AGENT'];
- }
- return $parts;
- }
- }
|