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/config.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 '')
| -rw-r--r-- | webmentions_ssg/config.py | 46 |
1 files changed, 22 insertions, 24 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.") |
