From 9a51270dba8a1f0ca282411a9da62a81a10cb6bb Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Thu, 7 Aug 2014 09:20:59 -0400 Subject: [PATCH] [1.7.x] Added a warning that remove_tags() output shouldn't be considered safe. Backport of 7efce77de2 from master --- docs/ref/templates/builtins.txt | 26 +++++++++++++++++++------- docs/ref/utils.txt | 10 +++++++++- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/docs/ref/templates/builtins.txt b/docs/ref/templates/builtins.txt index d904d06df4..ac0fc9e55c 100644 --- a/docs/ref/templates/builtins.txt +++ b/docs/ref/templates/builtins.txt @@ -1939,15 +1939,27 @@ Removes a space-separated list of [X]HTML tags from the output. For example:: - {{ value|removetags:"b span"|safe }} + {{ value|removetags:"b span" }} If ``value`` is ``"Joel a slug"`` the -output will be ``"Joel a slug"``. +unescaped output will be ``"Joel a slug"``. Note that this filter is case-sensitive. If ``value`` is ``"Joel a slug"`` the -output will be ``"Joel a slug"``. +unescaped output will be ``"Joel a slug"``. + +.. admonition:: No safety guarantee + + Note that ``removetags`` doesn't give any guarantee about its output being + HTML safe. In particular, it doesn't work recursively, so an input like + ``"ript>alert('XSS')ript>"`` won't be safe even if + you apply ``|removetags:"script"``. So if the input is user provided, + **NEVER** apply the ``safe`` filter to a ``removetags`` output. If you are + looking for something more robust, you can use the ``bleach`` Python + library, notably its `clean`_ method. + +.. _clean: http://bleach.readthedocs.org/en/latest/clean.html .. templatefilter:: rjust @@ -2064,10 +2076,10 @@ output will be ``"Joel is a slug"``. .. admonition:: No safety guarantee Note that ``striptags`` doesn't give any guarantee about its output being - entirely HTML safe, particularly with non valid HTML input. So **NEVER** - apply the ``safe`` filter to a ``striptags`` output. - If you are looking for something more robust, you can use the ``bleach`` - Python library, notably its `clean`_ method. + HTML safe, particularly with non valid HTML input. So **NEVER** apply the + ``safe`` filter to a ``striptags`` output. If you are looking for something + more robust, you can use the ``bleach`` Python library, notably its + `clean`_ method. .. _clean: http://bleach.readthedocs.org/en/latest/clean.html diff --git a/docs/ref/utils.txt b/docs/ref/utils.txt index 91104a6de6..70ac768bd5 100644 --- a/docs/ref/utils.txt +++ b/docs/ref/utils.txt @@ -597,7 +597,8 @@ escaping HTML. Tries to remove anything that looks like an HTML tag from the string, that is anything contained within ``<>``. - Absolutely NO guaranty is provided about the resulting string being entirely + + Absolutely NO guarantee is provided about the resulting string being HTML safe. So NEVER mark safe the result of a ``strip_tag`` call without escaping it first, for example with :func:`~django.utils.html.escape`. @@ -621,6 +622,13 @@ escaping HTML. Removes a space-separated list of [X]HTML tag names from the output. + Absolutely NO guarantee is provided about the resulting string being HTML + safe. In particular, it doesn't work recursively, so the output of + ``remove_tags("ript>alert('XSS')ript>", "script")`` + won't remove the "nested" script tags. So if the ``value`` is untrusted, + NEVER mark safe the result of a ``remove_tags()`` call without escaping it + first, for example with :func:`~django.utils.html.escape`. + For example:: remove_tags(value, "b span")