diff --git a/src/Symfony/Component/Security/Http/Authentication/DefaultAuthenticationFailureHandler.php b/src/Symfony/Component/Security/Http/Authentication/DefaultAuthenticationFailureHandler.php index c6ffa4527969b..2b005db5005db 100644 --- a/src/Symfony/Component/Security/Http/Authentication/DefaultAuthenticationFailureHandler.php +++ b/src/Symfony/Component/Security/Http/Authentication/DefaultAuthenticationFailureHandler.php @@ -91,7 +91,9 @@ public function onAuthenticationFailure(Request $request, AuthenticationExceptio $this->logger?->debug('Authentication failure, redirect triggered.', ['failure_path' => $options['failure_path']]); - $request->getSession()->set(SecurityRequestAttributes::AUTHENTICATION_ERROR, $exception); + if ($request->hasPreviousSession()) { + $request->getSession()->set(SecurityRequestAttributes::AUTHENTICATION_ERROR, $exception); + } return $this->httpUtils->createRedirectResponse($request, $options['failure_path']); } diff --git a/src/Symfony/Component/Security/Http/Authentication/DefaultAuthenticationSuccessHandler.php b/src/Symfony/Component/Security/Http/Authentication/DefaultAuthenticationSuccessHandler.php index cb7c23b9c812d..6521cf124e34a 100644 --- a/src/Symfony/Component/Security/Http/Authentication/DefaultAuthenticationSuccessHandler.php +++ b/src/Symfony/Component/Security/Http/Authentication/DefaultAuthenticationSuccessHandler.php @@ -103,7 +103,7 @@ protected function determineTargetUrl(Request $request): string } $firewallName = $this->getFirewallName(); - if (null !== $firewallName && $targetUrl = $this->getTargetPath($request->getSession(), $firewallName)) { + if (null !== $firewallName && $request->hasPreviousSession() && $targetUrl = $this->getTargetPath($request->getSession(), $firewallName)) { $this->removeTargetPath($request->getSession(), $firewallName); return $targetUrl; diff --git a/src/Symfony/Component/Security/Http/Tests/Authentication/DefaultAuthenticationFailureHandlerTest.php b/src/Symfony/Component/Security/Http/Tests/Authentication/DefaultAuthenticationFailureHandlerTest.php index e1f35a123bd64..d83c8eba54de9 100644 --- a/src/Symfony/Component/Security/Http/Tests/Authentication/DefaultAuthenticationFailureHandlerTest.php +++ b/src/Symfony/Component/Security/Http/Tests/Authentication/DefaultAuthenticationFailureHandlerTest.php @@ -85,6 +85,10 @@ public function testExceptionIsPersistedInSession() $this->session->expects($this->once()) ->method('set')->with(SecurityRequestAttributes::AUTHENTICATION_ERROR, $this->exception); + // hasPreviousSession + $this->session->expects($this->once())->method('getName')->willReturn('test_session_name'); + $this->request->cookies->set('test_session_name', 'session_cookie_val'); + $handler = new DefaultAuthenticationFailureHandler($this->httpKernel, $this->httpUtils, [], $this->logger); $handler->onAuthenticationFailure($this->request, $this->exception); } diff --git a/src/Symfony/Component/Security/Http/Tests/Authentication/DefaultAuthenticationSuccessHandlerTest.php b/src/Symfony/Component/Security/Http/Tests/Authentication/DefaultAuthenticationSuccessHandlerTest.php index 2d63821b42ccd..0214fc17bf32f 100644 --- a/src/Symfony/Component/Security/Http/Tests/Authentication/DefaultAuthenticationSuccessHandlerTest.php +++ b/src/Symfony/Component/Security/Http/Tests/Authentication/DefaultAuthenticationSuccessHandlerTest.php @@ -43,8 +43,10 @@ public function testRequestRedirectionsWithTargetPathInSessions() $session = $this->createMock(SessionInterface::class); $session->expects($this->once())->method('get')->with('_security.admin.target_path')->willReturn('/admin/dashboard'); $session->expects($this->once())->method('remove')->with('_security.admin.target_path'); + $session->expects($this->once())->method('getName')->willReturn('test_session_name'); $requestWithSession = Request::create('/'); $requestWithSession->setSession($session); + $requestWithSession->cookies->set('test_session_name', 'session_cookie_val'); $urlGenerator = $this->createMock(UrlGeneratorInterface::class); $urlGenerator->expects($this->any())->method('generate')->willReturn('http://localhost/login');