Files
romm/backend/handler/notification_channels/config.py
zurdi 28fc82c63b fix: address the code review and security audit of notification channels
- Bound a webhook delivery: 15 s in all, and only the first KiB of a
  refusal's body is read, so a hostile endpoint can't hold the API or a
  worker with an endless or slow reply.
- Key the confirmation-code cooldown per user and per address instead of
  per channel, which a delete and re-create reset, so a user can't mail
  an address without limit.
- Don't carry a stored secret to another origin or format: it has to be
  given again (Discord, which uses none, drops it), so a changed URL
  can't be used to read an ntfy token or HMAC key.
- Refuse a webhook URL whose port isn't a number; one got saved and then
  broke the channel list with a 500.
- Refuse an unknown SMTP_SECURITY instead of logging in over plaintext.
- Tell the reset form whether reset links are really emailed, which also
  needs a shareable ROMM_BASE_URL (heartbeat NOTIFICATIONS.EMAILS_RESET_LINKS).
- Say a code went out only when the address is new to the channel.
- Match a channel's address whole, so a trailing newline isn't kept.
- Test that forgot-password answers alike and sends the link afterwards,
  and document the new cache keys.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-23 18:07:00 +02:00

43 lines
1.2 KiB
Python

"""What a channel keeps sealed: where it sends, and how."""
from typing import Any, NotRequired, TypedDict
from urllib.parse import urlsplit
from utils.secret_box import UnsealError, seal, unseal
class WebhookConfig(TypedDict):
url: str
format: str
# The HMAC key of a JSON webhook, or the access token of an ntfy topic.
secret: NotRequired[str | None]
class EmailConfig(TypedDict):
address: str
def seal_config(config: WebhookConfig | EmailConfig) -> str:
return seal(dict(config))
def read_config(sealed: str) -> dict[str, Any]:
"""A channel's config; empty when the auth secret changed since it was saved."""
try:
return unseal(sealed)
except UnsealError:
return {}
def origin(url: str) -> str:
"""The URL's scheme, host and port: whom a request to it reaches."""
parts = urlsplit(url)
port = f":{parts.port}" if parts.port else ""
return f"{parts.scheme}://{parts.hostname or ''}{port}"
def masked_url(url: str) -> str:
"""The URL with its path hidden, which is where a webhook keeps its token."""
tail = urlsplit(url).path.rstrip("/")[-4:]
return f"{origin(url)}/…{tail}" if tail else origin(url)