Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 3f4a2a7

Browse files
Hotfix: disable autoescape by default when rendering Jinja2 templates (#8394)
#8037 changed the default `autoescape` option when rendering Jinja2 templates from `False` to `True`. This caused some bugs, noticeably around redirect URLs being escaped in SAML2 auth confirmation templates, causing those URLs to break for users. This change returns the previous behaviour as it stood. We may want to look at each template individually and see whether autoescaping is a good idea at some point, but for now lets just fix the breakage.
1 parent d191dbd commit 3f4a2a7

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

changelog.d/8394.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix URLs being accidentally escaped in Jinja2 templates. Broke in v1.20.0.

synapse/config/_base.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,10 @@ def read_file(cls, file_path, config_name):
194194
return file_stream.read()
195195

196196
def read_templates(
197-
self, filenames: List[str], custom_template_directory: Optional[str] = None,
197+
self,
198+
filenames: List[str],
199+
custom_template_directory: Optional[str] = None,
200+
autoescape: bool = False,
198201
) -> List[jinja2.Template]:
199202
"""Load a list of template files from disk using the given variables.
200203
@@ -210,6 +213,9 @@ def read_templates(
210213
custom_template_directory: A directory to try to look for the templates
211214
before using the default Synapse template directory instead.
212215
216+
autoescape: Whether to autoescape variables before inserting them into the
217+
template.
218+
213219
Raises:
214220
ConfigError: if the file's path is incorrect or otherwise cannot be read.
215221
@@ -233,7 +239,7 @@ def read_templates(
233239
search_directories.insert(0, custom_template_directory)
234240

235241
loader = jinja2.FileSystemLoader(search_directories)
236-
env = jinja2.Environment(loader=loader, autoescape=True)
242+
env = jinja2.Environment(loader=loader, autoescape=autoescape)
237243

238244
# Update the environment with our custom filters
239245
env.filters.update(

synapse/config/saml2_config.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,10 @@ def read_config(self, config, **kwargs):
169169
saml2_config.get("saml_session_lifetime", "15m")
170170
)
171171

172+
# We enable autoescape here as the message may potentially come from a
173+
# remote resource
172174
self.saml2_error_html_template = self.read_templates(
173-
["saml_error.html"], saml2_config.get("template_dir")
175+
["saml_error.html"], saml2_config.get("template_dir"), autoescape=True
174176
)[0]
175177

176178
def _default_saml_config_dict(

0 commit comments

Comments
 (0)