Skip to content

[2.7] bpo-34155: Dont parse domains containing @ (GH-13079) #16006

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Lib/email/_parseaddr.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,12 @@ def getaddrspec(self):
aslist.append('@')
self.pos += 1
self.gotonext()
return EMPTYSTRING.join(aslist) + self.getdomain()
domain = self.getdomain()
if not domain:
# Invalid domain, return an empty address instead of returning a
# local part to denote failed parsing.
return EMPTYSTRING
return EMPTYSTRING.join(aslist) + domain

def getdomain(self):
"""Get the complete domain name from an address."""
Expand All @@ -351,6 +356,10 @@ def getdomain(self):
elif self.field[self.pos] == '.':
self.pos += 1
sdlist.append('.')
elif self.field[self.pos] == '@':
# bpo-34155: Don't parse domains with two `@` like
# `[email protected]@important.com`.
return EMPTYSTRING
elif self.field[self.pos] in self.atomends:
break
else:
Expand Down
14 changes: 14 additions & 0 deletions Lib/email/test/test_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -2306,6 +2306,20 @@ def test_parseaddr_empty(self):
self.assertEqual(Utils.parseaddr('<>'), ('', ''))
self.assertEqual(Utils.formataddr(Utils.parseaddr('<>')), '')

def test_parseaddr_multiple_domains(self):
self.assertEqual(
Utils.parseaddr('a@b@c'),
('', '')
)
self.assertEqual(
Utils.parseaddr('[email protected]@c'),
('', '')
)
self.assertEqual(
Utils.parseaddr('[email protected]@c'),
('', '')
)

def test_noquote_dump(self):
self.assertEqual(
Utils.formataddr(('A Silly Person', '[email protected]')),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix parsing of invalid email addresses with more than one ``@`` (e.g. a@[email protected].) to not return the part before 2nd ``@`` as valid email address. Patch by maxking & jpic.