Skip to content

Commit 81bcdbd

Browse files
committed
Merge branch '4.2'
* 4.2: Improved a reference Simplified the docs about caching pages with CSRF forms Fix words Minor rewords in the CSRF docs
2 parents f760075 + 51688b8 commit 81bcdbd

File tree

8 files changed

+38
-69
lines changed

8 files changed

+38
-69
lines changed

_build/redirection_map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,3 +401,4 @@
401401
/weblink /web_link
402402
/components/weblink /components/web_link
403403
/frontend/encore/installation-no-flex /frontend/encore/installation
404+
/http_cache/form_csrf_caching /security/csrf

event_dispatcher/before_after_filters.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ A controller that implements this interface looks like this::
106106
Creating an Event Subscriber
107107
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
108108

109-
Next, you'll need to create an event listener, which will hold the logic
109+
Next, you'll need to create an event subscriber, which will hold the logic
110110
that you want to be executed before your controllers. If you're not familiar with
111-
event listeners, you can learn more about them at :doc:`/event_dispatcher`::
111+
event subscribers, you can learn more about them at :doc:`/event_dispatcher`::
112112

113113
// src/EventSubscriber/TokenSubscriber.php
114114
namespace App\EventSubscriber;

forms.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ Learn more
714714
/form/*
715715
/controller/upload_file
716716
/reference/forms/types
717-
/http_cache/form_csrf_caching
717+
/security/csrf
718718

719719
.. _`Symfony Form component`: https://github.com/symfony/form
720720
.. _`DateTime`: https://php.net/manual/en/class.datetime.php

http_cache/form_csrf_caching.rst

Lines changed: 0 additions & 43 deletions
This file was deleted.

http_cache/varnish.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ at least for some parts of the site, e.g. when using forms with
6565
:doc:`CSRF Protection </security/csrf>`. In this situation, make sure to
6666
:doc:`only start a session when actually needed </session/avoid_session_start>`
6767
and clear the session when it is no longer needed. Alternatively, you can look
68-
into :doc:`/http_cache/form_csrf_caching`.
68+
into :ref:`caching pages that contain CSRF protected forms <caching-pages-that-contain-csrf-protected-forms>`.
6969

7070
Cookies created in JavaScript and used only in the frontend, e.g. when using
7171
Google Analytics, are nonetheless sent to the server. These cookies are not

performance.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ Learn more
138138
----------
139139

140140
* :doc:`/http_cache/varnish`
141-
* :doc:`/http_cache/form_csrf_caching`
142141

143142
.. _`byte code caches`: https://en.wikipedia.org/wiki/List_of_PHP_accelerators
144143
.. _`OPcache`: https://php.net/manual/en/book.opcache.php

reference/twig_reference.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ form_rest
277277
Renders all fields that have not yet been rendered, more information in
278278
:ref:`the Twig Form reference <reference-forms-twig-rest>`.
279279

280+
.. _reference-twig-function-csrf-token:
281+
280282
csrf_token
281283
~~~~~~~~~~
282284

@@ -285,10 +287,10 @@ csrf_token
285287
{{ csrf_token(intention) }}
286288
287289
``intention``
288-
**type**: ``string``
290+
**type**: ``string`` - an arbitrary string used to generate the token value.
289291

290-
Renders a CSRF token. Use this function if you want CSRF protection without
291-
creating a form.
292+
Renders a CSRF token. Use this function if you want :doc:`CSRF protection </security/csrf>`
293+
in a regular HTML form not managed by the Symfony Form component.
292294

293295
is_granted
294296
~~~~~~~~~~

security/csrf.rst

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
5874
CSRF 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-
106111
CSRF Protection in Login Forms
107112
------------------------------
108113

109114
See :doc:`/security/form_login_setup` for a login form that is protected from
110115
CSRF 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

Comments
 (0)