Skip to content

Commit 59d8f38

Browse files
authored
[Backport] Add max length check to validate_email (#7673)
1 parent 69b92b5 commit 59d8f38

3 files changed

Lines changed: 10 additions & 0 deletions

File tree

‎changes/7673-hramezani.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix: Add max length check to `pydantic.validate_email`

‎pydantic/networks.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,10 @@ def validate(cls, value: NetworkType) -> Union[IPv4Network, IPv6Network]:
702702

703703

704704
pretty_email_regex = re.compile(r'([\w ]*?) *<(.*)> *')
705+
MAX_EMAIL_LENGTH = 2048
706+
"""Maximum length for an email.
707+
A somewhat arbitrary but very generous number compared to what is allowed by most implementations.
708+
"""
705709

706710

707711
def validate_email(value: Union[str]) -> Tuple[str, str]:
@@ -714,6 +718,10 @@ def validate_email(value: Union[str]) -> Tuple[str, str]:
714718
"""
715719
if email_validator is None:
716720
import_email_validator()
721+
722+
if len(value) > MAX_EMAIL_LENGTH:
723+
raise errors.EmailError()
724+
717725
m = pretty_email_regex.fullmatch(value)
718726
name: Union[str, None] = None
719727
if m:

‎tests/test_networks.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,7 @@ def test_address_valid(value, name, email):
789789
'\"@example.com',
790790
',@example.com',
791791
'foobar <foobar<@example.com>',
792+
'foobar <' + 'a' * 4096 + '@example.com>',
792793
],
793794
)
794795
def test_address_invalid(value):

0 commit comments

Comments
 (0)