From 419f4ac5db1ffaa4d5dabd40dbaf2b5eff0af5a9 Mon Sep 17 00:00:00 2001 From: Richard Mathot Date: Wed, 21 Sep 2016 15:27:14 +0200 Subject: [PATCH] [FIX] tools: prevent html_translate crash with malformed HTML If an user stores malformed HTML code in a *translatable HTML field*, the ORM won't prevent it (this could be a fix for master as it breaks existing behaviours) and it could be rendered anyway by browsers. When reading/prefetching the record, the translation function was crashing because malformed HTML is obviously malformed XML (the translation function uses XML internals). This fix will return the source value as a fallback if the HTML field is malformed (and thus not translatable) + log the error. --- openerp/tools/translate.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/openerp/tools/translate.py b/openerp/tools/translate.py index 34de366d56e..6929b31f954 100644 --- a/openerp/tools/translate.py +++ b/openerp/tools/translate.py @@ -305,12 +305,16 @@ def html_translate(callback, value): if not value: return value - parser = etree.HTMLParser(encoding='utf-8') - trans = XMLTranslator(callback, 'html', parser) - wrapped = "
%s
" % encode(value) - root = etree.fromstring(wrapped, parser) - trans.process(root[0][0]) # html > body > div - return trans.get_done()[5:-6] # remove tags
and
+ try: + parser = etree.HTMLParser(encoding='utf-8') + trans = XMLTranslator(callback, 'html', parser) + wrapped = "
%s
" % encode(value) + root = etree.fromstring(wrapped, parser) + trans.process(root[0][0]) # html > body > div + value = trans.get_done()[5:-6] # remove tags
and
+ except ValueError: + _logger.exception("Cannot translate malformed HTML, using source value instead") + return value #