diff options
| author | Dennis Fink | 2026-10-30 08:35:46 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-10-30 08:41:42 +0100 |
| commit | 4485aeb573ceb37237ea1fbc80665ed6c4136116 (patch) | |
| tree | bce18a2c7fadd657d9f9809ebbc65d06c6cc4dc4 /tests | |
| parent | 9f093cf07a3531ee4b7d02c20151e21c141a93de (diff) | |
| download | webmentions-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 'tests')
| -rw-r--r-- | tests/test_config.py | 45 | ||||
| -rw-r--r-- | tests/test_configuration_hardening.py | 119 |
2 files changed, 164 insertions, 0 deletions
diff --git a/tests/test_config.py b/tests/test_config.py new file mode 100644 index 0000000..8d5aa5b --- /dev/null +++ b/tests/test_config.py @@ -0,0 +1,45 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me> +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Test built-in session cookie defaults and explicit overrides.""" + +import pytest + +from webmentions_ssg import create_app +from webmentions_ssg.config import ( + DefaultConfig, + DevelopmentConfig, + ProductionConfig, + TestingConfig, +) + + +@pytest.mark.parametrize( + ("config_class", "expected_secure"), + [ + (DefaultConfig, True), + (ProductionConfig, True), + (DevelopmentConfig, False), + (TestingConfig, False), + ], +) +def test_session_cookie_defaults( + config_class: type[DefaultConfig], expected_secure: bool +) -> None: + assert config_class.SESSION_COOKIE_SECURE is expected_secure + assert config_class.SESSION_COOKIE_HTTPONLY is True + assert config_class.SESSION_COOKIE_SAMESITE == "Lax" + + +def test_explicit_config_overrides_session_cookie_defaults() -> None: + class CookieOverrideConfig(TestingConfig): + SESSION_COOKIE_SECURE = True + SESSION_COOKIE_HTTPONLY = False + SESSION_COOKIE_SAMESITE = "Strict" + + app = create_app(CookieOverrideConfig) + + assert app.config["SESSION_COOKIE_SECURE"] is True + assert app.config["SESSION_COOKIE_HTTPONLY"] is False + assert app.config["SESSION_COOKIE_SAMESITE"] == "Strict" diff --git a/tests/test_configuration_hardening.py b/tests/test_configuration_hardening.py new file mode 100644 index 0000000..753f4ca --- /dev/null +++ b/tests/test_configuration_hardening.py @@ -0,0 +1,119 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me> +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Regression tests for configuration loading and endpoint hostname policy.""" + +from pathlib import Path + +import pytest +from wtforms import Field, ValidationError +from wtforms.form import BaseForm + +from webmentions_ssg import create_app +from webmentions_ssg.config import ( + DefaultConfig, + DevelopmentConfig, + ProductionConfig, + TestingConfig, +) +from webmentions_ssg.forms.validators import AllowedHostname + + +def test_missing_production_configuration_is_fatal(tmp_path: Path) -> None: + class MissingProductionConfig(ProductionConfig): + CONFIG_FILE = str(tmp_path / "missing.cfg") + + with pytest.raises(FileNotFoundError, match="missing.cfg"): + create_app(MissingProductionConfig) + + +def test_missing_development_configuration_is_optional(tmp_path: Path) -> None: + class LocalDevelopmentConfig(DevelopmentConfig): + CONFIG_FILE = str(tmp_path / "missing.cfg") + LOG_FILE = str(tmp_path / "development.log") + + app = create_app(LocalDevelopmentConfig) + assert app.config["ALLOWED_HOSTNAMES"] == set() + + +def test_endpoint_limits_are_inherited_from_defaults() -> None: + for config in (DefaultConfig, DevelopmentConfig, ProductionConfig, TestingConfig): + assert config.MAX_REDIRECTS == 20 + assert config.MAX_SOURCE_BYTES == 1_000_000 + assert config.REQUEST_TIMEOUT == 5.0 + + assert DefaultConfig.ALLOWED_HOSTNAMES == set() + + +def test_allowed_hostnames_are_normalized_at_startup(tmp_path: Path) -> None: + class MixedCaseConfig(TestingConfig): + LOG_FILE = str(tmp_path / "test.log") + ALLOWED_HOSTNAMES = [" DennisFink.Me ", "EXAMPLE.COM", "dennisfink.me"] + + app = create_app(MixedCaseConfig) + assert app.config["ALLOWED_HOSTNAMES"] == {"dennisfink.me", "example.com"} + assert isinstance(app.config["ALLOWED_HOSTNAMES"], set) + + # The form validator must compare complete hostnames, never substrings. + target = Field() + validator = AllowedHostname(message="Hostname not allowed") + form = BaseForm({}) + with app.app_context(): + for url in ("https://dennisfink.me/post", "https://EXAMPLE.com/post"): + target.data = url + validator(form, target) + + for url in ("https://fink/post", "https://evil-dennisfink.me/post"): + target.data = url + with pytest.raises(ValidationError, match="Hostname not allowed"): + validator(form, target) + + +@pytest.mark.parametrize( + "hostnames,expected_exception", + [ + ("dennisfink.me", TypeError), + (None, TypeError), + ({"dennisfink.me": True}, TypeError), + (123, TypeError), + (["example.com", None], ValueError), + (["example.com", ""], ValueError), + (["example.com", " "], ValueError), + ], +) +def test_invalid_allowed_hostnames_fail_at_startup( + tmp_path: Path, hostnames: object, expected_exception: type[Exception] +) -> None: + class InvalidHostnameConfig(TestingConfig): + LOG_FILE = str(tmp_path / "test.log") + ALLOWED_HOSTNAMES = hostnames + + with pytest.raises(expected_exception, match="ALLOWED_HOSTNAMES"): + create_app(InvalidHostnameConfig) + + +def test_production_requires_nonempty_allowed_hostnames(tmp_path: Path) -> None: + config_path = tmp_path / "production.cfg" + config_path.write_text('RATELIMIT_STORAGE_URI = "redis://localhost:6379"\n') + + class EmptyHostnamesConfig(ProductionConfig): + CONFIG_FILE = str(config_path) + + with pytest.raises(RuntimeError, match="ALLOWED_HOSTNAMES"): + create_app(EmptyHostnamesConfig) + + +def test_environment_allowed_hostnames_override_config_file( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + config_path = tmp_path / "development.cfg" + config_path.write_text('ALLOWED_HOSTNAMES = ["first.example"]\n') + monkeypatch.setenv("WEBMENTIONS_SSG_ALLOWED_HOSTNAMES", '["SECOND.EXAMPLE"]') + + class LocalDevelopmentConfig(DevelopmentConfig): + CONFIG_FILE = str(config_path) + LOG_FILE = str(tmp_path / "development.log") + + app = create_app(LocalDevelopmentConfig) + assert app.config["ALLOWED_HOSTNAMES"] == {"second.example"} |
