diff options
| author | Dennis Fink | 2026-10-31 20:43:33 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-10-31 20:43:33 +0100 |
| commit | 7035e9f9467684cccae2b7ff3faaa62e6a9f91aa (patch) | |
| tree | 8d14e16fa0b669353052ee51eeeb1da60fa16d78 | |
| parent | 3a3284abe23e143cbb638877f47958c82afa3367 (diff) | |
| download | webmentions-ssg-7035e9f9467684cccae2b7ff3faaa62e6a9f91aa.tar.gz webmentions-ssg-7035e9f9467684cccae2b7ff3faaa62e6a9f91aa.zip | |
fix(core): harden local setup and Huey configuration
Create local development and test directories automatically so fresh
checkouts can start without pre-existing logging or instance paths.
Reject in-memory and blackhole Huey backends in production because they
cannot reliably deliver queued work across separate web and worker
processes.
| -rw-r--r-- | .gitignore | 3 | ||||
| -rw-r--r-- | contrib/webmentions-ssg.example.cfg | 2 | ||||
| -rw-r--r-- | tests/test_config.py | 79 | ||||
| -rw-r--r-- | webmentions_ssg/__init__.py | 30 |
4 files changed, 113 insertions, 1 deletions
@@ -2,6 +2,9 @@ # # SPDX-License-Identifier: BSD-3-Clause +# Local development/test configuration, SQLite databases, and logs. +testing/ + # Created by https://www.toptal.com/developers/gitignore/api/git,python,windows,linux,macos,vim # Edit at https://www.toptal.com/developers/gitignore?templates=git,python,windows,linux,macos,vim diff --git a/contrib/webmentions-ssg.example.cfg b/contrib/webmentions-ssg.example.cfg index a5dbaf9..4beb678 100644 --- a/contrib/webmentions-ssg.example.cfg +++ b/contrib/webmentions-ssg.example.cfg @@ -162,6 +162,8 @@ MAX_TARGET_BYTES = 1_000_000 # scheduled-task and result data before restarting *both* with JSON. # Old pickle payloads cannot be read by the new serializer. Do not deserialize # legacy data from any backend that untrusted parties could have modified. +# Production startup refuses the implicit memory:// fallback, as well as +# blackhole://, since neither can reliably deliver work to a separate process. HUEY_URL = "redis://127.0.0.1:6379/0" # --------------------------------------------------------------------------- diff --git a/tests/test_config.py b/tests/test_config.py index c483ee7..cc58f73 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -70,6 +70,32 @@ def test_missing_development_configuration_is_optional(tmp_path: Path) -> None: assert app.config["ALLOWED_HOSTNAMES"] == set() +def test_development_creates_local_directory_on_first_start( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + monkeypatch.chdir(tmp_path) + assert not (tmp_path / "testing").exists() + + class FreshDevelopmentConfig(DevelopmentConfig): + pass + + app = create_app(FreshDevelopmentConfig) + + assert Path(app.instance_path) == tmp_path / "testing" + assert (tmp_path / "testing" / "webmentions-ssg.log").is_file() + + +def test_testing_creates_local_log_directory_on_first_start( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + monkeypatch.chdir(tmp_path) + assert not (tmp_path / "testing").exists() + + create_app(TestingConfig) + + assert (tmp_path / "testing" / "webmentions-ssg-tests.log").is_file() + + def test_endpoint_limits_are_inherited_from_defaults() -> None: for config in (DefaultConfig, DevelopmentConfig, ProductionConfig, TestingConfig): assert config.MAX_REDIRECTS == 20 @@ -175,6 +201,57 @@ def test_production_requires_nonempty_allowed_hostnames(tmp_path: Path) -> None: create_app(EmptyHostnamesConfig) +@pytest.mark.parametrize( + "queue_settings", + [ + {}, # missing URL silently selected Huey's memory:// fallback + {"HUEY_URL": "memory://"}, + {"HUEY_URL": "MEMORY:///"}, + {"HUEY_URL": "blackhole://"}, + {"HUEY": {"url": "memory://"}}, + {"HUEY": {"url": "blackhole://"}}, + ], +) +def test_production_rejects_ephemeral_or_discarding_huey_queue( + tmp_path: Path, queue_settings: dict[str, object] +) -> None: + config_path = tmp_path / "production.cfg" + config_path.write_text("# Valid options are provided by the config subclass.\n") + + class ProductionWithQueue(ProductionConfig): + CONFIG_FILE = str(config_path) + SECRET_KEY = "a-long-secret-string-for-a-production-test" + ALLOWED_HOSTNAMES = {"example.com"} + RATELIMIT_STORAGE_URI = "redis://localhost:6379/1" + LOG_FILE = str(tmp_path / "production.log") + + for key, value in queue_settings.items(): + setattr(ProductionWithQueue, key, value) + + with pytest.raises(RuntimeError, match="HUEY_URL"): + create_app(ProductionWithQueue) + + +def test_production_prefers_explicit_huey_url_over_legacy_option( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + monkeypatch.setattr(LIMITER, "init_app", lambda app: None) + config_path = tmp_path / "production.cfg" + config_path.write_text("# Configuration inherited from subclass.\n") + + class ProductionWithExplicitQueue(ProductionConfig): + CONFIG_FILE = str(config_path) + SECRET_KEY = "a-long-secret-string-for-a-production-test" + ALLOWED_HOSTNAMES = {"example.com"} + RATELIMIT_STORAGE_URI = "redis://localhost:6379/1" + LOG_FILE = str(tmp_path / "production.log") + HUEY = {"url": "memory://"} + HUEY_URL = f"sqlite:///{tmp_path / 'huey.db'}" + + app = create_app(ProductionWithExplicitQueue) + assert app.extensions["huey"].huey.__class__.__name__ == "SqliteHuey" + + def test_environment_allowed_hostnames_override_config_file( tmp_path: Path, monkeypatch: pytest.MonkeyPatch ) -> None: @@ -233,6 +310,7 @@ def test_production_uses_persistent_configured_secret( LOG_FILE = str(tmp_path / "production.log") ALLOWED_HOSTNAMES = {"example.com"} RATELIMIT_STORAGE_URI = "redis://localhost:6379" + HUEY_URL = f"sqlite:///{tmp_path / 'huey.db'}" first = create_app(ConfiguredProduction) second = create_app(ConfiguredProduction) @@ -254,6 +332,7 @@ def test_production_accepts_environment_secret_override( LOG_FILE = str(tmp_path / "production.log") ALLOWED_HOSTNAMES = {"example.com"} RATELIMIT_STORAGE_URI = "redis://localhost:6379" + HUEY_URL = f"sqlite:///{tmp_path / 'huey.db'}" app = create_app(EnvConfiguredProduction) assert app.secret_key == key diff --git a/webmentions_ssg/__init__.py b/webmentions_ssg/__init__.py index f8fd8f1..8fed1a7 100644 --- a/webmentions_ssg/__init__.py +++ b/webmentions_ssg/__init__.py @@ -8,6 +8,7 @@ import logging import logging.handlers import os from pathlib import Path +from urllib.parse import urlsplit import coloredlogs from flask import Flask @@ -75,7 +76,12 @@ def create_app(config_class: type[DefaultConfig] | None = None) -> Flask: config_class = DevelopmentConfig if development else ProductionConfig app = Flask( - __name__, instance_path=str(Path("testing").absolute()) if development else None + __name__, + instance_path=( + str(Path("testing").absolute()) + if issubclass(config_class, DevelopmentConfig) + else None + ), ) app.config.from_object(config_class) @@ -129,6 +135,28 @@ def create_app(config_class: type[DefaultConfig] | None = None) -> Flask: "Configure RATELIMIT_STORAGE_URI with shared storage before running in production." ) + if not app.testing and not app.debug: + # HUEY_URL overrides the legacy HUEY.url option; absent either, Huey + # falls back to an in-process queue that cannot serve a separate worker. + huey_url = app.config.get( + "HUEY_URL", app.config.get("HUEY", {}).get("url", "memory://") + ) + if isinstance(huey_url, str) and urlsplit(huey_url).scheme.lower() in { + "memory", + "blackhole", + }: + raise RuntimeError( + "Configure HUEY_URL with a shared, persistent queue before " + "running in production (memory:// and blackhole:// are unsafe)." + ) + + # Local development and tests should start from a fresh checkout: their + # default log directory and development SQLite instance are generated. + if app.testing or app.debug: + Path(app.config["LOG_FILE"]).parent.mkdir(parents=True, exist_ok=True) + if app.debug: + Path(app.instance_path).mkdir(parents=True, exist_ok=True) + default_logging_formatter = logging.Formatter(app.config["LOG_FORMAT"]) log_file_handler = logging.handlers.RotatingFileHandler( |
