diff --git a/checkdmarc.py b/checkdmarc.py index 868beb3..ed7bc93 100644 --- a/checkdmarc.py +++ b/checkdmarc.py @@ -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\-!#$%&'*+-/=?^_`{|}~]" \ @@ -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): @@ -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(): diff --git a/tests.py b/tests.py index 6e59529..aa04acc 100755 --- a/tests.py +++ b/tests.py @@ -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;" ]