Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion src/Symfony/Component/Security/Http/Firewall/ContextListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class ContextListener implements ListenerInterface
private $dispatcher;
private $registered;

private static $unserializeExceptionCode = 0x37313bc;

public function __construct(TokenStorageInterface $tokenStorage, array $userProviders, $contextKey, LoggerInterface $logger = null, EventDispatcherInterface $dispatcher = null)
{
if (empty($contextKey)) {
Expand Down Expand Up @@ -77,7 +79,7 @@ public function handle(GetResponseEvent $event)
return;
}

$token = unserialize($token);
$token = $this->safelyUnserialize($token);

if (null !== $this->logger) {
$this->logger->debug('Read existing security token from the session.', array('key' => $this->sessionKey));
Expand Down Expand Up @@ -171,4 +173,43 @@ protected function refreshUser(TokenInterface $token)

throw new \RuntimeException(sprintf('There is no user provider for user "%s".', get_class($user)));
}

private function safelyUnserialize($serializedToken)
{
$e = $token = null;
$prevUnserializeHandler = ini_set('unserialize_callback_func', __CLASS__.'::handleUnserializeCallback');
$prevErrorHandler = set_error_handler(function ($type, $msg, $file, $line, $context = array()) use (&$prevErrorHandler) {
if (__FILE__ === $file) {
throw new \UnexpectedValueException($msg, self::$unserializeExceptionCode);
}

return $prevErrorHandler ? $prevErrorHandler($type, $msg, $file, $line, $context) : false;
});

try {
$token = unserialize($serializedToken);
} catch (\Error $e) {
} catch (\Exception $e) {
}
restore_error_handler();
ini_set('unserialize_callback_func', $prevUnserializeHandler);
if ($e) {
if (!$e instanceof \UnexpectedValueException || self::$unserializeExceptionCode !== $e->getCode()) {
throw $e;
}
if ($this->logger) {
$this->logger->warning('Failed to unserialize the security token from the session.', array('key' => $this->sessionKey, 'received' => $serializedToken, 'exception' => $e));
}
}

return $token;
}

/**
* @internal
*/
public static function handleUnserializeCallback($class)
{
throw new \UnexpectedValueException('Class not found: '.$class, self::$unserializeExceptionCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ public function testInvalidTokenInSession($token)
public function provideInvalidToken()
{
return array(
array('foo'),
array('O:8:"NotFound":0:{}'),
array(serialize(new \__PHP_Incomplete_Class())),
array(serialize(null)),
array(null),
Expand Down