Skip to content

Feat: Add alerts destination external email - #100

Closed
liamburja wants to merge 2 commits into
developmentfrom
feat/99-alertas-destinatarios-email-externos-con-verificación-y-dominios-permitidos

Hidden character warning

The head ref may contain hidden characters: "feat/99-alertas-destinatarios-email-externos-con-verificaci\u00f3n-y-dominios-permitidos"
Closed

liamburja wants to merge 2 commits into
developmentfrom
feat/99-alertas-destinatarios-email-externos-con-verificación-y-dominios-permitidos

Conversation

@liamburja

@liamburja liamburja commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Alerts can now email people without a GitOps account (vendors, on-call mailboxes), limited to domains the organization allows and only after each address confirms it wants to receive them.

  • Allowed domains: new /org/<org>/settings/notifications page. The organization lists the domains it allows (needs organization:settings:update). The list is empty by default, which means no external recipients. Matching is exact; *.example.com covers subdomains. Domains are normalized to lower case and punycode, and the check runs on the domain of each address. Removing a domain marks its recipients blocked without deleting them; allowing it again restores their previous status.
  • External recipients: the alert editor takes addresses in recipients.externalEmails. The API validates them (domain_not_allowed, invalid_email). Adding addresses needs organization:notifications:update or project:notifications:update; keeping or removing existing ones does not. Addresses live in a new GitDB entity external_recipients, one per organization and shared by all its alerts.
  • Confirmation: each new address gets the external-recipient-verify email. The token is random, stored hashed, valid for 7 days and single use. Resends are rate limited to 3/h per address and 30/h per organization. The link opens a public /notifications/verify page, allowed without a session in hooks.server.ts.
  • Delivery: only verified addresses of a currently allowed domain get alerts. They skip the cancan filter, since they have no account, and never get system notifications. Their email is a reduced version: no links into the app, no catalog fields marked sensitive (vault secret name and path), plus an unsubscribe link and a List-Unsubscribe header.
  • Unsubscribe: a public /notifications/unsubscribe page stops every alert of the organization to that address. The token is stateless (HMAC with GITDB_ENCRYPTION_KEY, 90 days), made single use by a version counter on the recipient. Both public pages only act on POST, so mail scanners opening a link change nothing.
  • Send test email: new editor button, and "send": true on POST /api/alerts/test and POST /api/alerts/[id]/test. It really emails the recipients an alert would reach, using a complete example event and a [Test] subject. It needs SMTP, is limited to 5 per user every 10 minutes and never touches the throttle. On an unsaved alert, new external addresses get their confirmation email first and receive the next test once confirmed.

Shared pieces, built to be reused by the channels work (#95):

  • organization|project:notifications:read|create|update|delete permissions, granted to the default Organization Admin and Project Admin roles.
  • A $modules/notifications module.
  • #lib/server/infra/tokens.ts (one-time and signed tokens) and rate-limit.ts.
  • Headers on MailNotification, and server-side email translations and parts moved to infra/notifications.

Fixes found while testing:

  • Test email summary: the test sample event had an empty payload, which made the analysis email summary throw. The notifier now survives a failing mailSummary or link.
  • Mail queue results: the queue now reports failed sends (they were counted as sent), and logs the error name of non-SMTP failures.
  • Dev email links: under vite dev, email links use the dev server's real origin. Production still uses only ORIGIN.
  • Dev module reloads: process-wide singletons (alert engine, mail queue, verification limiters) no longer keep stale code or a stale GitDB cache after Vite reloads modules.
  • Sidebar active item (bug from before this PR): the sidebar kept the first page's item highlighted after client-side navigation.

Related issue

Closes #99

Part of #82

Change type

  • Bug fix
  • Feature
  • Documentation
  • Refactor
  • Security or permissions
  • Tests or tooling

Verification

  • bun run check: only the existing error in src/lib/server/gitdb/index.ts (branch option), also present on main
  • bun run lint
  • bun run test: 696 tests pass
  • bun run format:check: fails on 108 files already unformatted on main; every file changed here passes Prettier
  • UI tested manually, if applicable: with Mailpit, the full flow of allowed domain → confirmation → test email → unsubscribe; plus Playwright e2e for alerts, sidebar and organization settings

Review notes

  • New GitDB entities: notification_settings and external_recipients. No migration needed; both start empty.
  • Existing roles are not migrated: organizations created before this change need organization:notifications:* enabled in the role editor to manage external recipients, as with events:all.
  • ORIGIN is required: without it, confirmation and unsubscribe links can't be built, so nothing is sent to external addresses. The settings page shows a warning.
  • Unsubscribe token is not stored: the issue asks for hashed tokens. The unsubscribe token is not stored at all; it is an HMAC checked on use. Nothing usable lives in GitDB, and no commit happens per email.
  • No List-Unsubscribe-Post (RFC 8058 one-click): mail providers send that POST without an Origin header, and SvelteKit's CSRF check rejects it. Supporting it would mean moving the CSRF check into hooks.server.ts with an exception for that route. Left as a follow-up decision.
  • In-memory rate limits: verification and test-email limits are per process and reset on restart.
  • Security: no secrets in logs or responses. The mail logs keep a recipient hash, never the address; the API never returns token hashes.

🤖 Generated with Claude Code

@liamburja liamburja closed this Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants