summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Kramkowski <tk@the-tk.com>2018-03-05 22:28:58 +0000
committerTomasz Kramkowski <tk@the-tk.com>2018-03-05 22:28:58 +0000
commit4c5be848f6562cb1b504a40c28434c87d91b65c5 (patch)
treea9b8f9865fed847bf1c9189204ff4ead6bd72675
parenta75a9ed6d9cef8af8e56dcbd42ea3f8fcbd77ed1 (diff)
downloaddmarcpipe-4c5be848f6562cb1b504a40c28434c87d91b65c5.tar.gz
dmarcpipe-4c5be848f6562cb1b504a40c28434c87d91b65c5.tar.xz
dmarcpipe-4c5be848f6562cb1b504a40c28434c87d91b65c5.zip
Add application/octet-stream and application/x-gzip support
Some non-compliant DMARC reporters are using application/octet-stream and application/x-gzip mime types for attachments. dmarcpipe now supports both
-rwxr-xr-xdmarcpipe.py12
1 files changed, 11 insertions, 1 deletions
diff --git a/dmarcpipe.py b/dmarcpipe.py
index 7e25572..139694e 100755
--- a/dmarcpipe.py
+++ b/dmarcpipe.py
@@ -24,6 +24,7 @@ from re import compile as re_compile
from sql import store_dmarc
from sys import stdin, stdout, argv
from zipfile import ZipFile
+from magic import detect_from_content as magic
import email
re_valid_filename = re_compile('^[^\\s!]+![^\\s!]+![0-9]+![0-9]+(![^\\s!]+)?.(xml(.gz)?|zip)$')
@@ -46,9 +47,12 @@ def zip2xml(data):
return z.open(n[0]).read()
gzip2xml = lambda data: gz_decompress(data)
+octet_stream = lambda data: decode(data, magic(data).mime_type)
decoders = {
'application/zip': zip2xml,
+ 'application/x-gzip': gzip2xml,
+ 'application/octet-stream': octet_stream,
'application/x-zip': zip2xml,
'application/x-zip-compressed': zip2xml,
'application/zlib': gzip2xml,
@@ -56,6 +60,12 @@ decoders = {
'text/xml': lambda data: data,
}
+def decode(data, mime):
+ try:
+ return decoders[mime](data)
+ except KeyError:
+ raise FalseReportException('invalid content type: {}'.format(mime))
+
def process_message(m):
if m.get_content_maintype() != 'multipart' and \
m.get_content_type() not in decoders.keys():
@@ -80,7 +90,7 @@ def process_message(m):
if att is None:
raise FalseReportException('attachment not found{}'.format(inv_fn == True and ' (invalid filename)' or ''))
- xml = decoders[att.get_content_type()](att.get_payload(decode=True))
+ xml = decode(att.get_payload(decode=True), att.get_content_type())
dmarc = parse_dmarc(xml)
store_dmarc(dbfile, dmarc)