aboutsummaryrefslogtreecommitdiff
path: root/tests/test_source_policy.py
diff options
context:
space:
mode:
authorDennis Fink2026-11-17 14:44:00 +0100
committerDennis Fink2026-11-17 14:44:00 +0100
commit227092b57acc886469e16bcc4c0d3256e86d3d1e (patch)
treec1de8d5f8f3a37562e482f84e8017213c08a05d6 /tests/test_source_policy.py
parentf98b0e446b41325c494bd97900b7bbf730a7d67e (diff)
downloadwebmentions-ssg-227092b57acc886469e16bcc4c0d3256e86d3d1e.tar.gz
webmentions-ssg-227092b57acc886469e16bcc4c0d3256e86d3d1e.zip
feat(receiver): add source domain access rules
Add persistent source block and allow lists with authenticated management, single-row and bulk blocking actions, and optional fail-closed allowlist mode. Reject disallowed sources before enqueueing and recheck policy in verification workers, while sharing canonical hostname normalization across receiver, sender, and configuration paths.
Diffstat (limited to 'tests/test_source_policy.py')
-rw-r--r--tests/test_source_policy.py67
1 files changed, 67 insertions, 0 deletions
diff --git a/tests/test_source_policy.py b/tests/test_source_policy.py
new file mode 100644
index 0000000..cddfa3a
--- /dev/null
+++ b/tests/test_source_policy.py
@@ -0,0 +1,67 @@
+# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me>
+#
+# SPDX-License-Identifier: BSD-3-Clause
+
+"""Domain normalization and receiver admission policy."""
+
+import pytest
+from flask import Flask
+
+from webmentions_ssg import db
+from webmentions_ssg.models import AllowedSource, BlockedSource
+from webmentions_ssg.source_policy import (
+ source_domain_from_url,
+ source_rejection_reason,
+)
+
+
+def test_source_domain_from_url() -> None:
+ assert (
+ source_domain_from_url("https://BÜCHER.example.:8443/post")
+ == "xn--bcher-kva.example"
+ )
+ assert source_domain_from_url("https://[2001:db8::1]/post") == "2001:db8::1"
+ assert source_domain_from_url("invalid") is None
+
+
+def test_blocked_domain_matches_itself_and_subdomains_but_not_suffixes(
+ app: Flask,
+) -> None:
+ with app.app_context():
+ db.session.add(BlockedSource(domain="example.org"))
+ db.session.commit()
+ for source in (
+ "https://example.org/post",
+ "https://blog.example.org/post",
+ "https://A.blog.Example.ORG./post",
+ ):
+ assert source_rejection_reason(source) == "Source domain is blocked"
+ for source in (
+ "https://notexample.org/post",
+ "https://example.org.evil/post",
+ "https://other.net/post",
+ ):
+ assert source_rejection_reason(source) is None
+
+
+def test_allowlist_is_fail_closed_and_blocklist_takes_precedence(app: Flask) -> None:
+ app.config["SOURCE_ALLOWLIST_ENABLED"] = True
+ with app.app_context():
+ assert (
+ source_rejection_reason("https://example.org/post")
+ == "Source domain is not allowlisted"
+ )
+ db.session.add(AllowedSource(domain="example.org"))
+ db.session.commit()
+ assert source_rejection_reason("https://blog.example.org/post") is None
+ assert (
+ source_rejection_reason("https://notexample.org/post")
+ == "Source domain is not allowlisted"
+ )
+ db.session.add(BlockedSource(domain="blog.example.org"))
+ db.session.commit()
+ assert (
+ source_rejection_reason("https://blog.example.org/post")
+ == "Source domain is blocked"
+ )
+ assert source_rejection_reason("https://example.org/post") is None