aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg/config.py
diff options
context:
space:
mode:
authorDennis Fink2026-10-30 08:35:46 +0100
committerDennis Fink2026-10-30 08:41:42 +0100
commit4485aeb573ceb37237ea1fbc80665ed6c4136116 (patch)
treebce18a2c7fadd657d9f9809ebbc65d06c6cc4dc4 /webmentions_ssg/config.py
parent9f093cf07a3531ee4b7d02c20151e21c141a93de (diff)
downloadwebmentions-ssg-4485aeb573ceb37237ea1fbc80665ed6c4136116.tar.gz
webmentions-ssg-4485aeb573ceb37237ea1fbc80665ed6c4136116.zip
fix(core): harden application configuration
Fail production startup when required configuration is missing, normalize allowed hostnames before validation, and move shared request limits into the default configuration so production does not depend on testing-only values. Set secure session cookie defaults while preserving explicit overrides and document the new rate-limit and notification settings in the example config.
Diffstat (limited to '')
-rw-r--r--webmentions_ssg/config.py31
1 files changed, 27 insertions, 4 deletions
diff --git a/webmentions_ssg/config.py b/webmentions_ssg/config.py
index 2661704..8cad51f 100644
--- a/webmentions_ssg/config.py
+++ b/webmentions_ssg/config.py
@@ -8,12 +8,31 @@ import logging
import secrets
+def normalize_allowed_hostnames(value: object) -> set[str]:
+ """Validate and normalize the configured collection of allowed hostnames."""
+ if not isinstance(value, (list, tuple, set, frozenset)):
+ raise TypeError(
+ "ALLOWED_HOSTNAMES must be a list, tuple, or set of hostnames, not a string."
+ )
+
+ hostnames: set[str] = set()
+ for hostname in value:
+ if not isinstance(hostname, str) or not hostname.strip():
+ raise ValueError("ALLOWED_HOSTNAMES must contain non-empty strings only.")
+ hostnames.add(hostname.strip().lower())
+ return hostnames
+
+
class DefaultConfig:
TESTING = False
DEBUG = False
SECRET_KEY = secrets.token_urlsafe()
BOOTSTRAP_SERVE_LOCAL = True
# BOOTSTRAP_BOOTSWATCH_THEME = "Materia"
+ # Secure by default; deployment configuration can override these values.
+ SESSION_COOKIE_SECURE = True
+ SESSION_COOKIE_HTTPONLY = True
+ SESSION_COOKIE_SAMESITE = "Lax"
LOG_FILE = "/var/log/webmentions-ssg.log"
LOG_FORMAT = "[%(asctime)s]:%(levelname)s:%(name)s %(message)s"
LOG_LEVEL = logging.INFO
@@ -21,6 +40,12 @@ class DefaultConfig:
LOGFILE_BACKUP_COUNT = 10
SQLALCHEMY_DATABASE_URI = "sqlite:///webmentions-ssg.db"
+ # Empty by default: no target hostnames are accepted until configured.
+ ALLOWED_HOSTNAMES = set()
+ MAX_REDIRECTS = 20
+ MAX_SOURCE_BYTES = 1_000_000
+ REQUEST_TIMEOUT = 5.0
+
SOURCE_DIRECTORY = None
SOURCE_BASE_URL = None
SCANNER_SCHEDULE = "0 * * * *"
@@ -48,6 +73,7 @@ class DefaultConfig:
class DevelopmentConfig(DefaultConfig):
DEBUG = True
+ SESSION_COOKIE_SECURE = False
LOG_FILE = "testing/webmentions-ssg.log"
LOG_LEVEL = logging.DEBUG
CONFIG_FILE = "testing/webmentions-ssg.cfg"
@@ -59,6 +85,7 @@ class ProductionConfig(DefaultConfig):
class TestingConfig(DefaultConfig):
TESTING = True
+ SESSION_COOKIE_SECURE = False
SECRET_KEY = "testing-secret-key"
WTF_CSRF_ENABLED = False
@@ -72,7 +99,3 @@ class TestingConfig(DefaultConfig):
HUEY_URL = "memory://"
ALLOWED_HOSTNAMES = {"dennisfink.me"}
-
- MAX_REDIRECTS = 20
- MAX_SOURCE_BYTES = 1_000_000
- REQUEST_TIMEOUT = 5.0