diff --git a/Lib/email/_parseaddr.py b/Lib/email/_parseaddr.py index 690db2c..dc49d2e 100644 --- a/Lib/email/_parseaddr.py +++ b/Lib/email/_parseaddr.py @@ -336,7 +336,12 @@ class AddrlistClass: 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.""" @@ -351,6 +356,10 @@ class AddrlistClass: elif self.field[self.pos] == '.': self.pos += 1 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: break else: diff --git a/Lib/email/test/test_email.py b/Lib/email/test/test_email.py index b32da9d..1739033 100644 --- a/Lib/email/test/test_email.py +++ b/Lib/email/test/test_email.py @@ -2308,6 +2308,20 @@ class TestMiscellaneous(TestEmailBase): 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('a@b.c@c'), + ('', '') + ) + self.assertEqual( + Utils.parseaddr('a@172.17.0.1@c'), + ('', '') + ) + def test_noquote_dump(self): self.assertEqual( Utils.formataddr(('A Silly Person', 'person@dom.ain')),