From 17dce7b884d822f66b59428e3e4b4db96421f083 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Sat, 9 Jun 2018 10:45:48 +0200 Subject: [PATCH] misc: don't crash templates on variables with invalid characters (#24393) --- tests/test_templates.py | 4 ++++ wcs/qommon/template.py | 5 ++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/test_templates.py b/tests/test_templates.py index ab872db5..28396e20 100644 --- a/tests/test_templates.py +++ b/tests/test_templates.py @@ -108,3 +108,7 @@ def test_datetime_templatetags(): tmpl = Template('{{ plop|parse_time|date:"H i" }}') assert tmpl.render({'plop': '10:32'}) == '10 32' assert tmpl.render({'plop': 'x'}) == '' + +def test_variable_unicode_error_handling(): + tmpl = Template('{{ form_var_éléphant }}') + assert tmpl.render() == '' diff --git a/wcs/qommon/template.py b/wcs/qommon/template.py index d150ad80..fe56c3e2 100644 --- a/wcs/qommon/template.py +++ b/wcs/qommon/template.py @@ -523,7 +523,10 @@ class Template(object): variable_resolve_orig = django.template.base.Variable.resolve def variable_resolve(self, context): - value = variable_resolve_orig(self, context) + try: + value = variable_resolve_orig(self, context) + except UnicodeEncodeError: + return context.template.engine.string_if_invalid if isinstance(value, SafeString): return SafeUnicode(value, 'utf-8') if isinstance(value, str): -- 2.17.1