Skip to content

Commit

Permalink
Merge pull request #170 from aidecoe/dkim-validity-filter
Browse files Browse the repository at this point in the history
Add filter verifing DKIM signature
  • Loading branch information
flokli authored Feb 6, 2018
2 parents 6e8e403 + 9e32573 commit 4c389d5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
39 changes: 39 additions & 0 deletions afew/filters/DKIMValidityFilter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: ISC
# Copyright (c) Amadeusz Zolnowski <aidecoe@aidecoe.name>

from __future__ import print_function, absolute_import, unicode_literals

import dkim

from .BaseFilter import Filter


def verify_dkim(path):
'''
Verify DKIM signature of an e-mail file.
:param path: Path to the e-mail file.
:returns: Whether DKIM signature is valid or not.
'''
with open(path, 'rb') as message_file:
message_bytes = message_file.read()

return dkim.verify(message_bytes)


class DKIMValidityFilter(Filter):
'''
Verifies DKIM signature of an e-mail which has DKIM header.
'''
message = 'Verify DKIM signature'
header = 'DKIM-Signature'

def __init__(self, database, ok_tag='dkim-ok', fail_tag='dkim-fail'):
super(DKIMValidityFilter, self).__init__(database)
self.dkim_tag = {True: ok_tag, False: fail_tag}

def handle_message(self, message):
if message.get_header(self.header):
dkim_ok = all(map(verify_dkim, message.get_filenames()))
self.add_tags(message, self.dkim_tag[dkim_ok])
1 change: 1 addition & 0 deletions afew/tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def test_all_builtin_FilterRegistrys_exist(self):
from afew import FilterRegistry
self.assertEqual(sorted(['FolderNameFilter',
'ArchiveSentMailsFilter',
'DKIMValidityFilter',
'DMARCReportInspectionFilter',
'InboxFilter',
'SpamFilter',
Expand Down
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def get_requires():
if os.environ.get('TRAVIS') != 'true':
yield 'notmuch'
yield 'chardet'
yield 'dkimpy'

setup(
name='afew',
Expand All @@ -30,6 +31,7 @@ def get_requires():
'afew.filter': [
'Filter = afew.filters.BaseFilter:Filter',
'ArchiveSentMailsFilter = afew.filters.ArchiveSentMailsFilter:ArchiveSentMailsFilter',
'DKIMValidityFilter = afew.filters.DKIMValidityFilter:DKIMValidityFilter',
'DMARCReportInspectionFilter = afew.filters.DMARCReportInspectionFilter:DMARCReportInspectionFilter',
'FolderNameFilter = afew.filters.FolderNameFilter:FolderNameFilter',
'HeaderMatchingFilter = afew.filters.HeaderMatchingFilter:HeaderMatchingFilter',
Expand Down

0 comments on commit 4c389d5

Please sign in to comment.