Skip to content

Commit f1ee7c6

Browse files
committed
minor #1312 Use logout_path() helper + add CSRF protection to logout (wouterj)
This PR was merged into the main branch. Discussion ---------- Use logout_path() helper + add CSRF protection to logout The login form already had CSRF protection, but logout didn't yet in this demo application. Generally, I think it's a good idea to add CSRF protection to logout. At the very least, it avoids annoying situations where a website can force users to logout from your service on each visit. But depending on the type of application, things can also get more serious and cause actual security issues when CSRF on logout isn't enabled. Fortunately, CSRF protection on logout is quite easy using the `logout_path()` helper: it automatically knows the logout URL of the current firewall and it automatically adds the correct CSRF token to the URL. I think the logout path/url helpers are little known gems in Symfony, so let's showcase them :) Commits ------- 6fc7aeb Use logout_path() helper + add CSRF protection to logout
2 parents e05392d + 6fc7aeb commit f1ee7c6

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

config/packages/security.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ security:
5050
# The name of the route to redirect to after logging out
5151
target: homepage
5252

53+
# Secure the logout against CSRF
54+
csrf_parameter: logout
55+
csrf_token_generator: security.csrf.token_manager
56+
5357
# needed because in tests we redefine the 'main' firewall to use
5458
# HTTP Basic instead of the login form, so this firewall has
5559
# multiple authenticators

templates/base.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
</li>
8484
<li class="divider"></li>
8585
<li>
86-
<a href="{{ path('security_logout') }}">
86+
<a href="{{ logout_path() }}">
8787
<i class="fa fa-sign-out" aria-hidden="true"></i> {{ 'menu.logout'|trans }}
8888
</a>
8989
</li>

0 commit comments

Comments
 (0)