Skip to content

Commit

Permalink
[Backport] Add max length check to validate_email (#7673)
Browse files Browse the repository at this point in the history
  • Loading branch information
hramezani committed Sep 27, 2023
1 parent 69b92b5 commit 59d8f38
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions changes/7673-hramezani.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix: Add max length check to `pydantic.validate_email`
8 changes: 8 additions & 0 deletions pydantic/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,10 @@ def validate(cls, value: NetworkType) -> Union[IPv4Network, IPv6Network]:


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


def validate_email(value: Union[str]) -> Tuple[str, str]:
Expand All @@ -714,6 +718,10 @@ def validate_email(value: Union[str]) -> Tuple[str, str]:
"""
if email_validator is None:
import_email_validator()

if len(value) > MAX_EMAIL_LENGTH:
raise errors.EmailError()

m = pretty_email_regex.fullmatch(value)
name: Union[str, None] = None
if m:
Expand Down
1 change: 1 addition & 0 deletions tests/test_networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,7 @@ def test_address_valid(value, name, email):
'\"@example.com',
',@example.com',
'foobar <foobar<@example.com>',
'foobar <' + 'a' * 4096 + '@example.com>',
],
)
def test_address_invalid(value):
Expand Down

0 comments on commit 59d8f38

Please sign in to comment.