Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Security] Fix infinite loop when token storage has no token #37314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ public function __construct(TokenStorageInterface $tokenStorage, AccessDecisionM
*/
public function handle(GetResponseEvent $event)
{
if (null === $token = $this->tokenStorage->getToken()) {
throw new AuthenticationCredentialsNotFoundException('A Token was not found in the TokenStorage.');
}

$request = $event->getRequest();

list($attributes) = $this->map->getPatterns($request);
Expand All @@ -59,6 +55,10 @@ public function handle(GetResponseEvent $event)
return;
}

if (null === $token = $this->tokenStorage->getToken()) {
throw new AuthenticationCredentialsNotFoundException('A Token was not found in the TokenStorage.');
}

if (!$token->isAuthenticated()) {
$token = $this->authManager->authenticate($token);
$this->tokenStorage->setToken($token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
namespace Symfony\Component\Security\Http\Tests\Firewall;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestMatcher;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
use Symfony\Component\Security\Http\AccessMap;
use Symfony\Component\Security\Http\Firewall\AccessListener;

class AccessListenerTest extends TestCase
Expand Down Expand Up @@ -181,25 +189,51 @@ public function testHandleWhenThereIsNoAccessMapEntryMatchingTheRequest()
$listener->handle($event);
}

public function testHandleWhenTheSecurityTokenStorageHasNoToken()
public function testHandleWhenTheSecurityTokenStorageHasNoTokenAndOnAnAccessControlledPathShouldThrowException()
{
$this->expectException('Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException');
$tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock();
$tokenStorage
->expects($this->any())
->method('getToken')
->willReturn(null)
;
$accessMap = new AccessMap();
$accessMap->add(new RequestMatcher('/private'), ['ROLE_USER']);

$listener = new AccessListener(
$tokenStorage,
$this->getMockBuilder('Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface')->getMock(),
$this->getMockBuilder('Symfony\Component\Security\Http\AccessMapInterface')->getMock(),
$this->getMockBuilder('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface')->getMock()
$accessListener = new AccessListener(
$tokenStorage = new TokenStorage(),
$this->createMock(AccessDecisionManagerInterface::class),
$accessMap,
$this->createMock(AuthenticationManagerInterface::class)
);

$event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')->disableOriginalConstructor()->getMock();
$request = Request::create('/private/profile');
$requestEvent = new GetResponseEvent(
$this->createMock(KernelInterface::class),
$request,
KernelInterface::MASTER_REQUEST
);

$listener->handle($event);
$accessListener->handle($requestEvent);
}

/**
* @doesNotPerformAssertions
*/
public function testHandleWhenTheSecurityTokenStorageHasNoTokenButOutOfAnAccessControlledPathShouldNotThrowException()
{
$accessMap = new AccessMap();
$accessMap->add(new RequestMatcher('/private'), ['ROLE_USER']);

$accessListener = new AccessListener(
$tokenStorage = new TokenStorage(),
$this->createMock(AccessDecisionManagerInterface::class),
$accessMap,
$this->createMock(AuthenticationManagerInterface::class)
);

$request = Request::create('/login');
$requestEvent = new GetResponseEvent(
$this->createMock(KernelInterface::class),
$request,
KernelInterface::MASTER_REQUEST
);

$accessListener->handle($requestEvent);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.