From f655febc08c51ae6a992eb2a121050b72c59aeae Mon Sep 17 00:00:00 2001 From: Uwe Kamper Date: Fri, 6 Sep 2024 17:18:16 +0200 Subject: [PATCH] #1474 check for URI scheme in DMARC parser, add tests for DMARC parser --- checks/tasks/dmarc_parser.py | 4 +++- checks/test/test_dmarc_parser.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 checks/test/test_dmarc_parser.py diff --git a/checks/tasks/dmarc_parser.py b/checks/tasks/dmarc_parser.py index 42ad38f79..c44b3db97 100644 --- a/checks/tasks/dmarc_parser.py +++ b/checks/tasks/dmarc_parser.py @@ -74,9 +74,11 @@ def _check_dmarc_uri(tokens): uri, numeric = uri.split("!") dmarc_uri_numeric.parseString(numeric) try: - urlparse(uri) + parsed_url = urlparse(uri) except ValueError: raise ParseException("Could not parse URI.") + if parsed_url.scheme == "": + raise ParseException("URI scheme is missing (mailto:).") return None diff --git a/checks/test/test_dmarc_parser.py b/checks/test/test_dmarc_parser.py new file mode 100644 index 000000000..cc77412e1 --- /dev/null +++ b/checks/test/test_dmarc_parser.py @@ -0,0 +1,30 @@ +# Copyright: 2024, ECP, NLnet Labs and the Internet.nl contributors +# SPDX-License-Identifier: Apache-2.0 +import pytest +from pyparsing import ParseException + +from checks.tasks.dmarc_parser import _check_dmarc_uri, parse + + +def test__check_dmarc_uri(): + """ + Check if None is returned on valid URI + """ + assert _check_dmarc_uri(["mailto:test@example.com"]) is None + + +def test__check_dmarc_uri_detect_missing_uri_scheme(): + """ + Many people forget to add the mailto: scheme to their DMARC URI. + This common error should be detected. + """ + with pytest.raises(ParseException): + _check_dmarc_uri(["test@example.com"]) + + +def test_parse(): + sample_record = "v=DMARC1; p=none; rua=mailto:dmarc@example.com" + result = parse(sample_record) + assert result.version == "v=DMARC1" + assert result.directives.request == "p=none" + assert result.directives.auri == "rua=mailto:dmarc@example.com"