Skip to content

Commit

Permalink
Allowing whitespace according to RFC7489 (#110)
Browse files Browse the repository at this point in the history
* Allowing whitespace according to RFC7489

* linter
  • Loading branch information
kazet authored Dec 16, 2023
1 parent 8a095a7 commit bdf126c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
16 changes: 12 additions & 4 deletions checkdmarc.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,12 @@

__version__ = "4.8.5"

DMARC_VERSION_REGEX_STRING = r"v *= *DMARC1;"
WSP_REGEX = "[ \t]"
DMARC_VERSION_REGEX_STRING = fr"v{WSP_REGEX}*={WSP_REGEX}*DMARC1{WSP_REGEX}*;"
BIMI_VERSION_REGEX_STRING = r"v=BIMI1;"
DMARC_TAG_VALUE_REGEX_STRING = r"([a-z]{1,5}) *= *([\w.:@/+!,_\- ]+)"
DMARC_TAG_VALUE_REGEX_STRING = (
fr"([a-z]{{1,5}}){WSP_REGEX}*={WSP_REGEX}*([\w.:@/+!,_\- ]+)"
)
BIMI_TAG_VALUE_REGEX_STRING = r"([a-z]{1}) *= *(.*)"
MAILTO_REGEX_STRING = r"^(mailto):" \
r"([\w\-!#$%&'*+-/=?^_`{|}~]" \
Expand Down Expand Up @@ -327,7 +330,12 @@ class _DMARCGrammar(Grammar):
"""Defines Pyleri grammar for DMARC records"""
version_tag = Regex(DMARC_VERSION_REGEX_STRING, IGNORECASE)
tag_value = Regex(DMARC_TAG_VALUE_REGEX_STRING, IGNORECASE)
START = Sequence(version_tag, List(tag_value, delimiter=";", opt=True))
START = Sequence(
version_tag,
List(
tag_value,
delimiter=Regex(f"{WSP_REGEX}*;{WSP_REGEX}*"),
opt=True))


class _BIMIGrammar(Grammar):
Expand Down Expand Up @@ -1399,7 +1407,7 @@ def parse_dmarc_record(
# Find explicit tags
for pair in pairs:
tags[pair[0].lower()] = OrderedDict(
[("value", str(pair[1])), ("explicit", True)])
[("value", str(pair[1].strip())), ("explicit", True)])

# Include implicit tags and their defaults
for tag in tag_values.keys():
Expand Down
2 changes: 2 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ def testDMARCMixedFormatting(self):
examples = [
"v=DMARC1;p=ReJect",
"v = DMARC1;p=reject;",
"v = DMARC1\t;\tp=reject\t;",
"v = DMARC1\t;\tp\t\t\t=\t\t\treject\t;",
"V=DMARC1;p=reject;"
]

Expand Down

0 comments on commit bdf126c

Please sign in to comment.