|
| 1 | +.. index:: |
| 2 | + single: Forms; CSRF protection |
| 3 | + |
| 4 | +How to Implement CSRF Protection |
| 5 | +================================ |
| 6 | + |
| 7 | +CSRF - or `Cross-site request forgery`_ - is a method by which a malicious |
| 8 | +user attempts to make your legitimate users unknowingly submit data that |
| 9 | +they don't intend to submit. Fortunately, CSRF attacks can be prevented by |
| 10 | +using a CSRF token inside your forms. |
| 11 | + |
| 12 | +The good news is that, by default, Symfony embeds and validates CSRF tokens |
| 13 | +automatically for you. This means that you can take advantage of the CSRF |
| 14 | +protection without doing anything. In fact, every form in this chapter has |
| 15 | +taken advantage of the CSRF protection! |
| 16 | + |
| 17 | +CSRF protection works by adding a hidden field to your form - called ``_token`` |
| 18 | +by default - that contains a value that only you and your user knows. This |
| 19 | +ensures that the user - not some other entity - is submitting the given data. |
| 20 | +Symfony automatically validates the presence and accuracy of this token. |
| 21 | + |
| 22 | +The ``_token`` field is a hidden field and will be automatically rendered |
| 23 | +if you include the ``form_end()`` function in your template, which ensures |
| 24 | +that all un-rendered fields are output. |
| 25 | + |
| 26 | +.. caution:: |
| 27 | + |
| 28 | + Since the token is stored in the session, a session is started automatically |
| 29 | + as soon as you render a form with CSRF protection. |
| 30 | + |
| 31 | +The CSRF token can be customized on a form-by-form basis. For example:: |
| 32 | + |
| 33 | + use Symfony\Component\OptionsResolver\OptionsResolver; |
| 34 | + |
| 35 | + class TaskType extends AbstractType |
| 36 | + { |
| 37 | + // ... |
| 38 | + |
| 39 | + public function configureOptions(OptionsResolver $resolver) |
| 40 | + { |
| 41 | + $resolver->setDefaults(array( |
| 42 | + 'data_class' => 'AppBundle\Entity\Task', |
| 43 | + 'csrf_protection' => true, |
| 44 | + 'csrf_field_name' => '_token', |
| 45 | + // a unique key to help generate the secret token |
| 46 | + 'csrf_token_id' => 'task_item', |
| 47 | + )); |
| 48 | + } |
| 49 | + |
| 50 | + // ... |
| 51 | + } |
| 52 | + |
| 53 | +.. _form-disable-csrf: |
| 54 | + |
| 55 | +To disable CSRF protection, set the ``csrf_protection`` option to false. |
| 56 | +Customizations can also be made globally in your project. For more information, |
| 57 | +see the :ref:`form configuration reference <reference-framework-form>` |
| 58 | +section. |
| 59 | + |
| 60 | +.. note:: |
| 61 | + |
| 62 | + The ``csrf_token_id`` option is optional but greatly enhances the security |
| 63 | + of the generated token by making it different for each form. |
| 64 | + |
| 65 | +.. caution:: |
| 66 | + |
| 67 | + CSRF tokens are meant to be different for every user. This is why you |
| 68 | + need to be cautious if you try to cache pages with forms including this |
| 69 | + kind of protection. For more information, see |
| 70 | + :doc:`/cache/form_csrf_caching`. |
| 71 | + |
| 72 | +.. _`Cross-site request forgery`: http://en.wikipedia.org/wiki/Cross-site_request_forgery |
0 commit comments