Projet

Général

Profil

0001-misc-don-t-crash-templates-on-variables-with-invalid.patch

Frédéric Péters, 09 juin 2018 10:46

Télécharger (1,49 ko)

Voir les différences:

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(-)
tests/test_templates.py
108 108
    tmpl = Template('{{ plop|parse_time|date:"H i" }}')
109 109
    assert tmpl.render({'plop': '10:32'}) == '10 32'
110 110
    assert tmpl.render({'plop': 'x'}) == ''
111

  
112
def test_variable_unicode_error_handling():
113
    tmpl = Template('{{ form_var_éléphant }}')
114
    assert tmpl.render() == ''
wcs/qommon/template.py
523 523
variable_resolve_orig = django.template.base.Variable.resolve
524 524

  
525 525
def variable_resolve(self, context):
526
    value = variable_resolve_orig(self, context)
526
    try:
527
        value = variable_resolve_orig(self, context)
528
    except UnicodeEncodeError:
529
        return context.template.engine.string_if_invalid
527 530
    if isinstance(value, SafeString):
528 531
        return SafeUnicode(value, 'utf-8')
529 532
    if isinstance(value, str):
530
-