Commit 4cbcd2f8 authored by Roberto C. Sánchez's avatar Roberto C. Sánchez Committed by Miss Islington (bot)

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



This change skips parsing of email addresses where domains include a "@" character, which can be maliciously used since the local part is returned as a complete address. 

(cherry picked from commit 8cb65d13)

Excludes changes to Lib/email/_header_value_parser.py, which did not
exist in 2.7.
Co-authored-by: default avatarjpic <jpic@users.noreply.github.com>


https://bugs.python.org/issue34155
parent 0d63669e
...@@ -336,7 +336,12 @@ class AddrlistClass: ...@@ -336,7 +336,12 @@ class AddrlistClass:
aslist.append('@') aslist.append('@')
self.pos += 1 self.pos += 1
self.gotonext() 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): def getdomain(self):
"""Get the complete domain name from an address.""" """Get the complete domain name from an address."""
...@@ -351,6 +356,10 @@ class AddrlistClass: ...@@ -351,6 +356,10 @@ class AddrlistClass:
elif self.field[self.pos] == '.': elif self.field[self.pos] == '.':
self.pos += 1 self.pos += 1
sdlist.append('.') sdlist.append('.')
elif self.field[self.pos] == '@':
# bpo-34155: Don't parse domains with two `@` like
# `a@malicious.org@important.com`.
return EMPTYSTRING
elif self.field[self.pos] in self.atomends: elif self.field[self.pos] in self.atomends:
break break
else: else:
......
...@@ -2306,6 +2306,20 @@ class TestMiscellaneous(TestEmailBase): ...@@ -2306,6 +2306,20 @@ class TestMiscellaneous(TestEmailBase):
self.assertEqual(Utils.parseaddr('<>'), ('', '')) self.assertEqual(Utils.parseaddr('<>'), ('', ''))
self.assertEqual(Utils.formataddr(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('a@b.c@c'),
('', '')
)
self.assertEqual(
Utils.parseaddr('a@172.17.0.1@c'),
('', '')
)
def test_noquote_dump(self): def test_noquote_dump(self):
self.assertEqual( self.assertEqual(
Utils.formataddr(('A Silly Person', 'person@dom.ain')), Utils.formataddr(('A Silly Person', 'person@dom.ain')),
......
Fix parsing of invalid email addresses with more than one ``@`` (e.g. a@b@c.com.) to not return the part before 2nd ``@`` as valid email address. Patch by maxking & jpic.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment