caelum0x ccb966b4d8 feat(mail): box1 deploy — Dockerfile, fixed relay envelope sender, key parsing + Dodo webhook hardening
- apps/mail/Dockerfile + .dockerignore: rust:1-bookworm build -> debian slim runtime
  (pure rustls, only ca-certificates), dashboard baked in at /app/dashboard
- RELAY_RETURN_PATH: fixed MAIL FROM for relays that only accept approved senders
  (OCI Email Delivery approves exact addresses, so per-email VERP is rejected);
  test mode keeps VERP so the simulated transport still recovers the email id
- mail-auth keys: try hex first — 64 hex chars are also valid standard base64
  (48 bytes), which made AUTH_SIGNING_KEY=<hex> fail at boot
- Dodo webhook: an account_id from checkout metadata that does not exist is now
  ignored (200) instead of failing on the customer FK (500 + endless retries)

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-11-13 14:03:39 +03:00
..

mail-hooks

The Supabase Auth Send Email Hook endpoint for Scribase Mail. Supabase calls POST /hooks/supabase/{slug} with { user, email_data } signed per Standard Webhooks; we verify the signature and timestamp, render the template for email_action_type, build the verify link and send through mail-core on behalf of the project's account.

Independent implementation, written without reference to useSend source. Licence: MIT. User guide: SUPABASE.md.

Endpoints

Method + path Purpose Auth
POST /hooks/supabase/{slug} the hook Supabase calls webhook-id / webhook-timestamp / webhook-signature
POST /hooks/projects create a project; returns hook_secret (v1,whsec_...) and endpoint_path Bearer sk_ (full access)
GET /hooks/projects, GET/PATCH/DELETE /hooks/projects/{id} manage projects of the key's account Bearer sk_
GET /hooks/projects/{id}/deliveries?limit= recent hook calls (status, email ids, errors) Bearer sk_
GET /hooks/projects/{id}/preview/{email_action_type} render a template with sample data, no send Bearer sk_

Create body: { "name", "from", "app_name", "slug"?, "hook_secret"?, "site_url"?, "templates"? }. Errors use Supabase's hook shape { "error": { "http_code": 401, "message": "..." } }.

Behaviour

  • Signature: HMAC-SHA256(secret, "{webhook-id}.{webhook-timestamp}.{body}"), base64, compared constant-time against every v1, entry in webhook-signature. Secret accepted as v1,whsec_<b64>, whsec_<b64> or bare base64. Timestamp outside HOOKS_TIMESTAMP_TOLERANCE_SECS (300) → 401.
  • Replay: (project, webhook-id) is unique in hook_deliveries; a repeated id returns 200 {} without sending again.
  • Verify link, per Supabase's docs: {site_url}/auth/v1/verify?token={token_hash}&type={email_action_type}&redirect_to={redirect_to}. A project site_url overrides the payload's.
  • email_change: with both token pairs present (Secure Email Change) two emails go out: to user.email using token + token_hash_new, and to user.new_email using token_new + token_hash (field names crossed, as documented). With one pair, one email to the new address.
  • Built-in templates (HTML + text, no emoji): signup, invite, magiclink, recovery, email_change (both directions), reauthentication (code), email (code), and the seven *_notification types. Unknown types get a generic notice.
  • Custom templates per project and action type: {"magiclink": {"subject", "html", "text"}} with {{confirmation_url}}, {{token}}, {{token_hash}}, {{new_token}}, {{email}}, {{new_email}}, {{old_email}}, {{site_url}}, {{redirect_to}}, {{app_name}}, {{email_action_type}}. Supabase's names ({{ .ConfirmationURL }}, {{ .Token }}, {{ .SiteURL }}, ...) are accepted too, so existing templates paste over.
  • Emails are tagged source=supabase_hook and email_action_type=<type>.

Run

export DATABASE_URL=postgres://localhost/mail_dev_core   # same DB as mail-server (run it first)
export HOOKS_BIND_ADDR=127.0.0.1:8793 DEV_TRUST_UNVERIFIED_DOMAINS=on
cargo run -p mail-hooks

Embed (what mail-server does): mail_hooks::migrate(&pool).await?; app.merge(mail_hooks::router(HookState::new(store, core_cfg, HooksConfig::from_env()))). Admin keys are resolved with mail_core::authenticate_api_key; sends go through mail_core::CoreSender::for_account (the same queue as POST /emails). Tables are hook_projects and hook_deliveries (idempotent SQL in migrations/, applied by migrate, independent of _sqlx_migrations).