From 4c5be848f6562cb1b504a40c28434c87d91b65c5 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Mon, 5 Mar 2018 22:28:58 +0000 Subject: 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 --- dmarcpipe.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) 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) -- cgit v1.2.3-54-g00ecf