-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Documented CSRF protection in login forms #3327
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fafa019
[Book][Validation] validate method in Validator returns ContraintViol…
zczapran add8389
applies WouterJ suggestion
zczapran 7d87c7b
Small fixes
wouterj 1149740
Documented CSRF protection in login forms
wouterj 7f4b3e8
Added references
wouterj 3a073d5
Fixed comments
wouterj d1e6b4f
Updated toctree and index
wouterj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
.. index:: | ||
single: Security; CSRF in the Login Form | ||
|
||
Using CSRF in the Login Form | ||
============================ | ||
|
||
When using a login form, you should make sure that you are protected against CSRF | ||
(`Cross-site request forgery`_). The Security component already has built-in support | ||
for CSRF. In this article you'll learn how you can use it in your login form. | ||
|
||
Configuring CSRF | ||
---------------- | ||
|
||
At first, you have to configure the Security component so it can use CSRF protection. | ||
The Security component needs a CSRF provider. You can set this to use the default | ||
provider available in the Form component: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
|
||
# app/config/security.yml | ||
security: | ||
firewalls: | ||
secured_area: | ||
# ... | ||
form_login: | ||
# ... | ||
csrf_provider: form.csrf_provider | ||
|
||
.. code-block:: xml | ||
|
||
<!-- app/config/config.xml --> | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<srv:container xmlns="http://symfony.com/schema/dic/security" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:srv="http://symfony.com/schema/dic/services" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<config> | ||
<firewall name="secured_area"> | ||
<!-- ... --> | ||
|
||
<form-login csrf-provider="form.csrf_provider" /> | ||
</firewall> | ||
</config> | ||
</srv:container> | ||
|
||
.. code-block:: php | ||
|
||
// app/config/security.php | ||
$container->loadFromExtension('security', array( | ||
'firewalls' => array( | ||
'secured_area' => array( | ||
// ... | ||
'form_login' => array( | ||
// ... | ||
'csrf_provider' => 'form.csrf_provider', | ||
) | ||
) | ||
) | ||
)); | ||
|
||
The Security component can be configured further, but this is all information it needs | ||
to be able to use CSRF in the login form. | ||
|
||
Rendering the CSRF field | ||
------------------------ | ||
|
||
Now the Security component checks for CSRF tokens, you have to add a *hidden* field | ||
to the login form containing the CSRF token. By default, this field is named as | ||
``_csrf_token``. That hidden field has to contain the CSRF token, which can be generated | ||
by using the ``csrf_token`` function. That function requires a token ID, which must | ||
be set to ``authenticate`` when using the login form: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: html+twig | ||
|
||
{# src/Acme/SecurityBundle/Resources/views/Security/login.html.twig #} | ||
|
||
{# ... #} | ||
<form action="{{ path('login_check') }}" method="post"> | ||
{# ... the login fields #} | ||
|
||
<input type="hidden" name="_csrf_token" | ||
value="{{ csrf_token('authenticate') }}" | ||
> | ||
|
||
<button type="submit">login</button> | ||
</form> | ||
|
||
.. code-block:: html+php | ||
|
||
<!-- src/Acme/SecurityBundle/Resources/views/Security/login.html.php --> | ||
|
||
<!-- ... --> | ||
<form action="<?php echo $view['router']->generate('login_check') ?>" method="post"> | ||
<!-- ... the login fields --> | ||
|
||
<input type="hidden" name="_csrf_token" | ||
value="<?php echo $view['form']->csrfToken('authenticate') ?>" | ||
> | ||
|
||
<button type="submit">login</button> | ||
</form> | ||
|
||
After this, you have protected your login form against CSRF attacks. | ||
|
||
.. tip:: | ||
|
||
You can change the name of the field by setting ``csrf_parameter`` and change | ||
the token ID by setting ``intention`` in your configuration: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
|
||
# app/config/security.yml | ||
security: | ||
firewalls: | ||
secured_area: | ||
# ... | ||
form_login: | ||
# ... | ||
csrf_parameter: _csrf_security_token | ||
intention: a_private_string | ||
|
||
.. code-block:: xml | ||
|
||
<!-- app/config/config.xml --> | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<srv:container xmlns="http://symfony.com/schema/dic/security" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:srv="http://symfony.com/schema/dic/services" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<config> | ||
<firewall name="secured_area"> | ||
<!-- ... --> | ||
|
||
<form-login csrf-parameter="_csrf_security_token" | ||
intention="a_private_string" /> | ||
</firewall> | ||
</config> | ||
</srv:container> | ||
|
||
.. code-block:: php | ||
|
||
// app/config/security.php | ||
$container->loadFromExtension('security', array( | ||
'firewalls' => array( | ||
'secured_area' => array( | ||
// ... | ||
'form_login' => array( | ||
// ... | ||
'csrf_parameter' => '_csrf_security_token', | ||
'intention' => 'a_private_string', | ||
) | ||
) | ||
) | ||
)); | ||
|
||
.. _`Cross-site request forgery`: http://en.wikipedia.org/wiki/Cross-site_request_forgery |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ Security | |
custom_provider | ||
custom_authentication_provider | ||
target_path | ||
csrf_in_login_form |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so this is not done automatically when i do
{{ form(form) }}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there is no form variable available when using login forms afaik