Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escape strings for JavaScript using Jinja2?

Tags:

How do I escape HTML with Jinja2 so that it can be used as a string in JavaScript (jQuery)?

If I were using Django's templating system I could write:

$("#mydiv").append("{{ html_string|escapejs }}"); 

Django's |escapejs filter would escape things in html_string (eg quotes, special chars) that could break the intended use of this code block, but Jinja2 does not seem to have an equivalent filter (am I wrong here?).

Is there a cleaner solution than copying/pasting the code from Django?

like image 442
meshy Avatar asked Sep 09 '12 14:09

meshy


People also ask

How do you escape Jinja2?

To escape jinja2 syntax in a jinja2 template with Python Flask, we can put render the template code without interpretation by putting the code in the {% raw %} block.

Can you use Jinja2 in Javascript?

JsJinja lets you use your Jinja2 templates in Javascript. It compile the Jinja2 templates to Javascript with no restrictions. The js can be generated via command line jsjinja <template file> or through the {% jsjinja %} tag in the templates.

How do you write a for loop in Jinja2?

Jinja2 being a templating language has no need for wide choice of loop types so we only get for loop. For loops start with {% for my_item in my_collection %} and end with {% endfor %} . This is very similar to how you'd loop over an iterable in Python.


2 Answers

Jinja2 has nice filter tojson. If you make json from string, it will generate string enclosed in double quotes "". You can safely use it in javascript. And you don't need put quotes around by yourself.

$("#mydiv").append({{ html_string|tojson }}); 

It also escapes <>& symbols. So it is safe even when sting contains something XSS dangerous like <script>

like image 193
Alexander C Avatar answered Oct 01 '22 12:10

Alexander C


This is a escapejs filter, based on Django's one, that I wrote for use in Jinja2 templates:

_js_escapes = {         '\\': '\\u005C',         '\'': '\\u0027',         '"': '\\u0022',         '>': '\\u003E',         '<': '\\u003C',         '&': '\\u0026',         '=': '\\u003D',         '-': '\\u002D',         ';': '\\u003B',         u'\u2028': '\\u2028',         u'\u2029': '\\u2029' } # Escape every ASCII character with a value less than 32. _js_escapes.update(('%c' % z, '\\u%04X' % z) for z in xrange(32)) def jinja2_escapejs_filter(value):         retval = []         for letter in value:                 if _js_escapes.has_key(letter):                         retval.append(_js_escapes[letter])                 else:                         retval.append(letter)          return jinja2.Markup("".join(retval)) JINJA_ENVIRONMENT.filters['escapejs'] = jinja2_escapejs_filter 

Example safe usage in a template:

<script type="text/javascript"> <!-- var variableName = "{{ variableName | escapejs }}"; … //--> </script> 

When variableName is a str or unicode.

like image 45
Tometzky Avatar answered Oct 01 '22 13:10

Tometzky