This repository has been archived by the owner on Dec 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent remember-me cookie from being set when two-factor authenticat…
…ion is required, fixes "Bypass 2fa" #253
- Loading branch information
Showing
10 changed files
with
353 additions
and
8 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
DependencyInjection/Compiler/RememberMeServicesDecoratorCompilerPass.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,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Scheb\TwoFactorBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\ChildDefinition; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
/** | ||
* Decorates all remember-me services instances so that the remember-me cookie doesn't leak when two-factor | ||
* authentication is required. | ||
*/ | ||
class RememberMeServicesDecoratorCompilerPass implements CompilerPassInterface | ||
{ | ||
private const REMEMBER_ME_LISTENER_ID_PREFIX = 'security.authentication.listener.rememberme.'; | ||
|
||
public function process(ContainerBuilder $container) | ||
{ | ||
// Find all remember-me listener definitions | ||
$prefixLength = strlen(self::REMEMBER_ME_LISTENER_ID_PREFIX); | ||
foreach ($container->getDefinitions() as $definitionId => $definition) { | ||
if (substr($definitionId, 0, $prefixLength) === self::REMEMBER_ME_LISTENER_ID_PREFIX) { | ||
$this->decorateRememberMeServices($container, $definition); | ||
} | ||
} | ||
} | ||
|
||
private function decorateRememberMeServices(ContainerBuilder $container, Definition $authListenerDefinition): void | ||
{ | ||
// Get the remember-me services from the listener and decorate it | ||
$rememberMeServicesId = (string) $authListenerDefinition->getArgument(1); | ||
if ($rememberMeServicesId) { | ||
$decoratedServiceId = $rememberMeServicesId.'.two_factor_decorator'; | ||
$container | ||
->setDefinition($decoratedServiceId, new ChildDefinition('scheb_two_factor.security.rememberme_services_decorator')) | ||
->setDecoratedService($rememberMeServicesId) | ||
->replaceArgument(0, new Reference($decoratedServiceId.'.inner')); | ||
} | ||
} | ||
} |
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
54 changes: 54 additions & 0 deletions
54
Security/Authentication/RememberMe/RememberMeServicesDecorator.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,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Scheb\TwoFactorBundle\Security\Authentication\RememberMe; | ||
|
||
use Scheb\TwoFactorBundle\Security\Authentication\Token\TwoFactorTokenInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
use Symfony\Component\Security\Http\Logout\LogoutHandlerInterface; | ||
use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface; | ||
|
||
class RememberMeServicesDecorator implements RememberMeServicesInterface, LogoutHandlerInterface | ||
{ | ||
/** | ||
* @var RememberMeServicesInterface|LogoutHandlerInterface | ||
*/ | ||
private $decoratedRememberMeServices; | ||
|
||
public function __construct($decoratedRememberMeServices) | ||
{ | ||
$this->decoratedRememberMeServices = $decoratedRememberMeServices; | ||
} | ||
|
||
public function loginSuccess(Request $request, Response $response, TokenInterface $token) | ||
{ | ||
if ($token instanceof TwoFactorTokenInterface) { | ||
// Create a fake response to capture the remember-me cookie but not let it leak to the real response. | ||
$cookieCaptureResponse = new Response(); | ||
$this->decoratedRememberMeServices->loginSuccess($request, $cookieCaptureResponse, $token); | ||
$rememberMeCookies = $cookieCaptureResponse->headers->getCookies(); | ||
$token->setAttribute(TwoFactorTokenInterface::ATTRIBUTE_NAME_REMEMBER_ME_COOKIE, $rememberMeCookies); | ||
} else { | ||
// Not a TwoFactorToken => default behaviour | ||
$this->decoratedRememberMeServices->loginSuccess($request, $response, $token); | ||
} | ||
} | ||
|
||
public function autoLogin(Request $request) | ||
{ | ||
return $this->decoratedRememberMeServices->autoLogin($request); | ||
} | ||
|
||
public function loginFail(Request $request, \Exception $exception = null) | ||
{ | ||
$this->decoratedRememberMeServices->loginFail($request, $exception); | ||
} | ||
|
||
public function logout(Request $request, Response $response, TokenInterface $token) | ||
{ | ||
$this->decoratedRememberMeServices->logout($request, $response, $token); | ||
} | ||
} |
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
76 changes: 76 additions & 0 deletions
76
Tests/DependencyInjection/Compiler/RememberMeServicesDecoratorCompilerPassTest.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 | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Scheb\TwoFactorBundle\Tests\DependencyInjection\Compiler; | ||
|
||
use Scheb\TwoFactorBundle\DependencyInjection\Compiler\AuthenticationProviderDecoratorCompilerPass; | ||
use Scheb\TwoFactorBundle\DependencyInjection\Compiler\RememberMeServicesDecoratorCompilerPass; | ||
use Scheb\TwoFactorBundle\Tests\TestCase; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\Security\Http\Firewall\RememberMeListener; | ||
use Symfony\Component\Security\Http\RememberMe\AbstractRememberMeServices; | ||
|
||
class RememberMeServicesDecoratorCompilerPassTest extends TestCase | ||
{ | ||
/** | ||
* @var ContainerBuilder | ||
*/ | ||
private $container; | ||
|
||
/** | ||
* @var AuthenticationProviderDecoratorCompilerPass | ||
*/ | ||
private $compilerPass; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->container = new ContainerBuilder(); | ||
$this->compilerPass = new RememberMeServicesDecoratorCompilerPass(); | ||
} | ||
|
||
private function stubRememberMeListenersWithServices(array $firewalls): void | ||
{ | ||
foreach ($firewalls as $firewallName) { | ||
$rememberMeServicesId = 'rememberme_services.'.$firewallName; | ||
$rememberMeServicesDefinition = new Definition(AbstractRememberMeServices::class); | ||
$this->container->setDefinition($rememberMeServicesId, $rememberMeServicesDefinition); | ||
|
||
$listenerId = 'security.authentication.listener.rememberme.'.$firewallName; | ||
$listenerDefinition = new Definition(RememberMeListener::class); | ||
$listenerDefinition->setArgument(1, new Reference($rememberMeServicesId)); | ||
$this->container->setDefinition($listenerId, $listenerDefinition); | ||
} | ||
} | ||
|
||
private function assertContainerHasDecoratedProvider(string $rememberMeServicesId): void | ||
{ | ||
$expectedDecoratorId = $rememberMeServicesId.'.two_factor_decorator'; | ||
$expectedDecoratedId = $expectedDecoratorId.'.inner'; | ||
|
||
$this->assertTrue($this->container->hasDefinition($expectedDecoratorId), 'Must have service "'.$expectedDecoratorId.'" defined.'); | ||
|
||
$decoratorDefinition = $this->container->getDefinition($expectedDecoratorId); | ||
$decoratedServiceReference = $decoratorDefinition->getArgument(0); | ||
$this->assertEquals($expectedDecoratedId, (string) $decoratedServiceReference); | ||
$this->assertEquals($rememberMeServicesId, $decoratorDefinition->getDecoratedService()[0]); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function process_hasMultipleRemembermeServices_decorateAll(): void | ||
{ | ||
$this->stubRememberMeListenersWithServices([ | ||
'firewall1', | ||
'firewall2', | ||
]); | ||
|
||
$this->compilerPass->process($this->container); | ||
|
||
$this->assertContainerHasDecoratedProvider('rememberme_services.firewall1'); | ||
$this->assertContainerHasDecoratedProvider('rememberme_services.firewall2'); | ||
} | ||
} |
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
101 changes: 101 additions & 0 deletions
101
Tests/Security/Authentication/RememberMe/RememberMeServicesDecoratorTest.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,101 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Scheb\TwoFactorBundle\Tests\Security\Authentication\RememberMe; | ||
|
||
use PHPUnit\Framework\MockObject\MockObject; | ||
use Scheb\TwoFactorBundle\Security\Authentication\RememberMe\RememberMeServicesDecorator; | ||
use Scheb\TwoFactorBundle\Security\Authentication\Token\TwoFactorTokenInterface; | ||
use Scheb\TwoFactorBundle\Tests\TestCase; | ||
use Symfony\Component\HttpFoundation\Cookie; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface; | ||
|
||
class RememberMeServicesDecoratorTest extends TestCase | ||
{ | ||
/** | ||
* @var MockObject|Request | ||
*/ | ||
private $request; | ||
|
||
/** | ||
* @var MockObject|Response | ||
*/ | ||
private $response; | ||
|
||
/** | ||
* @var MockObject|RememberMeServicesInterface | ||
*/ | ||
private $innerRememberMeServices; | ||
|
||
/** | ||
* @var RememberMeServicesDecorator | ||
*/ | ||
private $decorator; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->request = $this->createMock(Request::class); | ||
$this->response = $this->createMock(Response::class); | ||
$this->innerRememberMeServices = $this->createMock(RememberMeServicesInterface::class); | ||
$this->decorator = new RememberMeServicesDecorator($this->innerRememberMeServices); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function loginSuccess_noATwoFactorToken_forwardCall() | ||
{ | ||
$token = $this->createMock(TokenInterface::class); | ||
$this->innerRememberMeServices | ||
->expects($this->once()) | ||
->method('loginSuccess') | ||
->with( | ||
$this->identicalTo($this->request), | ||
$this->identicalTo($this->response), | ||
$this->identicalTo($token) | ||
); | ||
|
||
$this->decorator->loginSuccess($this->request, $this->response, $token); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function loginSuccess_isTwoFactorToken_setRememberMeAttribute() | ||
{ | ||
$token = $this->createMock(TwoFactorTokenInterface::class); | ||
|
||
$responseCallback = function ($argument) { | ||
/** @var Response $argument */ | ||
$this->assertInstanceOf(Response::class, $argument); | ||
$this->assertFalse($argument === $this->response, 'Response objects must NOT be identical'); | ||
$argument->headers->setCookie(new Cookie('name', 'value')); | ||
|
||
return true; | ||
}; | ||
|
||
$this->innerRememberMeServices | ||
->expects($this->once()) | ||
->method('loginSuccess') | ||
->with( | ||
$this->identicalTo($this->request), | ||
$this->callback($responseCallback), // 2nd argument is a different Response instance | ||
$this->identicalTo($token) | ||
); | ||
|
||
$token | ||
->expects($this->once()) | ||
->method('setAttribute') | ||
->with(TwoFactorTokenInterface::ATTRIBUTE_NAME_REMEMBER_ME_COOKIE, $this->callback(function ($argument) { | ||
$this->assertContainsOnlyInstancesOf(Cookie::class, $argument); | ||
|
||
return true; | ||
})); | ||
|
||
$this->decorator->loginSuccess($this->request, $this->response, $token); | ||
} | ||
} |
Oops, something went wrong.