Skip to content

Commit 34d44eb

Browse files
committed
bug symfony#53172 [SecurityBundle] Prevent to login/logout without a request context (symfonyaml)
This PR was squashed before being merged into the 6.3 branch. Discussion ---------- [SecurityBundle] Prevent to login/logout without a request context | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix symfony#53170 | License | MIT Using `Security::login()` in a context without request throws a type error See all details in the issue symfony#53170 In this PR, we prevent to use `Security::login()` and `Security::logout()` without a request context, to avoid a fatal error. Commits ------- aaa9392 [SecurityBundle] Prevent to login/logout without a request context
2 parents e20260d + aaa9392 commit 34d44eb

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

src/Symfony/Bundle/SecurityBundle/Security.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ public function getFirewallConfig(Request $request): ?FirewallConfig
6262
public function login(UserInterface $user, string $authenticatorName = null, string $firewallName = null): ?Response
6363
{
6464
$request = $this->container->get('request_stack')->getCurrentRequest();
65+
if (null === $request) {
66+
throw new LogicException('Unable to login without a request context.');
67+
}
68+
6569
$firewallName ??= $this->getFirewallConfig($request)?->getName();
6670

6771
if (!$firewallName) {
@@ -86,15 +90,18 @@ public function login(UserInterface $user, string $authenticatorName = null, str
8690
*/
8791
public function logout(bool $validateCsrfToken = true): ?Response
8892
{
93+
$request = $this->container->get('request_stack')->getMainRequest();
94+
if (null === $request) {
95+
throw new LogicException('Unable to logout without a request context.');
96+
}
97+
8998
/** @var TokenStorageInterface $tokenStorage */
9099
$tokenStorage = $this->container->get('security.token_storage');
91100

92101
if (!($token = $tokenStorage->getToken()) || !$token->getUser()) {
93102
throw new LogicException('Unable to logout as there is no logged-in user.');
94103
}
95104

96-
$request = $this->container->get('request_stack')->getMainRequest();
97-
98105
if (!$firewallConfig = $this->container->get('security.firewall.map')->getFirewallConfig($request)) {
99106
throw new LogicException('Unable to logout as the request is not behind a firewall.');
100107
}

src/Symfony/Bundle/SecurityBundle/Tests/SecurityTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,28 @@ public function testLoginWithoutAuthenticatorThrows()
252252
$security->login($user);
253253
}
254254

255+
public function testLoginWithoutRequestContext()
256+
{
257+
$requestStack = new RequestStack();
258+
$user = $this->createMock(UserInterface::class);
259+
260+
$container = $this->createMock(ContainerInterface::class);
261+
$container
262+
->expects($this->atLeastOnce())
263+
->method('get')
264+
->willReturnMap([
265+
['request_stack', $requestStack],
266+
])
267+
;
268+
269+
$security = new Security($container, ['main' => null]);
270+
271+
$this->expectException(\LogicException::class);
272+
$this->expectExceptionMessage('Unable to login without a request context.');
273+
274+
$security->login($user);
275+
}
276+
255277
public function testLogout()
256278
{
257279
$request = new Request();
@@ -458,6 +480,27 @@ public function testLogoutWithValidCsrf()
458480
$this->assertEquals('a custom response', $response->getContent());
459481
}
460482

483+
public function testLogoutWithoutRequestContext()
484+
{
485+
$requestStack = new RequestStack();
486+
487+
$container = $this->createMock(ContainerInterface::class);
488+
$container
489+
->expects($this->atLeastOnce())
490+
->method('get')
491+
->willReturnMap([
492+
['request_stack', $requestStack],
493+
])
494+
;
495+
496+
$security = new Security($container, ['main' => null]);
497+
498+
$this->expectException(\LogicException::class);
499+
$this->expectExceptionMessage('Unable to logout without a request context.');
500+
501+
$security->logout();
502+
}
503+
461504
private function createContainer(string $serviceId, object $serviceObject): ContainerInterface
462505
{
463506
return new ServiceLocator([$serviceId => fn () => $serviceObject]);

0 commit comments

Comments
 (0)