From 442fa2a1da9ea5b8df94cea7f918d60aede67798 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Wed, 30 Oct 2019 13:26:38 +0100 Subject: [PATCH] Display decoding error in HTML editor If the bytes can not be decoded in UTF-8, we should display a nice error message instead of failing with a 500. Also if the text is not a string, we must return a 400 as it is a wrong request. issue8577 review264451002 --- trytond/ir/routes.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/trytond/ir/routes.py b/trytond/ir/routes.py index 103e1db164..5d4d6c2406 100644 --- a/trytond/ir/routes.py +++ b/trytond/ir/routes.py @@ -5,6 +5,7 @@ try: except ImportError: from http import client as HTTPStatus +from werkzeug.exceptions import abort from werkzeug.utils import redirect from werkzeug.wrappers import Response @@ -63,9 +64,15 @@ def html_editor(request, pool, model, record, field): error = gettext('ir.msg_html_editor_save_fail') csrf_token = get_token(record) - text = getattr(record, field.name) + text = getattr(record, field.name) or '' if isinstance(text, bytes): - text = text.decode('utf-8') + try: + text = text.decode('utf-8') + except UnicodeDecodeError as e: + error = str(e).replace("'", "\\'") + text = '' + elif not isinstance(text, str): + abort(HTTPStatus.BAD_REQUEST) title = '%(model)s "%(name)s" %(field)s - %(title)s' % { 'model': field.model.name, 'name': record.rec_name,