Skip to content
This repository was archived by the owner on May 31, 2024. It is now read-only.

Commit 0eccbbd

Browse files
committed
Merge branch '3.4' into 4.0
* 3.4: (26 commits) [Serializer] Fixed throwing exception with option JSON_PARTIAL_OUTPUT_ON_ERROR [HttpKernel] Fix session handling: decouple "save" from setting response "private" swap filter/function and package names [HttpFoundation] Always call proxied handler::destroy() in StrictSessionHandler [HttpKernel] Fix compile error when a legacy container is fresh again Add tests for the HttpKernel request collector and redirection via cookies Uses cookies to track the requests redirection Tweaked some styles in the profiler tables Add type string to docblock for Process::setInput() [Security] Fail gracefully if the security token cannot be unserialized from the session [Form] AbstractLayoutTest - fix DOMDocument casing Run simple-phpunit with --no-suggest option [FrameworkBundle] Fix using "annotations.cached_reader" in after-removing passes bumped Symfony version to 3.4.4 updated VERSION for 3.4.3 updated CHANGELOG for 3.4.3 bumped Symfony version to 3.3.16 updated VERSION for 3.3.15 updated CHANGELOG for 3.3.15 bumped Symfony version to 2.8.34 ...
2 parents dfc0ef2 + d4487cf commit 0eccbbd

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

Http/Firewall/ContextListener.php

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class ContextListener implements ListenerInterface
4444
private $registered;
4545
private $trustResolver;
4646

47+
private static $unserializeExceptionCode = 0x37313bc;
48+
4749
/**
4850
* @param TokenStorageInterface $tokenStorage
4951
* @param iterable|UserProviderInterface[] $userProviders
@@ -91,7 +93,7 @@ public function handle(GetResponseEvent $event)
9193
return;
9294
}
9395

94-
$token = unserialize($token);
96+
$token = $this->safelyUnserialize($token);
9597

9698
if (null !== $this->logger) {
9799
$this->logger->debug('Read existing security token from the session.', array(
@@ -210,4 +212,43 @@ protected function refreshUser(TokenInterface $token)
210212

211213
throw new \RuntimeException(sprintf('There is no user provider for user "%s".', get_class($user)));
212214
}
215+
216+
private function safelyUnserialize($serializedToken)
217+
{
218+
$e = $token = null;
219+
$prevUnserializeHandler = ini_set('unserialize_callback_func', __CLASS__.'::handleUnserializeCallback');
220+
$prevErrorHandler = set_error_handler(function ($type, $msg, $file, $line, $context = array()) use (&$prevErrorHandler) {
221+
if (__FILE__ === $file) {
222+
throw new \UnexpectedValueException($msg, self::$unserializeExceptionCode);
223+
}
224+
225+
return $prevErrorHandler ? $prevErrorHandler($type, $msg, $file, $line, $context) : false;
226+
});
227+
228+
try {
229+
$token = unserialize($serializedToken);
230+
} catch (\Error $e) {
231+
} catch (\Exception $e) {
232+
}
233+
restore_error_handler();
234+
ini_set('unserialize_callback_func', $prevUnserializeHandler);
235+
if ($e) {
236+
if (!$e instanceof \UnexpectedValueException || self::$unserializeExceptionCode !== $e->getCode()) {
237+
throw $e;
238+
}
239+
if ($this->logger) {
240+
$this->logger->warning('Failed to unserialize the security token from the session.', array('key' => $this->sessionKey, 'received' => $serializedToken, 'exception' => $e));
241+
}
242+
}
243+
244+
return $token;
245+
}
246+
247+
/**
248+
* @internal
249+
*/
250+
public static function handleUnserializeCallback($class)
251+
{
252+
throw new \UnexpectedValueException('Class not found: '.$class, self::$unserializeExceptionCode);
253+
}
213254
}

Http/Tests/Firewall/ContextListenerTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ public function testInvalidTokenInSession($token)
173173
public function provideInvalidToken()
174174
{
175175
return array(
176+
array('foo'),
177+
array('O:8:"NotFound":0:{}'),
176178
array(serialize(new \__PHP_Incomplete_Class())),
177179
array(serialize(null)),
178180
array(null),

0 commit comments

Comments
 (0)