Blame SOURCES/00332-CVE-2019-16056.patch

4f4dad
diff --git a/Lib/email/_header_value_parser.py b/Lib/email/_header_value_parser.py
4f4dad
index 1fb8cb4..9815e4e 100644
4f4dad
--- a/Lib/email/_header_value_parser.py
4f4dad
+++ b/Lib/email/_header_value_parser.py
4f4dad
@@ -1561,6 +1561,8 @@ def get_domain(value):
4f4dad
         token, value = get_dot_atom(value)
4f4dad
     except errors.HeaderParseError:
4f4dad
         token, value = get_atom(value)
4f4dad
+    if value and value[0] == '@':
4f4dad
+        raise errors.HeaderParseError('Invalid Domain')
4f4dad
     if leader is not None:
4f4dad
         token[:0] = [leader]
4f4dad
     domain.append(token)
4f4dad
diff --git a/Lib/email/_parseaddr.py b/Lib/email/_parseaddr.py
4f4dad
index cdfa372..41ff6f8 100644
4f4dad
--- a/Lib/email/_parseaddr.py
4f4dad
+++ b/Lib/email/_parseaddr.py
4f4dad
@@ -379,7 +379,12 @@ class AddrlistClass:
4f4dad
         aslist.append('@')
4f4dad
         self.pos += 1
4f4dad
         self.gotonext()
4f4dad
-        return EMPTYSTRING.join(aslist) + self.getdomain()
4f4dad
+        domain = self.getdomain()
4f4dad
+        if not domain:
4f4dad
+            # Invalid domain, return an empty address instead of returning a
4f4dad
+            # local part to denote failed parsing.
4f4dad
+            return EMPTYSTRING
4f4dad
+        return EMPTYSTRING.join(aslist) + domain
4f4dad
 
4f4dad
     def getdomain(self):
4f4dad
         """Get the complete domain name from an address."""
4f4dad
@@ -394,6 +399,10 @@ class AddrlistClass:
4f4dad
             elif self.field[self.pos] == '.':
4f4dad
                 self.pos += 1
4f4dad
                 sdlist.append('.')
4f4dad
+            elif self.field[self.pos] == '@':
4f4dad
+                # bpo-34155: Don't parse domains with two `@` like
4f4dad
+                # `a@malicious.org@important.com`.
4f4dad
+                return EMPTYSTRING
4f4dad
             elif self.field[self.pos] in self.atomends:
4f4dad
                 break
4f4dad
             else:
4f4dad
diff --git a/Lib/test/test_email/test__header_value_parser.py b/Lib/test/test_email/test__header_value_parser.py
4f4dad
index 676732b..577dc43 100644
4f4dad
--- a/Lib/test/test_email/test__header_value_parser.py
4f4dad
+++ b/Lib/test/test_email/test__header_value_parser.py
4f4dad
@@ -1418,6 +1418,16 @@ class TestParser(TestParserMixin, TestEmailBase):
4f4dad
         self.assertEqual(addr_spec.domain, 'example.com')
4f4dad
         self.assertEqual(addr_spec.addr_spec, 'star.a.star@example.com')
4f4dad
 
4f4dad
+    def test_get_addr_spec_multiple_domains(self):
4f4dad
+        with self.assertRaises(errors.HeaderParseError):
4f4dad
+            parser.get_addr_spec('star@a.star@example.com')
4f4dad
+
4f4dad
+        with self.assertRaises(errors.HeaderParseError):
4f4dad
+            parser.get_addr_spec('star@a@example.com')
4f4dad
+
4f4dad
+        with self.assertRaises(errors.HeaderParseError):
4f4dad
+            parser.get_addr_spec('star@172.17.0.1@example.com')
4f4dad
+
4f4dad
     # get_obs_route
4f4dad
 
4f4dad
     def test_get_obs_route_simple(self):
4f4dad
diff --git a/Lib/test/test_email/test_email.py b/Lib/test/test_email/test_email.py
4f4dad
index f97ccc6..68d0522 100644
4f4dad
--- a/Lib/test/test_email/test_email.py
4f4dad
+++ b/Lib/test/test_email/test_email.py
4f4dad
@@ -3035,6 +3035,20 @@ class TestMiscellaneous(TestEmailBase):
4f4dad
         self.assertEqual(utils.parseaddr('<>'), ('', ''))
4f4dad
         self.assertEqual(utils.formataddr(utils.parseaddr('<>')), '')
4f4dad
 
4f4dad
+    def test_parseaddr_multiple_domains(self):
4f4dad
+        self.assertEqual(
4f4dad
+            utils.parseaddr('a@b@c'),
4f4dad
+            ('', '')
4f4dad
+        )
4f4dad
+        self.assertEqual(
4f4dad
+            utils.parseaddr('a@b.c@c'),
4f4dad
+            ('', '')
4f4dad
+        )
4f4dad
+        self.assertEqual(
4f4dad
+            utils.parseaddr('a@172.17.0.1@c'),
4f4dad
+            ('', '')
4f4dad
+        )
4f4dad
+
4f4dad
     def test_noquote_dump(self):
4f4dad
         self.assertEqual(
4f4dad
             utils.formataddr(('A Silly Person', 'person@dom.ain')),