-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
security #cve-2018-11406 clear CSRF tokens when the user is logged out
* cve-2018-11406-2.7: clear CSRF tokens when the user is logged out
- Loading branch information
Showing
15 changed files
with
327 additions
and
4 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
...ecurityBundle/DependencyInjection/Compiler/RegisterCsrfTokenClearingLogoutHandlerPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
/** | ||
* @author Christian Flothmann <christian.flothmann@sensiolabs.de> | ||
*/ | ||
class RegisterCsrfTokenClearingLogoutHandlerPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
if (!$container->has('security.logout_listener') || !$container->has('security.csrf.token_storage')) { | ||
return; | ||
} | ||
|
||
$csrfTokenStorage = $container->findDefinition('security.csrf.token_storage'); | ||
$csrfTokenStorageClass = $container->getParameterBag()->resolveValue($csrfTokenStorage->getClass()); | ||
|
||
if (!is_subclass_of($csrfTokenStorageClass, 'Symfony\Component\Security\Csrf\TokenStorage\ClearableTokenStorageInterface')) { | ||
return; | ||
} | ||
|
||
$container->register('security.logout.handler.csrf_token_clearing', 'Symfony\Component\Security\Http\Logout\CsrfTokenClearingLogoutHandler') | ||
->addArgument(new Reference('security.csrf.token_storage')) | ||
->setPublic(false); | ||
|
||
$container->findDefinition('security.logout_listener')->addMethodCall('addHandler', array(new Reference('security.logout.handler.csrf_token_clearing'))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...y/Bundle/SecurityBundle/Tests/Functional/app/LogoutWithoutSessionInvalidation/bundles.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
use Symfony\Bundle\SecurityBundle\SecurityBundle; | ||
use Symfony\Bundle\FrameworkBundle\FrameworkBundle; | ||
|
||
return array( | ||
new FrameworkBundle(), | ||
new SecurityBundle(), | ||
); |
26 changes: 26 additions & 0 deletions
26
...ny/Bundle/SecurityBundle/Tests/Functional/app/LogoutWithoutSessionInvalidation/config.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
imports: | ||
- { resource: ./../config/framework.yml } | ||
|
||
security: | ||
encoders: | ||
Symfony\Component\Security\Core\User\User: plaintext | ||
|
||
providers: | ||
in_memory: | ||
memory: | ||
users: | ||
johannes: { password: test, roles: [ROLE_USER] } | ||
|
||
firewalls: | ||
default: | ||
form_login: | ||
check_path: login | ||
remember_me: true | ||
require_previous_session: false | ||
remember_me: | ||
always_remember_me: true | ||
key: key | ||
logout: | ||
invalidate_session: false | ||
anonymous: ~ | ||
stateless: true |
5 changes: 5 additions & 0 deletions
5
...y/Bundle/SecurityBundle/Tests/Functional/app/LogoutWithoutSessionInvalidation/routing.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
login: | ||
path: /login | ||
|
||
logout: | ||
path: /logout |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/Symfony/Component/Security/Csrf/TokenStorage/ClearableTokenStorageInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Security\Csrf\TokenStorage; | ||
|
||
/** | ||
* @author Christian Flothmann <christian.flothmann@sensiolabs.de> | ||
*/ | ||
interface ClearableTokenStorageInterface extends TokenStorageInterface | ||
{ | ||
/** | ||
* Removes all CSRF tokens. | ||
*/ | ||
public function clear(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/Symfony/Component/Security/Http/Logout/CsrfTokenClearingLogoutHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Security\Http\Logout; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
use Symfony\Component\Security\Csrf\TokenStorage\ClearableTokenStorageInterface; | ||
|
||
/** | ||
* @author Christian Flothmann <christian.flothmann@sensiolabs.de> | ||
*/ | ||
class CsrfTokenClearingLogoutHandler implements LogoutHandlerInterface | ||
{ | ||
private $csrfTokenStorage; | ||
|
||
public function __construct(ClearableTokenStorageInterface $csrfTokenStorage) | ||
{ | ||
$this->csrfTokenStorage = $csrfTokenStorage; | ||
} | ||
|
||
public function logout(Request $request, Response $response, TokenInterface $token) | ||
{ | ||
$this->csrfTokenStorage->clear(); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
src/Symfony/Component/Security/Http/Tests/Logout/CsrfTokenClearingLogoutHandlerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Security\Http\Tests\Logout; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpFoundation\Session\Session; | ||
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; | ||
use Symfony\Component\Security\Csrf\TokenStorage\SessionTokenStorage; | ||
use Symfony\Component\Security\Http\Logout\CsrfTokenClearingLogoutHandler; | ||
|
||
class CsrfTokenClearingLogoutHandlerTest extends TestCase | ||
{ | ||
private $session; | ||
private $csrfTokenStorage; | ||
private $csrfTokenClearingLogoutHandler; | ||
|
||
protected function setUp() | ||
{ | ||
$this->session = new Session(new MockArraySessionStorage()); | ||
$this->csrfTokenStorage = new SessionTokenStorage($this->session, 'foo'); | ||
$this->csrfTokenStorage->setToken('foo', 'bar'); | ||
$this->csrfTokenStorage->setToken('foobar', 'baz'); | ||
$this->csrfTokenClearingLogoutHandler = new CsrfTokenClearingLogoutHandler($this->csrfTokenStorage); | ||
} | ||
|
||
public function testCsrfTokenCookieWithSameNamespaceIsRemoved() | ||
{ | ||
$this->assertSame('bar', $this->session->get('foo/foo')); | ||
$this->assertSame('baz', $this->session->get('foo/foobar')); | ||
|
||
$this->csrfTokenClearingLogoutHandler->logout(new Request(), new Response(), $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock()); | ||
|
||
$this->assertFalse($this->csrfTokenStorage->hasToken('foo')); | ||
$this->assertFalse($this->csrfTokenStorage->hasToken('foobar')); | ||
|
||
$this->assertFalse($this->session->has('foo/foo')); | ||
$this->assertFalse($this->session->has('foo/foobar')); | ||
} | ||
|
||
public function testCsrfTokenCookieWithDifferentNamespaceIsNotRemoved() | ||
{ | ||
$barNamespaceCsrfSessionStorage = new SessionTokenStorage($this->session, 'bar'); | ||
$barNamespaceCsrfSessionStorage->setToken('foo', 'bar'); | ||
$barNamespaceCsrfSessionStorage->setToken('foobar', 'baz'); | ||
|
||
$this->assertSame('bar', $this->session->get('foo/foo')); | ||
$this->assertSame('baz', $this->session->get('foo/foobar')); | ||
$this->assertSame('bar', $this->session->get('bar/foo')); | ||
$this->assertSame('baz', $this->session->get('bar/foobar')); | ||
|
||
$this->csrfTokenClearingLogoutHandler->logout(new Request(), new Response(), $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock()); | ||
|
||
$this->assertTrue($barNamespaceCsrfSessionStorage->hasToken('foo')); | ||
$this->assertTrue($barNamespaceCsrfSessionStorage->hasToken('foobar')); | ||
$this->assertSame('bar', $barNamespaceCsrfSessionStorage->getToken('foo')); | ||
$this->assertSame('baz', $barNamespaceCsrfSessionStorage->getToken('foobar')); | ||
$this->assertFalse($this->csrfTokenStorage->hasToken('foo')); | ||
$this->assertFalse($this->csrfTokenStorage->hasToken('foobar')); | ||
|
||
$this->assertFalse($this->session->has('foo/foo')); | ||
$this->assertFalse($this->session->has('foo/foobar')); | ||
$this->assertSame('bar', $this->session->get('bar/foo')); | ||
$this->assertSame('baz', $this->session->get('bar/foobar')); | ||
} | ||
} |
Oops, something went wrong.