Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/configuration/offline-publishing.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 10 additions & 1 deletion publisher/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down
29 changes: 28 additions & 1 deletion tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,41 @@ 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}

assert "OFFLINE_PUBLISH_SHARED_SECRET" in errors(**allowed)
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"]),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Entries that differ only in case should dedupe too:

Suggested change
("smtp,smtp", ["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")
Expand Down
Loading