aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg/config.py
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--webmentions_ssg/config.py46
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.")