Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing of emails for whitelist #19

Merged
merged 6 commits into from
Dec 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion bandit/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import logging

from email.utils import parseaddr
from functools import reduce
from operator import and_

Expand Down Expand Up @@ -31,7 +32,8 @@ def send_messages(self, email_messages):
[email for name, email in admins])

def is_approved(email):
local_part, _, domain = email.rpartition('@')
_, email = parseaddr(email)
_, _, domain = email.rpartition('@')
return email in approved_emails or domain in whitelist_emails

to_send = []
Expand Down
6 changes: 4 additions & 2 deletions bandit/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,14 @@ def test_send_multiple(self):
self.assertEqual(message.get_all('to'), ['admin@example.com', ])

def test_whitelist_domain(self):
addresses = ['foo@whitelisted.test.com', 'bar@whitelisted.test.com']
addresses = ['foo@whitelisted.test.com',
'<bar@whitelisted.test.com>',
'Foo Bar <baz@whitelisted.test.com>']
emails = [EmailMessage( 'Subject', 'Content', 'from@example.com', addresses)]
num_sent = self.get_connection().send_messages(emails)
self.assertEqual(len(emails), num_sent)
messages = self.get_mailbox_content()
self.assertEqual(messages[0].get_all('to'), [', '.join(addresses)])
self.assertEqual(messages[0].get_all('to')[0].replace('\n', ''), ', '.join(addresses))


class LogOnlyBackendTestCase(BaseBackendTestCase):
Expand Down
7 changes: 3 additions & 4 deletions runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,14 @@


def runtests():
if django.VERSION > (1, 7):
# http://django.readthedocs.org/en/latest/releases/1.7.html#standalone-scripts
django.setup()
django.setup()
from django.test.utils import get_runner
TestRunner = get_runner(settings)
test_runner = TestRunner(verbosity=1, interactive=True, failfast=True)
failures = test_runner.run_tests(['bandit', ])
failures = test_runner.run_tests(['bandit'])
if failures:
sys.exit(1)


if __name__ == '__main__':
runtests(*sys.argv[1:])