@@ -55,6 +55,22 @@ for more information):
5555 'csrf_protection' => null,
5656 ));
5757
58+ The tokens used for CSRF protection are meant to be different for every user and
59+ they are stored in the session. That's why a session is started automatically as
60+ soon as you render a form with CSRF protection.
61+
62+ .. _caching-pages-that-contain-csrf-protected-forms :
63+
64+ Moreover, this means that you cannot fully cache pages that include CSRF
65+ protected forms. As an alternative, you can:
66+
67+ * Embed the form inside an uncached :doc: `ESI fragment </http_cache/esi >` and
68+ cache the rest of the page contents;
69+ * Cache the entire page and load the form via an uncached AJAX request;
70+ * Cache the entire page and use :doc: `hinclude.js </templating/hinclude >` to
71+ load just the CSRF token with an uncached AJAX request and replace the form
72+ field value with it.
73+
5874CSRF Protection in Symfony Forms
5975--------------------------------
6076
@@ -92,35 +108,29 @@ this can be customized on a form-by-form basis::
92108 // ...
93109 }
94110
95- .. caution ::
96-
97- Since the token is stored in the session, a session is started automatically
98- as soon as you render a form with CSRF protection.
99-
100- .. caution ::
101-
102- CSRF tokens are meant to be different for every user. Beware of that when
103- caching pages that include forms containing CSRF tokens. For more
104- information, see :doc: `/http_cache/form_csrf_caching `.
105-
106111CSRF Protection in Login Forms
107112------------------------------
108113
109114See :doc: `/security/form_login_setup ` for a login form that is protected from
110115CSRF attacks.
111116
112- CSRF Protection in HTML Forms
113- -----------------------------
117+ .. _csrf-protection-in-html-forms :
118+
119+ Generating and Checking CSRF Tokens Manually
120+ --------------------------------------------
121+
122+ Although Symfony Forms provide automatic CSRF protection by default, you may
123+ need to generate and check CSRF tokens manually for example when using regular
124+ HTML forms not managed by the Symfony Form component.
114125
115- It's also possible to add CSRF protection to regular HTML forms not managed by
116- the Symfony Form component, for example the simple forms used to delete items.
117- First, use the ``csrf_token() `` function in the Twig template to generate a CSRF
118- token and store it as a hidden field of the form:
126+ Consider a simple HTML form created to allow deleting items. First, use the
127+ :ref: `csrf_token() Twig function <reference-twig-function-csrf-token >` to
128+ generate a CSRF token in the template and store it as a hidden form field:
119129
120130.. code-block :: twig
121131
122132 <form action="{{ url('admin_post_delete', { id: post.id }) }}" method="post">
123- {# the argument of csrf_token() is an arbitrary value used to generate the token #}
133+ {# the argument of csrf_token() is an arbitrary string used to generate the token #}
124134 <input type="hidden" name="token" value="{{ csrf_token('delete-item') }}" />
125135
126136 <button type="submit">Delete item</button>
0 commit comments