aboutsummaryrefslogtreecommitdiff
path: root/tests/test_hostname.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_hostname.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_hostname.py')
-rw-r--r--tests/test_hostname.py72
1 files changed, 72 insertions, 0 deletions
diff --git a/tests/test_hostname.py b/tests/test_hostname.py
new file mode 100644
index 0000000..0faaacd
--- /dev/null
+++ b/tests/test_hostname.py
@@ -0,0 +1,72 @@
+# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me>
+#
+# SPDX-License-Identifier: BSD-3-Clause
+
+"""Regression tests for the one shared hostname normalizer."""
+
+import pytest
+
+from webmentions_ssg.hostname import normalize_hostname
+
+
+@pytest.mark.parametrize(
+ ("value", "expected"),
+ [
+ ("Example.COM.", "example.com"),
+ ("blog.Example.COM", "blog.example.com"),
+ ("bücher.example", "xn--bcher-kva.example"),
+ ("198.51.100.10", "198.51.100.10"),
+ ("2001:db8::1", "2001:db8::1"),
+ ],
+)
+def test_normalize_bare_hostname(value: str, expected: str) -> None:
+ assert normalize_hostname(value) == expected
+
+
+@pytest.mark.parametrize(
+ "value",
+ [
+ "",
+ " example.com",
+ "https://example.com",
+ "example.com/path",
+ "*.example.com",
+ "-bad.example",
+ "bad-.example",
+ "example..com",
+ "example.com:8080",
+ "hello_foo.example",
+ "a" * 64 + ".example",
+ ],
+)
+def test_reject_invalid_bare_hostname(value: str) -> None:
+ with pytest.raises(ValueError):
+ normalize_hostname(value)
+
+
+@pytest.mark.parametrize(
+ ("url", "expected"),
+ [
+ ("https://EXAMPLE.com./path", "example.com"),
+ ("http://[2001:db8::1]/", "2001:db8::1"),
+ ("https://BÜCHER.example.:8443/post", "xn--bcher-kva.example"),
+ ],
+)
+def test_normalize_url_hostname(url: str, expected: str) -> None:
+ assert normalize_hostname(url, from_url=True) == expected
+
+
+@pytest.mark.parametrize("url", ["https:///path", "http://[::1", "invalid"])
+def test_reject_url_without_valid_hostname(url: str) -> None:
+ with pytest.raises(ValueError):
+ normalize_hostname(url, from_url=True)
+
+
+def test_wildcard_mode_is_explicit() -> None:
+ assert normalize_hostname("*.EXAMPLE.com.", allow_patterns=True) == "*.example.com"
+ assert (
+ normalize_hostname("blog?.example.com", allow_patterns=True)
+ == "blog?.example.com"
+ )
+ with pytest.raises(ValueError):
+ normalize_hostname("*.example.com")