diff --git a/mentions/config.py b/mentions/config.py index c3cbfb0..9502bdc 100644 --- a/mentions/config.py +++ b/mentions/config.py @@ -1,10 +1,13 @@ """Helper functions that derive from `mentions.options` values.""" +import logging from typing import Optional, Set from urllib.parse import urljoin from mentions import options from mentions.util import compatibility, get_domain +log = logging.getLogger(__name__) + def base_url() -> str: """Get the base URL of this server from current `options`.""" @@ -45,6 +48,10 @@ def accept_domain_incoming( domain = get_domain(url) + if not domain: + log.warning(f"accept_domain_incoming received invalid URL: {url}") + return False + if domains_allow: return _domain_in_set(domain, domains_allow) @@ -74,6 +81,10 @@ def accept_domain_outgoing( """ domain = get_domain(url) + if not domain: + log.warning(f"accept_domain_outgoing received invalid URL: {url}") + return False + if not allow_self_mention and domain == options.domain_name(): return False if domains_allow and domains_deny: diff --git a/mentions/tasks/incoming/process.py b/mentions/tasks/incoming/process.py index 6b55397..e8f57d7 100644 --- a/mentions/tasks/incoming/process.py +++ b/mentions/tasks/incoming/process.py @@ -38,13 +38,10 @@ def process_incoming_webmention( ) -> Optional[Webmention]: log.info(f"Processing webmention '{source_url}' -> '{target_url}'") - domains_allow = domains_allow or options.incoming_domains_allow() - domains_deny = domains_deny or options.incoming_domains_deny() - allow_source_domain = config.accept_domain_incoming( source_url, - domains_allow=domains_allow, - domains_deny=domains_deny, + domains_allow=domains_allow or options.incoming_domains_allow(), + domains_deny=domains_deny or options.incoming_domains_deny(), ) if not allow_source_domain: log.warning( diff --git a/tests/tests/test_tasks/test_incoming_webmention.py b/tests/tests/test_tasks/test_incoming_webmention.py index 6f2828c..4af08e6 100644 --- a/tests/tests/test_tasks/test_incoming_webmention.py +++ b/tests/tests/test_tasks/test_incoming_webmention.py @@ -3,7 +3,9 @@ """ import logging -from mentions import config +from django.test import override_settings + +from mentions import options from mentions.exceptions import SourceNotAccessible, TargetWrongDomain from mentions.models import Webmention from mentions.models.mixins import IncomingMentionType @@ -246,22 +248,7 @@ def test_target_object_with_object_not_required(self): class AllowDenyOptionTests(SimpleTestCase): - def test_is_domain_acceptable(self): - self.assertTrue(config.accept_domain_incoming("https://allow.org", None, None)) - self.assertTrue( - config.accept_domain_incoming("https://allow.org", {"allow.org"}, None) - ) - self.assertFalse( - config.accept_domain_incoming("https://deny.org", {"allow.org"}, None) - ) - - self.assertTrue( - config.accept_domain_incoming("https://allow.org", None, {"deny.org"}) - ) - self.assertFalse( - config.accept_domain_incoming("https://deny.org", None, {"deny.org"}) - ) - + @override_settings(**{options.SETTING_DOMAINS_INCOMING_ALLOW: {"allow.org"}}) @patch_http_get(text=SOURCE_TEXT_DEFAULT) def test_process_incoming_webmention_with_domains_allow(self): self.assertIsNotNone( @@ -269,7 +256,6 @@ def test_process_incoming_webmention_with_domains_allow(self): testfunc.random_url(subdomain="", domain="allow.org", port=""), TARGET_URL, sent_by=testfunc.random_url(), - domains_allow={"allow.org"}, ) ) self.assertIsNone( @@ -277,10 +263,10 @@ def test_process_incoming_webmention_with_domains_allow(self): testfunc.random_url(subdomain="", domain="other.org", port=""), TARGET_URL, sent_by=testfunc.random_url(), - domains_allow={"allow.org"}, ) ) + @override_settings(**{options.SETTING_DOMAINS_INCOMING_DENY: {"deny.org"}}) @patch_http_get(text=SOURCE_TEXT_DEFAULT) def test_process_incoming_webmention_with_domains_deny(self): self.assertIsNotNone( @@ -288,7 +274,6 @@ def test_process_incoming_webmention_with_domains_deny(self): testfunc.random_url(subdomain="", domain="allow.org", port=""), TARGET_URL, sent_by=testfunc.random_url(), - domains_deny={"deny.org"}, ) ) self.assertIsNone( @@ -296,14 +281,5 @@ def test_process_incoming_webmention_with_domains_deny(self): testfunc.random_url(subdomain="", domain="deny.org", port=""), TARGET_URL, sent_by=testfunc.random_url(), - domains_deny={"deny.org"}, - ) - ) - self.assertIsNone( - incoming.process_incoming_webmention( - testfunc.random_url(domain="deny.org", port=""), - TARGET_URL, - sent_by=testfunc.random_url(), - domains_deny={"*.deny.org"}, ) )