Skip to content

Commit 1149740

Browse files
committed
Documented CSRF protection in login forms
1 parent 7d87c7b commit 1149740

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
.. index::
2+
single: Security; CSRF in the Login Form
3+
4+
Using CSRF in the Login Form
5+
============================
6+
7+
When using a login form, you should make sure that you are protected for CSRF
8+
(`Cross-site request forgery`_). The Security component already has build-in support
9+
form CSRF. In this article, you'll learn how you can use it in your login form.
10+
11+
Configuring CSRF
12+
----------------
13+
14+
At first, you have to configure the security component so it can use CSRF protection.
15+
The security component needs a CSRF provider. You can set this to use the default
16+
provider provided by the Form component:
17+
18+
.. configuration-block::
19+
20+
.. code-block:: yaml
21+
22+
# app/config/security.yml
23+
security:
24+
firewalls:
25+
secured_area:
26+
# ...
27+
form_login:
28+
# ...
29+
csrf_provider: form.csrf_provider
30+
31+
.. code-block:: xml
32+
33+
<!-- app/config/config.xml -->
34+
<?xml version="1.0" encoding="UTF-8" ?>
35+
<srv:container xmlns="http://symfony.com/schema/dic/security"
36+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
37+
xmlns:srv="http://symfony.com/schema/dic/services"
38+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
39+
40+
<config>
41+
<firewall name="secured_area">
42+
<!-- ... -->
43+
44+
<form-login csrf-provider="form.csrf_provider" />
45+
</firewall>
46+
</config>
47+
</srv:container>
48+
49+
.. code-block:: php
50+
51+
// app/config/security.php
52+
$container->loadFromExtension('security', array(
53+
'firewalls' => array(
54+
'secured_area' => array(
55+
// ...
56+
'form_login' => array(
57+
// ...
58+
'csrf_provider' => 'form.csrf_provider',
59+
)
60+
)
61+
)
62+
));
63+
64+
The Security component can be configured further, but this is all information it needs
65+
to be able to use CSRF in the login form.
66+
67+
Rendering the CSRF field
68+
------------------------
69+
70+
Now the Security component checks for CSRF tokens, you have to add a *hidden* field
71+
to the login form containing the CSRF token. By default, this field is named as
72+
``_csrf_token``. That hidden field has to contain the CSRF token, which can be generated
73+
by using the ``csrf_token`` function. That function requires a token ID, which is
74+
must be set to ``authenticate`` when using the login form:
75+
76+
.. configuration-block::
77+
78+
.. code-block:: html+twig
79+
80+
{# src/Acme/SecurityBundle/Resources/views/Security/login.html.twig #}
81+
82+
{# ... #}
83+
<form action="{{ path('login_check') }}" method="post">
84+
{# ... the login fields #}
85+
86+
<input type="hidden" name="_csrf_token"
87+
value="{{ csrf_token('authenticate') }}"
88+
>
89+
90+
<button type="submit">login</button>
91+
</form>
92+
93+
.. code-block:: html+php
94+
95+
<!-- src/Acme/SecurityBundle/Resources/views/Security/login.html.php -->
96+
97+
<!-- ... -->
98+
<form action="<?php echo $view['router']->generate('login_check') ?>" method="post">
99+
<!-- ... the login fields -->
100+
101+
<input type="hidden" name="_csrf_token"
102+
value="<?php echo $view['form']->csrfToken('authenticate') ?>"
103+
>
104+
105+
<button type="submit">login</button>
106+
</form>
107+
108+
After this, you have protected your login form for CSRF attacks.
109+
110+
.. tip::
111+
112+
You can change the name of the field by setting ``csrf_parameter`` and the token
113+
ID by setting ``intention`` in your configuration.
114+
115+
.. _`Cross-site request forgery`: http://en.wikipedia.org/wiki/Cross-site_request_forgery

0 commit comments

Comments
 (0)