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
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user