bpo-34155: Dont parse domains containing @ (GH-13079) (GH-16006) 17/214117/1 accepted/tizen/5.0/base/20190924.043148 submit/tizen_5.0_base/20190918.081119
authorRoberto C. Sánchez <roberto@connexer.com>
Sat, 14 Sep 2019 17:26:38 +0000 (13:26 -0400)
committerDongHun Kwak <dh0128.kwak@samsung.com>
Wed, 18 Sep 2019 07:35:33 +0000 (16:35 +0900)
This change skips parsing of email addresses where domains include a "@" character, which can be maliciously used since the local part is returned as a complete address.

(cherry picked from commit 8cb65d1381b027f0b09ee36bfed7f35bb4dec9a9)

Excludes changes to Lib/email/_header_value_parser.py, which did not
exist in 2.7.

Co-authored-by: jpic <jpic@users.noreply.github.com>
https://bugs.python.org/issue34155
Signed-off-by: DongHun Kwak <dh0128.kwak@samsung.com>
Change-Id: I480580265c87bc59b762c5f7be46caca8fd32d7a

Lib/email/_parseaddr.py

index 690db2c22d34d358b6707d6befa0fcf96e6373bc..dc49d2e45a5eb7e934aac4750dd153d95be87ebb 100644 (file)
@@ -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: