When decoding inbound emails, decode encoded headers. Fixes #420

This commit is contained in:
Pēteris Caune 2020-09-08 12:06:32 +03:00
parent bd98174d4c
commit 66a1a108bf
No known key found for this signature in database
GPG Key ID: E28D7679E9A9EDE2
3 changed files with 17 additions and 1 deletions

View File

@ -18,6 +18,7 @@ All notable changes to this project will be documented in this file.
- Don't allow duplicate team memberships
- When copying a check, copy all fields from the "Filtering Rules" dialog (#417)
- Fix missing Resume button (#421)
- When decoding inbound emails, decode encoded headers (#420)
## v1.16.0 - 2020-08-04

View File

@ -1,5 +1,6 @@
import asyncore
import email
import email.policy
import re
from smtpd import SMTPServer
@ -41,7 +42,9 @@ def _process_message(remote_addr, mailfrom, mailto, data):
action = "success"
if check.subject or check.subject_fail:
action = "ign"
subject = email.message_from_string(data).get("subject", "")
# Specify policy, the default policy does not decode encoded headers:
parsed = email.message_from_string(data, policy=email.policy.SMTP)
subject = parsed.get("subject", "")
if check.subject and _match(subject, check.subject):
action = "success"
elif check.subject_fail and _match(subject, check.subject_fail):

View File

@ -98,3 +98,15 @@ class SmtpdTestCase(BaseTestCase):
self.assertEqual(ping.scheme, "email")
self.assertEqual(ping.ua, "Email from foo@example.org")
self.assertEqual(ping.kind, "fail")
def test_it_handles_encoded_subject(self):
self.check.subject = "SUCCESS"
self.check.save()
body = PAYLOAD_TMPL % "=?US-ASCII?B?W1NVQ0NFU1NdIEJhY2t1cCBjb21wbGV0ZWQ=?="
_process_message("1.2.3.4", "foo@example.org", self.email, body.encode("utf8"))
ping = Ping.objects.latest("id")
self.assertEqual(ping.scheme, "email")
self.assertEqual(ping.ua, "Email from foo@example.org")
self.assertEqual(ping.kind, None)