Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1474 check for URI scheme in DMARC parser, add tests for DMARC parser #1493

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion checks/tasks/dmarc_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
30 changes: 30 additions & 0 deletions checks/test/test_dmarc_parser.py
Original file line number Diff line number Diff line change
@@ -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:[email protected]"]) 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(["[email protected]"])


def test_parse():
sample_record = "v=DMARC1; p=none; rua=mailto:[email protected]"
result = parse(sample_record)
assert result.version == "v=DMARC1"
assert result.directives.request == "p=none"
assert result.directives.auri == "rua=mailto:[email protected]"