Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch for v1.26.0 #52

Open
wants to merge 3 commits into
base: origin-v1.26.0-1733769101
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions UPGRADE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,43 @@ for example:
wget https://packages.matrix.org/debian/pool/main/m/matrix-synapse-py3/matrix-synapse-py3_1.3.0+stretch1_amd64.deb
dpkg -i matrix-synapse-py3_1.3.0+stretch1_amd64.deb

Upgrading to v1.27.0
====================

Changes to HTML templates
-------------------------

The HTML templates for SSO and email notifications now have `Jinja2's autoescape <https://jinja.palletsprojects.com/en/2.11.x/api/#autoescaping>`_
enabled for files ending in ``.html``, ``.htm``, and ``.xml``. If you hae customised
these templates and see issues when viewing them you might need to update them.
It is expected that most configurations will need no changes.

If you have customised the templates *names* for these templates it is recommended
to verify they end in ``.html`` to ensure autoescape is enabled.

The above applies to the following templates:

* ``add_threepid.html``
* ``add_threepid_failure.html``
* ``add_threepid_success.html``
* ``notice_expiry.html``
* ``notice_expiry.html``
* ``notif_mail.html`` (which, by default, includes ``room.html`` and ``notif.html``)
* ``password_reset.html``
* ``password_reset_confirmation.html``
* ``password_reset_failure.html``
* ``password_reset_success.html``
* ``registration.html``
* ``registration_failure.html``
* ``registration_success.html``
* ``sso_account_deactivated.html``
* ``sso_auth_bad_user.html``
* ``sso_auth_confirm.html``
* ``sso_auth_success.html``
* ``sso_error.html``
* ``sso_login_idp_picker.html``
* ``sso_redirect_confirm.html``

Upgrading to v1.26.0
====================

Expand Down
1 change: 1 addition & 0 deletions changelog.d/9200.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Clean-up template loading code.
42 changes: 26 additions & 16 deletions synapse/config/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,36 +203,50 @@ def read_file(cls, file_path, config_name):
with open(file_path) as file_stream:
return file_stream.read()

def read_template(self, filename: str) -> jinja2.Template:
"""Load a template file from disk.

This function will attempt to load the given template from the default Synapse
template directory.

Files read are treated as Jinja templates. The templates is not rendered yet
and has autoescape enabled.

Args:
filename: A template filename to read.

Raises:
ConfigError: if the file's path is incorrect or otherwise cannot be read.

Returns:
A jinja2 template.
"""
return self.read_templates([filename])[0]

def read_templates(
self,
filenames: List[str],
custom_template_directory: Optional[str] = None,
autoescape: bool = False,
self, filenames: List[str], custom_template_directory: Optional[str] = None,
) -> List[jinja2.Template]:
"""Load a list of template files from disk using the given variables.

This function will attempt to load the given templates from the default Synapse
template directory. If `custom_template_directory` is supplied, that directory
is tried first.

Files read are treated as Jinja templates. These templates are not rendered yet.
Files read are treated as Jinja templates. The templates are not rendered yet
and have autoescape enabled.

Args:
filenames: A list of template filenames to read.

custom_template_directory: A directory to try to look for the templates
before using the default Synapse template directory instead.

autoescape: Whether to autoescape variables before inserting them into the
template.

Raises:
ConfigError: if the file's path is incorrect or otherwise cannot be read.

Returns:
A list of jinja2 templates.
"""
templates = []
search_directories = [self.default_template_dir]

# The loader will first look in the custom template directory (if specified) for the
Expand All @@ -249,7 +263,7 @@ 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(),)

# Update the environment with our custom filters
env.filters.update(
Expand All @@ -259,12 +273,8 @@ def read_templates(
}
)

for filename in filenames:
# Load the template
template = env.get_template(filename)
templates.append(template)

return templates
# Load the templates
return [env.get_template(filename) for filename in filenames]


def _format_ts_filter(value: int, format: str):
Expand Down
4 changes: 1 addition & 3 deletions synapse/config/captcha.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ def read_config(self, config, **kwargs):
"recaptcha_siteverify_api",
"https://www.recaptcha.net/recaptcha/api/siteverify",
)
self.recaptcha_template = self.read_templates(
["recaptcha.html"], autoescape=True
)[0]
self.recaptcha_template = self.read_template("recaptcha.html")

def generate_config_section(self, **kwargs):
return """\
Expand Down
2 changes: 1 addition & 1 deletion synapse/config/consent_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def __init__(self, *args):

def read_config(self, config, **kwargs):
consent_config = config.get("user_consent")
self.terms_template = self.read_templates(["terms.html"], autoescape=True)[0]
self.terms_template = self.read_template("terms.html")

if consent_config is None:
return
Expand Down
4 changes: 1 addition & 3 deletions synapse/config/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,7 @@ def read_config(self, config, **kwargs):
self.session_lifetime = session_lifetime

# The success template used during fallback auth.
self.fallback_success_template = self.read_templates(
["auth_success.html"], autoescape=True
)[0]
self.fallback_success_template = self.read_template("auth_success.html")

def generate_config_section(self, generate_secrets=False, **kwargs):
if generate_secrets:
Expand Down
4 changes: 2 additions & 2 deletions synapse/config/tls.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import warnings
from datetime import datetime
from hashlib import sha256
from typing import List, Optional
from typing import List, Optional, Pattern

from unpaddedbase64 import encode_base64

Expand Down Expand Up @@ -125,7 +125,7 @@ def read_config(self, config: dict, config_dir_path: str, **kwargs):
fed_whitelist_entries = []

# Support globs (*) in whitelist values
self.federation_certificate_verification_whitelist = [] # type: List[str]
self.federation_certificate_verification_whitelist = [] # type: List[Pattern]
for entry in fed_whitelist_entries:
try:
entry_regex = glob_to_regex(entry.encode("ascii").decode("ascii"))
Expand Down
Loading