Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commitcd427c3

Browse files
[Security] Throw an explicit error when authenticating a token with a null user
1 parentcb5fdaf commitcd427c3

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

‎src/Symfony/Component/Security/Http/Firewall/ContextListener.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ public function authenticate(RequestEvent $event): void
123123
]);
124124

125125
if ($tokeninstanceof TokenInterface) {
126+
if (!$token->getUser()) {
127+
thrownew \UnexpectedValueException(\sprintf('Cannot authenticate a "%s" token because it doesn\'t store a user.',$token::class));
128+
}
129+
126130
$originalToken =$token;
127131
$token =$this->refreshUser($token);
128132

‎src/Symfony/Component/Security/Http/Tests/Firewall/ContextListenerTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
useSymfony\Component\Security\Core\User\UserInterface;
3737
useSymfony\Component\Security\Core\User\UserProviderInterface;
3838
useSymfony\Component\Security\Http\Firewall\ContextListener;
39+
useSymfony\Component\Security\Http\Tests\Fixtures\NullUserToken;
3940
useSymfony\Contracts\Service\ServiceLocatorTrait;
4041

4142
class ContextListenerTestextends TestCase
@@ -58,6 +59,30 @@ public function testUserProvidersNeedToImplementAnInterface()
5859
$this->handleEventWithPreviousSession([new \stdClass()]);
5960
}
6061

62+
publicfunctiontestTokenReturnsNullUser()
63+
{
64+
$tokenStorage =newTokenStorage();
65+
$tokenStorage->setToken(newNullUserToken());
66+
67+
$session =newSession(newMockArraySessionStorage());
68+
$session->set('_security_context_key',serialize($tokenStorage->getToken()));
69+
70+
$request =newRequest();
71+
$request->setSession($session);
72+
$request->cookies->set('MOCKSESSID',true);
73+
74+
$listener =newContextListener($tokenStorage, [],'context_key');
75+
76+
$this->expectException(\UnexpectedValueException::class);
77+
$this->expectExceptionMessage('Cannot authenticate a "Symfony\Component\Security\Http\Tests\Fixtures\NullUserToken" token because it doesn\'t store a user.');
78+
79+
$listener->authenticate(newRequestEvent(
80+
$this->createMock(HttpKernelInterface::class),
81+
$request,
82+
HttpKernelInterface::MAIN_REQUEST,
83+
));
84+
}
85+
6186
publicfunctiontestOnKernelResponseWillAddSession()
6287
{
6388
$session =$this->runSessionOnKernelResponse(
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\Security\Http\Tests\Fixtures;
13+
14+
useSymfony\Component\Security\Core\Authentication\Token\AbstractToken;
15+
useSymfony\Component\Security\Core\User\UserInterface;
16+
17+
class NullUserTokenextends AbstractToken
18+
{
19+
publicfunctiongetUser(): ?UserInterface
20+
{
21+
returnnull;
22+
}
23+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp