Skip to content

Commit

Permalink
#881 fix style errors found by black
Browse files Browse the repository at this point in the history
  • Loading branch information
uwekamper committed Apr 8, 2024
1 parent 63b7e5b commit b886bcc
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
2 changes: 1 addition & 1 deletion checks/migrations/0016_add_tlsrpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Migration(migrations.Migration):
]

operations = [
migrations.AddField(
migrations.AddField(
model_name="mailtestauth",
name="tlsrpt_score",
field=models.IntegerField(null=True),
Expand Down
2 changes: 1 addition & 1 deletion checks/scoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@
MAIL_AUTH_SPF_WORST_STATUS = STATUS_FAIL

MAIL_AUTH_TLSRPT_PASS = NO_POINTS
MAIL_AUTH_TLSRPT_FAIL = NO_POINTS # TLS-RPT fail does not give a points penalty
MAIL_AUTH_TLSRPT_FAIL = NO_POINTS # TLS-RPT fail does not give a points penalty
MAIL_AUTH_TLSRPT_ERROR = NO_POINTS
MAIL_AUTH_TLSRPT_WORST_STATUS = STATUS_FAIL

Expand Down
15 changes: 7 additions & 8 deletions checks/tasks/tlsrpt_parsing.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Copyright: 2022-2024, ECP, NLnet Labs, the Internet.nl contributors and SYS4 AG.
# SPDX-License-Identifier: Apache-2.0

'''
"""
SMTP TLS Reporting policy parser as defined by:
RFC 8460, Section "3. Reporting Policy", see:
https://datatracker.ietf.org/doc/html/rfc8460#section-3
'''
"""

from pyparsing import (
Literal,
Expand All @@ -23,23 +23,22 @@
)


WSP = White(ws=' ', exact=1).suppress() # Whitespace
WSP = White(ws=" ", exact=1).suppress() # Whitespace

field_delim = ZeroOrMore(WSP) + Literal(';') + ZeroOrMore(WSP) # Fields are semicolon-delimited
ura_delim = ZeroOrMore(WSP) + Literal(',') + ZeroOrMore(WSP) # multiple RUAs are comma-delimited
field_delim = ZeroOrMore(WSP) + Literal(";") + ZeroOrMore(WSP) # Fields are semicolon-delimited
ura_delim = ZeroOrMore(WSP) + Literal(",") + ZeroOrMore(WSP) # multiple RUAs are comma-delimited

tlsrpt_ext_name = Word(alphanums, alphanums + "_-.", max=32)
tlsrpt_ext_value = Word(alphanums, alphanums + "_-.")
tlsrpt_extension = ZeroOrMore(tlsrpt_ext_name + Literal('=') + tlsrpt_ext_value)
tlsrpt_extension = ZeroOrMore(tlsrpt_ext_name + Literal("=") + tlsrpt_ext_value)

# RegEx for parsing email.
regex_tld = r"(?:[a-zA-Z]{2,63}|xn--[a-zA-Z0-9]+)"
regex_mailaddr = (
r"(?P<mailaddr>([a-zA-Z0-9]{0,61}@)?([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+" r"" + regex_tld + ")"
)
mail_uri = Combine(CaselessLiteral("mailto:") + Regex(regex_mailaddr))
tlsrpt_rua = Literal("rua=") + \
delimitedList(mail_uri | pyparsing_common.url, delim=',').setResultsName('tlsrpt_uri')
tlsrpt_rua = Literal("rua=") + delimitedList(mail_uri | pyparsing_common.url, delim=",").setResultsName("tlsrpt_uri")

tlsrpt_field = tlsrpt_rua + ZeroOrMore(field_delim + tlsrpt_extension)

Expand Down
18 changes: 9 additions & 9 deletions checks/test/test_tlsrpt_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,36 @@
def test_record_parse_simple_mailto():
TXT_RECORD = "v=TLSRPTv1; rua=mailto:[email protected]"
parsed = tlsrpt_parsing.record.parseString(TXT_RECORD)
assert parsed.tlsrpt_version == 'v=TLSRPTv1'
assert parsed.tlsrpt_uri[0] == 'mailto:[email protected]'
assert parsed.tlsrpt_version == "v=TLSRPTv1"
assert parsed.tlsrpt_uri[0] == "mailto:[email protected]"


def test_record_parse_multiple_mailto():
TXT_RECORD = "v=TLSRPTv1;rua=mailto:[email protected],mailto:[email protected]"
parsed = tlsrpt_parsing.record.parseString(TXT_RECORD)
assert parsed.tlsrpt_version == 'v=TLSRPTv1'
assert parsed.tlsrpt_uri[0] == 'mailto:[email protected]'
assert parsed.tlsrpt_uri[1] == 'mailto:[email protected]'
assert parsed.tlsrpt_version == "v=TLSRPTv1"
assert parsed.tlsrpt_uri[0] == "mailto:[email protected]"
assert parsed.tlsrpt_uri[1] == "mailto:[email protected]"


def test_record_parse_simple_https():
TXT_RECORD = "v=TLSRPTv1; rua=https://reporting.example.com/v1/tlsrpt"
parsed = tlsrpt_parsing.record.parseString(TXT_RECORD)
assert parsed.tlsrpt_version == 'v=TLSRPTv1'
assert parsed.tlsrpt_uri[0] == 'https://reporting.example.com/v1/tlsrpt'
assert parsed.tlsrpt_version == "v=TLSRPTv1"
assert parsed.tlsrpt_uri[0] == "https://reporting.example.com/v1/tlsrpt"


def test_record_parse_with_extension():
TXT_RECORD = "v=TLSRPTv1; rua=https://reporting.example.com/v1/tlsrpt; ext=extvalue"
parsed = tlsrpt_parsing.record.parseString(TXT_RECORD)
assert parsed.tlsrpt_version == 'v=TLSRPTv1'
assert parsed.tlsrpt_version == "v=TLSRPTv1"


def test_parse_silent():
"""
Check that parse_silent does not throw a ParseException but instead returns
None if the TLSRPT policy record is malformed.
"""
TXT_RECORD = "v=TLSRPTv1; rua=!!" # broken TLSRPT
TXT_RECORD = "v=TLSRPTv1; rua=!!" # broken TLSRPT
parsed = tlsrpt_parsing.parse_silent(TXT_RECORD)
assert parsed is None

0 comments on commit b886bcc

Please sign in to comment.