Skip to content

Commit c668648

Browse files
committed
feature #1364 Add CSRF protection to logout url in the user controller (rosier)
This PR was merged into the main branch. Discussion ---------- Add CSRF protection to logout url in the user controller Fixes: #1314 Related: #1312 Commits ------- 044b910 Add CSRF protection to logout url in the user controller
2 parents 0e63b36 + 044b910 commit c668648

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

config/services.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ services:
3333
App\EventSubscriber\CommentNotificationSubscriber:
3434
$sender: '%app.notifications.email_sender%'
3535

36+
Symfony\Component\Security\Http\Logout\LogoutUrlGenerator: '@security.logout_url_generator'
37+
3638
when@test:
3739
services:
3840
test.user_password_hasher:

src/Controller/UserController.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Component\Routing\Annotation\Route;
2222
use Symfony\Component\Security\Http\Attribute\CurrentUser;
2323
use Symfony\Component\Security\Http\Attribute\IsGranted;
24+
use Symfony\Component\Security\Http\Logout\LogoutUrlGenerator;
2425

2526
/**
2627
* Controller used to manage current user. The #[CurrentUser] attribute
@@ -61,14 +62,15 @@ public function changePassword(
6162
#[CurrentUser] User $user,
6263
Request $request,
6364
EntityManagerInterface $entityManager,
65+
LogoutUrlGenerator $logoutUrlGenerator,
6466
): Response {
6567
$form = $this->createForm(ChangePasswordType::class, $user);
6668
$form->handleRequest($request);
6769

6870
if ($form->isSubmitted() && $form->isValid()) {
6971
$entityManager->flush();
7072

71-
return $this->redirectToRoute('security_logout');
73+
return $this->redirect($logoutUrlGenerator->getLogoutPath());
7274
}
7375

7476
return $this->render('user/change_password.html.twig', [

tests/Controller/UserControllerTest.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,10 @@ public function testChangePassword(): void
9393
'change_password[newPassword][second]' => $newUserPassword,
9494
]);
9595

96-
$this->assertResponseRedirects(
96+
$this->assertResponseRedirects();
97+
$this->assertStringStartsWith(
9798
'/en/logout',
98-
Response::HTTP_FOUND,
99+
$client->getResponse()->headers->get('Location') ?? '',
99100
'Changing password logout the user.'
100101
);
101102
}

0 commit comments

Comments
 (0)