aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg/moderation_policy.py
diff options
context:
space:
mode:
authorDennis Fink2026-11-17 17:09:59 +0100
committerDennis Fink2026-11-17 17:09:59 +0100
commit6d6d079a3a99d3d3a922a640b6925eb2adf044de (patch)
tree33cc19241f4ce86ea43b2f194087c58e3fdc8a4d /webmentions_ssg/moderation_policy.py
parent227092b57acc886469e16bcc4c0d3256e86d3d1e (diff)
downloadwebmentions-ssg-6d6d079a3a99d3d3a922a640b6925eb2adf044de.tar.gz
webmentions-ssg-6d6d079a3a99d3d3a922a640b6925eb2adf044de.zip
refactor(core)!: split forms and models by feature
Replace monolithic form and model modules with focused packages for receiver, moderation, source, user, and shared validation concerns. Rename source policy concepts to domain moderation, including routes, models, tables, configuration, templates, and tests. BREAKING CHANGE: form and model import paths, moderation routes, and `SOURCE_ALLOWLIST_ENABLED` have changed; use `ALLOWLIST_ENABLED` and the new feature-specific modules.
Diffstat (limited to 'webmentions_ssg/moderation_policy.py')
-rw-r--r--webmentions_ssg/moderation_policy.py62
1 files changed, 62 insertions, 0 deletions
diff --git a/webmentions_ssg/moderation_policy.py b/webmentions_ssg/moderation_policy.py
new file mode 100644
index 0000000..51e7995
--- /dev/null
+++ b/webmentions_ssg/moderation_policy.py
@@ -0,0 +1,62 @@
+# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me>
+#
+# SPDX-License-Identifier: BSD-3-Clause
+
+"""Admission policy for incoming Webmention source hostnames."""
+
+import ipaddress
+
+import sqlalchemy as sa
+from flask import current_app
+
+from .extensions import db
+from .hostname import normalize_hostname
+from .models.moderation import AllowedDomain, BlockedDomain
+
+
+def domain_from_url(source: str) -> str | None:
+ """Extract and canonicalize the hostname without a network request."""
+ try:
+ return normalize_hostname(source, from_url=True)
+ except ValueError:
+ return None
+
+
+def has_matching_domain(
+ model: type[AllowedDomain | BlockedDomain], domain: str
+) -> bool:
+ """Match the hostname itself and parent domains, never partial labels."""
+ candidates = [domain]
+ # An IP address can only match itself, not a suffix of its textual form.
+ try:
+ ipaddress.ip_address(domain)
+ except ValueError:
+ parts = domain.split(".")
+ candidates.extend(".".join(parts[i:]) for i in range(1, len(parts)))
+
+ return (
+ db.session.scalar(
+ sa.select(model.id).where(model.domain.in_(candidates)).limit(1)
+ )
+ is not None
+ )
+
+
+def rejection_reason(source: str) -> str | None:
+ """Return the reason a source cannot be received, or ``None`` if accepted.
+
+ The blocklist always wins. In allowlist mode an empty allowlist rejects
+ every source. The database, not a stale per-worker cache, is consulted.
+ """
+ if (domain := domain_from_url(source)) is None:
+ return "Invalid source hostname"
+
+ if has_matching_domain(BlockedDomain, domain):
+ return "Source domain is blocked"
+
+ if current_app.config["ALLOWLIST_ENABLED"] and not has_matching_domain(
+ AllowedDomain, domain
+ ):
+ return "Source domain is not allowlisted"
+
+ return None