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:
@@ -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]
|
||||
@@ -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()
|
||||
@@ -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)."""
|
||||
|
||||
@@ -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)
|
||||
Reference in New Issue
Block a user