|
1 | 1 | # Copyright (C) Lutra Consulting Limited |
2 | 2 | # |
3 | 3 | # SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-MerginMaps-Commercial |
| 4 | + |
4 | 5 | import re |
5 | 6 | import safe |
6 | 7 | from flask_wtf import FlaskForm |
|
17 | 18 |
|
18 | 19 | from .models import MAX_USERNAME_LENGTH, User |
19 | 20 | from ..app import UpdateForm, CustomStringField |
20 | | -from .utils import get_email_domain |
21 | 21 |
|
22 | 22 |
|
23 | 23 | def username_validation(form, field): |
@@ -51,25 +51,20 @@ class ExtendedEmail(Email): |
51 | 51 | 3, multiple @ symbols, |
52 | 52 | 4, leading, trailing, or consecutive dots in the local part, |
53 | 53 | 5, invalid domain part - missing top level domain (user@example), consecutive dots, |
54 | | - Custom check for |
55 | | - - additional invalid characters disallows |'— |
56 | | - - non-ASCII characters in the domain part |
57 | | - because they make our email sending service to fail |
| 54 | + The extended validation checks email addresses using the regex provided by Brevo, |
| 55 | + so that we stay consistent with their validation rules and avoid API failures. |
58 | 56 | """ |
59 | 57 |
|
60 | 58 | def __call__(self, form, field): |
61 | 59 | super().__call__(form, field) |
62 | 60 |
|
63 | | - if re.search(r"[|'—]", field.data): |
64 | | - raise ValidationError( |
65 | | - f"Email address '{field.data}' contains an invalid character." |
66 | | - ) |
| 61 | + email = field.data.strip() |
67 | 62 |
|
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 | | - ) |
| 63 | + pattern = r"^[\x60#&*\/=?^{!}~'+\w-]+(\.[\x60#&*\/=?^{!}~'+\w-]+)*\.?@([_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*\.)[a-zA-Z0-9-]*[a-zA-Z0-9]{2,}$" |
| 64 | + email_regexp = re.compile(pattern, re.IGNORECASE) |
| 65 | + |
| 66 | + if not email_regexp.match(email): |
| 67 | + raise ValidationError(f"Email address '{email}' is invalid.") |
73 | 68 |
|
74 | 69 |
|
75 | 70 | class PasswordValidator: |
|
0 commit comments