|
1 | 1 | # Copyright (C) Lutra Consulting Limited |
2 | 2 | # |
3 | 3 | # SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-MerginMaps-Commercial |
4 | | - |
5 | | -import regex |
| 4 | +import re |
6 | 5 | import safe |
7 | 6 | from flask_wtf import FlaskForm |
8 | 7 | from sqlalchemy import func |
|
18 | 17 |
|
19 | 18 | from .models import MAX_USERNAME_LENGTH, User |
20 | 19 | from ..app import UpdateForm, CustomStringField |
| 20 | +from .utils import get_email_domain |
21 | 21 |
|
22 | 22 |
|
23 | 23 | def username_validation(form, field): |
@@ -57,20 +57,19 @@ class ExtendedEmail(Email): |
57 | 57 | because they make our email sending service to fail |
58 | 58 | """ |
59 | 59 |
|
60 | | - EMAIL_PATTERN = regex.compile( |
61 | | - r"""(?i)^[\x60#&*\/=?^{!}~'_\p{L}0-9\-\+]+ |
62 | | - (\.[\x60#&*\/=?^{!}~'_\p{L}0-9\-\+]+)*\.?@ |
63 | | - ([_a-z0-9-]+(\.[_a-z0-9-]+)*\.) |
64 | | - [a-z0-9-]*[a-z0-9]{2,}$""", |
65 | | - regex.VERBOSE, |
66 | | - ) |
67 | | - |
68 | 60 | def __call__(self, form, field): |
69 | 61 | super().__call__(form, field) |
70 | 62 |
|
71 | | - value = field.data.strip() |
72 | | - if not self.EMAIL_PATTERN.match(value): |
73 | | - raise ValidationError(f"Email address '{value}' is invalid.") |
| 63 | + if re.search(r"[|'—]", field.data): |
| 64 | + raise ValidationError( |
| 65 | + f"Email address '{field.data}' contains an invalid character." |
| 66 | + ) |
| 67 | + |
| 68 | + domain = get_email_domain(field.data) |
| 69 | + if not domain.isascii(): |
| 70 | + raise ValidationError( |
| 71 | + f"Email address '{field.data}' contains non-ASCII characters in the domain part." |
| 72 | + ) |
74 | 73 |
|
75 | 74 |
|
76 | 75 | class PasswordValidator: |
|
0 commit comments