#33631: Blocktranslate asvar escapes variables, but stores the result as str
instance, leading to double escaping
-------------------------------------+-------------------------------------
Reporter: Richard | Owner: nobody
Ebeling |
Type: Bug | Status: new
Component: | Version: 4.0
Uncategorized | Keywords: blocktranslate
Severity: Normal | asvar escape
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
In the docs, this snippet is given as an example usage of `blocktranslate`
with the `asvar` argument (here:
[https://docs.djangoproject.com/en/4.0/topics/i18n/translation
/#blocktranslate-template-tag]:
{{{
{% blocktranslate asvar the_title %}The title is {{ title }}.{%
endblocktranslate %}
<title>{{ the_title }}</title>
<meta name="description" content="{{ the_title }}">
}}}
However, this template is buggy when `title` is a string, which I'd argue
is a common use case.
`title` will be escaped when formatting the content of the
`blocktranslate` block, but the "was escaped" information is discarded,
and `the_title` will be a `str` instance with escaped content.
When later using the `the_title` variable, it will be conditionally
escaped. Since it is a `str`, it will be escaped, so control characters
are escaped again, breaking their display on the final page.
Minimal example to reproduce (can be put in any view):
{{{
from django.template import Template, Context
template_content = """
{% blocktranslate asvar the_title %}The title is {{ title }}.{%
endblocktranslate %}
<title>{{ the_title }}</title>
<meta name="description" content="{{ the_title }}">
"""
rendered = Template(template_content).render(Context({"title": "<>&
Title"}))
assert "&lt;" not in rendered, "> was escaped two times"
}}}
I'd argue that `blocktranslate` should:
* Either assign a `SafeString` instance to prevent future escaping
* or not escape the variables used within the translation, and store them
marked as unsafe (= as `str` instance)
--
Ticket URL: <https://code.djangoproject.com/ticket/33631>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/0107018010a207bd-aa8804d8-a778-4eba-9368-b5029ffdbecc-000000%40eu-central-1.amazonses.com.