Fix HTML being escaped in preview of email subject

`EmailPreviewTemplate.subject` returns a string of HTML, with any
user-submitted HTML already escaped:
b5a61bfb7b/notifications_utils/template.py (L672)

What won’t be escaped is the HTML needed to redact the placeholders. We
generate this HTML so we know its safe, and doesn’t need to be escaped.
However when we pass it to Jinja, Jinja doesn’t know this, so will try
to escape it. This means users will see the raw HTML.

We can get around this by using Flask’s `Markup` class to tell Jinja
that the string is already sanitised and doesn’t need escaping again.

Text message templates don’t have this problem because they already
return `Markup`: b5a61bfb7b/notifications_utils/template.py (L288)

Letter templates don’t suffer from this problem (because they don’t
support redaction) but without making the same change they would still
double-escape ampersands, greater-than symbols, and so on.
This commit is contained in:
Chris Hill-Scott
2020-08-04 15:05:56 +01:00
parent a74501f1d8
commit 4d65b94c77
2 changed files with 25 additions and 7 deletions

View File

@@ -3,6 +3,7 @@
from functools import partial
from flask import (
Markup,
Response,
abort,
flash,
@@ -438,14 +439,14 @@ def get_preview_of_content(notification):
))
if notification['template']['template_type'] == 'email':
return EmailPreviewTemplate(
return Markup(EmailPreviewTemplate(
notification['template'],
notification['personalisation'],
redact_missing_personalisation=True,
).subject
).subject)
if notification['template']['template_type'] == 'letter':
return LetterPreviewTemplate(
return Markup(LetterPreviewTemplate(
notification['template'],
notification['personalisation'],
).subject
).subject)