Skip to content

Commit

Permalink
Merge pull request #8 from fusionbox/develop
Browse files Browse the repository at this point in the history
Allow whitelisting entire domains
  • Loading branch information
mlavin committed Nov 9, 2015
2 parents 250665c + ea50608 commit 8713f18
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
9 changes: 7 additions & 2 deletions bandit/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,19 @@ def send_messages(self, email_messages):
admins = getattr(settings, 'ADMINS', ())
server_email = getattr(settings, 'SERVER_EMAIL', 'root@localhost')
bandit_email = getattr(settings, 'BANDIT_EMAIL', server_email)
whitelist_emails = getattr(settings, 'BANDIT_WHITELIST', ())
whitelist_emails = set(getattr(settings, 'BANDIT_WHITELIST', ()))
approved_emails = set([server_email, bandit_email, ] + list(whitelist_emails) +
[email for name, email in admins])

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

to_send = []
logged_count = 0
for message in email_messages:
recipients = message.to + message.cc + message.bcc
all_approved = reduce(and_, map(lambda e: e in approved_emails, recipients))
all_approved = reduce(and_, map(is_approved, recipients))
if all_approved:
to_send.append(message)
else:
Expand Down
8 changes: 8 additions & 0 deletions bandit/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ def test_send_multiple(self):
message = messages[1]
self.assertEqual(message.get_all('to'), ['[email protected]', ])

def test_whitelist_domain(self):
addresses = ['[email protected]', '[email protected]']
emails = [EmailMessage( 'Subject', 'Content', '[email protected]', 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)])


class LogOnlyBackendTestCase(BaseBackendTestCase):

Expand Down
5 changes: 5 additions & 0 deletions docs/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ add those email addresses to
``BANDIT_WHITELIST``. ``BANDIT_WHITELIST`` defaults to an empty tuple::

BANDIT_WHITELIST = ('[email protected]', )

A domain can also be put in the BANDIT_WHITELIST. This will whitelist any email
to that domain.

BANDIT_WHITELIST = ('example.com', )
1 change: 1 addition & 0 deletions runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
SITE_ID=1,
ADMINS=(('Admin', '[email protected]'), ),
BANDIT_EMAIL='[email protected]',
BANDIT_WHITELIST=('whitelisted.test.com', ),
BASE_DIR='', # tells compatibility checker not to emit warning
)

Expand Down

0 comments on commit 8713f18

Please sign in to comment.