feat: verify custom domains by a CNAME unique to the page
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 7m28s
ci / test (pull_request) Successful in 12m52s
ci / build (pull_request) Successful in 11m21s
ci / image (pull_request) Successful in 4m38s

A custom domain now needs a single record: a CNAME to
<label>.<status page host>, where the label is derived from the page's
domain token. The wildcard record of the status page host routes it, and
since the target belongs to one page alone it also proves who set it up;
a CNAME to the shared host would not, as any organization could then
claim a domain pointing at the platform. The TXT record stays for domains
whose CNAME cannot be seen, behind a proxy or without a subdomain.

- The domain watch looks for the records of a new domain every minute for
  a quarter of an hour, then every ten minutes for two days, and verifies
  it on its own; "Check now" still works.
- Verified domains are checked daily. When the record is gone the owners
  are told, and a week later the domain is switched off, so a record left
  behind cannot be taken over. Domains verified before keep working
  without checks.
- The editor shows the CNAME record first, the TXT way for proxies and
  apex domains, how the domain was verified and when a missing record will
  switch it off. The API returns cname_record and domain_missing_since.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-11-25 22:58:54 +01:00
1 parent 66537f4e65
commit 5934bd74e8
16 files changed
+1132 -307

No files matched your search

+176
View File
@@ -0,0 +1,176 @@
package api
import (
"context"
"crypto/sha256"
"encoding/base32"
"net"
"slices"
"strings"
"time"
"github.com/google/uuid"
"git.zb-server.de/ZB-Server/uptimesebi/internal/notify"
"git.zb-server.de/ZB-Server/uptimesebi/internal/store"
"git.zb-server.de/ZB-Server/uptimesebi/internal/tenant"
)
// A custom domain proves it belongs to its page in one of two ways:
//
// - a CNAME to <label>.<status page host>, where the label is unique to
// the page. The platform's status page host has a wildcard record, so
// the CNAME also routes the domain; one record does both. A CNAME to
// the shared host alone would not do: it proves the domain points at
// the platform, not which organization put it there.
// - the TXT record _uptimesebi.<domain>, for domains whose CNAME cannot be
// seen: behind a proxy such as Cloudflare's, or a domain without a
// subdomain using ALIAS or CNAME flattening.
//
// The domain watch looks for them on its own after a domain is set, and
// checks verified domains daily. One whose record has been missing for
// domainGrace is switched off, so a record left behind after a page is
// gone or moved cannot be claimed by anybody else.
const (
domainWatchEvery = time.Minute
domainGrace = 7 * 24 * time.Hour
keyDomainWatch = "domainwatch:lock"
)
// lookupCNAME is replaced in tests.
var lookupCNAME = net.DefaultResolver.LookupCNAME
var labelEncoding = base32.NewEncoding("abcdefghijklmnopqrstuvwxyz234567").WithPadding(base32.NoPadding)
// cnameLabel is the page's label below the status page host, derived from
// its domain token.
func cnameLabel(token string) string {
sum := sha256.Sum256([]byte("cname\x00" + token))
return "p" + labelEncoding.EncodeToString(sum[:10])
}
// cnameFor is the CNAME target of the page with token, or empty when the
// platform's status page host is not known.
func (s *Server) cnameFor(token string) string {
host := s.cnameTarget()
if host == "" {
return ""
}
return cnameLabel(token) + "." + host
}
// checkDomain looks for the records proving domain belongs to the page
// with token and returns "cname", "txt" or "" for neither.
func (s *Server) checkDomain(ctx context.Context, domain, token string) string {
if target := s.cnameFor(token); target != "" {
if c, err := lookupCNAME(ctx, domain); err == nil && strings.EqualFold(strings.TrimSuffix(c, "."), target) {
return "cname"
}
}
if records, err := lookupTXT(ctx, verifyPrefix+domain); err == nil && slices.Contains(records, token) {
return "txt"
}
return ""
}
// RunDomainWatch checks custom domains until ctx ends. Replicas take
// turns through a lock in Redis.
func (s *Server) RunDomainWatch(ctx context.Context) {
tick := time.NewTicker(domainWatchEvery)
defer tick.Stop()
for {
if ok, err := s.rdb.SetNX(ctx, keyDomainWatch, 1, domainWatchEvery-5*time.Second).Result(); err == nil && ok {
if err := s.WatchDomains(ctx, time.Now()); err != nil && ctx.Err() == nil {
s.log.WarnContext(ctx, "checking custom domains failed", "error", err)
}
}
select {
case <-ctx.Done():
return
case <-tick.C:
}
}
}
// WatchDomains checks the domains that are due once.
func (s *Server) WatchDomains(ctx context.Context, now time.Time) error {
return s.watchDomains(ctx, now, nil)
}
// watchDomains is WatchDomains limited to the page only, unless it is nil.
func (s *Server) watchDomains(ctx context.Context, now time.Time, only *uuid.UUID) error {
var due []store.DomainsToCheckRow
if err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
var err error
due, err = q.DomainsToCheck(ctx, store.DomainsToCheckParams{Now: now, PageID: only})
return err
}); err != nil {
return err
}
for _, d := range due {
if err := s.watchDomain(ctx, d, now); err != nil {
return err
}
}
return nil
}
func (s *Server) watchDomain(ctx context.Context, d store.DomainsToCheckRow, now time.Time) error {
method := s.checkDomain(ctx, d.Domain, d.DomainToken)
var (
failing *time.Time
off bool
)
err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
if method != "" {
return q.MarkDomainVerified(ctx, store.MarkDomainVerifiedParams{ID: d.ID, Now: now, Method: method})
}
var err error
if failing, err = q.MarkDomainUnconfirmed(ctx, store.MarkDomainUnconfirmedParams{ID: d.ID, Now: now}); err != nil || failing == nil {
return err
}
if now.Sub(*failing) >= domainGrace {
off = true
return q.UnverifyDomain(ctx, d.ID)
}
return nil
})
if err != nil {
return err
}
switch {
case method != "" && d.DomainVerifiedAt == nil:
s.log.InfoContext(ctx, "custom domain verified", "page", d.ID, "domain", d.Domain, "method", method)
case off:
s.log.InfoContext(ctx, "custom domain switched off: its record is gone", "page", d.ID, "domain", d.Domain)
s.mailDomain(ctx, d, "email.domain_off", now)
case failing != nil && d.DomainFailingSince == nil:
s.log.InfoContext(ctx, "custom domain record missing", "page", d.ID, "domain", d.Domain)
s.mailDomain(ctx, d, "email.domain_missing", now.Add(domainGrace))
}
return nil
}
// mailDomain tells the organization's owners about d.
func (s *Server) mailDomain(ctx context.Context, d store.DomainsToCheckRow, kind string, at time.Time) {
if s.smtp == nil {
return
}
var to []string
if err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
var err error
to, err = q.OrgOwnerEmails(ctx, d.OrgID)
return err
}); err != nil || len(to) == 0 {
return
}
l := s.catalog.Match("")
data := map[string]any{
"Domain": d.Domain, "Page": d.Title, "Link": s.ticketLink("/statuspages/" + d.ID.String()),
"Date": at.UTC().Format("2006-01-02 15:04 MST"),
}
if err := s.smtp.Send(ctx, notify.Mail{To: to, Subject: l.T(kind+".subject", data), Body: l.T(kind+".body", data)}); err != nil {
s.log.WarnContext(ctx, "mail about a custom domain failed", "page", d.ID, "error", err)
}
}