diff options
| author | Dennis Fink | 2026-11-17 14:44:00 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-11-17 14:44:00 +0100 |
| commit | 227092b57acc886469e16bcc4c0d3256e86d3d1e (patch) | |
| tree | c1de8d5f8f3a37562e482f84e8017213c08a05d6 /webmentions_ssg/source_policy.py | |
| parent | f98b0e446b41325c494bd97900b7bbf730a7d67e (diff) | |
| download | webmentions-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 'webmentions_ssg/source_policy.py')
| -rw-r--r-- | webmentions_ssg/source_policy.py | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/webmentions_ssg/source_policy.py b/webmentions_ssg/source_policy.py new file mode 100644 index 0000000..2c62527 --- /dev/null +++ b/webmentions_ssg/source_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 import AllowedSource, BlockedSource + + +def source_domain_from_url(source: str) -> str | None: + """Extract and canonicalize the source hostname without a network request.""" + try: + return normalize_hostname(source, from_url=True) + except ValueError: + return None + + +def has_matching_domain( + model: type[AllowedSource | BlockedSource], 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 source_rejection_reason(source: str) -> str | None: + """Return the reason a source cannot be received, or None when 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 := source_domain_from_url(source)) is None: + return "Invalid source hostname" + + if has_matching_domain(BlockedSource, domain): + return "Source domain is blocked" + + if current_app.config["SOURCE_ALLOWLIST_ENABLED"] and not has_matching_domain( + AllowedSource, domain + ): + return "Source domain is not allowlisted" + + return None |
