Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Security] Fail gracefully if the security token cannot be unserialized from the session #25669

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 8, 2018
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 43 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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.