summary refs log tree commit diff
path: root/synapse/util/threepids.py
diff options
context:
space:
mode:
Diffstat (limited to 'synapse/util/threepids.py')
-rw-r--r--synapse/util/threepids.py33
1 files changed, 31 insertions, 2 deletions
diff --git a/synapse/util/threepids.py b/synapse/util/threepids.py

index 63f955acff..eb005007d5 100644 --- a/synapse/util/threepids.py +++ b/synapse/util/threepids.py
@@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # Copyright 2018 New Vector Ltd # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -19,8 +18,18 @@ import re logger = logging.getLogger(__name__) +# it's unclear what the maximum length of an email address is. RFC3696 (as corrected +# by errata) says: +# the upper limit on address lengths should normally be considered to be 254. +# +# In practice, mail servers appear to be more tolerant and allow 400 characters +# or so. Let's allow 500, which should be plenty for everyone. +# +MAX_EMAIL_ADDRESS_LENGTH = 500 + + async def check_3pid_allowed(hs, medium, address): - """Checks whether a given 3PID is allowed to be used on this HS + """Checks whether a given format of 3PID is allowed to be used on this HS Args: hs (synapse.server.HomeServer): server @@ -98,3 +107,23 @@ def canonicalise_email(address: str) -> str: raise ValueError("Unable to parse email address") return parts[0].casefold() + "@" + parts[1].lower() + + +def validate_email(address: str) -> str: + """Does some basic validation on an email address. + + Returns the canonicalised email, as returned by `canonicalise_email`. + + Raises a ValueError if the email is invalid. + """ + # First we try canonicalising in case that fails + address = canonicalise_email(address) + + # Email addresses have to be at least 3 characters. + if len(address) < 3: + raise ValueError("Unable to parse email address") + + if len(address) > MAX_EMAIL_ADDRESS_LENGTH: + raise ValueError("Unable to parse email address") + + return address