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

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