Skip to content

Commit

Permalink
[Security] Throw an explicit error when authenticating a token with a…
Browse files Browse the repository at this point in the history
… null user
  • Loading branch information
alexandre-daubois committed Jan 28, 2025
1 parent 5f16d09 commit 54f2ccc
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Firewall/ContextListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ public function authenticate(RequestEvent $event): void
]);

if ($token instanceof TokenInterface) {
if (!$token->getUser()) {
throw new \UnexpectedValueException(\sprintf('Cannot authenticate a "%s" token because it doesn\'t store a user.', $token::class));
}

$originalToken = $token;
$token = $this->refreshUser($token);

Expand Down
25 changes: 25 additions & 0 deletions Tests/Firewall/ContextListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Http\Firewall\ContextListener;
use Symfony\Component\Security\Http\Tests\Fixtures\NullUserToken;
use Symfony\Contracts\Service\ServiceLocatorTrait;

class ContextListenerTest extends TestCase
Expand All @@ -58,6 +59,30 @@ public function testUserProvidersNeedToImplementAnInterface()
$this->handleEventWithPreviousSession([new \stdClass()]);
}

public function testTokenReturnsNullUser()
{
$tokenStorage = new TokenStorage();
$tokenStorage->setToken(new NullUserToken());

$session = new Session(new MockArraySessionStorage());
$session->set('_security_context_key', serialize($tokenStorage->getToken()));

$request = new Request();
$request->setSession($session);
$request->cookies->set('MOCKSESSID', true);

$listener = new ContextListener($tokenStorage, [], 'context_key');

$this->expectException(\UnexpectedValueException::class);
$this->expectExceptionMessage('Cannot authenticate a "Symfony\Component\Security\Http\Tests\Fixtures\NullUserToken" token because it doesn\'t store a user.');

$listener->authenticate(new RequestEvent(
$this->createMock(HttpKernelInterface::class),
$request,
HttpKernelInterface::MAIN_REQUEST,
));
}

public function testOnKernelResponseWillAddSession()
{
$session = $this->runSessionOnKernelResponse(
Expand Down
23 changes: 23 additions & 0 deletions Tests/Fixtures/NullUserToken.php
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\Http\Tests\Fixtures;

use Symfony\Component\Security\Core\Authentication\Token\AbstractToken;
use Symfony\Component\Security\Core\User\UserInterface;

class NullUserToken extends AbstractToken
{
public function getUser(): ?UserInterface
{
return null;
}
}

0 comments on commit 54f2ccc

Please sign in to comment.