Mercurial > kallithea
view CONTRIBUTORS @ 6532:33b71a130b16
templates: properly escape inline JavaScript values
TLDR: Kallithea has issues with escaping values for use in inline JS.
Despite judicious poking of the code, no actual security vulnerabilities
have been found, just lots of corner-case bugs. This patch fixes those,
and hardens the code against actual security issues.
The long version:
To embed a Python value (typically a 'unicode' plain-text value) in a
larger file, it must be escaped in a context specific manner. Example:
>>> s = u'<script>alert("It\'s a trap!");</script>'
1) Escaped for insertion into HTML element context
>>> print cgi.escape(s)
<script>alert("It's a trap!");</script>
2) Escaped for insertion into HTML element or attribute context
>>> print h.escape(s)
<script>alert("It's a trap!");</script>
This is the default Mako escaping, as usually used by Kallithea.
3) Encoded as JSON
>>> print json.dumps(s)
"<script>alert(\"It's a trap!\");</script>"
4) Escaped for insertion into a JavaScript file
>>> print '(' + json.dumps(s) + ')'
("<script>alert(\"It's a trap!\");</script>")
The parentheses are not actually required for strings, but may be needed
to avoid syntax errors if the value is a number or dict (object).
5) Escaped for insertion into a HTML inline <script> element
>>> print h.js(s)
("\x3cscript\x3ealert(\"It's a trap!\");\x3c/script\x3e")
Here, we need to combine JS and HTML escaping, further complicated by
the fact that "<script>" tag contents can either be parsed in XHTML mode
(in which case '<', '>' and '&' must additionally be XML escaped) or
HTML mode (in which case '</script>' must be escaped, but not using HTML
escaping, which is not available in HTML "<script>" tags). Therefore,
the XML special characters (which can only occur in string literals) are
escaped using JavaScript string literal escape sequences.
(This, incidentally, is why modern web security best practices ban all
use of inline JavaScript...)
Unsurprisingly, Kallithea does not do (5) correctly. In most cases,
Kallithea might slap a pair of single quotes around the HTML escaped
Python value. A typical benign example:
$('#child_link').html('${_('No revisions')}');
This works in English, but if a localized version of the string contains
an apostrophe, the result will be broken JavaScript. In the more severe
cases, where the text is user controllable, it leaves the door open to
injections. In this example, the script inserts the string as HTML, so
Mako's implicit HTML escaping makes sense; but in many other cases, HTML
escaping is actually an error, because the value is not used by the
script in an HTML context.
The good news is that the HTML escaping thwarts attempts at XSS, since
it's impossible to inject syntactically valid JavaScript of any useful
complexity. It does allow JavaScript errors and gibberish to appear on
the page, though.
In these cases, the escaping has been fixed to use either the new 'h.js'
helper, which does JavaScript escaping (but not HTML escaping), OR the
new 'h.jshtml' helper (which does both), in those cases where it was
unclear if the value might be used (by the script) in an HTML context.
Some of these can probably be "relaxed" from h.jshtml to h.js later, but
for now, using h.jshtml fixes escaping and doesn't introduce new errors.
In a few places, Kallithea JSON encodes values in the controller, then
inserts the JSON (without any further escaping) into <script> tags. This
is also wrong, and carries actual risk of XSS vulnerabilities. However,
in all cases, security vulnerabilities were narrowly avoided due to other
filtering in Kallithea. (E.g. many special characters are banned from
appearing in usernames.) In these cases, the escaping has been fixed
and moved to the template, making it immediately visible that proper
escaping has been performed.
Mini-FAQ (frequently anticipated questions):
Q: Why do everything in one big, hard to review patch?
Q: Why add escaping in specific case FOO, it doesn't seem needed?
Because the goal here is to have "escape everywhere" as the default
policy, rather than identifying individual bugs and fixing them one
by one by adding escaping where needed. As such, this patch surely
introduces a lot of needless escaping. This is no different from
how Mako/Pylons HTML escape everything by default, even when not
needed: it's errs on the side of needless work, to prevent erring
on the side of skipping required (and security critical) work.
As for reviewability, the most important thing to notice is not where
escaping has been introduced, but any places where it might have been
missed (or where h.jshtml is needed, but h.js is used).
Q: The added escaping is kinda verbose/ugly.
That is not a question, but yes, I agree. Hopefully it'll encourage us
to move away from inline JavaScript altogether. That's a significantly
larger job, though; with luck this patch will keep us safe and secure
until such a time as we can implement the real fix.
Q: Why not use Mako filter syntax ("${val|h.js}")?
Because of long-standing Mako bug #140, preventing use of 'h' in
filters.
Q: Why not work around bug #140, or even use straight "${val|js}"?
Because Mako still applies the default h.escape filter before the
explicitly specified filters.
Q: Where do we go from here?
Longer term, we should stop doing variable expansions in script blocks,
and instead pass data to JS via e.g. data attributes, or asynchronously
using AJAX calls. Once we've done that, we can remove inline JavaScript
altogether in favor of separate script files, and set a strict Content
Security Policy explicitly blocking inline scripting, and thus also the
most common kind of cross-site scripting attack.
author | Søren Løvborg <sorenl@unity3d.com> |
---|---|
date | Tue, 28 Feb 2017 17:19:00 +0100 |
parents | 02cfb2197c63 |
children | a48802bf7134 |
line wrap: on
line source
List of contributors to Kallithea project: Mads Kiilerich <madski@unity3d.com> 2012-2016 Takumi IINO <trot.thunder@gmail.com> 2012-2016 Unity Technologies 2012-2016 Andrew Shadura <andrew@shadura.me> 2012 2014-2016 Dominik Ruf <dominikruf@gmail.com> 2012 2014-2016 Thomas De Schampheleire <thomas.de.schampheleire@gmail.com> 2014-2016 Étienne Gilli <etienne.gilli@gmail.com> 2015-2016 Jan Heylen <heyleke@gmail.com> 2015-2016 Robert Martinez <ntttq@inboxen.org> 2015-2016 Robert Rauch <mail@robertrauch.de> 2015-2016 Søren Løvborg <sorenl@unity3d.com> 2015-2016 Angel Ezquerra <angel.ezquerra@gmail.com> 2016 Asterios Dimitriou <steve@pci.gr> 2016 Kateryna Musina <kateryna@unity3d.com> 2016 Konstantin Veretennicov <kveretennicov@gmail.com> 2016 Oscar Curero <oscar@naiandei.net> 2016 Robert James Dennington <tinytimrob@googlemail.com> 2016 timeless@gmail.com 2016 YFdyh000 <yfdyh000@gmail.com> 2016 Aras Pranckevičius <aras@unity3d.com> 2012-2013 2015 Sean Farley <sean.michael.farley@gmail.com> 2013-2015 Christian Oyarzun <oyarzun@gmail.com> 2014-2015 Joseph Rivera <rivera.d.joseph@gmail.com> 2014-2015 Michal Čihař <michal@cihar.com> 2014-2015 Anatoly Bubenkov <bubenkoff@gmail.com> 2015 Andrew Bartlett <abartlet@catalyst.net.nz> 2015 Balázs Úr <urbalazs@gmail.com> 2015 Ben Finney <ben@benfinney.id.au> 2015 Branko Majic <branko@majic.rs> 2015 Daniel Hobley <danielh@unity3d.com> 2015 David Avigni <david.avigni@ankapi.com> 2015 Denis Blanchette <dblanchette@coveo.com> 2015 duanhongyi <duanhongyi@doopai.com> 2015 EriCSN Chang <ericsning@gmail.com> 2015 Grzegorz Krason <grzegorz.krason@gmail.com> 2015 Jiří Suchan <yed@vanyli.net> 2015 Kazunari Kobayashi <kobanari@nifty.com> 2015 Kevin Bullock <kbullock@ringworld.org> 2015 kobanari <kobanari@nifty.com> 2015 Marc Abramowitz <marc@marc-abramowitz.com> 2015 Marc Villetard <marc.villetard@gmail.com> 2015 Matthias Zilk <matthias.zilk@gmail.com> 2015 Michael Pohl <michael@mipapo.de> 2015 Michael V. DePalatis <mike@depalatis.net> 2015 Morten Skaaning <mortens@unity3d.com> 2015 Nick High <nick@silverchip.org> 2015 Niemand Jedermann <predatorix@web.de> 2015 Peter Vitt <petervitt@web.de> 2015 Ronny Pfannschmidt <opensource@ronnypfannschmidt.de> 2015 Sam Jaques <sam.jaques@me.com> 2015 Tuux <tuxa@galaxie.eu.org> 2015 Viktar Palstsiuk <vipals@gmail.com> 2015 Ante Ilic <ante@unity3d.com> 2014 Bradley M. Kuhn <bkuhn@sfconservancy.org> 2014 Calinou <calinou@opmbx.org> 2014 Daniel Anderson <daniel@dattrix.com> 2014 Henrik Stuart <hg@hstuart.dk> 2014 Ingo von Borstel <kallithea@planetmaker.de> 2014 Jelmer Vernooij <jelmer@samba.org> 2014 Jim Hague <jim.hague@acm.org> 2014 Matt Fellows <kallithea@matt-fellows.me.uk> 2014 Max Roman <max@choloclos.se> 2014 Na'Tosha Bard <natosha@unity3d.com> 2014 Rasmus Selsmark <rasmuss@unity3d.com> 2014 Tim Freund <tim@freunds.net> 2014 Travis Burtrum <android@moparisthebest.com> 2014 Zoltan Gyarmati <mr.zoltan.gyarmati@gmail.com> 2014 Marcin Kuźmiński <marcin@python-works.com> 2010-2013 xpol <xpolife@gmail.com> 2012-2013 Aparkar <aparkar@icloud.com> 2013 Dennis Brakhane <brakhane@googlemail.com> 2013 Grzegorz Rożniecki <xaerxess@gmail.com> 2013 Jonathan Sternberg <jonathansternberg@gmail.com> 2013 Leonardo Carneiro <leonardo@unity3d.com> 2013 Magnus Ericmats <magnus.ericmats@gmail.com> 2013 Martin Vium <martinv@unity3d.com> 2013 Simon Lopez <simon.lopez@slopez.org> 2013 Ton Plomp <tcplomp@gmail.com> 2013 Augusto Herrmann <augusto.herrmann@planejamento.gov.br> 2011-2012 Dan Sheridan <djs@adelard.com> 2012 Dies Koper <diesk@fast.au.fujitsu.com> 2012 Erwin Kroon <e.kroon@smartmetersolutions.nl> 2012 H Waldo G <gwaldo@gmail.com> 2012 hppj <hppj@postmage.biz> 2012 Indra Talip <indra.talip@gmail.com> 2012 mikespook 2012 nansenat16 <nansenat16@null.tw> 2012 Philip Jameson <philip.j@hostdime.com> 2012 Raoul Thill <raoul.thill@gmail.com> 2012 Stefan Engel <mail@engel-stefan.de> 2012 Tony Bussieres <t.bussieres@gmail.com> 2012 Vincent Caron <vcaron@bearstech.com> 2012 Vincent Duvert <vincent@duvert.net> 2012 Vladislav Poluhin <nuklea@gmail.com> 2012 Zachary Auclair <zach101@gmail.com> 2012 Ankit Solanki <ankit.solanki@gmail.com> 2011 Dmitri Kuznetsov 2011 Jared Bunting <jared.bunting@peachjean.com> 2011 Jason Harris <jason@jasonfharris.com> 2011 Les Peabody <lpeabody@gmail.com> 2011 Liad Shani <liadff@gmail.com> 2011 Lorenzo M. Catucci <lorenzo@sancho.ccd.uniroma2.it> 2011 Matt Zuba <matt.zuba@goodwillaz.org> 2011 Nicolas VINOT <aeris@imirhil.fr> 2011 Shawn K. O'Shea <shawn@eth0.net> 2011 Thayne Harbaugh <thayne@fusionio.com> 2011 Łukasz Balcerzak <lukaszbalcerzak@gmail.com> 2010 Andrew Kesterson <andrew@aklabs.net> cejones David A. Sjøen <david.sjoen@westcon.no> James Rhodes <jrhodes@redpointsoftware.com.au> Jonas Oberschweiber <jonas.oberschweiber@d-velop.de> larikale RhodeCode GmbH Sebastian Kreutzberger <sebastian@rhodecode.com> Steve Romanow <slestak989@gmail.com> SteveCohen Thomas <thomas@rhodecode.com> Thomas Waldmann <tw-public@gmx.de>