How to Implement CSRF Protection

How to Implement CSRF Protection

CSRF - or Cross-site request forgery - is a method by which a malicious user attempts to make your legitimate users unknowingly submit data that they don’t intend to submit.

CSRF protection works by adding a hidden field to your form that contains a value that only you and your user know. This ensures that the user - not some other entity - is submitting the given data.

Before using the CSRF protection, install it in your project:

  1. $ composer require symfony/security-csrf

Then, enable/disable the CSRF protection with the csrf_protection option (see the CSRF configuration reference for more information):

  • YAML

    1. # config/packages/framework.yaml
    2. framework:
    3. # ...
    4. csrf_protection: ~
  • XML

    1. <!-- config/packages/framework.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:framework="http://symfony.com/schema/dic/symfony"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd
    8. http://symfony.com/schema/dic/symfony
    9. https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
    10. <framework:config>
    11. <framework:csrf-protection enabled="true"/>
    12. </framework:config>
    13. </container>
  • PHP

    1. // config/packages/framework.php
    2. $container->loadFromExtension('framework', [
    3. 'csrf_protection' => null,
    4. ]);

The tokens used for CSRF protection are meant to be different for every user and they are stored in the session. That’s why a session is started automatically as soon as you render a form with CSRF protection.

Moreover, this means that you cannot fully cache pages that include CSRF protected forms. As an alternative, you can:

  • Embed the form inside an uncached ESI fragment and cache the rest of the page contents;
  • Cache the entire page and load the form via an uncached AJAX request;
  • Cache the entire page and use hinclude.js to load the CSRF token with an uncached AJAX request and replace the form field value with it.

CSRF Protection in Symfony Forms

Forms created with the Symfony Form component include CSRF tokens by default and Symfony checks them automatically, so you don’t have to do anything to be protected against CSRF attacks.

By default Symfony adds the CSRF token in a hidden field called _token, but this can be customized on a form-by-form basis:

  1. // src/Form/TaskType.php
  2. namespace App\Form;
  3. // ...
  4. use App\Entity\Task;
  5. use Symfony\Component\OptionsResolver\OptionsResolver;
  6. class TaskType extends AbstractType
  7. {
  8. // ...
  9. public function configureOptions(OptionsResolver $resolver): void
  10. {
  11. $resolver->setDefaults([
  12. 'data_class' => Task::class,
  13. // enable/disable CSRF protection for this form
  14. 'csrf_protection' => true,
  15. // the name of the hidden HTML field that stores the token
  16. 'csrf_field_name' => '_token',
  17. // an arbitrary string used to generate the value of the token
  18. // using a different string for each form improves its security
  19. 'csrf_token_id' => 'task_item',
  20. ]);
  21. }
  22. // ...
  23. }

You can also customize the rendering of the CSRF form field creating a custom form theme and using csrf_token as the prefix of the field (e.g. define {% block csrf_token_widget %} ... {% endblock %} to customize the entire form field contents).

New in version 4.3: The csrf_token form field prefix was introduced in Symfony 4.3.

CSRF Protection in Login Forms

See How to Build a Login Form for a login form that is protected from CSRF attacks. You can also configure the CSRF protection for the logout action.

Generating and Checking CSRF Tokens Manually

Although Symfony Forms provide automatic CSRF protection by default, you may need to generate and check CSRF tokens manually for example when using regular HTML forms not managed by the Symfony Form component.

Consider a HTML form created to allow deleting items. First, use the csrf_token() Twig function to generate a CSRF token in the template and store it as a hidden form field:

  1. <form action="{{ url('admin_post_delete', { id: post.id }) }}" method="post">
  2. {# the argument of csrf_token() is an arbitrary string used to generate the token #}
  3. <input type="hidden" name="token" value="{{ csrf_token('delete-item') }}"/>
  4. <button type="submit">Delete item</button>
  5. </form>

Then, get the value of the CSRF token in the controller action and use the [isCsrfTokenValid()](https://github.com/symfony/symfony/blob/4.4/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php "Symfony\Bundle\FrameworkBundle\Controller\AbstractController::isCsrfTokenValid()") to check its validity:

  1. use Symfony\Component\HttpFoundation\Request;
  2. use Symfony\Component\HttpFoundation\Response;
  3. // ...
  4. public function delete(Request $request): Response
  5. {
  6. $submittedToken = $request->request->get('token');
  7. // 'delete-item' is the same value used in the template to generate the token
  8. if ($this->isCsrfTokenValid('delete-item', $submittedToken)) {
  9. // ... do something, like deleting an object
  10. }
  11. }

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.