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

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