Skip to content

Commit

Permalink
Store session validator data once and validate only once. Refs #394 (#…
Browse files Browse the repository at this point in the history
…406)

* Store session validator data once and validate only once. Refs #394

* Don't read session validator keys that will not be validated.
  • Loading branch information
colinmollenhour committed Jan 17, 2018
1 parent 72a4503 commit de06e67
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions app/code/core/Mage/Core/Model/Session/Abstract/Varien.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class Mage_Core_Model_Session_Abstract_Varien extends Varien_Object
const VALIDATOR_SESSION_EXPIRE_TIMESTAMP = 'session_expire_timestamp';
const SECURE_COOKIE_CHECK_KEY = '_secure_cookie_check';

/** @var bool Flag true if session validator data has already been evaluated */
protected static $isValidated = FALSE;

/**
* Map of session enabled hosts
* @example array('host.name' => true)
Expand Down Expand Up @@ -406,16 +409,21 @@ public function getValidateHttpUserAgentSkip()
/**
* Validate session
*
* @param string $namespace
* @throws Mage_Core_Model_Session_Exception
* @return Mage_Core_Model_Session_Abstract_Varien
*/
public function validate()
{
if (!isset($this->_data[self::VALIDATOR_KEY])) {
$this->_data[self::VALIDATOR_KEY] = $this->getValidatorData();
// Backwards compatibility with legacy sessions (validator data stored per-namespace)
if (isset($this->_data[self::VALIDATOR_KEY])) {
$_SESSION[self::VALIDATOR_KEY] = $this->_data[self::VALIDATOR_KEY];
unset($this->_data[self::VALIDATOR_KEY]);
}
if (!isset($_SESSION[self::VALIDATOR_KEY])) {
$_SESSION[self::VALIDATOR_KEY] = $this->getValidatorData();
}
else {
if (!$this->_validate()) {
if ( ! self::$isValidated && ! $this->_validate()) {
$this->getCookie()->delete(session_name());
// throw core session exception
throw new Mage_Core_Model_Session_Exception('');
Expand All @@ -432,8 +440,9 @@ public function validate()
*/
protected function _validate()
{
$sessionData = $this->_data[self::VALIDATOR_KEY];
$sessionData = $_SESSION[self::VALIDATOR_KEY];
$validatorData = $this->getValidatorData();
self::$isValidated = TRUE; // Only validate once since the validator data is the same for every namespace

if ($this->useValidateRemoteAddr()
&& $sessionData[self::VALIDATOR_REMOTE_ADDR_KEY] != $validatorData[self::VALIDATOR_REMOTE_ADDR_KEY]) {
Expand All @@ -444,10 +453,8 @@ protected function _validate()
return false;
}

$sessionValidateHttpXForwardedForKey = $sessionData[self::VALIDATOR_HTTP_X_FORVARDED_FOR_KEY];
$validatorValidateHttpXForwardedForKey = $validatorData[self::VALIDATOR_HTTP_X_FORVARDED_FOR_KEY];
if ($this->useValidateHttpXForwardedFor()
&& $sessionValidateHttpXForwardedForKey != $validatorValidateHttpXForwardedForKey ) {
&& $sessionData[self::VALIDATOR_HTTP_X_FORVARDED_FOR_KEY] != $validatorData[self::VALIDATOR_HTTP_X_FORVARDED_FOR_KEY]) {
return false;
}
if ($this->useValidateHttpUserAgent()
Expand Down

0 comments on commit de06e67

Please sign in to comment.