Will django escaping ever consider context of javascript and CSS?

797 views
Skip to first unread message

Voulnet

unread,
Apr 27, 2012, 6:58:44 PM4/27/12
to django-d...@googlegroups.com
Hello provides great protection from XSS by escaping output to
webpages, but it only does it in HTML context. XSS can be executed
when user input is inserted into javascript or CSS, which have
different context and rules than HTML, so HTML context escaping
doesn't help/protect.

Are there any remote chance of django escaping extending to other
contexts beside HTML?

Tom Evans

unread,
May 3, 2012, 11:34:03 AM5/3/12
to django-d...@googlegroups.com
There is the built-in escapejs filter that works fine for making user
generated content safe.

https://docs.djangoproject.com/en/1.4/ref/templates/builtins/#escapejs

Although it would be nice to be able to alter the escaping algorithm
in use, so that one can do things like this:

{% block head %}
<link rel="stylesheet" .../>
<script type="...">
{% autoescape javascript %}
var user = '{{ user.name }}';
{% endautoescape %}
</script>
{% endblock %}

Cheers

Tom

Voulnet

unread,
May 3, 2012, 12:49:12 PM5/3/12
to django-d...@googlegroups.com

The document you linked says it doesn't make it safe to use, but rather helps in fixing syntax errors.

" escapejs

Escapes characters for use in JavaScript strings. This does not make the string safe for use in HTML, but does protect you from syntax errors when using templates to generate JavaScript/JSON."

And there is also the CSS context, using things like expression which can execute JavaScript. I'm just saying it will be great to have filtering context for those

--
You received this message because you are subscribed to the Google Groups "Django developers" group.
To post to this group, send email to django-d...@googlegroups.com.
To unsubscribe from this group, send email to django-develop...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.

Luke Plant

unread,
Jun 7, 2012, 11:19:44 AM6/7/12
to django-d...@googlegroups.com
On 03/05/12 17:49, Voulnet wrote:
> The document you linked says it doesn't make it safe to use, but rather
> helps in fixing syntax errors.
>
> " escapejs
>
> Escapes characters for use in JavaScript strings. This does not make the
> string safe for use in HTML, but does protect you from syntax errors
> when using templates to generate JavaScript/JSON."

This means that it is not safe for use *in HTML*. It does guarantee that
all the data ends up as a single javascript string literal, but that
javascript string will still need HTML escaping if you are planning on
inserting it in the DOM. This needs to be done using a javascript escape
function (not provided).

We've looked at custom escape mechanisms in the past. There are big
difficulties due to the fact that builtin filters only work correctly
with the context of HTML escaping.

Some relevant previous discussions:

http://goo.gl/XZ7Pt

http://goo.gl/T8tkx

Luke

--
OSBORN'S LAW
Variables won't, constants aren't.

Luke Plant || http://lukeplant.me.uk/
Reply all
Reply to author
Forward
0 new messages