8000 Simplified the docs about caching pages with CSRF forms by javiereguiluz · Pull Request #10877 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

Simplified the docs about caching pages with CSRF forms #10877

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions _build/redirection_map
Original file line number Diff line number Diff line change
Expand Up @@ -401,3 +401,4 @@
/weblink /web_link
/components/weblink /components/web_link
/frontend/encore/installation-no-flex /frontend/encore/installation
/http_cache/form_csrf_caching /security/csrf
2 changes: 1 addition & 1 deletion forms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ Learn more
/form/*
/controller/upload_file
/reference/forms/types
/http_cache/form_csrf_caching
/security/csrf

.. _`Symfony Form component`: https://github.com/symfony/form
.. _`DateTime`: https://php.net/manual/en/class.datetime.php
Expand Down
43 changes: 0 additions & 43 deletions http_cache/form_csrf_caching.rst

This file was deleted.

2 changes: 1 addition & 1 deletion http_cache/varnish.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ at least for some parts of the site, e.g. when using forms with
:doc:`CSRF Protection </security/csrf>`. In this situation, make sure to
:doc:`only start a session when actually needed </session/avoid_session_start>`
and clear the session when it is no longer needed. Alternatively, you can look
into :doc:`/http_cache/form_csrf_caching`.
into :doc:`/security/csrf`.

Cookies created in JavaScript and used only in the frontend, e.g. when using
Google Analytics, are nonetheless sent to the server. These cookies are not
Expand Down
1 change: 0 additions & 1 deletion performance.rst
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ Learn more
----------

* :doc:`/http_cache/varnish`
* :doc:`/http_cache/form_csrf_caching`

.. _`byte code caches`: https://en.wikipedia.org/wiki/List_of_PHP_accelerators
.. _`OPcache`: https://php.net/manual/en/book.opcache.php
Expand Down
28 changes: 17 additions & 11 deletions security/csrf.rst
8000
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@ for more information):
'csrf_protection' => null,
));

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.

.. _caching-pages-that-contain-csrf-protected-forms:

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 :doc:`ESI fragment </http_cache/esi>` 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 :doc:`hinclude.js </templating/hinclude>` to
load just the CSRF token with an uncached AJAX request and replace the form
field value with it.

CSRF Protection in Symfony Forms
--------------------------------

Expand Down Expand Up @@ -92,17 +108,6 @@ this can be customized on a form-by-form basis::
// ...
}

.. caution::

Since the token is stored in the session, a session is started automatically
as soon as you render a form with CSRF protection.

.. caution::

CSRF tokens are meant to be different for every user. Beware of that when
caching pages that include forms containing CSRF tokens. For more
information, see :doc:`/http_cache/form_csrf_caching`.

CSRF Protection in Login Forms
------------------------------

Expand All @@ -113,6 +118,7 @@ CSRF Protection in HTML Forms
-----------------------------

.. versionadded:: 4.1

In Symfony versions prior to 4.1, CSRF support required installing the
Symfony Form component even if you didn't use it.

Expand Down
0