From 6b60c62a60655391a5c60e414dcbdc6cb1f9143e Mon Sep 17 00:00:00 2001 From: Shahadat Islam Koyel <179203101+ShahadatTest@users.noreply.github.com> Date: Fri, 6 Nov 2026 14:02:41 +0600 Subject: [PATCH 1/2] fix: validate OFFLINE_PUBLISH_ALLOWED_PROTOCOLS entries (#220) --- publisher/config.py | 14 +++++++++++++- tests/test_config.py | 27 +++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/publisher/config.py b/publisher/config.py index ea20c52..1107fba 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,18 @@ class OfflinePublishConfig(Section): @classmethod def load(cls, read: _Reader) -> Self: - allowed = read.get_list("OFFLINE_PUBLISH_ALLOWED_PROTOCOLS") + allowed: list[str] = [] + for protocol in read.get_list("OFFLINE_PUBLISH_ALLOWED_PROTOCOLS"): + choice = protocol.lower() + if choice not in OFFLINE_PUBLISH_PROTOCOLS: + read.fail( + "OFFLINE_PUBLISH_ALLOWED_PROTOCOLS", + f"expected one of {', '.join(OFFLINE_PUBLISH_PROTOCOLS)}, " + f"got {protocol!r}", + ) + continue + if choice not in allowed: + allowed.append(choice) 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..2356812 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -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"]), + ("", []), + ], +) +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): + assert "OFFLINE_PUBLISH_ALLOWED_PROTOCOLS" in errors( + OFFLINE_PUBLISH_ALLOWED_PROTOCOLS=protocols, + OFFLINE_PUBLISH_SHARED_SECRET=KEY, + ) + + 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") From b1d527ce077f5737fd25312c614e78d09010bf66 Mon Sep 17 00:00:00 2001 From: Shahadat Islam Koyel <179203101+ShahadatTest@users.noreply.github.com> Date: Sun, 8 Nov 2026 02:10:40 +0600 Subject: [PATCH 2/2] fix(config): retain invalid protocols while reporting validation errors --- docs/configuration/offline-publishing.md | 1 + publisher/config.py | 11 ++++------- tests/test_config.py | 10 +++++----- 3 files changed, 10 insertions(+), 12 deletions(-) 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 1107fba..deb305e 100644 --- a/publisher/config.py +++ b/publisher/config.py @@ -346,18 +346,15 @@ class OfflinePublishConfig(Section): @classmethod def load(cls, read: _Reader) -> Self: - allowed: list[str] = [] - for protocol in read.get_list("OFFLINE_PUBLISH_ALLOWED_PROTOCOLS"): - choice = protocol.lower() - if choice not in OFFLINE_PUBLISH_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}", ) - continue - if choice not in allowed: - allowed.append(choice) 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 2356812..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} @@ -213,6 +213,7 @@ def test_offline_https_requires_shared_secret(protocols): ("SMTP", ["smtp"]), ("https,smtp", ["https", "smtp"]), ("smtp,smtp", ["smtp"]), + ("smtp,SMTP", ["smtp"]), ("", []), ], ) @@ -227,10 +228,9 @@ def test_offline_allowed_protocols_are_normalized(protocols, expected): @pytest.mark.parametrize("protocols", ["smpt", "SMTP,ftp", "http"]) def test_offline_allowed_protocols_reject_unknown_entries(protocols): - assert "OFFLINE_PUBLISH_ALLOWED_PROTOCOLS" in errors( - OFFLINE_PUBLISH_ALLOWED_PROTOCOLS=protocols, - OFFLINE_PUBLISH_SHARED_SECRET=KEY, - ) + 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():