From 35fe66e615412cd25ced11737c5e40d6e0300398 Mon Sep 17 00:00:00 2001 From: Morris Jobke Date: Mon, 30 Jan 2017 19:04:44 -0600 Subject: [PATCH] Verify the from email address is set to a valid value Signed-off-by: Morris Jobke --- isso/config.py | 19 ++++++++++++++++--- isso/tests/test_config.py | 21 +++++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/isso/config.py b/isso/config.py index 234a0fe..e3fcf61 100644 --- a/isso/config.py +++ b/isso/config.py @@ -149,8 +149,21 @@ def load(default, user=None): logger.info("Your `session-key` has been stored in the " "database itself, this option is now unused") - if not parseaddr(parser.get("smtp", "from"))[0]: - parser.set("smtp", "from", - formataddr(("Ich schrei sonst!", parser.get("smtp", "from")))) + from_address = parser.get("smtp", "from").strip() + + if from_address != '' and parseaddr(from_address) == (None, None): + if '@' not in from_address: + logger.warn("Your `from` address doesn't contain an @ sign. " + "It is reset to an empty address.") + parser.set("smtp", "from", "") + + elif ' ' in from_address: + logger.warn("Your `from` address contains a space. " + "It is reset to an empty address.") + parser.set("smtp", "from", "") + + else: + parser.set("smtp", "from", + formataddr(("Ich schrei sonst!", from_address))) return parser diff --git a/isso/tests/test_config.py b/isso/tests/test_config.py index dc40e65..2ac5bf2 100644 --- a/isso/tests/test_config.py +++ b/isso/tests/test_config.py @@ -30,3 +30,24 @@ class TestConfig(unittest.TestCase): self.assertEqual(parser.getlist("foo", "spam"), ['a', 'b', 'cdef']) self.assertEqual(list(parser.getiter("foo", "bla")), ['spam', 'ham']) self.assertEqual(list(parser.getiter("foo", "asd")), ['fgh']) + + def test_smtp(self): + + cases = [ + ('"abc" ', '"abc" '), + ('abc ', 'abc '), + ('"abc def" ', '"abc def" '), + ('abc def ', 'abc def '), + ('def@example.org', 'Ich schrei sonst! '), + ('"abc" def@example.org', ''), + ('"abc"', ''), + ('abc', ''), + ] + + for (config_value, expected) in cases: + parser = config.IssoParser(allow_no_value=True) + parser.read_file(io.StringIO(u""" + [smtp] + from = """ + config_value)) + + self.assertEqual(parser.get("smtp", "from"), expected)