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

71928b
diff --git a/Lib/email/_header_value_parser.py b/Lib/email/_header_value_parser.py
71928b
index 737951e4b1b1..bc9c9b6241d4 100644
71928b
--- a/Lib/email/_header_value_parser.py
71928b
+++ b/Lib/email/_header_value_parser.py
71928b
@@ -1561,6 +1561,8 @@ def get_domain(value):
71928b
         token, value = get_dot_atom(value)
71928b
     except errors.HeaderParseError:
71928b
         token, value = get_atom(value)
71928b
+    if value and value[0] == '@':
71928b
+        raise errors.HeaderParseError('Invalid Domain')
71928b
     if leader is not None:
71928b
         token[:0] = [leader]
71928b
     domain.append(token)
71928b
diff --git a/Lib/email/_parseaddr.py b/Lib/email/_parseaddr.py
71928b
index cdfa3729adc7..41ff6f8c000d 100644
71928b
--- a/Lib/email/_parseaddr.py
71928b
+++ b/Lib/email/_parseaddr.py
71928b
@@ -379,7 +379,12 @@ def getaddrspec(self):
71928b
         aslist.append('@')
71928b
         self.pos += 1
71928b
         self.gotonext()
71928b
-        return EMPTYSTRING.join(aslist) + self.getdomain()
71928b
+        domain = self.getdomain()
71928b
+        if not domain:
71928b
+            # Invalid domain, return an empty address instead of returning a
71928b
+            # local part to denote failed parsing.
71928b
+            return EMPTYSTRING
71928b
+        return EMPTYSTRING.join(aslist) + domain
71928b
 
71928b
     def getdomain(self):
71928b
         """Get the complete domain name from an address."""
71928b
@@ -394,6 +399,10 @@ def getdomain(self):
71928b
             elif self.field[self.pos] == '.':
71928b
                 self.pos += 1
71928b
                 sdlist.append('.')
71928b
+            elif self.field[self.pos] == '@':
71928b
+                # bpo-34155: Don't parse domains with two `@` like
71928b
+                # `a@malicious.org@important.com`.
71928b
+                return EMPTYSTRING
71928b
             elif self.field[self.pos] in self.atomends:
71928b
                 break
71928b
             else:
71928b
diff --git a/Lib/test/test_email/test__header_value_parser.py b/Lib/test/test_email/test__header_value_parser.py
71928b
index a2c900fa7fd2..02ef3e1006c6 100644
71928b
--- a/Lib/test/test_email/test__header_value_parser.py
71928b
+++ b/Lib/test/test_email/test__header_value_parser.py
71928b
@@ -1418,6 +1418,16 @@ def test_get_addr_spec_dot_atom(self):
71928b
         self.assertEqual(addr_spec.domain, 'example.com')
71928b
         self.assertEqual(addr_spec.addr_spec, 'star.a.star@example.com')
71928b
 
71928b
+    def test_get_addr_spec_multiple_domains(self):
71928b
+        with self.assertRaises(errors.HeaderParseError):
71928b
+            parser.get_addr_spec('star@a.star@example.com')
71928b
+
71928b
+        with self.assertRaises(errors.HeaderParseError):
71928b
+            parser.get_addr_spec('star@a@example.com')
71928b
+
71928b
+        with self.assertRaises(errors.HeaderParseError):
71928b
+            parser.get_addr_spec('star@172.17.0.1@example.com')
71928b
+
71928b
     # get_obs_route
71928b
 
71928b
     def test_get_obs_route_simple(self):
71928b
diff --git a/Lib/test/test_email/test_email.py b/Lib/test/test_email/test_email.py
71928b
index f97ccc6711cc..68d052279987 100644
71928b
--- a/Lib/test/test_email/test_email.py
71928b
+++ b/Lib/test/test_email/test_email.py
71928b
@@ -3035,6 +3035,20 @@ def test_parseaddr_empty(self):
71928b
         self.assertEqual(utils.parseaddr('<>'), ('', ''))
71928b
         self.assertEqual(utils.formataddr(utils.parseaddr('<>')), '')
71928b
 
71928b
+    def test_parseaddr_multiple_domains(self):
71928b
+        self.assertEqual(
71928b
+            utils.parseaddr('a@b@c'),
71928b
+            ('', '')
71928b
+        )
71928b
+        self.assertEqual(
71928b
+            utils.parseaddr('a@b.c@c'),
71928b
+            ('', '')
71928b
+        )
71928b
+        self.assertEqual(
71928b
+            utils.parseaddr('a@172.17.0.1@c'),
71928b
+            ('', '')
71928b
+        )
71928b
+
71928b
     def test_noquote_dump(self):
71928b
         self.assertEqual(
71928b
             utils.formataddr(('A Silly Person', 'person@dom.ain')),
71928b
diff --git a/Misc/NEWS.d/next/Security/2019-05-04-13-33-37.bpo-34155.MJll68.rst b/Misc/NEWS.d/next/Security/2019-05-04-13-33-37.bpo-34155.MJll68.rst
71928b
new file mode 100644
71928b
index 000000000000..50292e29ed1d
71928b
--- /dev/null
71928b
+++ b/Misc/NEWS.d/next/Security/2019-05-04-13-33-37.bpo-34155.MJll68.rst
71928b
@@ -0,0 +1 @@
71928b
+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.