diff --git a/docs/configuration/offline-publishing.md b/docs/configuration/offline-publishing.md index 277f23e..49cf80c 100644 --- a/docs/configuration/offline-publishing.md +++ b/docs/configuration/offline-publishing.md @@ -9,6 +9,7 @@ Each payload is tagged with the protocol it arrived on: ## Rules +- Listing anything other than the protocols above fails `manage.sh check`, and the services refuse to start. - An offline payload from a protocol not in `OFFLINE_PUBLISH_ALLOWED_PROTOCOLS` is discarded. An empty list allows every protocol. - An offline payload over `https` must carry `OFFLINE_PUBLISH_SHARED_SECRET` in the request's `tag` field, or it's discarded. - Leaving `OFFLINE_PUBLISH_ALLOWED_PROTOCOLS` empty, or listing `https`, without the secret fails `manage.sh check`, and the services refuse to start. diff --git a/publisher/config.py b/publisher/config.py index ea20c52..deb305e 100644 --- a/publisher/config.py +++ b/publisher/config.py @@ -21,6 +21,7 @@ ROOT = Path(__file__).resolve().parents[1] DATABASE_DIALECTS = ("sqlite", "mysql", "postgres") CELERY_BROKERS = ("sqlite", "redis", "rabbitmq") +OFFLINE_PUBLISH_PROTOCOLS = ("https", "smtp") GITHUB_ORG_PATTERN = re.compile(r"^[a-z0-9](?:[a-z0-9-]{0,37}[a-z0-9])?$") # Fill unset variables from .env. Values already in the environment win. @@ -345,7 +346,15 @@ class OfflinePublishConfig(Section): @classmethod def load(cls, read: _Reader) -> Self: - allowed = read.get_list("OFFLINE_PUBLISH_ALLOWED_PROTOCOLS") + names = read.get_list("OFFLINE_PUBLISH_ALLOWED_PROTOCOLS") + allowed = list(dict.fromkeys(name.lower() for name in names)) + for protocol in allowed: + if protocol not in OFFLINE_PUBLISH_PROTOCOLS: + read.fail( + "OFFLINE_PUBLISH_ALLOWED_PROTOCOLS", + f"expected one of {', '.join(OFFLINE_PUBLISH_PROTOCOLS)}, " + f"got {protocol!r}", + ) https_allowed = not allowed or "https" in allowed return cls( allowed_protocols=allowed, diff --git a/tests/test_config.py b/tests/test_config.py index 2564798..6400f2b 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -198,7 +198,7 @@ def test_offline_shared_secret_must_be_a_key(): assert offline.shared_secret == KEY -@pytest.mark.parametrize("protocols", ["https", "smtp,https", ""]) +@pytest.mark.parametrize("protocols", ["https", "HTTPS", "smtp,https", ""]) def test_offline_https_requires_shared_secret(protocols): allowed = {"OFFLINE_PUBLISH_ALLOWED_PROTOCOLS": protocols} @@ -206,6 +206,33 @@ def test_offline_https_requires_shared_secret(protocols): assert not errors(**allowed, OFFLINE_PUBLISH_SHARED_SECRET=KEY) +@pytest.mark.parametrize( + ("protocols", "expected"), + [ + ("smtp", ["smtp"]), + ("SMTP", ["smtp"]), + ("https,smtp", ["https", "smtp"]), + ("smtp,smtp", ["smtp"]), + ("smtp,SMTP", ["smtp"]), + ("", []), + ], +) +def test_offline_allowed_protocols_are_normalized(protocols, expected): + offline = load( + OfflinePublishConfig, + OFFLINE_PUBLISH_ALLOWED_PROTOCOLS=protocols, + OFFLINE_PUBLISH_SHARED_SECRET=KEY, + ) + assert offline.allowed_protocols == expected + + +@pytest.mark.parametrize("protocols", ["smpt", "SMTP,ftp", "http"]) +def test_offline_allowed_protocols_reject_unknown_entries(protocols): + problems = errors(OFFLINE_PUBLISH_ALLOWED_PROTOCOLS=protocols) + assert "OFFLINE_PUBLISH_ALLOWED_PROTOCOLS" in problems + assert "OFFLINE_PUBLISH_SHARED_SECRET" not in problems + + def test_template_env_paths_match_defaults(): # scripts/lib.sh falls back to template.env for paths unset in .env. template = dotenv_values(config.ROOT / "template.env")