Skip to content

Commit 23e6fee

Browse files
Avoid using scream
1 parent 825b9bb commit 23e6fee

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

src/Symfony/Component/Security/Http/Firewall/ContextListener.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ public function handle(GetResponseEvent $event)
7777
return;
7878
}
7979

80-
$token = @unserialize($serializedToken);
81-
82-
if (false === $token) {
80+
try {
81+
$token = $this->unserialize($serializedToken);
82+
} catch (\ErrorException $e) {
8383
if (null !== $this->logger) {
84-
$this->logger->warning('Failed to unserialize the security token from the session.', array('key' => $this->sessionKey, 'received' => $serializedToken));
84+
$this->logger->warning('Failed to unserialize the security token from the session.', array('key' => $this->sessionKey, 'received' => $serializedToken, 'exception' => $e));
8585
}
8686
$token = null;
8787
}
@@ -178,4 +178,27 @@ protected function refreshUser(TokenInterface $token)
178178

179179
throw new \RuntimeException(sprintf('There is no user provider for user "%s".', get_class($user)));
180180
}
181+
182+
private function unserialize($serialized)
183+
{
184+
$prevUnserializeHandler = ini_set('unserialize_callback_func', '');
185+
set_error_handler(function ($type, $message, $file, $line) {
186+
throw new \ErrorException($message, 0, $type, $file, $line);
187+
});
188+
189+
try {
190+
$unserialized = unserialize($serialized);
191+
} catch (\ErrorException $e) {
192+
// To be rethrown after restoring the error handler.
193+
}
194+
195+
ini_set('unserialize_callback_func', $prevUnserializeHandler);
196+
restore_error_handler();
197+
198+
if (isset($e)) {
199+
throw $e;
200+
}
201+
202+
return $unserialized;
203+
}
181204
}

0 commit comments

Comments
 (0)