Skip to content

Commit c51fc10

Browse files
committed
avoid fatal error on invalid session
1 parent 3d32a0b commit c51fc10

File tree

2 files changed

+76
-8
lines changed

2 files changed

+76
-8
lines changed

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,26 @@ public function handle(GetResponseEvent $event)
6666

6767
if (null === $session || null === $token = $session->get('_security_'.$this->contextKey)) {
6868
$this->context->setToken(null);
69-
} else {
70-
if (null !== $this->logger) {
71-
$this->logger->debug('Read SecurityContext from the session');
72-
}
69+
return;
70+
}
7371

74-
$token = unserialize($token);
72+
$token = unserialize($token);
7573

76-
if (null !== $token) {
77-
$token = $this->refreshUser($token);
74+
if (null !== $this->logger) {
75+
$this->logger->debug('Read SecurityContext from the session');
76+
}
77+
78+
if ($token instanceof TokenInterface) {
79+
$token = $this->refreshUser($token);
80+
} elseif (null !== $token) {
81+
if (null !== $this->logger) {
82+
$this->logger->warn(sprintf('Session includes a "%s" where a security token is expected', is_object($value) ? get_class($value) : gettype($value)));
7883
}
7984

80-
$this->context->setToken($token);
85+
$token = null;
8186
}
87+
88+
$this->context->setToken($token);
8289
}
8390

8491
/**
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony framework.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Symfony\Tests\Component\Security\Http\Firewall;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\Security\Http\Firewall\ContextListener;
16+
17+
class ContextListenerTest extends \PHPUnit_Framework_TestCase
18+
{
19+
/**
20+
* @dataProvider provideInvalidToken
21+
*/
22+
public function testInvalidTokenInSession($token)
23+
{
24+
$context = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
25+
$event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')
26+
->disableOriginalConstructor()
27+
->getMock();
28+
$request = $this->getMock('Symfony\Component\HttpFoundation\Request');
29+
$session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session')
30+
->disableOriginalConstructor()
31+
->getMock();
32+
33+
$event->expects($this->any())
34+
->method('getRequest')
35+
->will($this->returnValue($request));
36+
$request->expects($this->any())
37+
->method('hasPreviousSession')
38+
->will($this->returnValue(true));
39+
$request->expects($this->any())
40+
->method('getSession')
41+
->will($this->returnValue($session));
42+
$session->expects($this->any())
43+
->method('get')
44+
->with('_security_key123')
45+
->will($this->returnValue(serialize($token)));
46+
$context->expects($this->once())
47+
->method('setToken')
48+
->with(null);
49+
50+
$listener = new ContextListener($context, array(), 'key123');
51+
$listener->handle($event);
52+
}
53+
54+
public function provideInvalidToken()
55+
{
56+
return array(
57+
array(new \__PHP_Incomplete_Class()),
58+
array(null),
59+
);
60+
}
61+
}

0 commit comments

Comments
 (0)