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
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:
1 parent
66537f4e65
commit
5934bd74e8
16 files changed
+1132
-307
No files matched your search
@@ -49,7 +49,7 @@ func TestCertificateOrdersAreLimited(t *testing.T) {
|
||||
// The editor explains the records, with the platform's servers as the
|
||||
// CNAME target.
|
||||
_, _, html := owner.page("/statuspages/" + id1)
|
||||
for _, want := range []string{"Prove the domain is yours", "status.platform.test.", "ALIAS or ANAME", "at most 10 times a day"} {
|
||||
for _, want := range []string{"Create this CNAME record", ".status.platform.test.", "Prove the domain with this TXT record", "ALIAS or ANAME", "at most 10 times a day"} {
|
||||
if !strings.Contains(html, want) {
|
||||
t.Errorf("the editor lacks %q", want)
|
||||
}
|
||||
|
||||
@@ -1142,7 +1142,7 @@ func (s *Server) statusPageEditor(c *gin.Context, p *principal, page web.Page, m
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
v := viewStatusPage(row)
|
||||
v := s.viewStatusPage(row)
|
||||
e := web.PageEditor{
|
||||
ID: row.ID.String(), Title: row.Title, Slug: row.Slug, Description: row.Description,
|
||||
Published: row.Published, ShowDetails: row.ShowComponentDetails,
|
||||
@@ -1155,8 +1155,13 @@ func (s *Server) statusPageEditor(c *gin.Context, p *principal, page web.Page, m
|
||||
e.RecordName, e.RecordValue = v.DomainRecord.Name, v.DomainRecord.Value
|
||||
}
|
||||
e.DomainChangesPerDay = domainChangesPerDay
|
||||
if !own {
|
||||
e.CNAMETarget = s.cnameTarget()
|
||||
if v.CNAMERecord != nil {
|
||||
e.CNAMEValue = v.CNAMERecord.Value
|
||||
}
|
||||
e.VerifiedBy = deref(row.DomainVerifyMethod)
|
||||
if f := row.DomainFailingSince; f != nil {
|
||||
e.MissingSince = f.Local().Format("2006-01-02 15:04")
|
||||
e.SwitchOffAt = f.Add(domainGrace).Local().Format("2006-01-02 15:04")
|
||||
}
|
||||
newPageGroupTree(pageGroups).walk(func(g store.StatusPageGroup, depth int) {
|
||||
var parentID string
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
package api_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/api"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/config"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/notify/notifytest"
|
||||
)
|
||||
|
||||
// fakeDNS answers CNAME lookups from a map, and TXT lookups with nothing.
|
||||
type fakeDNS struct {
|
||||
mu sync.Mutex
|
||||
cname map[string]string
|
||||
}
|
||||
|
||||
func (d *fakeDNS) set(name, target string) {
|
||||
d.mu.Lock()
|
||||
defer d.mu.Unlock()
|
||||
if target == "" {
|
||||
delete(d.cname, name)
|
||||
} else {
|
||||
d.cname[name] = target
|
||||
}
|
||||
}
|
||||
|
||||
func useFakeDNS(t *testing.T) *fakeDNS {
|
||||
d := &fakeDNS{cname: map[string]string{}}
|
||||
savedC, savedT := *api.LookupCNAME, *api.LookupTXT
|
||||
t.Cleanup(func() { *api.LookupCNAME, *api.LookupTXT = savedC, savedT })
|
||||
*api.LookupCNAME = func(_ context.Context, name string) (string, error) {
|
||||
d.mu.Lock()
|
||||
defer d.mu.Unlock()
|
||||
if c, ok := d.cname[name]; ok {
|
||||
return c, nil
|
||||
}
|
||||
return "", errors.New("no such host")
|
||||
}
|
||||
*api.LookupTXT = func(context.Context, string) ([]string, error) { return nil, errors.New("no such record") }
|
||||
return d
|
||||
}
|
||||
|
||||
func TestCustomDomainByCNAME(t *testing.T) {
|
||||
mail := notifytest.NewSMTP(t)
|
||||
e := newEnvWith(t, func(c *config.API) {
|
||||
c.SMTPURL, c.SignupVerify = mail.URL(), "off"
|
||||
c.StatusPageURL = "https://status.platform.test"
|
||||
})
|
||||
ctx := context.Background()
|
||||
dns := useFakeDNS(t)
|
||||
platform := e.statuspageClient(t, nil)
|
||||
|
||||
newPage := func(b *browser) uuid.UUID {
|
||||
slug := uniq("cn-")
|
||||
_, p := b.do("POST", "/api/v1/org/statuspages", map[string]any{"slug": slug, "title": "Shop status"})
|
||||
b.do("PUT", "/api/v1/org/statuspages/"+p["id"].(string), map[string]any{"slug": slug, "title": "Shop status", "published": true})
|
||||
return uuid.MustParse(p["id"].(string))
|
||||
}
|
||||
setDomain := func(b *browser, id uuid.UUID, domain string) string {
|
||||
t.Helper()
|
||||
st, body := b.do("PUT", "/api/v1/org/statuspages/"+id.String()+"/domain", map[string]any{"domain": domain})
|
||||
if st != http.StatusOK {
|
||||
t.Fatalf("set domain = %d %v", st, body)
|
||||
}
|
||||
rec, _ := body["cname_record"].(map[string]any)
|
||||
if rec == nil || rec["name"] != domain {
|
||||
t.Fatalf("cname_record = %v", body["cname_record"])
|
||||
}
|
||||
return rec["value"].(string)
|
||||
}
|
||||
served := func(domain string) bool {
|
||||
_, err := platform.Page(ctx, domain, "")
|
||||
return err == nil
|
||||
}
|
||||
|
||||
a := e.browser(t)
|
||||
aEmail := "a@" + uniq("cn") + ".test"
|
||||
a.signup(aEmail, uniq("cn-"))
|
||||
pageA := newPage(a)
|
||||
domain := uniq("status") + ".shop.test"
|
||||
target := setDomain(a, pageA, domain)
|
||||
if !strings.HasSuffix(target, ".status.platform.test") || len(target) < 30 {
|
||||
t.Errorf("CNAME target %q is not unique to the page below the status host", target)
|
||||
}
|
||||
if _, _, html := a.page("/statuspages/" + pageA.String()); !strings.Contains(html, target+".") || !strings.Contains(html, "Check now") {
|
||||
t.Error("the editor does not show the page's CNAME record")
|
||||
}
|
||||
|
||||
// A CNAME to the shared host alone proves nothing.
|
||||
now := time.Now()
|
||||
dns.set(domain, "status.platform.test.")
|
||||
if err := e.api.WatchDomainOf(ctx, now, pageA); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if served(domain) {
|
||||
t.Fatal("a CNAME to the shared host verified the domain")
|
||||
}
|
||||
// The page's own target verifies it, without anybody pressing a button.
|
||||
dns.set(domain, strings.ToUpper(target)+".")
|
||||
if err := e.api.WatchDomainOf(ctx, now.Add(time.Minute), pageA); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !served(domain) {
|
||||
t.Fatal("the watch did not verify the domain by its CNAME")
|
||||
}
|
||||
if _, _, html := a.page("/statuspages/" + pageA.String()); !strings.Contains(html, "verified by its CNAME record") {
|
||||
t.Error("the editor does not say how the domain was verified")
|
||||
}
|
||||
|
||||
// Another organization taking the domain over, while the CNAME still
|
||||
// points at the first page, gets nowhere.
|
||||
a.do("PUT", "/api/v1/org/statuspages/"+pageA.String()+"/domain", map[string]any{"domain": ""})
|
||||
b := e.browser(t)
|
||||
b.signup("b@"+uniq("cn")+".test", uniq("cn-"))
|
||||
pageB := newPage(b)
|
||||
setDomain(b, pageB, domain)
|
||||
if err := e.api.WatchDomainOf(ctx, now.Add(2*time.Minute), pageB); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if st, _ := b.do("POST", "/api/v1/org/statuspages/"+pageB.String()+"/domain/verify", nil); st != http.StatusUnprocessableEntity || served(domain) {
|
||||
t.Fatalf("a record left by another page verified the domain (verify = %d)", st)
|
||||
}
|
||||
b.do("PUT", "/api/v1/org/statuspages/"+pageB.String()+"/domain", map[string]any{"domain": ""})
|
||||
|
||||
// Once verified, the record is checked daily. Gone, the owners are told
|
||||
// and, a week later, the domain is switched off.
|
||||
setDomain(a, pageA, domain)
|
||||
if st, body := a.do("POST", "/api/v1/org/statuspages/"+pageA.String()+"/domain/verify", nil); st != http.StatusNoContent {
|
||||
t.Fatalf("verify = %d %v", st, body)
|
||||
}
|
||||
dns.set(domain, "")
|
||||
day := now.Add(25 * time.Hour)
|
||||
if err := e.api.WatchDomainOf(ctx, day, pageA); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
waitFor(t, 5*time.Second, "a mail that the record is gone", func() bool {
|
||||
return mailedTo(mail, aEmail, "is gone", domain)
|
||||
})
|
||||
if !served(domain) {
|
||||
t.Error("the domain was switched off at once")
|
||||
}
|
||||
if _, _, html := a.page("/statuspages/" + pageA.String()); !strings.Contains(html, "has not been found since") {
|
||||
t.Error("the editor does not warn about the missing record")
|
||||
}
|
||||
if err := e.api.WatchDomainOf(ctx, day.Add(8*24*time.Hour), pageA); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if served(domain) {
|
||||
t.Error("the domain is still served a week after its record went")
|
||||
}
|
||||
waitFor(t, 5*time.Second, "a mail that the domain was switched off", func() bool {
|
||||
return mailedTo(mail, aEmail, "switched off", domain)
|
||||
})
|
||||
}
|
||||
|
||||
func TestDomainsVerifiedBeforeAreNotCheckedAgain(t *testing.T) {
|
||||
e := newEnvWith(t, func(c *config.API) { c.StatusPageURL = "https://status.platform.test" })
|
||||
ctx := context.Background()
|
||||
useFakeDNS(t)
|
||||
owner := e.browser(t)
|
||||
owner.signup("o@"+uniq("old")+".test", uniq("old-"))
|
||||
slug := uniq("old-")
|
||||
_, p := owner.do("POST", "/api/v1/org/statuspages", map[string]any{"slug": slug, "title": "Status"})
|
||||
id := p["id"].(string)
|
||||
owner.do("PUT", "/api/v1/org/statuspages/"+id, map[string]any{"slug": slug, "title": "Status", "published": true})
|
||||
domain := uniq("old") + ".example.test"
|
||||
owner.do("PUT", "/api/v1/org/statuspages/"+id+"/domain", map[string]any{"domain": domain})
|
||||
if err := e.asSystem(t, func(tx pgx.Tx) error {
|
||||
_, err := tx.Exec(ctx, `UPDATE status_pages SET domain_verified_at = now() - interval '1 year', domain_set_at = now() - interval '1 year' WHERE id = $1`, id)
|
||||
return err
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := e.api.WatchDomainOf(ctx, time.Now().Add(30*24*time.Hour), uuid.MustParse(id)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := e.statuspageClient(t, nil).Page(ctx, domain, ""); err != nil {
|
||||
t.Errorf("a domain verified before the checks was switched off: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func mailedTo(mail *notifytest.SMTP, to string, want ...string) bool {
|
||||
for _, m := range mail.Messages() {
|
||||
if !strings.Contains(strings.Join(m.To, ","), to) {
|
||||
continue
|
||||
}
|
||||
ok := true
|
||||
for _, w := range want {
|
||||
ok = ok && strings.Contains(m.Data, w)
|
||||
}
|
||||
if ok {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -30,3 +30,11 @@ func OpenAPI() map[string]any { return buildOpenAPI("https://uptime.example") }
|
||||
func (s *Server) WatchRegionsOf(ctx context.Context, now time.Time, ids ...uuid.UUID) error {
|
||||
return s.watchRegions(ctx, now, ids)
|
||||
}
|
||||
|
||||
// LookupCNAME lets tests replace DNS lookups for domain verification.
|
||||
var LookupCNAME = &lookupCNAME
|
||||
|
||||
// WatchDomainOf runs the domain watch for one page only.
|
||||
func (s *Server) WatchDomainOf(ctx context.Context, now time.Time, page uuid.UUID) error {
|
||||
return s.watchDomains(ctx, now, &page)
|
||||
}
|
||||
@@ -480,6 +480,13 @@ type statusPageView struct {
|
||||
DomainVerified bool `json:"domain_verified"`
|
||||
// DomainRecord is the TXT record that proves control of CustomDomain.
|
||||
DomainRecord *dnsRecord `json:"domain_record,omitempty"`
|
||||
// CNAMERecord points CustomDomain at the platform's status page
|
||||
// servers and proves control of it in one, where the platform knows
|
||||
// their host.
|
||||
CNAMERecord *dnsRecord `json:"cname_record,omitempty"`
|
||||
// DomainMissingSince is set when the record of a verified domain has
|
||||
// not been found since then; it is switched off a week later.
|
||||
DomainMissingSince *time.Time `json:"domain_missing_since,omitempty"`
|
||||
// Suspended is set when platform admins took the page offline; it is
|
||||
// not served whatever Published says.
|
||||
Suspended bool `json:"suspended,omitempty"`
|
||||
@@ -492,7 +499,7 @@ type dnsRecord struct {
|
||||
Value string `json:"value"`
|
||||
}
|
||||
|
||||
func viewStatusPage(p store.StatusPage) statusPageView {
|
||||
func (s *Server) viewStatusPage(p store.StatusPage) statusPageView {
|
||||
v := statusPageView{
|
||||
ID: p.ID, Slug: p.Slug, Title: p.Title, Description: p.Description,
|
||||
Published: p.Published, ShowComponentDetails: p.ShowComponentDetails,
|
||||
@@ -502,6 +509,10 @@ func viewStatusPage(p store.StatusPage) statusPageView {
|
||||
_ = json.Unmarshal(p.Theme, &v.Theme)
|
||||
if p.CustomDomain != nil {
|
||||
v.DomainRecord = &dnsRecord{Name: verifyPrefix + *p.CustomDomain, Type: "TXT", Value: p.DomainToken}
|
||||
if target := s.cnameFor(p.DomainToken); target != "" {
|
||||
v.CNAMERecord = &dnsRecord{Name: *p.CustomDomain, Type: "CNAME", Value: target}
|
||||
}
|
||||
v.DomainMissingSince = p.DomainFailingSince
|
||||
}
|
||||
return v
|
||||
}
|
||||
@@ -540,7 +551,7 @@ func (s *Server) listStatusPages(c *gin.Context) {
|
||||
out := []statusPageView{}
|
||||
for _, r := range rows {
|
||||
if p.Grants.Can(authz.StatusPagesRead, pageResource(r.ID)) {
|
||||
out = append(out, viewStatusPage(r))
|
||||
out = append(out, s.viewStatusPage(r))
|
||||
}
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"pages": out})
|
||||
@@ -582,7 +593,7 @@ func (s *Server) createStatusPage(c *gin.Context) {
|
||||
internalError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, viewStatusPage(row))
|
||||
c.JSON(http.StatusCreated, s.viewStatusPage(row))
|
||||
}
|
||||
|
||||
func newDomainToken() string {
|
||||
@@ -621,7 +632,7 @@ func (s *Server) updateStatusPage(c *gin.Context) {
|
||||
case err != nil:
|
||||
internalError(c, err)
|
||||
default:
|
||||
c.JSON(http.StatusOK, viewStatusPage(row))
|
||||
c.JSON(http.StatusOK, s.viewStatusPage(row))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -770,14 +781,14 @@ func (s *Server) setPageDomain(c *gin.Context) {
|
||||
case err != nil:
|
||||
internalError(c, err)
|
||||
default:
|
||||
c.JSON(http.StatusOK, viewStatusPage(row))
|
||||
c.JSON(http.StatusOK, s.viewStatusPage(row))
|
||||
}
|
||||
}
|
||||
|
||||
// lookupTXT is replaced in tests.
|
||||
var lookupTXT = net.DefaultResolver.LookupTXT
|
||||
|
||||
// verifyPageDomain checks the TXT record. Only verified domains are served,
|
||||
// verifyPageDomain looks for the CNAME or the TXT record now. Only verified domains are served,
|
||||
// so nobody can claim a domain they do not control.
|
||||
func (s *Server) verifyPageDomain(c *gin.Context) {
|
||||
id := uuid.MustParse(c.Param("id"))
|
||||
@@ -804,12 +815,14 @@ func (s *Server) verifyPageDomain(c *gin.Context) {
|
||||
fail(c, http.StatusConflict, "no_domain")
|
||||
return
|
||||
}
|
||||
records, err := lookupTXT(ctx, verifyPrefix+*row.CustomDomain)
|
||||
if err != nil || !slices.Contains(records, row.DomainToken) {
|
||||
method := s.checkDomain(ctx, *row.CustomDomain, row.DomainToken)
|
||||
if method == "" {
|
||||
fail(c, http.StatusUnprocessableEntity, "domain_not_verified")
|
||||
return
|
||||
}
|
||||
err = s.db.Tx(ctx, p.scope(), func(q *store.Queries) error { return q.MarkDomainVerified(ctx, id) })
|
||||
err = s.db.Tx(ctx, p.scope(), func(q *store.Queries) error {
|
||||
return q.MarkDomainVerified(ctx, store.MarkDomainVerifiedParams{ID: id, Now: time.Now(), Method: method})
|
||||
})
|
||||
if err != nil {
|
||||
internalError(c, err)
|
||||
return
|
||||
|
||||
Reference in new issue
Block a user