Skip to content
This repository has been archived by the owner on Dec 2, 2021. It is now read-only.

Commit

Permalink
Additionally check if trusted option is set by context
Browse files Browse the repository at this point in the history
Otherwise it's possible to set it explicitly in the context and be trusted even when it should not have been
  • Loading branch information
zerkms committed Aug 14, 2016
1 parent e9f3d5e commit 17fffbd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Security/TwoFactor/Trusted/TrustedFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function requestAuthenticationCode(AuthenticationContextInterface $contex
if ($response instanceof Response) {

// Set trusted cookie
if ($context->isAuthenticated() && $request->get($this->trustedName)) {
if ($context->isAuthenticated() && $context->useTrustedOption() && $request->get($this->trustedName)) {
$cookie = $this->cookieManager->createTrustedCookie($request, $user);
$response->headers->setCookie($cookie);
}
Expand Down
35 changes: 35 additions & 0 deletions Tests/Security/TwoFactor/Trusted/TrustedFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,11 @@ public function requestAuthenticationCode_authenticatedAndTrustedChecked_setTrus
->method('isAuthenticated')
->will($this->returnValue(true));

$context
->expects($this->once())
->method('useTrustedOption')
->will($this->returnValue(true));

//Stub the authentication handler
$response = $this->getResponse();
$this->authHandler
Expand All @@ -354,4 +359,34 @@ public function requestAuthenticationCode_authenticatedAndTrustedChecked_setTrus

$this->trustedFilter->requestAuthenticationCode($context);
}

/**
* @test
*/
public function requestAuthenticationCode_shouldCheckIfTrustedIsAllowedByContext()
{
$context = $this->getAuthenticationContext();

$context
->expects($this->once())
->method('isAuthenticated')
->will($this->returnValue(true));

$context->expects($this->once())
->method('useTrustedOption')
->will($this->returnValue(false));

$this->authHandler
->expects($this->once())
->method('requestAuthenticationCode')
->with($context)
->will($this->returnValue(new Response('<form></form>')));

$this->cookieManager
->expects($this->never())
->method('createTrustedCookie');

$returnValue = $this->trustedFilter->requestAuthenticationCode($context);
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $returnValue);
}
}

0 comments on commit 17fffbd

Please sign in to comment.