From 05746b7068827247fce84b8fa0f19b3f254d20d2 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Mon, 27 May 2019 10:59:43 +0000 Subject: [PATCH] [IMP] *: script-safe JSON serialisation * add a json_scriptsafe module, which re-exports the stdlib's json but provides a script-safe `dumps` function, because of the way it works the script-safe version is fine / safe to use outside script contexts, so there's no need to provide different functions to qweb contexts * make the json.dumps in qweb contexts diff --git a/addons/web/models/ir_http.py b/addons/web/models/ir_http.py index f2bfc70f9b4..6c3dac17193 100644 --- a/addons/web/models/ir_http.py +++ b/addons/web/models/ir_http.py @@ -18,7 +18,7 @@ class Http(models.AbstractModel): def webclient_rendering_context(self): return { 'menu_data': request.env['ir.ui.menu'].load_menus(request.session.debug), - 'session_info': json.dumps(self.session_info()), + 'session_info': self.session_info(), } def session_info(self): diff --git a/addons/web/views/webclient_templates.xml b/addons/web/views/webclient_templates.xml index aa3f67af07d..e67005f0eaf 100644 --- a/addons/web/views/webclient_templates.xml +++ b/addons/web/views/webclient_templates.xml @@ -833,7 +833,7 @@ + + + """ + }) + rendered = view.render({'malicious': '1 but doesn't + interpret anything else, this means standard htmlescaping does not + work (it breaks double quotes, and e.g. `<` will become `<` *in + the resulting JSON/JS* not just inside the page). + + However, failing to escape embedded json means the json strings could + contains `` and thus become XSS vector. + + The solution turns out to be very simple: use JSON-level unicode + escapes for HTML-unsafe characters (e.g. "<" -> "\u003C". This removes + the XSS issue without breaking the json, and there is no difference to + the end result once it's been parsed back from JSON. So it will work + properly even for HTML attributes or raw text. + + Also handle U+2028 and U+2029 the same way just in case as these are + interpreted as newlines in javascript but not in JSON, which could + lead to oddities and issues. + + .. warning:: + + except inside