This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change validation of mail address from 'lower()' to 'casefold'
- Loading branch information
Showing
8 changed files
with
87 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
Fix inconsistent handling of upper and lower cases of email addresses that is use only lower cases. | ||
Fix inconsistent handling of upper and lower cases of email addresses that is use only case folding (MSC2265). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,8 @@ | |
import logging | ||
import re | ||
|
||
from synapse.api.errors import SynapseError | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
|
@@ -48,3 +50,23 @@ def check_3pid_allowed(hs, medium, address): | |
return True | ||
|
||
return False | ||
|
||
|
||
def canonicalise_email(address) -> str: | ||
"""'Canonicalise' email address | ||
Case folding of local part of email address and lowercase domain part | ||
See MSC2265, https://github.com/matrix-org/matrix-doc/pull/2265 | ||
Args: | ||
address (str): email address within that medium (e.g. "[email protected]") | ||
Returns: | ||
(str) The canonical form of the email address | ||
Raises: | ||
SynapseError if the number could not be parsed. | ||
""" | ||
|
||
try: | ||
address = address.split("@") | ||
return address[0].casefold() + "@" + address[1].lower() | ||
except IndexError: | ||
raise SynapseError(400, "Unable to parse email address") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright 2020 Dirk Klimpel | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from synapse.api.errors import SynapseError | ||
from synapse.util.threepids import canonicalise_email | ||
|
||
from tests.unittest import HomeserverTestCase | ||
|
||
|
||
class CanonicaliseEmailTests(HomeserverTestCase): | ||
def test_invalid_format(self): | ||
with self.assertRaises(SynapseError) as cm: | ||
canonicalise_email("address-without-at.bar") | ||
e = cm.exception | ||
self.assertEqual(e.code, 400) | ||
|
||
def test_valid_format(self): | ||
self.assertEqual(canonicalise_email("[email protected]"), "[email protected]") | ||
|
||
def test_domain_to_lower(self): | ||
self.assertEqual(canonicalise_email("[email protected]"), "[email protected]") | ||
|
||
def test_domain_with_umlaut(self): | ||
self.assertEqual(canonicalise_email("foo@Öumlaut.com"), "foo@öumlaut.com") | ||
|
||
def test_address_casefold(self): | ||
self.assertEqual( | ||
canonicalise_email("Strauß@Example.com"), "[email protected]" | ||
) |