Skip to content

Commit e7d5408

Browse files
committed
minor #19862 [Security] Improve the docs related to CSRF (javiereguiluz)
This PR was squashed before being merged into the 6.4 branch. Discussion ---------- [Security] Improve the docs related to CSRF We shouldn't explain CSRF in detail (we provide a link for folks wanting to learn more about that) but I think it'd be nice if we show a simple but realistic example of the CSRF attack. I also did some tweaks in other sections of this page. Thanks. Commits ------- 6a8dbc6 [Security] Improve the docs related to CSRF
2 parents 9fd90b2 + 6a8dbc6 commit e7d5408

File tree

1 file changed

+47
-15
lines changed

1 file changed

+47
-15
lines changed

security/csrf.rst

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,44 @@
11
How to Implement CSRF Protection
22
================================
33

4-
CSRF - or `Cross-site request forgery`_ - is a method by which a malicious
5-
user attempts to make your legitimate users unknowingly submit data that
6-
they don't intend to submit.
4+
CSRF, or `Cross-site request forgery`_, is a type of attack where a malicious actor
5+
tricks a user into performing actions on a web application without their knowledge
6+
or consent.
77

8-
CSRF protection works by adding a hidden field to your form that contains a
9-
value that only you and your user know. This ensures that the user - not some
10-
other entity - is submitting the given data.
8+
The attack is based on the trust that a web application has in a user's browser
9+
(e.g. on session cookies). Here's a real example of a CSRF attack: a malicious
10+
actor could create the following website:
1111

12-
Before using the CSRF protection, install it in your project:
12+
.. code-block:: html
13+
14+
<html>
15+
<body>
16+
<form action="https://example.com/settings/update-email" method="POST">
17+
<input type="hidden" name="email" value="malicious-actor-address@some-domain.com"/>
18+
</form>
19+
<script>
20+
document.forms[0].submit();
21+
</script>
22+
23+
<!-- some content here to distract the user -->
24+
</body>
25+
</html>
26+
27+
If you visit this website (e.g. by clicking on some email link or some social
28+
network post) and you were already logged in on the ``https://example.com`` site,
29+
the malicious actor could change the email address associated to your account
30+
(effectively taking over your account) without you even being aware of it.
31+
32+
An effective way of preventing CSRF attacks is to use anti-CSRF tokens. These are
33+
unique tokens added to forms as hidden fields. The legit server validates them to
34+
ensure that the request originated from the expected source and not some other
35+
malicious website.
36+
37+
Installation
38+
------------
39+
40+
Symfony provides all the needed features to generate and validate the anti-CSRF
41+
tokens. Before using them, install this package in your project:
1342

1443
.. code-block:: terminal
1544
@@ -75,9 +104,9 @@ protected forms. As an alternative, you can:
75104
CSRF Protection in Symfony Forms
76105
--------------------------------
77106

78-
Forms created with the Symfony Form component include CSRF tokens by default
79-
and Symfony checks them automatically, so you don't have to do anything to be
80-
protected against CSRF attacks.
107+
:doc:`Symfony Forms </forms>` include CSRF tokens by default and Symfony also
108+
checks them automatically for you. So, when using Symfony Forms, you don't have
109+
o do anything to be protected against CSRF attacks.
81110

82111
.. _form-csrf-customization:
83112

@@ -117,12 +146,15 @@ You can also customize the rendering of the CSRF form field creating a custom
117146
the field (e.g. define ``{% block csrf_token_widget %} ... {% endblock %}`` to
118147
customize the entire form field contents).
119148

120-
CSRF Protection in Login Forms
121-
------------------------------
149+
.. _csrf-protection-in-login-forms:
150+
151+
CSRF Protection in Login Form and Logout Action
152+
-----------------------------------------------
153+
154+
Read the following:
122155

123-
See :ref:`form_login-csrf` for a login form that is protected from CSRF
124-
attacks. You can also configure the
125-
:ref:`CSRF protection for the logout action <reference-security-logout-csrf>`.
156+
* :ref:`CSRF Protection in Login Forms <form_login-csrf>`;
157+
* :ref:`CSRF protection for the logout action <reference-security-logout-csrf>`.
126158

127159
.. _csrf-protection-in-html-forms:
128160

0 commit comments

Comments
 (0)