ValiMail / authentication-headers

library for the generation of email authentication headers
Other
16 stars 4 forks source link

I have fixed an uncaught exception and am sharing it here (dkim.MessageFormatError) #17

Closed niftylettuce closed 3 years ago

niftylettuce commented 4 years ago

The following error was occurring:

133902:Error: Traceback (most recent call last):
133903-  File "/var/www/production/source/node_modules/authheaders/scripts/authenticate-message.py", line 36, in <module>
133904-    main()
133905-  File "/var/www/production/source/node_modules/authheaders/scripts/authenticate-message.py", line 25, in main
133906-    header = authheaders.authenticate_message(msg=message, authserv_id=authservId, ip=ip, mail_from=mailFrom, helo=helo, spf=True, dkim=True, arc=True)
133907-  File "/home/deploy/.local/lib/python3.6/site-packages/authheaders/__init__.py", line 286, in authenticate_message
133908-    dkim_result = check_dkim(msg, dnsfunc=dnsfunc)
133909-  File "/home/deploy/.local/lib/python3.6/site-packages/authheaders/__init__.py", line 63, in check_dkim
133910-    d = DKIM(msg)
133911-  File "/home/deploy/.local/lib/python3.6/site-packages/dkim/__init__.py", line 504, in __init__
133912-    self.set_message(message)
133913-  File "/home/deploy/.local/lib/python3.6/site-packages/dkim/__init__.py", line 607, in set_message
133914-    self.headers, self.body = rfc822_parse(message)
133915-  File "/home/deploy/.local/lib/python3.6/site-packages/dkim/__init__.py", line 356, in rfc822_parse
133916-    raise MessageFormatError("Unexpected characters in RFC822 header: %s" % lines[i])
133917-dkim.MessageFormatError: Unexpected characters in RFC822 header: b'This is a multi-part message in MIME format.'
133918-
133919-    at ChildProcess.<anonymous> (/var/www/production/source/node_modules/authheaders/index.js:61:44)
133920-    at ChildProcess.emit (events.js:323:22)
133921-    at maybeClose (internal/child_process.js:1021:16)
133922-    at Process.ChildProcess._handle.onexit (internal/child_process.js:286:5)

I noticed that this package needed this changed in authheaders/__init__.py:

def check_dkim(msg, dnsfunc=None):
-    d = DKIM(msg)
    try:
+        d = DKIM(msg)
        if(dnsfunc):
            res = d.verify(dnsfunc=dnsfunc) and 'pass' or 'fail'
        else:

The reason is because DKIM(msg) will then call dkimpy's set_message function, which will then call rfc822_parse, which, if it contains an unexpected character, will throw an exception.

For example, you could send across the following headers, and this would cause this lib to throw an exception instead of saying that DKIM failed (which is what my fix properly does).

Received: foobar
Subject:   foobar
To: foo@bar.com
From: foo@beep.com
Date: Fri, 7 Aug 2020 00:41:34 -0300
MIME-Version: 1.0
This is a multi-part message in MIME format.

Foobar

Here is the link to my commit with the fix in my independently maintained version:

https://github.com/forwardemail/authentication-headers/commit/1046cd72103e432f4aa2d152b75955db1d481486

kitterma commented 3 years ago

Fixed in https://github.com/ValiMail/authentication-headers/commit/b36571421cbadc00eebb3b27dd62f6f2d7e51ee9