aboutsummaryrefslogtreecommitdiff
path: root/tests/test_config.py
diff options
context:
space:
mode:
authorDennis Fink2026-10-31 16:42:44 +0100
committerDennis Fink2026-10-31 16:42:44 +0100
commit3494eab1208dc18478485e02c61e292556502fa6 (patch)
tree5d9e95d709b119a0245d0294837f9b6a7e38bff1 /tests/test_config.py
parentfdb1762715658eae987dd104a0e6d3cb92a4a8d7 (diff)
downloadwebmentions-ssg-3494eab1208dc18478485e02c61e292556502fa6.tar.gz
webmentions-ssg-3494eab1208dc18478485e02c61e292556502fa6.zip
fix(core): harden session and credential security
Require persistent non-placeholder signing keys outside local development and bind login sessions to the current password hash so password changes revoke existing cookies. Enforce a minimal password policy for user administration while preserving long Unicode passphrases without arbitrary composition limits.
Diffstat (limited to 'tests/test_config.py')
-rw-r--r--tests/test_config.py88
1 files changed, 86 insertions, 2 deletions
diff --git a/tests/test_config.py b/tests/test_config.py
index 1b45946..dd02c01 100644
--- a/tests/test_config.py
+++ b/tests/test_config.py
@@ -10,7 +10,7 @@ import pytest
from wtforms import Field, ValidationError
from wtforms.form import BaseForm
-from webmentions_ssg import create_app
+from webmentions_ssg import LIMITER, create_app
from webmentions_ssg.config import (
DefaultConfig,
DevelopmentConfig,
@@ -126,7 +126,10 @@ def test_invalid_allowed_hostnames_fail_at_startup(
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')
+ config_path.write_text(
+ 'RATELIMIT_STORAGE_URI = "redis://localhost:6379"\n'
+ 'SECRET_KEY = "the-production-secret-key-for-this-test"\n'
+ )
class EmptyHostnamesConfig(ProductionConfig):
CONFIG_FILE = str(config_path)
@@ -148,3 +151,84 @@ def test_environment_allowed_hostnames_override_config_file(
app = create_app(LocalDevelopmentConfig)
assert app.config["ALLOWED_HOSTNAMES"] == {"second.example"}
+
+
+@pytest.mark.parametrize(
+ ("secret", "expected_exception"),
+ [
+ (None, TypeError),
+ ("", RuntimeError),
+ ("short", RuntimeError),
+ ("REPLACE_WITH_A_LONG_RANDOM_SECRET", RuntimeError),
+ ("INSECURE_DEVELOPMENT_ONLY_SECRET_KEY", RuntimeError),
+ ("change_me_to_a_new_strong_secret_key", RuntimeError),
+ (123, TypeError),
+ ],
+)
+def test_production_refuses_missing_weak_or_example_secret_key(
+ tmp_path: Path, secret: object, expected_exception: type[Exception]
+) -> None:
+ config_file = tmp_path / "production.cfg"
+ config_file.write_text("# This file intentionally has no signing key.\n")
+
+ class SecretKeyConfig(ProductionConfig):
+ CONFIG_FILE = str(config_file)
+ LOG_FILE = str(tmp_path / "production.log")
+ ALLOWED_HOSTNAMES = {"example.com"}
+ RATELIMIT_STORAGE_URI = "redis://localhost:6379"
+ SECRET_KEY = secret
+
+ with pytest.raises(expected_exception, match="SECRET_KEY"):
+ create_app(SecretKeyConfig)
+
+
+def test_production_uses_persistent_configured_secret(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ # The test concerns secret-key persistence, not Redis initialization.
+ monkeypatch.setattr(LIMITER, "init_app", lambda app: None)
+ config_file = tmp_path / "production.cfg"
+ key = "a-fixed-key-with-32-plus-random-characters"
+ config_file.write_text(f"SECRET_KEY = {key!r}\n")
+
+ class ConfiguredProduction(ProductionConfig):
+ CONFIG_FILE = str(config_file)
+ LOG_FILE = str(tmp_path / "production.log")
+ ALLOWED_HOSTNAMES = {"example.com"}
+ RATELIMIT_STORAGE_URI = "redis://localhost:6379"
+
+ first = create_app(ConfiguredProduction)
+ second = create_app(ConfiguredProduction)
+ assert first.secret_key == key == second.secret_key
+
+
+def test_production_accepts_environment_secret_override(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ config_file = tmp_path / "production.cfg"
+ config_file.write_text('SECRET_KEY = "REPLACE_WITH_A_LONG_RANDOM_SECRET"\n')
+ key = "another-long-persistent-secret-for-a-deployment"
+ monkeypatch.setenv("WEBMENTIONS_SSG_SECRET_KEY", key)
+ # Avoid requiring the optional Redis dependency for this configuration test.
+ monkeypatch.setattr(LIMITER, "init_app", lambda app: None)
+
+ class EnvConfiguredProduction(ProductionConfig):
+ CONFIG_FILE = str(config_file)
+ LOG_FILE = str(tmp_path / "production.log")
+ ALLOWED_HOSTNAMES = {"example.com"}
+ RATELIMIT_STORAGE_URI = "redis://localhost:6379"
+
+ app = create_app(EnvConfiguredProduction)
+ assert app.secret_key == key
+
+
+def test_development_rejects_placeholder_even_with_debug(tmp_path: Path) -> None:
+ config_file = tmp_path / "development.cfg"
+ config_file.write_text('SECRET_KEY = "REPLACE_WITH_A_LONG_RANDOM_SECRET"\n')
+
+ class LocalDevelopment(DevelopmentConfig):
+ CONFIG_FILE = str(config_file)
+ LOG_FILE = str(tmp_path / "development.log")
+
+ with pytest.raises(RuntimeError, match="SECRET_KEY"):
+ create_app(LocalDevelopment)