Last active
October 14, 2016 20:02
-
-
Save enumag/4709159 to your computer and use it in GitHub Desktop.
UserStorage with fixed security hole (http://forum.nette.org/cs/13410-bezpecnostni-dira-v-nette-v-session-zustavaji-i-administratorem-odebrane-role).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
class UserStorage extends \Nette\Http\UserStorage | |
{ | |
/** Log-out reason */ | |
const IDENTITY_CHANGED = 16; | |
/** @var \Model\UserRepository */ | |
private $userRepository; | |
/** | |
* @param \Nette\Http\Session $session | |
* @param \Model\UserRepository $userRepository | |
*/ | |
public function __construct(\Nette\Http\Session $session, \Model\UserRepository $userRepository) | |
{ | |
parent::__construct($session); | |
$this->userRepository = $userRepository; | |
} | |
/** | |
* Checks if the identity is still valid. | |
* @param \Nette\Security\IIdentity $identity | |
* @return bool | |
*/ | |
protected function isIdentityValid(\Nette\Security\IIdentity $identity) | |
{ | |
$entity = $this->userRepository->get($identity->getId()); | |
return $entity && $identity->identityHash === $entity->identityHash; | |
} | |
/** | |
* Returns and initializes $this->sessionSection. | |
* @param bool $need | |
* @return SessionSection | |
*/ | |
protected function getSessionSection($need) | |
{ | |
$section = parent::getSessionSection($need); | |
if ($section->authenticated && $section->identity instanceof \Nette\Security\IIdentity) { | |
if (!$this->isIdentityValid($section->identity)) { | |
$section->authenticated = FALSE; | |
$section->reason = self::IDENTITY_CHANGED; | |
if ($section->expireIdentity) { | |
unset($section->identity); | |
} | |
unset($section->expireTime, $section->expireDelta, $section->expireIdentity, | |
$section->expireBrowser, $section->browserCheck, $section->authTime); | |
} | |
} | |
return $section; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment