Skip to content

Commit 168b5f0

Browse files
committed
Disallow non-ASCII in email domain
1 parent 0bddcfa commit 168b5f0

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

server/mergin/auth/forms.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,12 @@ class ExtendedEmail(Email):
4848
1. spaces,
4949
2. special characters ,:;()<>[]\"
5050
3, multiple @ symbols,
51-
4, leading, trailing, or consecutive dots in the local part
52-
5, invalid domain part - missing top level domain (user@example), consecutive dots
53-
Custom check for additional invalid characters disallows |'— because they make our email sending service to fail
51+
4, leading, trailing, or consecutive dots in the local part,
52+
5, invalid domain part - missing top level domain (user@example), consecutive dots,
53+
Custom check for
54+
- additional invalid characters disallows |'—
55+
- non-ASCII characters in the domain part
56+
because they make our email sending service to fail
5457
"""
5558

5659
def __call__(self, form, field):
@@ -61,6 +64,17 @@ def __call__(self, form, field):
6164
f"Email address '{field.data}' contains an invalid character."
6265
)
6366

67+
try:
68+
local_part, domain_part = field.data.rsplit("@", 1)
69+
except ValueError:
70+
raise ValidationError(f"Invalid email address '{field.data}'.")
71+
72+
# character is one of the standard ASCII characters (0–127)
73+
if not all(ord(c) < 128 for c in domain_part):
74+
raise ValidationError(
75+
f"Email address '{field.data}' contains non-ASCII characters in the domain part."
76+
)
77+
6478

6579
class PasswordValidator:
6680
def __init__(self, min_length=8):

server/mergin/tests/test_auth.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ def test_logout(client):
128128
("verylonglonglonglonglonglonglongemail@example.com", "#pwd1234", 201),
129129
("us.er@mergin.com", "#pwd1234", 201), # dot is allowed
130130
("us er@mergin.com", "#pwd1234", 400), # space is disallowed
131+
("test@gmaiñ.com", "#pwd1234", 400), # non-ASCII character in the domain
131132
]
132133

133134

0 commit comments

Comments
 (0)