diff mbox

[08/11] parser: Correct empty email value check

Message ID 1468402860-3409-9-git-send-email-stephen.finucane@intel.com
State Superseded
Headers show

Commit Message

Stephen Finucane July 13, 2016, 9:40 a.m. UTC
The check for empty emails in 'find_author' checked for None, but it
was not possible to ever return None unless the 'From:' header was
missing altogether. Seeing as, per RFC822 and its revisions, this is
not possible, check for the empty string instead.

Signed-off-by: Stephen Finucane <stephen.finucane@intel.com>
---
 patchwork/parser.py            |    4 +++-
 patchwork/tests/test_parser.py |    5 +++++
 2 files changed, 8 insertions(+), 1 deletions(-)

Comments

Andy Doan July 19, 2016, 9:33 p.m. UTC | #1
On 07/13/2016 04:40 AM, Stephen Finucane wrote:
> The check for empty emails in 'find_author' checked for None, but it
> was not possible to ever return None unless the 'From:' header was
> missing altogether. Seeing as, per RFC822 and its revisions, this is
> not possible, check for the empty string instead.
>
> Signed-off-by: Stephen Finucane <stephen.finucane@intel.com>

Reviewed-by: Andy Doan <andy.doan@linaro.org>
diff mbox

Patch

diff --git a/patchwork/parser.py b/patchwork/parser.py
index 6601cd3..ef919c1 100644
--- a/patchwork/parser.py
+++ b/patchwork/parser.py
@@ -107,6 +107,8 @@  def find_author(mail):
     # 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]))),
@@ -128,7 +130,7 @@  def find_author(mail):
             (name, email) = fn(match.groups())
             break
 
-    if email is None:
+    if not email:
         raise ValueError("Invalid 'From' header")
 
     email = email.strip()
diff --git a/patchwork/tests/test_parser.py b/patchwork/tests/test_parser.py
index 3ca049b..7b5c71b 100644
--- a/patchwork/tests/test_parser.py
+++ b/patchwork/tests/test_parser.py
@@ -228,6 +228,11 @@  class SenderEncodingTest(TestCase):
         db_person = Person.objects.get(email=sender_email)
         self.assertEqual(person, db_person)
 
+    def test_empty(self):
+        email = self._create_email('')
+        with self.assertRaises(ValueError):
+            find_author(email)
+
     def test_ascii_encoding(self):
         from_header = 'example user <user@example.com>'
         sender_name = u'example user'