M4: Calendar plugin (builtin) + mail HTML/attachment fixes

Calendar plugin (webmail_plugin_calendar, non-removable core plugin):
- Local calendars/events CRUD with RLS isolation; recurrence expanded at
  query time (DST-correct), EXDATE + detached-override merge.
- Calendar app bundle: month/week/day/3-day views, mini-month, event form
  (color, show-as, timezone combobox, repeat, reminders, guests w/ recipient
  autocomplete, Jitsi video call), event popover with RSVP badges, resizable/
  collapsible left panel + theme toggle, overlap layout.
- iMIP invites: sdk.mail sends REQUEST from the organizer's mailbox (HTML +
  text/calendar + invite.ics); public /rsvp endpoint (vault-sealed token)
  records accepted/declined/tentative per attendee.
- "This and following" edit + scoped delete (this / this-and-following), with
  a delete confirmation dialog for recurring events.

Core:
- Settings: time_format (12/24h, default 24h) + jitsi_server; new Integrations
  section; date order follows time format (en-GB vs en-US) across Mail.
- Plugin SDK: mail (send from a user's account), vault (envelope encrypt),
  multi-method SSRF-guarded http; AppSwitcher product switcher; full-bleed
  PluginPage.

Mail HTML rendering:
- Sanitizer allows <style> blocks + class (CSS sanitized: drop script-like,
  strip @import/url() when remote images blocked); SANITIZER_VERSION 3 so
  already-indexed mail re-sanitizes on view.
- Attachments: a Content-Disposition: attachment part is a real attachment
  even when it also carries a Content-ID (fixes hidden invite.ics).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-28 16:33:34 +02:00
co-authored by Claude Opus 4.8
parent d46ad13b7b
commit 8c634f7967
56 changed files with 3308 additions and 37 deletions
+49
View File
@@ -12,6 +12,7 @@ from __future__ import annotations
import asyncio
import ipaddress
import socket
from dataclasses import dataclass
from urllib.parse import urlsplit
import httpx
@@ -87,3 +88,51 @@ async def guarded_get(url: str) -> str | None:
raise SsrfError("response exceeds 256 KB cap")
chunks.append(chunk)
return b"".join(chunks).decode("utf-8", "replace")
@dataclass(frozen=True)
class GuardedResponse:
status: int
headers: dict[str, str]
text: str
async def guarded_request(
method: str,
url: str,
*,
headers: dict[str, str] | None = None,
content: bytes | str | None = None,
auth: tuple[str, str] | None = None,
timeout: float = 15.0, # noqa: ASYNC109 - passed straight to httpx's own timeout
max_bytes: int = 25 * 1024 * 1024,
) -> GuardedResponse:
"""Arbitrary-method HTTPS request under the same SSRF policy as :func:`guarded_get`.
Used by plugins that speak WebDAV/CalDAV/CardDAV (PROPFIND/REPORT/PUT/DELETE) or
fetch ICS feeds. HTTPS-only, public host required, no off-domain redirects, body
capped at ``max_bytes``. ``auth`` is an (user, password) basic-auth pair. Returns
status + headers + body (all statuses; the caller decides what's an error)."""
parts = urlsplit(url)
if parts.scheme != "https":
raise SsrfError("only https is permitted")
if not parts.hostname:
raise SsrfError("missing host")
await assert_host_is_public(parts.hostname)
async with (
httpx.AsyncClient(follow_redirects=False, timeout=timeout, auth=auth) as client,
client.stream(method, url, headers=headers, content=content) as resp,
):
chunks: list[bytes] = []
total = 0
async for chunk in resp.aiter_bytes():
total += len(chunk)
if total > max_bytes:
raise SsrfError("response exceeds size cap")
chunks.append(chunk)
return GuardedResponse(
status=resp.status_code,
headers=dict(resp.headers),
text=b"".join(chunks).decode("utf-8", "replace"),
)
+11
View File
@@ -18,17 +18,22 @@ router = APIRouter(prefix="/settings", tags=["settings"])
DbSession = Annotated[AsyncSession, Depends(get_session)]
_ALLOWED_ORDER = {"asc", "desc"}
_ALLOWED_TIME_FORMAT = {"12", "24"}
class SettingsOut(BaseModel):
thread_order: str = "asc"
tz: str = "UTC"
time_format: str = "24" # 12 = AM/PM, 24 = 24-hour (default); applies app-wide
jitsi_server: str = "" # blank => https://meet.jit.si/
allowed_image_senders: list[str] = []
class SettingsPatch(BaseModel):
thread_order: str | None = None
tz: str | None = None
time_format: str | None = None
jitsi_server: str | None = None
class QuotaOut(BaseModel):
@@ -56,6 +61,8 @@ def _settings_out(user: User) -> SettingsOut:
return SettingsOut(
thread_order=str(s.get("thread_order", "asc")),
tz=str(s.get("tz", "UTC")),
time_format=str(s.get("time_format", "24")),
jitsi_server=str(s.get("jitsi_server", "")),
allowed_image_senders=sorted(allowed_image_senders(user)),
)
@@ -81,6 +88,10 @@ async def patch_settings(body: SettingsPatch, user: CurrentUser, db: DbSession)
updates["thread_order"] = body.thread_order
if body.tz is not None:
updates["tz"] = body.tz
if body.time_format is not None and body.time_format in _ALLOWED_TIME_FORMAT:
updates["time_format"] = body.time_format
if body.jitsi_server is not None:
updates["jitsi_server"] = body.jitsi_server.strip()
user.settings = updates
await db.commit()
return _settings_out(user)
+9 -2
View File
@@ -98,16 +98,23 @@ def _extract_attachments(msg: EmailMessage) -> list[ParsedAttachment]:
continue
cid = part.get("Content-ID")
filename = part.get_filename()
if not (filename or cid or part.get_content_disposition() == "attachment"):
disposition = part.get_content_disposition()
if not (filename or cid or disposition == "attachment"):
continue
payload = part.get_payload(decode=True)
if not isinstance(payload, bytes):
continue
# A part is an INLINE resource (cid: image rendered in-body, hidden from the
# attachment list) only when it carries a Content-ID AND is not explicitly an
# attachment. A part marked `Content-Disposition: attachment` is a real
# attachment even when it also has a Content-ID (e.g. Fastmail's invite.ics),
# so we drop the cid for it and it shows as a downloadable chip.
inline = cid is not None and disposition != "attachment"
out.append(
ParsedAttachment(
filename=filename or "attachment",
mime_type=part.get_content_type(),
content_id=cid.strip("<>") if cid else None,
content_id=cid.strip("<>") if (inline and cid is not None) else None,
payload=payload,
)
)
+36 -1
View File
@@ -17,7 +17,7 @@ import nh3
# Bump when the sanitization policy changes; body_parts.sanitizer_v records it so a
# render re-sanitizes stale content from the raw blob (SPEC §10.4).
SANITIZER_VERSION = 2
SANITIZER_VERSION = 3
_ALLOWED_TAGS: set[str] = {
# structure
@@ -76,6 +76,7 @@ _ALLOWED_TAGS: set[str] = {
"span",
"strike",
"strong",
"style",
"sub",
"summary",
"sup",
@@ -97,6 +98,7 @@ _ALLOWED_TAGS: set[str] = {
# Attributes allowed on any tag (presentational email formatting).
_GENERIC_ATTRS: set[str] = {
"style",
"class",
"align",
"valign",
"dir",
@@ -119,6 +121,7 @@ _TAG_SPECIFIC: dict[str, set[str]] = {
"th": {"colspan", "rowspan", "scope"},
"table": {"cellpadding", "cellspacing", "border"},
"font": {"face", "color", "size"},
"style": {"type", "media"},
}
# nh3 has no "generic attributes" option, so expand the generic set onto every tag.
@@ -132,6 +135,12 @@ _STYLE_DANGEROUS = re.compile(
r"(expression\s*\(|javascript:|vbscript:|@import|-moz-binding|behavior\s*:)", re.IGNORECASE
)
_STYLE_URL = re.compile(r"url\s*\([^)]*\)", re.IGNORECASE)
# Script-like CSS that voids a whole <style> block (unlike @import, which we can strip).
_STYLE_BLOCK_DANGEROUS = re.compile(
r"(expression\s*\(|javascript:|vbscript:|-moz-binding|behavior\s*:)", re.IGNORECASE
)
_STYLE_IMPORT = re.compile(r"@import[^;]*;?", re.IGNORECASE)
_STYLE_ELEMENT = re.compile(r"(<style\b[^>]*>)(.*?)(</style>)", re.IGNORECASE | re.DOTALL)
@dataclass(frozen=True)
@@ -150,6 +159,28 @@ def _sanitize_style(value: str, allow_remote: bool) -> str | None:
return value
def _sanitize_style_elements(html: str, allow_remote: bool, state: dict[str, bool]) -> str:
"""Sanitize CSS inside ``<style>`` blocks (nh3 keeps the tag but not its text).
Many mailers (e.g. Fastmail's calendar invites) put all layout in a ``<style>``
block of class rules — dropping it loses tables/buttons entirely. We keep the
block but neutralize script-like CSS and, unless remote images are approved,
strip ``@import``/``url()`` so it can't fetch remote/tracking resources.
"""
def repl(match: re.Match[str]) -> str:
css = match.group(2)
if _STYLE_BLOCK_DANGEROUS.search(css):
return "" # nuke the block entirely
css = _STYLE_IMPORT.sub("", css)
if not allow_remote and _STYLE_URL.search(css):
state["has_remote"] = True
css = _STYLE_URL.sub("none", css)
return f"{match.group(1)}{css}{match.group(3)}"
return _STYLE_ELEMENT.sub(repl, html)
def sanitize_html(
html: str, message_id: uuid.UUID, *, allow_remote_images: bool = False
) -> SanitizeResult:
@@ -186,8 +217,12 @@ def sanitize_html(
url_schemes=_URL_SCHEMES,
link_rel="noopener noreferrer nofollow",
strip_comments=True,
# nh3 strips <script>/<style> *content* by default; we allow <style> (above)
# and sanitize its CSS ourselves, so only script content is stripped here.
clean_content_tags={"script"},
attribute_filter=attribute_filter,
)
cleaned = _sanitize_style_elements(cleaned, allow_remote_images, state)
return SanitizeResult(
html=cleaned,
has_remote_images=state["has_remote"],
+41
View File
@@ -0,0 +1,41 @@
"""SSRF-guarded, multi-method HTTP client exposed to plugins (SPEC §10.6).
``sdk.http_get`` covers simple GETs; heavy plugins that speak CalDAV/CardDAV need
other verbs (PROPFIND/REPORT/PUT/DELETE) with request bodies and basic auth. This
facade routes all of them through the same SSRF policy (HTTPS-only, public host,
no off-domain redirects, size cap) and returns status + headers + body.
"""
from __future__ import annotations
from app.accounts.ssrf import GuardedResponse, guarded_request
class PluginHttp:
async def request(
self,
method: str,
url: str,
*,
headers: dict[str, str] | None = None,
content: bytes | str | None = None,
auth: tuple[str, str] | None = None,
) -> GuardedResponse:
return await guarded_request(
method, url, headers=headers, content=content, auth=auth
)
async def get(self, url: str, **kw: object) -> GuardedResponse:
return await self.request("GET", url, **kw) # type: ignore[arg-type]
async def put(self, url: str, **kw: object) -> GuardedResponse:
return await self.request("PUT", url, **kw) # type: ignore[arg-type]
async def delete(self, url: str, **kw: object) -> GuardedResponse:
return await self.request("DELETE", url, **kw) # type: ignore[arg-type]
async def propfind(self, url: str, **kw: object) -> GuardedResponse:
return await self.request("PROPFIND", url, **kw) # type: ignore[arg-type]
async def report(self, url: str, **kw: object) -> GuardedResponse:
return await self.request("REPORT", url, **kw) # type: ignore[arg-type]
+113
View File
@@ -0,0 +1,113 @@
"""Send an email *as the user*, from one of their own accounts (SPEC §11 SDK).
Heavy plugins (calendar iMIP invites, later contacts) need to send a message
through the organizer's real mailbox — reusing the same SMTP path as the compose
outbox, but with a plugin-built body (e.g. a ``text/calendar; method=REQUEST``
part). This helper looks up the user's account, assembles the MIME, and sends via
the account's provider. It is exposed as ``sdk(name).mail`` so plugins never touch
``app.providers`` / ``app.sync`` directly.
"""
from __future__ import annotations
import uuid
from datetime import UTC, datetime
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import format_datetime, make_msgid
from sqlalchemy import select
from app.db import session_scope
from app.models import Account
from app.sync.provider_factory import build_provider
class PluginMailError(Exception):
"""No usable account, or the send failed."""
class PluginMail:
"""Send outbound mail from one of a user's own accounts."""
async def send(
self,
user_id: uuid.UUID,
*,
to: list[str],
subject: str,
html: str,
text: str,
from_email: str | None = None,
ics: str | None = None,
ics_method: str = "REQUEST",
ics_filename: str = "invite.ics",
) -> None:
"""Send an email to ``to`` from the user's account.
``from_email`` selects which of the user's accounts to send from (falls
back to their first account). When ``ics`` is given it is added both as a
``text/calendar; method=<ics_method>`` alternative (so the recipient's
client shows its native RSVP UI) and as a downloadable attachment.
"""
recipients = [r for r in (to or []) if r]
if not recipients:
return
async with session_scope() as session:
account = None
if from_email:
account = await session.scalar(
select(Account).where(
Account.user_id == user_id,
Account.email == from_email,
)
)
if account is None:
account = await session.scalar(
select(Account).where(Account.user_id == user_id).order_by(Account.created_at)
)
if account is None:
raise PluginMailError("no account to send from")
raw = _build_message(
account, recipients, subject, html, text, ics, ics_method, ics_filename
)
envelope_from = account.email
provider = build_provider(account)
await provider.send(raw, envelope_from, recipients)
def _build_message(
account: Account,
to: list[str],
subject: str,
html: str,
text: str,
ics: str | None,
ics_method: str,
ics_filename: str,
) -> bytes:
root = MIMEMultipart("mixed")
from_name = account.display_name or ""
root["From"] = f"{from_name} <{account.email}>" if from_name else account.email
root["To"] = ", ".join(to)
root["Subject"] = subject
root["Date"] = format_datetime(datetime.now(UTC))
root["Message-ID"] = make_msgid(domain=account.email.rsplit("@", 1)[-1])
alt = MIMEMultipart("alternative")
alt.attach(MIMEText(text, "plain", "utf-8"))
alt.attach(MIMEText(html, "html", "utf-8"))
if ics is not None:
cal = MIMEText(ics, "calendar", "utf-8")
cal.set_param("method", ics_method)
cal.set_param("component", "VEVENT")
alt.attach(cal)
root.attach(alt)
if ics is not None:
att = MIMEApplication(ics.encode("utf-8"), _subtype="ics")
att.add_header("Content-Disposition", "attachment", filename=ics_filename)
root.attach(att)
return root.as_bytes()
+21
View File
@@ -29,7 +29,9 @@ from app.models import User
from app.outbox import service as outbox_service
from app.plugins.actions import PluginActions, PluginMessages
from app.plugins.db import plugin_session
from app.plugins.http import PluginHttp
from app.plugins.kv import PluginKvStore
from app.plugins.mail import PluginMail
from app.plugins.types import (
Hook,
JobFunc,
@@ -40,6 +42,7 @@ from app.plugins.types import (
Slot,
UiManifest,
)
from app.plugins.vault import PluginVault
from app.queue import enqueue_job
__all__ = [
@@ -122,6 +125,11 @@ class PluginSdk:
return outbox_service
# --- messages -----------------------------------------------------------
@property
def mail(self) -> PluginMail:
"""Send an email from one of the user's own accounts (iMIP invites, …)."""
return PluginMail()
@property
def messages(self) -> PluginMessages:
"""Read a user's message envelope (subject/sender/flags) for matching."""
@@ -137,6 +145,19 @@ class PluginSdk:
"""GET ``url`` through the §10.6 SSRF guard (blocks private IPs/oversize)."""
return await guarded_get(url)
@property
def http(self) -> PluginHttp:
"""Multi-method SSRF-guarded HTTP (GET/PUT/DELETE/PROPFIND/REPORT) for
CalDAV/CardDAV/ICS. Returns status + headers + body."""
return PluginHttp()
# --- secret vault -------------------------------------------------------
@property
def vault(self) -> PluginVault:
"""Envelope encrypt/decrypt for a plugin's own ``secret_ct`` columns
(CalDAV/CardDAV credentials, tokens) — §10.2 at-rest protection."""
return PluginVault()
def sdk(name: str) -> PluginSdk:
"""Return the SDK facade bound to plugin ``name`` (SPEC §11)."""
+21
View File
@@ -0,0 +1,21 @@
"""Secret encryption exposed to plugins (SPEC §10.2 vault).
Heavy plugins (calendar/contacts) store third-party credentials — CalDAV/CardDAV
passwords, ICS tokens — in their own ``secret_ct bytea`` columns. They encrypt via
this facade so those secrets are protected at rest with the same envelope scheme as
core account credentials, without importing ``app.security`` directly.
"""
from __future__ import annotations
from app.security.crypto import decrypt_secret, encrypt_secret
class PluginVault:
def encrypt(self, plaintext: bytes) -> bytes:
"""Envelope-encrypt ``plaintext`` for storage in a ``secret_ct`` column."""
return encrypt_secret(plaintext)
def decrypt(self, ciphertext: bytes) -> bytes:
"""Reverse :meth:`encrypt`. Raises on a wrong/rotated master key."""
return decrypt_secret(ciphertext)