From ce458c6b59f68d8aaab0fdb65386a6e8152c7fac Mon Sep 17 00:00:00 2001 From: Antoine Lamirault Date: Sat, 29 Oct 2022 17:57:17 +0200 Subject: [PATCH] [SecurityBundle] Set request stateless when firewall is stateless --- src/Symfony/Bundle/SecurityBundle/CHANGELOG.md | 1 + .../Bundle/SecurityBundle/Security/FirewallMap.php | 9 ++++++++- .../SecurityBundle/Tests/Security/FirewallMapTest.php | 6 ++++-- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Bundle/SecurityBundle/CHANGELOG.md b/src/Symfony/Bundle/SecurityBundle/CHANGELOG.md index a1ffdb0349c3a..9deb248e1365f 100644 --- a/src/Symfony/Bundle/SecurityBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/SecurityBundle/CHANGELOG.md @@ -5,6 +5,7 @@ CHANGELOG --- * Deprecate enabling bundle and not configuring it + * Add `_stateless` attribute to the request when firewall is stateless 6.2 --- diff --git a/src/Symfony/Bundle/SecurityBundle/Security/FirewallMap.php b/src/Symfony/Bundle/SecurityBundle/Security/FirewallMap.php index 21e5b8aa68279..d0151d10f9a28 100644 --- a/src/Symfony/Bundle/SecurityBundle/Security/FirewallMap.php +++ b/src/Symfony/Bundle/SecurityBundle/Security/FirewallMap.php @@ -72,7 +72,14 @@ private function getFirewallContext(Request $request): ?FirewallContext if (null === $requestMatcher || $requestMatcher->matches($request)) { $request->attributes->set('_firewall_context', $contextId); - return $this->container->get($contextId); + /** @var FirewallContext $context */ + $context = $this->container->get($contextId); + + if ($context->getConfig()?->isStateless()) { + $request->attributes->set('_stateless', true); + } + + return $context; } } diff --git a/src/Symfony/Bundle/SecurityBundle/Tests/Security/FirewallMapTest.php b/src/Symfony/Bundle/SecurityBundle/Tests/Security/FirewallMapTest.php index d174e13b5cff8..4acad02e65225 100644 --- a/src/Symfony/Bundle/SecurityBundle/Tests/Security/FirewallMapTest.php +++ b/src/Symfony/Bundle/SecurityBundle/Tests/Security/FirewallMapTest.php @@ -54,6 +54,7 @@ public function testGetListenersWithInvalidParameter() $this->assertEquals([[], null, null], $firewallMap->getListeners($request)); $this->assertNull($firewallMap->getFirewallConfig($request)); $this->assertFalse($request->attributes->has(self::ATTRIBUTE_FIREWALL_CONTEXT)); + $this->assertFalse($request->attributes->has('_stateless')); } public function testGetListeners() @@ -62,8 +63,8 @@ public function testGetListeners() $firewallContext = $this->createMock(FirewallContext::class); - $firewallConfig = new FirewallConfig('main', 'user_checker'); - $firewallContext->expects($this->once())->method('getConfig')->willReturn($firewallConfig); + $firewallConfig = new FirewallConfig('main', 'user_checker', null, true, true); + $firewallContext->expects($this->exactly(2))->method('getConfig')->willReturn($firewallConfig); $listener = function () {}; $firewallContext->expects($this->once())->method('getListeners')->willReturn([$listener]); @@ -88,5 +89,6 @@ public function testGetListeners() $this->assertEquals([[$listener], $exceptionListener, $logoutListener], $firewallMap->getListeners($request)); $this->assertEquals($firewallConfig, $firewallMap->getFirewallConfig($request)); $this->assertEquals('security.firewall.map.context.foo', $request->attributes->get(self::ATTRIBUTE_FIREWALL_CONTEXT)); + $this->assertTrue($request->attributes->get('_stateless')); } }