diff options
Diffstat (limited to 'webmentions_ssg')
| -rw-r--r-- | webmentions_ssg/__init__.py | 16 | ||||
| -rw-r--r-- | webmentions_ssg/config.py | 31 | ||||
| -rw-r--r-- | webmentions_ssg/forms/validators.py | 3 |
3 files changed, 43 insertions, 7 deletions
diff --git a/webmentions_ssg/__init__.py b/webmentions_ssg/__init__.py index c4ad4f6..518add0 100644 --- a/webmentions_ssg/__init__.py +++ b/webmentions_ssg/__init__.py @@ -20,7 +20,12 @@ from flask_sqlalchemy import SQLAlchemy from flask_wtf.csrf import CSRFProtect from sqlalchemy.orm import DeclarativeBase -from webmentions_ssg.config import DefaultConfig, DevelopmentConfig, ProductionConfig +from webmentions_ssg.config import ( + DefaultConfig, + DevelopmentConfig, + ProductionConfig, + normalize_allowed_hostnames, +) from .tasks import Huey from .url_security import is_http_url @@ -72,10 +77,17 @@ def create_app(config_class: type[DefaultConfig] | None = None) -> Flask: app.config.from_object(config_class) if not app.testing: - app.config.from_pyfile(app.config["CONFIG_FILE"], silent=True) + # Local development may omit its config file; production must not. + app.config.from_pyfile(app.config["CONFIG_FILE"], silent=app.debug) app.config.from_prefixed_env(prefix="FLASK") app.config.from_prefixed_env(prefix="WEBMENTIONS_SSG") + app.config["ALLOWED_HOSTNAMES"] = normalize_allowed_hostnames( + app.config["ALLOWED_HOSTNAMES"] + ) + if not app.testing and not app.debug and not app.config["ALLOWED_HOSTNAMES"]: + raise RuntimeError("Configure ALLOWED_HOSTNAMES before running in production.") + if ( not app.testing and not app.debug 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 diff --git a/webmentions_ssg/forms/validators.py b/webmentions_ssg/forms/validators.py index 67ab9c0..dd0ee9b 100644 --- a/webmentions_ssg/forms/validators.py +++ b/webmentions_ssg/forms/validators.py @@ -85,7 +85,8 @@ class AllowedHostname: :param field: Field containing the URL to validate. :raises ValidationError: If the URL hostname is not configured as allowed. """ - if urlsplit(field.data).hostname in current_app.config["ALLOWED_HOSTNAMES"]: + hostname = urlsplit(field.data).hostname + if hostname is not None and hostname in current_app.config["ALLOWED_HOSTNAMES"]: return raise ValidationError(self.message or field.gettext("Invalid input.")) |
