]> git.ipfire.org Git - thirdparty/patchwork.git/commitdiff
Validate email address in from_header
authorNisha Poyarekar <nisha.s.menon@gmail.com>
Wed, 26 Apr 2023 18:23:24 +0000 (14:23 -0400)
committerStephen Finucane <stephenfinucane@hotmail.com>
Mon, 8 May 2023 11:22:08 +0000 (12:22 +0100)
Resolves #512

Signed-off-by: Nisha Poyarekar <nisha.s.menon@gmail.com>
patchwork/parser.py
patchwork/tests/test_parser.py

index e9104ca87290140598fed62bb84cd8611c8b535d..2c863a519215e47e23e3ec3f2fb9638611fbfbe6 100644 (file)
@@ -338,8 +338,6 @@ def split_from_header(from_header):
     # tuple of (regex, fn)
     #  - where fn returns a (name, email) tuple from the match groups resulting
     #    from re.match().groups()
-    # TODO(stephenfin): Perhaps we should check for "real" email addresses
-    # instead of anything ('.*?')
     from_res = [
         # for "Firstname Lastname" <example@example.com> style addresses
         (re.compile(r'"?(.*?)"?\s*<([^>]+)>'), (lambda g: (g[0], g[1]))),
@@ -360,6 +358,14 @@ def split_from_header(from_header):
             (name, email) = fn(match.groups())
             break
 
+    # Checking for real email address.
+    email_pattern = (
+        r'^[\w!#$%&"*+/=?^`{|}~-]+(?:\.[\w!#$%&"*+/=?^`{|}~-]+)*'
+        r'@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+'
+        r'[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$'
+    )
+    if not re.match(email_pattern, email):
+        email = None
     return (name, email)
 
 
index 8001a9bba8b9749eee075ed17e375bd0ac1c5145..1eaecab11ebdd61d54e339ff859e02055a45f4b7 100644 (file)
@@ -270,6 +270,13 @@ class SenderEncodingTest(TestCase):
         with self.assertRaises(ValueError):
             get_or_create_author(email)
 
+    def test_invalid_email(self):
+        # Broken email clients garble email addresses -- #512
+        from_header = 'testH=?UTF-8?B?w6E=?=user=?UTF-8?B?w6E=?='
+        email = self._create_email(from_header)
+        with self.assertRaises(ValueError):
+            get_or_create_author(email)
+
     def test_ascii_encoding(self):
         from_header = 'example user <user@example.com>'
         sender_name = 'example user'