Skip to content

Commit 2298a7e

Browse files
committed
fix: authenticate_message: Do not set None to spf_result.smtp_mailfrom
Closes issue #33. ``` self = <test_authentication.TestAuthenticateMessage testMethod=test_authenticate_dmarc_prev_spf> 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) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ authheaders/test/test_authentication.py:155: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ authheaders/__init__.py:434: in authenticate_message return str(auth_res) ^^^^^^^^^^^^^ /usr/lib/python3/dist-packages/authres/core.py:476: in __str__ return ''.join((self.HEADER_FIELD_NAME, ': ', self.header_value())) ^^^^^^^^^^^^^^^^^^^ /usr/lib/python3/dist-packages/authres/core.py:492: in header_value strs.append(str(result)) ^^^^^^^^^^^ /usr/lib/python3/dist-packages/authres/core.py:222: in __str__ strs.append(str(property_)) ^^^^^^^^^^^^^^ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = <authres.core.AuthenticationResultProperty object at 0x7fc4178465f0> def __str__(self): if self.comment: return '%s.%s=%s (%s)' % (self.type, self.name, self.value.quote_if_needed(), self.comment) else: > return '%s.%s=%s' % (self.type, self.name, self.value.quote_if_needed()) ^^^^^^^^^^^^^^^^^^^^^^^^^^ E AttributeError: 'NoneType' object has no attribute 'quote_if_needed' /usr/lib/python3/dist-packages/authres/core.py:132: AttributeError ```
1 parent 9c62915 commit 2298a7e

2 files changed

Lines changed: 30 additions & 8 deletions

File tree

authheaders/__init__.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,13 @@ def dmarc_per_from(from_domain, spf_result=None, dkim_result=None, dnsfunc=None,
232232
if not policy_only and spf_result and spf_result.result == "pass":
233233
# The domain in SPF results often includes the local part, even though
234234
# generally it SHOULD NOT (RFC 7601, Section 2.7.2, last paragraph).
235-
try:
236-
mail_from_domain = get_domain_part(spf_result.smtp_mailfrom)
237-
except IndexError:
238-
mail_from_domain = None
239-
spf_result.smtp_mailfrom = mail_from_domain
235+
mail_from_domain = spf_result.smtp_mailfrom
236+
if mail_from_domain is not None and '@' in mail_from_domain:
237+
try:
238+
mail_from_domain = get_domain_part(mail_from_domain)
239+
spf_result.smtp_mailfrom = mail_from_domain
240+
except IndexError:
241+
mail_from_domain = None
240242
if aspf == "s" and from_domain == mail_from_domain:
241243
result = "pass"
242244
elif aspf == "r" and get_org_domain(from_domain) == get_org_domain(mail_from_domain):

authheaders/test/test_authentication.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,30 @@ def test_authenticate_dmarc_comma(self):
145145
res = authenticate_message(self.message8, "example.com", spf=False, dnsfunc=self.dnsfunc)
146146
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")
147147

148-
def test_prev(self):
148+
def test_authenticate_dkim_prev_spf(self):
149149
prev = "Authentication-Results: example.com; spf=pass smtp.mailfrom=gmail.com"
150-
res = authenticate_message(self.message2, "example.com", prev=prev, spf=False, dmarc=False, dnsfunc=self.dnsfunc)
151-
self.assertEqual(res, "Authentication-Results: example.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=example.com header.i=@example.com")
150+
res = authenticate_message(self.message2, "example.com", prev=prev, spf=False, dkim=True, dmarc=False, dnsfunc=self.dnsfunc)
151+
self.assertEqual(res, f"{prev}; dkim=pass header.d=example.com header.i=@example.com")
152+
153+
def test_authenticate_dmarc_prev_spf(self):
154+
prev = "Authentication-Results: example.com; spf=pass smtp.mailfrom=gmail.com"
155+
res = authenticate_message(self.message2, "example.com", prev=prev, spf=False, dkim=True, dmarc=True, dnsfunc=self.dnsfunc)
156+
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")
157+
158+
def test_authenticate_dmarc_prev_spf_mailfrom_at_domain(self):
159+
prev = "Authentication-Results: example.com; spf=pass smtp.mailfrom=bogus@gmail.com"
160+
res = authenticate_message(self.message2, "example.com", prev=prev, spf=False, dkim=True, dmarc=True, dnsfunc=self.dnsfunc)
161+
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")
162+
163+
def test_authenticate_dmarc_prev_spf_mailfrom_at_invalid(self):
164+
prev = "Authentication-Results: example.com; spf=pass smtp.mailfrom=bogus@"
165+
res = authenticate_message(self.message2, "example.com", prev=prev, spf=False, dkim=True, dmarc=True, dnsfunc=self.dnsfunc)
166+
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")
167+
168+
def test_authenticate_dmarc_prev_spf_no_mailfrom(self):
169+
prev = "Authentication-Results: example.com; spf=pass"
170+
res = authenticate_message(self.message2, "example.com", prev=prev, spf=False, dkim=True, dmarc=True, dnsfunc=self.dnsfunc)
171+
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")
152172

153173
def test_get_domain_part(self):
154174
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'], ]

0 commit comments

Comments
 (0)