_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; } }