Skip to content

Commit 044b910

Browse files
committed
Add CSRF protection to logout url in the user controller
1 parent 6511204 commit 044b910

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

config/services.yaml

Lines changed: 2 additions & 0 deletions
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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Symfony\Component\Routing\Annotation\Route;
2323
use Symfony\Component\Security\Http\Attribute\CurrentUser;
2424
use Symfony\Component\Security\Http\Attribute\IsGranted;
25+
use Symfony\Component\Security\Http\Logout\LogoutUrlGenerator;
2526

2627
/**
2728
* Controller used to manage current user. The #[CurrentUser] attribute
@@ -63,6 +64,7 @@ public function changePassword(
6364
Request $request,
6465
UserPasswordHasherInterface $passwordHasher,
6566
EntityManagerInterface $entityManager,
67+
LogoutUrlGenerator $logoutUrlGenerator,
6668
): Response {
6769
$form = $this->createForm(ChangePasswordType::class);
6870
$form->handleRequest($request);
@@ -74,7 +76,7 @@ public function changePassword(
7476
$user->setPassword($passwordHasher->hashPassword($user, $plainPassword));
7577
$entityManager->flush();
7678

77-
return $this->redirectToRoute('security_logout');
79+
return $this->redirect($logoutUrlGenerator->getLogoutPath());
7880
}
7981

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

tests/Controller/UserControllerTest.php

Lines changed: 3 additions & 2 deletions
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)