From 4364d6069996191de7dfc71d6cb1ef6548596fd4 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Thu, 21 Jan 2021 13:41:27 -0500 Subject: [PATCH 1/7] Enable autoescape for email templates. --- synapse/config/_base.py | 5 ++++- synapse/config/emailconfig.py | 9 +++++++-- synapse/push/mailer.py | 18 ++++++++++++++++-- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/synapse/config/_base.py b/synapse/config/_base.py index 94144efc87b3..1263bbbf7683 100644 --- a/synapse/config/_base.py +++ b/synapse/config/_base.py @@ -249,7 +249,10 @@ def read_templates( search_directories.insert(0, custom_template_directory) loader = jinja2.FileSystemLoader(search_directories) - env = jinja2.Environment(loader=loader, autoescape=autoescape) + env = jinja2.Environment( + loader=loader, + autoescape=jinja2.select_autoescape() if autoescape else False, + ) # Update the environment with our custom filters env.filters.update( diff --git a/synapse/config/emailconfig.py b/synapse/config/emailconfig.py index 6a487afd3495..e98e9331879c 100644 --- a/synapse/config/emailconfig.py +++ b/synapse/config/emailconfig.py @@ -246,6 +246,7 @@ def read_config(self, config, **kwargs): add_threepid_template_success_html, ], template_dir, + autoescape=True, ) # Render templates that do not contain any placeholders @@ -281,7 +282,9 @@ def read_config(self, config, **kwargs): self.email_notif_template_html, self.email_notif_template_text, ) = self.read_templates( - [notif_template_html, notif_template_text], template_dir, + [notif_template_html, notif_template_text], + template_dir, + autoescape=True, ) self.email_notif_for_new_users = email_config.get( @@ -303,7 +306,9 @@ def read_config(self, config, **kwargs): self.account_validity_template_html, self.account_validity_template_text, ) = self.read_templates( - [expiry_template_html, expiry_template_text], template_dir, + [expiry_template_html, expiry_template_text], + template_dir, + autoescape=True, ) subjects_config = email_config.get("subjects", {}) diff --git a/synapse/push/mailer.py b/synapse/push/mailer.py index 4d875dcb91ff..745b1dde9459 100644 --- a/synapse/push/mailer.py +++ b/synapse/push/mailer.py @@ -668,6 +668,15 @@ def make_unsubscribe_link( def safe_markup(raw_html: str) -> jinja2.Markup: + """ + Sanitise a raw HTML string to a set of allowed tags and attributes, and linkify any bare URLs. + + Args + raw_html: Unsafe HTML. + + Returns: + A Markup object ready to safely use in a Jinja template. + """ return jinja2.Markup( bleach.linkify( bleach.clean( @@ -684,8 +693,13 @@ def safe_markup(raw_html: str) -> jinja2.Markup: def safe_text(raw_text: str) -> jinja2.Markup: """ - Process text: treat it as HTML but escape any tags (ie. just escape the - HTML) then linkify it. + Sanitise text (escape any HTML tags), and then linkify any bare URLs. + + Args + raw_text: Unsafe text which might include HTML markup. + + Returns: + A Markup object ready to safely use in a Jinja template. """ return jinja2.Markup( bleach.linkify(bleach.clean(raw_text, tags=[], attributes={}, strip=False)) From ca6e3b1d051e1c0078a58cd71c81c0512a980912 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Thu, 21 Jan 2021 13:50:25 -0500 Subject: [PATCH 2/7] Enable autoescape for SSO templates. --- synapse/config/sso.py | 1 + synapse/res/templates/sso_auth_bad_user.html | 2 +- synapse/res/templates/sso_auth_confirm.html | 4 ++-- synapse/res/templates/sso_error.html | 2 +- synapse/res/templates/sso_login_idp_picker.html | 12 ++++++------ synapse/res/templates/sso_redirect_confirm.html | 6 +++--- 6 files changed, 14 insertions(+), 13 deletions(-) diff --git a/synapse/config/sso.py b/synapse/config/sso.py index 59be825532f5..77bae29abc1c 100644 --- a/synapse/config/sso.py +++ b/synapse/config/sso.py @@ -49,6 +49,7 @@ def read_config(self, config, **kwargs): "sso_auth_bad_user.html", ], template_dir, + autoescape=True, ) # These templates have no placeholders, so render them here diff --git a/synapse/res/templates/sso_auth_bad_user.html b/synapse/res/templates/sso_auth_bad_user.html index 3611191bf99d..f7099098c7f3 100644 --- a/synapse/res/templates/sso_auth_bad_user.html +++ b/synapse/res/templates/sso_auth_bad_user.html @@ -5,7 +5,7 @@

- We were unable to validate your {{server_name | e}} account via + We were unable to validate your {{ server_name }} account via single-sign-on (SSO), because the SSO Identity Provider returned different details than when you logged in.

diff --git a/synapse/res/templates/sso_auth_confirm.html b/synapse/res/templates/sso_auth_confirm.html index 0d9de9d46528..4e7ca3a2eda5 100644 --- a/synapse/res/templates/sso_auth_confirm.html +++ b/synapse/res/templates/sso_auth_confirm.html @@ -5,8 +5,8 @@

- A client is trying to {{ description | e }}. To confirm this action, - re-authenticate with single sign-on. + A client is trying to {{ description }}. To confirm this action, + re-authenticate with single sign-on. If you did not expect this, your account may be compromised!

diff --git a/synapse/res/templates/sso_error.html b/synapse/res/templates/sso_error.html index 944bc9c9cab2..af8459719ae4 100644 --- a/synapse/res/templates/sso_error.html +++ b/synapse/res/templates/sso_error.html @@ -12,7 +12,7 @@

There was an error during authentication:

-
{{ error_description | e }}
+
{{ error_description }}

If you are seeing this page after clicking a link sent to you via email, make sure you only click the confirmation link once, and that you open the diff --git a/synapse/res/templates/sso_login_idp_picker.html b/synapse/res/templates/sso_login_idp_picker.html index 5b384810123f..62a640dad25d 100644 --- a/synapse/res/templates/sso_login_idp_picker.html +++ b/synapse/res/templates/sso_login_idp_picker.html @@ -3,22 +3,22 @@ - {{server_name | e}} Login + {{ server_name }} Login

-

{{server_name | e}} Login

+

{{ server_name }} Login