-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #170 from aidecoe/dkim-validity-filter
Add filter verifing DKIM signature
- Loading branch information
Showing
3 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters