Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions authheaders/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,13 @@ def dmarc_per_from(from_domain, spf_result=None, dkim_result=None, dnsfunc=None,
if not policy_only and spf_result and spf_result.result == "pass":
# The domain in SPF results often includes the local part, even though
# generally it SHOULD NOT (RFC 7601, Section 2.7.2, last paragraph).
try:
mail_from_domain = get_domain_part(spf_result.smtp_mailfrom)
except IndexError:
mail_from_domain = None
spf_result.smtp_mailfrom = mail_from_domain
mail_from_domain = spf_result.smtp_mailfrom
if '@' in mail_from_domain:
try:
mail_from_domain = get_domain_part(mail_from_domain)
spf_result.smtp_mailfrom = mail_from_domain
except IndexError:
mail_from_domain = None
if aspf == "s" and from_domain == mail_from_domain:
result = "pass"
elif aspf == "r" and get_org_domain(from_domain) == get_org_domain(mail_from_domain):
Expand Down
21 changes: 18 additions & 3 deletions authheaders/test/test_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,25 @@ def test_authenticate_dmarc_comma(self):
res = authenticate_message(self.message8, "example.com", spf=False, dnsfunc=self.dnsfunc)
self.assertEqual(res, "Authentication-Results: example.com; dkim=pass header.d=example.com header.i=@example.com; dmarc=pass (Used From Domain Record) header.from=example.com policy.dmarc=reject")

def test_prev(self):
def test_authenticate_dkim_prev_spf(self):
prev = "Authentication-Results: example.com; spf=pass smtp.mailfrom=gmail.com"
res = authenticate_message(self.message2, "example.com", prev=prev, spf=False, dmarc=False, dnsfunc=self.dnsfunc)
self.assertEqual(res, "Authentication-Results: example.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=example.com header.i=@example.com")
res = authenticate_message(self.message2, "example.com", prev=prev, spf=False, dkim=True, dmarc=False, dnsfunc=self.dnsfunc)
self.assertEqual(res, f"{prev}; dkim=pass header.d=example.com header.i=@example.com")

def test_authenticate_dmarc_prev_spf(self):
prev = "Authentication-Results: example.com; spf=pass smtp.mailfrom=gmail.com"
res = authenticate_message(self.message2, "example.com", prev=prev, spf=False, dkim=True, dmarc=True, dnsfunc=self.dnsfunc)
self.assertEqual(res, f"{prev}; dkim=pass header.d=example.com header.i=@example.com; dmarc=pass (Used From Domain Record) header.from=example.com policy.dmarc=reject")

def test_authenticate_dmarc_prev_spf_mailfrom_at_domain(self):
prev = "Authentication-Results: example.com; spf=pass smtp.mailfrom=bogus@gmail.com"
res = authenticate_message(self.message2, "example.com", prev=prev, spf=False, dkim=True, dmarc=True, dnsfunc=self.dnsfunc)
self.assertEqual(res, f"{prev.replace('bogus@', '')}; dkim=pass header.d=example.com header.i=@example.com; dmarc=pass (Used From Domain Record) header.from=example.com policy.dmarc=reject")

def test_authenticate_dmarc_prev_spf_mailfrom_at_invalid(self):
prev = "Authentication-Results: example.com; spf=pass smtp.mailfrom=bogus@"
res = authenticate_message(self.message2, "example.com", prev=prev, spf=False, dkim=True, dmarc=True, dnsfunc=self.dnsfunc)
self.assertEqual(res, f"{prev}; dkim=pass header.d=example.com header.i=@example.com; dmarc=pass (Used From Domain Record) header.from=example.com policy.dmarc=reject")

def test_get_domain_part(self):
froms_to_test = [['test@example.com', 'example.com'], [""""Test, User" <test@example.com>""", 'example.com'], ["""Test User <test@sub2.example.biz>""", 'sub2.example.biz'], ["""=?UTF-8?B?QmVkIEJhdGggJiBCZXlvbmQ=?=<BdBth&Byond@example.com>""", 'example.com'], ]
Expand Down