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 | |
| 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')
| -rw-r--r-- | webmentions_ssg/config.py | 46 | ||||
| -rw-r--r-- | webmentions_ssg/forms.py | 35 | ||||
| -rw-r--r-- | webmentions_ssg/hostname.py | 61 | ||||
| -rw-r--r-- | webmentions_ssg/migrations/versions/3e8c87a96b2d_source_domain_rules.py | 39 | ||||
| -rw-r--r-- | webmentions_ssg/migrations/versions/b8179a2e46c3_merge_domain_rules_and_name_changes.py | 22 | ||||
| -rw-r--r-- | webmentions_ssg/models.py | 18 | ||||
| -rw-r--r-- | webmentions_ssg/net.py | 13 | ||||
| -rw-r--r-- | webmentions_ssg/source_policy.py | 62 | ||||
| -rw-r--r-- | webmentions_ssg/static/js/received-bulk-actions.js | 5 | ||||
| -rw-r--r-- | webmentions_ssg/tasks/source_parser.py | 8 | ||||
| -rw-r--r-- | webmentions_ssg/tasks/verification.py | 6 | ||||
| -rw-r--r-- | webmentions_ssg/templates/base.html | 2 | ||||
| -rw-r--r-- | webmentions_ssg/templates/received.html | 20 | ||||
| -rw-r--r-- | webmentions_ssg/templates/source_domains.html | 68 | ||||
| -rw-r--r-- | webmentions_ssg/views/admin.py | 140 | ||||
| -rw-r--r-- | webmentions_ssg/views/endpoint.py | 4 |
16 files changed, 499 insertions, 50 deletions
diff --git a/webmentions_ssg/config.py b/webmentions_ssg/config.py index 1419b82..576f376 100644 --- a/webmentions_ssg/config.py +++ b/webmentions_ssg/config.py @@ -9,6 +9,8 @@ from urllib.parse import urlsplit from flask.config import Config +from .hostname import normalize_hostname + MIN_SECRET_KEY_LENGTH = 32 @@ -52,6 +54,9 @@ class DefaultConfig: # Receiver # Empty by default: no target hostnames are accepted until configured. ALLOWED_TARGET_HOSTNAMES = frozenset() + # When enabled, only source domains listed in AllowedSource are accepted. + # Entries in BlockedSource always take precedence. + SOURCE_ALLOWLIST_ENABLED = False # Cap incoming HTTP request bodies (also applies to other Flask routes). MAX_CONTENT_LENGTH = 64 * 1024 # Independent UTF-8 byte limit for each submitted source/target URL. @@ -137,29 +142,6 @@ class TestingConfig(DefaultConfig): ALLOWED_TARGET_HOSTNAMES = frozenset({"dennisfink.me"}) -def normalize_hostnames(value: object) -> set[str]: - """Validate and normalize a collection of hostnames. - - :param value: List, tuple, set, or frozenset of hostnames to normalize. - :returns: Normalized, lowercase hostnames without trailing dots. - :raises TypeError: If the input is not a supported collection type. - :raises ValueError: If a collection item is not a nonempty hostname string. - """ - if not isinstance(value, (list, tuple, set, frozenset)): - raise TypeError("Expected a list, tuple, or set of hostnames.") - - hostnames: set[str] = set() - for hostname in value: - if not isinstance(hostname, str) or not ( - normalized := hostname.strip().lower().rstrip(".") - ): - raise ValueError("Hostnames must be non-empty strings.") - - hostnames.add(normalized) - - return hostnames - - def validate_secret_key(value: object) -> str | bytes: """Require a non-placeholder, sufficiently long deployment signing key. @@ -214,10 +196,26 @@ def validated_config_overrides( normalized: dict[str, object] = {} for key in ("ALLOWED_TARGET_HOSTNAMES", "IGNORED_TARGET_HOSTNAMES"): try: - normalized[key] = normalize_hostnames(config[key]) + value = config[key] + if not isinstance(value, (list, tuple, set, frozenset)): + raise TypeError("Expected a list, tuple, or set of hostnames.") + hostnames: set[str] = set() + for hostname in value: + if not isinstance(hostname, str) or not hostname.strip(): + raise ValueError("Hostnames must be non-empty strings.") + hostnames.add( + normalize_hostname( + hostname.strip(), + allow_patterns=key == "IGNORED_TARGET_HOSTNAMES", + ) + ) + normalized[key] = hostnames except (TypeError, ValueError) as exc: raise type(exc)(f"{key}: {exc}") from exc + if type(config["SOURCE_ALLOWLIST_ENABLED"]) is not bool: + raise ValueError("SOURCE_ALLOWLIST_ENABLED must be a boolean.") + proxy_hops = config["PROXY_FIX_TRUSTED_HOPS"] if type(proxy_hops) is not int or proxy_hops < 0: raise ValueError("PROXY_FIX_TRUSTED_HOPS must be a non-negative integer.") diff --git a/webmentions_ssg/forms.py b/webmentions_ssg/forms.py index ae969b3..3b61a8a 100644 --- a/webmentions_ssg/forms.py +++ b/webmentions_ssg/forms.py @@ -30,7 +30,8 @@ from wtforms.validators import ( StopValidation, ) -from .net import is_public_address, normalized_hostname +from .hostname import normalize_hostname +from .net import is_public_address from .validation import is_control_character RECEIVED_STATUS_CHOICES = { @@ -120,11 +121,12 @@ class AllowedTargetHostname: :param field: Field containing the URL to validate. :raises ValidationError: If the URL hostname is not configured as allowed. """ - hostname = normalized_hostname(field.data) - if ( - hostname is not None - and hostname in current_app.config["ALLOWED_TARGET_HOSTNAMES"] - ): + try: + hostname = normalize_hostname(field.data, from_url=True) + except ValueError: + hostname = None + + if hostname in current_app.config["ALLOWED_TARGET_HOSTNAMES"]: return raise ValidationError(self.message or field.gettext("Invalid input.")) @@ -133,9 +135,10 @@ class RejectNonPublicIPLiteral: """Reject non-public IP literals without performing DNS in the web worker.""" def __call__(self, form: BaseForm, field: Field) -> None: - hostname = normalized_hostname(field.data) - if hostname is None: - raise ValidationError(field.gettext("Invalid input.")) + try: + hostname = normalize_hostname(field.data, from_url=True) + except ValueError as exc: + raise ValidationError(field.gettext("Invalid input.")) from exc try: address = ipaddress.ip_address(hostname) @@ -249,3 +252,17 @@ class ConfirmActionForm(FlaskForm): """Form for confirming an administrative action.""" submit = SubmitField("Confirm") + + +class SourceDomainForm(FlaskForm): + """Add a domain to the source blocklist or allowlist.""" + + domain = StringField("Source domain", validators=[InputRequired()]) + submit = SubmitField("Add domain") + + def validate_domain(self, field: Field) -> None: + """Require a bare hostname and persist its canonical form.""" + try: + field.data = normalize_hostname(field.data) + except ValueError as exc: + raise ValidationError(str(exc)) from exc diff --git a/webmentions_ssg/hostname.py b/webmentions_ssg/hostname.py new file mode 100644 index 0000000..4e55069 --- /dev/null +++ b/webmentions_ssg/hostname.py @@ -0,0 +1,61 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me> +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Canonical hostname normalization shared by URL, config, and source rules.""" + +import ipaddress +import re +from urllib.parse import urlsplit + +LABEL = re.compile(r"[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\Z") +PATTERN_LABEL = re.compile(r"[a-z0-9*?\[\]-]+\Z") + + +def normalize_hostname( + value: str, *, from_url: bool = False, allow_patterns: bool = False +) -> str: + """Return the canonical IDNA hostname or IP literal. + + Bare hostnames are required by default. ``from_url=True`` extracts a + hostname from a URL, including bracketed IPv6 literals. ``allow_patterns`` + permits sender-side ignored-hostname glob patterns; it must not be used + for receiver allow/block entries or allowed target hosts. + + Whitespace and malformed hosts are rejected. Callers normalizing config + collections may strip surrounding whitespace before calling this function. + + :raises ValueError: If the hostname is missing or malformed. + """ + if not isinstance(value, str) or not value or value != value.strip(): + raise ValueError("Enter a hostname, without a URL or whitespace.") + + if from_url: + try: + hostname = urlsplit(value).hostname + except ValueError as exc: + raise ValueError("Invalid hostname.") from exc + if hostname is None: + raise ValueError("Invalid hostname.") + else: + hostname = value + + hostname = hostname.lower().rstrip(".") + try: + return str(ipaddress.ip_address(hostname)) + except ValueError: + pass + + try: + hostname = hostname.encode("idna").decode("ascii").lower() + except (UnicodeError, ValueError) as exc: + raise ValueError("Invalid hostname.") from exc + + label_pattern = PATTERN_LABEL if allow_patterns else LABEL + if ( + not hostname + or len(hostname) > 253 + or any(not label_pattern.fullmatch(label) for label in hostname.split(".")) + ): + raise ValueError("Invalid hostname.") + return hostname diff --git a/webmentions_ssg/migrations/versions/3e8c87a96b2d_source_domain_rules.py b/webmentions_ssg/migrations/versions/3e8c87a96b2d_source_domain_rules.py new file mode 100644 index 0000000..04f5b8d --- /dev/null +++ b/webmentions_ssg/migrations/versions/3e8c87a96b2d_source_domain_rules.py @@ -0,0 +1,39 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me> +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Add persistent source-domain block and allow lists. + +Revision ID: 3e8c87a96b2d +Revises: e62b1a7c4d90 +""" + +import sqlalchemy as sa +from alembic import op + +revision = "3e8c87a96b2d" +down_revision = "e62b1a7c4d90" +branch_labels = None +depends_on = None + + +def upgrade(): + op.create_table( + "blocked_sources", + sa.Column("id", sa.Integer(), nullable=False), + sa.Column("domain", sa.Text(), nullable=False), + sa.PrimaryKeyConstraint("id"), + sa.UniqueConstraint("domain"), + ) + op.create_table( + "allowed_sources", + sa.Column("id", sa.Integer(), nullable=False), + sa.Column("domain", sa.Text(), nullable=False), + sa.PrimaryKeyConstraint("id"), + sa.UniqueConstraint("domain"), + ) + + +def downgrade(): + op.drop_table("allowed_sources") + op.drop_table("blocked_sources") diff --git a/webmentions_ssg/migrations/versions/b8179a2e46c3_merge_domain_rules_and_name_changes.py b/webmentions_ssg/migrations/versions/b8179a2e46c3_merge_domain_rules_and_name_changes.py new file mode 100644 index 0000000..860e042 --- /dev/null +++ b/webmentions_ssg/migrations/versions/b8179a2e46c3_merge_domain_rules_and_name_changes.py @@ -0,0 +1,22 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me> +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Merge source-domain rules with Webmention lifecycle naming changes. + +Revision ID: b8179a2e46c3 +Revises: 3e8c87a96b2d, 9a4e0c8211d6 +""" + +revision = "b8179a2e46c3" +down_revision = ("3e8c87a96b2d", "9a4e0c8211d6") +branch_labels = None +depends_on = None + + +def upgrade() -> None: + """Both parent migrations have already applied their schema changes.""" + + +def downgrade() -> None: + """Downgrading past the merge leaves Alembic's two parent branches.""" diff --git a/webmentions_ssg/models.py b/webmentions_ssg/models.py index 2b375dd..2304788 100644 --- a/webmentions_ssg/models.py +++ b/webmentions_ssg/models.py @@ -101,6 +101,24 @@ class User(UserMixin, Base): return check_password_hash(self.password_hash, password) +class BlockedSource(Base): + """A source domain denied before verification or outbound fetching.""" + + __tablename__ = "blocked_sources" + + id: Mapped[int] = mapped_column(primary_key=True) + domain: Mapped[str] = mapped_column(Text(), nullable=False, unique=True) + + +class AllowedSource(Base): + """A source domain accepted when allowlist mode is enabled.""" + + __tablename__ = "allowed_sources" + + id: Mapped[int] = mapped_column(primary_key=True) + domain: Mapped[str] = mapped_column(Text(), nullable=False, unique=True) + + class ReceivedWebmention(Base): """Represent a Webmention received for verification.""" diff --git a/webmentions_ssg/net.py b/webmentions_ssg/net.py index b827180..df62ca7 100644 --- a/webmentions_ssg/net.py +++ b/webmentions_ssg/net.py @@ -279,19 +279,6 @@ def resolve_public_addresses( return tuple(str(address) for address in addresses) -def normalized_hostname(url: str) -> str | None: - """Return a normalized URL hostname, or None for malformed URLs. - - :param url: URL from which to extract a hostname. - :returns: Normalized hostname, or ``None`` when the URL is invalid or has no host. - """ - try: - hostname = urlsplit(url).hostname - except TypeError, ValueError: - return None - return hostname.rstrip(".") if hostname is not None else None - - def media_type_of(headers: Mapping[str, str]) -> str: """Return a normalized media type without Content-Type parameters. 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 diff --git a/webmentions_ssg/static/js/received-bulk-actions.js b/webmentions_ssg/static/js/received-bulk-actions.js index c4ac31b..06ab912 100644 --- a/webmentions_ssg/static/js/received-bulk-actions.js +++ b/webmentions_ssg/static/js/received-bulk-actions.js @@ -49,8 +49,11 @@ document.addEventListener("DOMContentLoaded", () => { const selection = selected(); const message = modal.querySelector("#confirmation-modal-message"); - if (event.relatedTarget.dataset.receivedBulkAction === "delete") { + const action = event.relatedTarget.dataset.receivedBulkAction; + if (action === "delete") { message.textContent = `This permanently deletes ${selection.length} selected webmentions and cannot be undone.`; + } else if (action === "block-sources") { + message.textContent = `Source domains from ${selection.length} selected webmentions (and their subdomains) will be blocked. Existing webmentions will be kept.`; } else { message.textContent = `${selection.length} selected webmentions will be fetched and verified again.`; } diff --git a/webmentions_ssg/tasks/source_parser.py b/webmentions_ssg/tasks/source_parser.py index e063525..bd0700a 100644 --- a/webmentions_ssg/tasks/source_parser.py +++ b/webmentions_ssg/tasks/source_parser.py @@ -18,7 +18,8 @@ import xxhash from bs4 import BeautifulSoup, Tag from bs4.element import NavigableString, PageElement -from ..net import is_http_url, normalized_hostname +from ..hostname import normalize_hostname +from ..net import is_http_url REACTION_PROPERTIES = ("in-reply-to", "like-of", "repost-of", "bookmark-of") @@ -284,7 +285,10 @@ def iter_outgoing_targets( for value in chain(hrefs, reactions): target = normalize_target_url(value, base_url=base_url, source_url=source_url) if target is not None: - hostname = normalized_hostname(target) or "" + try: + hostname = normalize_hostname(target, from_url=True) + except ValueError: + continue if not any(fnmatchcase(hostname, pattern) for pattern in ignored_hostnames): yield target diff --git a/webmentions_ssg/tasks/verification.py b/webmentions_ssg/tasks/verification.py index b0f4d0f..d55608c 100644 --- a/webmentions_ssg/tasks/verification.py +++ b/webmentions_ssg/tasks/verification.py @@ -25,10 +25,10 @@ from ..net import ( media_type_of, read_limited_body, ) +from ..source_policy import source_rejection_reason from ..version import APP_NAME, VERSION from .target_matching import html_mentions_target, text_mentions_target - RECOVERY_EXHAUSTED_REASON = "Verification recovery limit reached" @@ -200,6 +200,10 @@ def determine_verification_outcome( retried. :raises httpx.RequestError: If a transient HTTP request error should be retried. """ + # Recheck at worker time: a source may have been blocked since submission. + if reason := source_rejection_reason(claim.source): + return ReceivedWebmentionStatus.FAILED, reason + try: mentions_target = source_mentions_target(claim.source, claim.target) except SourceGoneError as exc: diff --git a/webmentions_ssg/templates/base.html b/webmentions_ssg/templates/base.html index 69e5215..d9c8e87 100644 --- a/webmentions_ssg/templates/base.html +++ b/webmentions_ssg/templates/base.html @@ -45,6 +45,8 @@ SPDX-License-Identifier: BSD-3-Clause <div class="navbar-nav flex-row gap-3"> <a class="nav-link{% if request.endpoint == 'webmentions.received_list' %} active{% endif %}" href='{{ url_for("webmentions.received_list") }}'>Received</a> + <a class="nav-link{% if request.endpoint and request.endpoint.startswith('webmentions.source_domain') %} active{% endif %}" + href="{{ url_for('webmentions.source_domain_list') }}">Source rules</a> <a class="nav-link{% if request.endpoint == 'webmentions.source_list' %} active{% endif %}" href='{{ url_for("webmentions.source_list") }}'>Sent</a> <form action="{{ url_for('webmentions.logout') }}" diff --git a/webmentions_ssg/templates/received.html b/webmentions_ssg/templates/received.html index de3a9b6..5fd9866 100644 --- a/webmentions_ssg/templates/received.html +++ b/webmentions_ssg/templates/received.html @@ -127,6 +127,17 @@ SPDX-License-Identifier: BSD-3-Clause data-confirm-label="Reverify selected" data-confirm-class="btn-primary">Reverify selected</button> <button type="button" + class="btn btn-sm btn-outline-warning" + disabled + data-bs-toggle="modal" + data-bs-target="#confirmation-modal" + data-received-bulk-action="block-sources" + data-action="{{ url_for('webmentions.block_selected_received_sources', page=webmentions.page, **filters) }}" + data-title="Block selected source domains?" + data-message="Selected source domains and their subdomains will be blocked from future Webmentions. Existing Webmentions will be kept." + data-confirm-label="Block selected domains" + data-confirm-class="btn-warning">Block selected source domains</button> + <button type="button" class="btn btn-sm btn-outline-danger" disabled data-bs-toggle="modal" @@ -206,6 +217,15 @@ SPDX-License-Identifier: BSD-3-Clause data-confirm-label="Reverify" data-confirm-class="btn-primary">Reverify</button> <button type="button" + class="btn btn-sm btn-warning" + data-bs-toggle="modal" + data-bs-target="#confirmation-modal" + data-action="{{ url_for('webmentions.block_received_source', webmention_uuid=webmention.uuid, page=webmentions.page, **filters) }}" + data-title="Block this source domain?" + data-message="Future Webmentions from this source hostname and its subdomains will be rejected." + data-confirm-label="Block domain" + data-confirm-class="btn-warning">Block this domain</button> + <button type="button" class="btn btn-sm btn-danger" data-bs-toggle="modal" data-bs-target="#confirmation-modal" diff --git a/webmentions_ssg/templates/source_domains.html b/webmentions_ssg/templates/source_domains.html new file mode 100644 index 0000000..2db942f --- /dev/null +++ b/webmentions_ssg/templates/source_domains.html @@ -0,0 +1,68 @@ +<!-- +SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me> + +SPDX-License-Identifier: BSD-3-Clause +--> +{% extends "base.html" %} + +{% block content %} + <h1 class="mb-3">Source domain rules</h1> + <p> + Entries match the named hostname and all of its subdomains (not similarly named domains). + Blocked domains always take precedence over allowed domains. Changes affect new + submissions and future verification attempts; existing mentions are not deleted. + </p> + <p> + Allowlist mode is <strong>{{ "enabled" if allowlist_enabled else "disabled" }}</strong>. + Set <code>SOURCE_ALLOWLIST_ENABLED = True</code> in the server configuration to + accept only listed source domains. An empty enabled allowlist rejects all sources. + </p> + {% for kind, entries in (("blocked", blocked), ("allowed", allowed)) %} + <section class="mb-4" aria-labelledby="{{ kind }}-heading"> + <h2 id="{{ kind }}-heading">{{ "Blocked" if kind == "blocked" else "Allowed" }} domains</h2> + <form action="{{ url_for('webmentions.source_domain_add', kind=kind) }}" + method="post" + class="row g-2 align-items-end mb-3"> + <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"> + <div class="col-sm-8 col-md-5"> + <label class="form-label" for="{{ kind }}-domain">Source domain</label> + <input class="form-control" + type="text" + id="{{ kind }}-domain" + name="domain" + placeholder="example.org" + required> + </div> + <div class="col-auto"> + <button type="submit" class="btn btn-primary">Add domain</button> + </div> + </form> + {% if entries %} + <table class="table table-striped align-middle"> + <thead> + <tr> + <th scope="col">Domain</th> + <th scope="col">Action</th> + </tr> + </thead> + <tbody> + {% for entry in entries %} + <tr> + <td class="text-break">{{ entry.domain }}</td> + <td> + <form action="{{ url_for('webmentions.source_domain_delete', kind=kind, entry_id=entry.id) }}" + method="post"> + <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"> + <button type="submit" class="btn btn-sm btn-outline-danger">{{ "Unblock" if kind == "blocked" else "Remove" }}</button> + </form> + </td> + </tr> + {% endfor %} + </tbody> + </table> + {% else %} + <p class="text-body-secondary">No {{ kind }} domains.</p> + {% endif %} + </section> + {% endfor %} +{% endblock %} diff --git a/webmentions_ssg/views/admin.py b/webmentions_ssg/views/admin.py index 6754443..eb5a5e7 100644 --- a/webmentions_ssg/views/admin.py +++ b/webmentions_ssg/views/admin.py @@ -13,17 +13,21 @@ import sqlalchemy as sa from flask import abort, current_app, flash, redirect, render_template, request, url_for from flask.typing import ResponseReturnValue from flask_login import login_required +from sqlalchemy.exc import IntegrityError from sqlalchemy.orm import selectinload from .. import forms from ..db_helpers import rows_affected from ..extensions import db from ..models import ( + AllowedSource, + BlockedSource, ReceivedWebmention, ReceivedWebmentionStatus, SentWebmention, SourceDocument, ) +from ..source_policy import source_domain_from_url from ..tasks.scanner import manual_scan_sources from ..tasks.sender import send_webmention from ..tasks.verification import verify_webmention @@ -38,6 +42,7 @@ RECEIVED_STATUS_FILTERS = { ReceivedWebmentionStatus.SOURCE_REMOVED, ), } +SOURCE_DOMAIN_MODELS = {"blocked": BlockedSource, "allowed": AllowedSource} class ReceivedFilterArgs(TypedDict, total=False): @@ -344,6 +349,141 @@ def delete_all_declined_received_webmentions() -> ResponseReturnValue: ) +def block_source_domains(domains: set[str]) -> int: + """Persist distinct source domains, tolerating concurrent blocklist inserts. + + Savepoints isolate unique-constraint races so one preexisting entry cannot + prevent other selected domains from being blocked. + """ + existing = set( + db.session.scalars( + sa.select(BlockedSource.domain).where(BlockedSource.domain.in_(domains)) + ) + ) + added = 0 + for domain in sorted(domains - existing): + try: + with db.session.begin_nested(): + db.session.add(BlockedSource(domain=domain)) + db.session.flush() + except IntegrityError: + # A concurrent admin action has already blocked this hostname. + continue + added += 1 + db.session.commit() + return added + + +@webmentions_bp.post("/received/bulk/block-sources") +@login_required +def block_selected_received_sources() -> ResponseReturnValue: + """Block the unique source domains of selected received Webmentions.""" + if not forms.ConfirmActionForm().validate_on_submit(): + abort(HTTPStatus.BAD_REQUEST) + + filters = received_filter_args(received_filter_form()) + webmentions = selected_received_webmentions() + domains = {source_domain_from_url(webmention.source) for webmention in webmentions} + if None in domains: + # Fail the entire selection instead of silently skipping a source. + abort(HTTPStatus.BAD_REQUEST) + + added = block_source_domains(cast(set[str], domains)) + flash( + f"{added} source domains newly blocked from {len(webmentions)} selected Webmentions.", + "success", + ) + return redirect( + url_for( + "webmentions.received_list", + page=request.args.get("page", 1, type=int), + **filters, + ) + ) + + +@webmentions_bp.post("/received/<uuid:webmention_uuid>/block-source") +@login_required +def block_received_source(webmention_uuid: uuid.UUID) -> ResponseReturnValue: + """Add a received Webmention's source hostname to the blocklist.""" + if not forms.ConfirmActionForm().validate_on_submit(): + abort(HTTPStatus.BAD_REQUEST) + + filters = received_filter_args(received_filter_form()) + webmention = db.get_or_404(ReceivedWebmention, webmention_uuid) + if (domain := source_domain_from_url(webmention.source)) is None: + abort(HTTPStatus.BAD_REQUEST) + + block_source_domains({domain}) + + flash(f"Source domain {domain} blocked.", "success") + return redirect( + url_for( + "webmentions.received_list", + page=request.args.get("page", 1, type=int), + **filters, + ) + ) + + +@webmentions_bp.get("/source-domains") +@login_required +def source_domain_list() -> ResponseReturnValue: + """Manage blocklist and allowlist entries used by the receiver.""" + return render_template( + "source_domains.html", + title="Source domain rules", + blocked=db.session.scalars( + sa.select(BlockedSource).order_by(BlockedSource.domain) + ).all(), + allowed=db.session.scalars( + sa.select(AllowedSource).order_by(AllowedSource.domain) + ).all(), + allowlist_enabled=current_app.config["SOURCE_ALLOWLIST_ENABLED"], + ) + + +@webmentions_bp.post("/source-domains/<kind>/add") +@login_required +def source_domain_add(kind: str) -> ResponseReturnValue: + """Validate and add one normalized hostname to the selected list.""" + if (model := SOURCE_DOMAIN_MODELS.get(kind)) is None: + abort(HTTPStatus.NOT_FOUND) + + form = forms.SourceDomainForm() + if not form.validate_on_submit(): + abort(HTTPStatus.BAD_REQUEST) + + domain = cast(str, form.domain.data) + if db.session.scalar(sa.select(model.id).where(model.domain == domain)) is None: + db.session.add(model(domain=domain)) + try: + db.session.commit() + except IntegrityError: + db.session.rollback() + + flash(f"Source domain {domain} added to {kind} list.", "success") + return redirect(url_for("webmentions.source_domain_list")) + + +@webmentions_bp.post("/source-domains/<kind>/<int:entry_id>/delete") +@login_required +def source_domain_delete(kind: str, entry_id: int) -> ResponseReturnValue: + """Remove one entry; a blocked domain becomes eligible for new submissions.""" + if (model := SOURCE_DOMAIN_MODELS.get(kind)) is None: + abort(HTTPStatus.NOT_FOUND) + + if not forms.ConfirmActionForm().validate_on_submit(): + abort(HTTPStatus.BAD_REQUEST) + + entry = db.get_or_404(model, entry_id) + domain = entry.domain + db.session.delete(entry) + db.session.commit() + flash(f"Source domain {domain} removed from {kind} list.", "success") + return redirect(url_for("webmentions.source_domain_list")) + + @webmentions_bp.route("/sources") @login_required def source_list() -> ResponseReturnValue: diff --git a/webmentions_ssg/views/endpoint.py b/webmentions_ssg/views/endpoint.py index 934ffad..6d84d92 100644 --- a/webmentions_ssg/views/endpoint.py +++ b/webmentions_ssg/views/endpoint.py @@ -18,6 +18,7 @@ from .. import forms from ..db_helpers import rows_affected from ..extensions import csrf, db, limiter from ..models import ReceivedWebmention, source_target_digest +from ..source_policy import source_rejection_reason from ..tasks.verification import verify_webmention from .blueprint import webmentions_bp @@ -152,6 +153,9 @@ def receive_webmention() -> ResponseReturnValue: source = cast(str, form.source.data) target = cast(str, form.target.data) + if reason := source_rejection_reason(source): + return {"error": reason}, HTTPStatus.FORBIDDEN + result = record_submission(source, target) if result is None: |
