carlos / platform Public

console: one buy admission path; neutrality guard test

Co-Authored-By: Astra (gpt-6-astra) via Codex <noreply@openai.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev cf401da13a00996ac13d20d81bc852d15136e517 parent cf78512
3 files changed, +295 −68
  • internal/console/apidomainbuy.go +1 −31
  • internal/console/domainbuy.go +42 −37
  • internal/console/domainbuy_admission_test.go +252 −0
diff --git a/internal/console/apidomainbuy.go b/internal/console/apidomainbuy.go
index a0defe85f..e2bc40421 100644
--- a/internal/console/apidomainbuy.go
+++ b/internal/console/apidomainbuy.go
@@ -35,38 +35,8 @@ func (s *Server) domainBuyAPIGate(w http.ResponseWriter, r *http.Request) (strin
return addr, acct, true
}
-// Check cannot name an app, so any existing claim is unavailable here. The
-// engine repeats its own checks before creating a purchase; this read reserves nothing.
func (s *Server) checkDomainBuy(ctx context.Context, acct *Account, name string) (domainreg.Quote, *doorRefusal) {
- if ref := s.customDomainRefusal(acct, name); ref != nil {
- return domainreg.Quote{}, ref
- }
- name, err := domainreg.Normalize(name)
- if err != nil {
- return domainreg.Quote{}, domainBuyErrorRefusal(err)
- }
- if !s.domainChecks.allow(acct.ID) {
- return domainreg.Quote{}, &doorRefusal{429, domainBuyCopy["rate"]}
- }
- for _, host := range []string{name, "www." + name} {
- if s.domainHostDenied(host, acct.Sqid) {
- return domainreg.Quote{}, claimRefusal(host, domains.ErrHostUnavailable)
- }
- if ref := s.heldElsewhere(ctx, host, acct.Sqid, ""); ref != nil {
- return domainreg.Quote{}, ref
- }
- _, err := domains.GetClaim(ctx, s.cfg.Releases, host)
- if err == nil {
- return domainreg.Quote{}, claimRefusal(host, domains.ErrHostUnavailable)
- }
- if !errors.Is(err, os.ErrNotExist) {
- return domainreg.Quote{}, domainBuyUnavailable()
- }
- }
- if ref := s.liveDomainPurchaseRefusal(ctx, name); ref != nil {
- return domainreg.Quote{}, ref
- }
- return s.quoteDomain(ctx, acct, name)
+ return s.admitDomainBuy(ctx, acct, name)
}
func (s *Server) liveDomainPurchaseRefusal(ctx context.Context, name string) *doorRefusal {
diff --git a/internal/console/domainbuy.go b/internal/console/domainbuy.go
index a24c41e63..99ea0e312 100644
--- a/internal/console/domainbuy.go
+++ b/internal/console/domainbuy.go
@@ -17,6 +17,7 @@ import (
"github.com/carlosframework/platform/internal/domains"
"github.com/carlosframework/platform/internal/registrar"
"github.com/carlosframework/platform/internal/zonehost"
+ "golang.org/x/net/publicsuffix"
)
func (s *Server) domainBuyReady() bool {
@@ -31,7 +32,11 @@ func domainBuyUnavailable() *doorRefusal {
return &doorRefusal{http.StatusServiceUnavailable, domainBuyCopy["unavailable"]}
}
-func (s *Server) quoteDomain(ctx context.Context, acct *Account, name string) (domainreg.Quote, *doorRefusal) {
+// admitDomainBuy is shared by every check and purchase door after owner/person
+// authorization. Claims and live purchases may only produce the neutral refusal:
+// name-only refusals and the shared budget come first, then registrar availability,
+// then the claim/live checks, and finally premium and price checks.
+func (s *Server) admitDomainBuy(ctx context.Context, acct *Account, name string) (domainreg.Quote, *doorRefusal) {
if !s.domainBuyReady() {
return domainreg.Quote{}, &doorRefusal{Status: http.StatusNotFound}
}
@@ -46,10 +51,43 @@ func (s *Server) quoteDomain(ctx context.Context, acct *Account, name string) (d
if err != nil || b == nil || b.Currency == "" || b.Customer == "" {
return domainreg.Quote{}, domainBuyUnavailable()
}
+ suffix, _ := publicsuffix.PublicSuffix(name)
+ tld, offered := s.cfg.DomainCatalog.TLDs[suffix]
+ if !offered || tld.Register[b.Currency] <= 0 {
+ return domainreg.Quote{}, domainBuyErrorRefusal(domainreg.ErrTLDNotOffered)
+ }
+ if !s.domainChecks.allow(acct.ID) {
+ return domainreg.Quote{}, &doorRefusal{http.StatusTooManyRequests, domainBuyCopy["rate"]}
+ }
a, err := s.cfg.Registrar.Check(ctx, name)
if err != nil {
return domainreg.Quote{}, domainBuyErrorRefusal(err)
}
+ if !a.Available {
+ return domainreg.Quote{}, claimRefusal(name, domains.ErrHostUnavailable)
+ }
+ for _, host := range []string{name, "www." + name} {
+ if s.domainHostDenied(host, acct.Sqid) {
+ return domainreg.Quote{}, claimRefusal(host, domains.ErrHostUnavailable)
+ }
+ if ref := s.heldElsewhere(ctx, host, acct.Sqid, ""); ref != nil {
+ if ref.Status == http.StatusConflict {
+ log.Printf("console: domain purchase: account=%s domain=%s reason=claim held elsewhere", acct.Sqid, name)
+ }
+ return domainreg.Quote{}, ref
+ }
+ _, err := domains.GetClaim(ctx, s.cfg.Releases, host)
+ if err == nil {
+ log.Printf("console: domain purchase: account=%s domain=%s reason=claim held elsewhere", acct.Sqid, name)
+ return domainreg.Quote{}, claimRefusal(host, domains.ErrHostUnavailable)
+ }
+ if !errors.Is(err, os.ErrNotExist) {
+ return domainreg.Quote{}, domainBuyUnavailable()
+ }
+ }
+ if ref := s.liveDomainPurchaseRefusal(ctx, name); ref != nil {
+ return domainreg.Quote{}, ref
+ }
q, err := s.cfg.DomainCatalog.Quote(name, b.Currency, a)
if err != nil {
return domainreg.Quote{}, domainBuyErrorRefusal(err)
@@ -58,16 +96,11 @@ func (s *Server) quoteDomain(ctx context.Context, acct *Account, name string) (d
}
func (s *Server) startDomainPurchase(ctx context.Context, acct *Account, app, name string, c registrar.Contact, principal string) (*DomainPurchase, string, *doorRefusal) {
- if !s.domainBuyReady() {
- return nil, "", &doorRefusal{Status: http.StatusNotFound}
- }
- if ref := s.customDomainRefusal(acct, name); ref != nil {
+ q, ref := s.admitDomainBuy(ctx, acct, name)
+ if ref != nil {
return nil, "", ref
}
- name, err := domainreg.Normalize(name)
- if err != nil {
- return nil, "", domainBuyErrorRefusal(err)
- }
+ name = q.Domain
exists, err := s.store.HasAppIn(acct.ID, app)
if err != nil {
return nil, "", domainBuyUnavailable()
@@ -87,34 +120,6 @@ func (s *Server) startDomainPurchase(ctx context.Context, acct *Account, app, na
if count >= s.cfg.DomainCatalog.DailyCap {
return nil, "", &doorRefusal{http.StatusTooManyRequests, domainPurchaseCopy.DailyCap}
}
- for _, host := range []string{name, "www." + name} {
- if s.domainHostDenied(host, acct.Sqid) {
- return nil, "", claimRefusal(host, domains.ErrHostUnavailable)
- }
- if ref := s.heldElsewhere(ctx, host, acct.Sqid, app); ref != nil {
- if ref.Status == http.StatusConflict {
- log.Printf("console: domain purchase: account=%s domain=%s reason=claim held elsewhere", acct.Sqid, name)
- }
- return nil, "", ref
- }
- claim, err := domains.GetClaim(ctx, s.cfg.Releases, host)
- if err != nil && !errors.Is(err, os.ErrNotExist) {
- return nil, "", domainBuyUnavailable()
- }
- if err == nil && (claim.Account != acct.Sqid || claim.App != app) {
- log.Printf("console: domain purchase: account=%s domain=%s reason=claim held elsewhere", acct.Sqid, name)
- return nil, "", claimRefusal(host, domains.ErrHostUnavailable)
- }
- }
- // A live journal stays a neutral refusal even after registration makes the
- // registrar report the name unavailable. The unique index still closes races.
- if ref := s.liveDomainPurchaseRefusal(ctx, name); ref != nil {
- return nil, "", ref
- }
- q, ref := s.quoteDomain(ctx, acct, name)
- if ref != nil {
- return nil, "", ref
- }
balance, err := s.cfg.Registrar.Balance(ctx)
if err != nil {
return nil, "", domainBuyErrorRefusal(err)
diff --git a/internal/console/domainbuy_admission_test.go b/internal/console/domainbuy_admission_test.go
new file mode 100644
index 000000000..599d9356f
--- /dev/null
+++ b/internal/console/domainbuy_admission_test.go
@@ -0,0 +1,252 @@
+package console
+
+import (
+ "context"
+ "fmt"
+ "net/url"
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/carlosframework/platform/internal/domains"
+ "github.com/carlosframework/platform/internal/registrar"
+)
+
+func TestBuyAdmissionNeutralityGuard(t *testing.T) {
+ for _, tc := range []struct {
+ name, suffix, key string
+ status, checks int
+ premium bool
+ err error
+ }{
+ {name: "offered", suffix: "com", status: 409, checks: 8},
+ {name: "outside-catalog", suffix: "io", key: "notOffered", status: 422},
+ {name: "non-registrable", suffix: "shop.com", key: "nameError", status: 422},
+ {name: "missing-currency", suffix: "com", key: "notOffered", status: 422},
+ {name: "held-premium", suffix: "com", status: 409, checks: 8, premium: true},
+ {name: "held-high-cost", suffix: "com", status: 409, checks: 8},
+ {name: "registered-premium", suffix: "com", status: 409, checks: 8, premium: true},
+ {name: "registrar-unavailable", suffix: "com", status: 409, checks: 8, err: fmt.Errorf("wrapped: %w", registrar.ErrUnavailable)},
+ {name: "registrar-error", suffix: "com", key: "unavailable", status: 503, checks: 8, err: registrar.ErrIndeterminate},
+ {name: "limited", suffix: "com", key: "rate", status: 429},
+ {name: "plan", suffix: "com", status: 409},
+ } {
+ for _, holder := range []string{"apex", "www", "own-apex", "live"} {
+ t.Run(tc.name+"/"+holder, func(t *testing.T) {
+ f := newPurchaseFixture(t)
+ held, unheld := "held."+tc.suffix, "unheld."+tc.suffix
+ switch holder {
+ case "live":
+ if err := f.st.CreateDomainPurchase(DomainPurchase{ID: "guard-live", AccountID: f.acct.ID, Domain: held, App: "kass", State: PurchaseAwaitingPayment}); err != nil {
+ t.Fatal(err)
+ }
+ case "own-apex":
+ if _, _, err := domains.AttachClaim(context.Background(), f.rel, domains.Claim{Schema: 1, Host: held, Account: f.acct.Sqid, App: "kass"}); err != nil {
+ t.Fatal(err)
+ }
+ default:
+ host := held
+ if holder == "www" {
+ host = "www." + held
+ }
+ holdBuyName(t, f, host)
+ }
+ for _, name := range []string{held, unheld} {
+ f.reg.Available[name] = registrar.Availability{Premium: tc.premium, Cost: registrar.Money{Currency: "usd", Minor: 900}}
+ }
+ // An offered held name can be free at the registrar. Its answer must
+ // match an unheld taken name, including when the claim belongs to us.
+ if tc.name == "offered" || tc.name == "held-premium" || tc.name == "held-high-cost" {
+ a := f.reg.Available[held]
+ a.Available = true
+ if tc.name == "held-high-cost" {
+ a.Cost.Minor = 1001
+ }
+ f.reg.Available[held] = a
+ }
+ if tc.err != nil {
+ f.srv.cfg.Registrar = admissionErrorRegistrar{f.reg, tc.err}
+ }
+ if tc.name == "missing-currency" {
+ tld := f.srv.cfg.DomainCatalog.TLDs["com"]
+ tld.Register = map[string]int64{"eur": 1500}
+ f.srv.cfg.DomainCatalog.TLDs["com"] = tld
+ }
+ now := time.Now()
+ f.srv.domainChecks.now = func() time.Time { return now }
+ if tc.checks == 0 {
+ for i := 0; i < domainCheckAccountBudget; i++ {
+ f.srv.domainChecks.allow(f.acct.ID)
+ }
+ }
+ if tc.name == "plan" {
+ if _, err := f.st.SetAccountPlan(f.acct.ID, "free", nil, 0, PlanSourceOperator, nil, "test", "operator", now); err != nil {
+ t.Fatal(err)
+ }
+ }
+ tok := buyToken(t, f)
+ browser := newBrowser(t)
+ f.setUser("owner" + f.acct.Sqid + "@example.com")
+ signUp(t, f.consoleFixture, browser, "admission-guard")
+ csrf := sessionToken(t, f.consoleFixture, browser)
+ wantMessage := hostUnavailableMsg
+ if tc.key != "" {
+ wantMessage = domainBuyCopy[tc.key]
+ }
+ if tc.name == "plan" {
+ wantMessage = f.srv.customDomainRefusal(f.acct, unheld).Msg
+ }
+ for _, transport := range []string{"cli", "browser"} {
+ var wantBody string
+ for _, door := range []string{"check", "purchases"} {
+ for _, name := range []string{unheld, held} {
+ var status int
+ var body string
+ if transport == "cli" {
+ payload := strings.ReplaceAll(buyBody(), "shop.com", name)
+ if door == "check" {
+ payload = fmt.Sprintf(`{"name":%q}`, name)
+ }
+ status, body = buyRequest(t, f, "POST", buyAPIPath(f)+"/"+door, tok, payload)
+ } else {
+ path := door
+ if path == "purchases" {
+ path = "buy"
+ }
+ values := url.Values{"csrf": {csrf}, "name": {name}}
+ for _, field := range buyContactFields(purchaseContact()) {
+ values.Set(field.Name, field.Value)
+ }
+ status, _, body = postForm(t, browser, f.ts.URL+"/a/"+f.acct.Sqid+"/apps/kass/domains/"+path, values)
+ }
+ if status != tc.status || !strings.Contains(body, wantMessage) {
+ t.Errorf("%s/%s/%s: status %d, want %d and message %q", transport, door, name, status, tc.status, wantMessage)
+ }
+ if wantBody == "" {
+ wantBody = body
+ }
+ if body != wantBody {
+ t.Errorf("%s/%s/%s: refusal bytes differ", transport, door, name)
+ }
+ }
+ }
+ }
+ if n := purchaseCalls(f.reg.Calls, "check:"); n != tc.checks {
+ t.Errorf("registrar calls %d, want %d", n, tc.checks)
+ }
+ if n := purchaseCalls(f.reg.Calls, "contact:"); n != 0 {
+ t.Errorf("refusal created %d contacts", n)
+ }
+ })
+ }
+ }
+}
+
+type admissionErrorRegistrar struct {
+ registrar.Registrar
+ err error
+}
+
+func (r admissionErrorRegistrar) Check(ctx context.Context, name string) (registrar.Availability, error) {
+ _, _ = r.Registrar.Check(ctx, name)
+ return registrar.Availability{}, r.err
+}
+
+func TestBuyAdmissionSharesCheckBudget(t *testing.T) {
+ f := newPurchaseFixture(t)
+ now := time.Now()
+ f.srv.domainChecks.now = func() time.Time { return now }
+ tok := buyToken(t, f)
+ browser := newBrowser(t)
+ f.setUser("owner" + f.acct.Sqid + "@example.com")
+ signUp(t, f.consoleFixture, browser, "admission-budget")
+ csrf := sessionToken(t, f.consoleFixture, browser)
+ holdBuyName(t, f, "held.com")
+ for i := 0; i < domainCheckAccountBudget+4; i++ {
+ name := "taken.com"
+ if i%2 == 0 {
+ name = "held.com"
+ }
+ var status int
+ switch i % 4 {
+ case 0:
+ status, _ = buyRequest(t, f, "POST", buyAPIPath(f)+"/check", tok, fmt.Sprintf(`{"name":%q}`, name))
+ case 1:
+ status, _ = buyRequest(t, f, "POST", buyAPIPath(f)+"/purchases", tok, strings.ReplaceAll(buyBody(), "shop.com", name))
+ default:
+ door := "check"
+ if i%4 == 3 {
+ door = "buy"
+ }
+ values := url.Values{"csrf": {csrf}, "name": {name}}
+ for _, field := range buyContactFields(purchaseContact()) {
+ values.Set(field.Name, field.Value)
+ }
+ status, _, _ = postForm(t, browser, f.ts.URL+"/a/"+f.acct.Sqid+"/apps/kass/domains/"+door, values)
+ }
+ want := 409
+ if i >= domainCheckAccountBudget {
+ want = 429
+ }
+ if status != want {
+ t.Errorf("request %d: status %d, want %d", i+1, status, want)
+ }
+ }
+ if n := purchaseCalls(f.reg.Calls, "check:"); n != domainCheckAccountBudget {
+ t.Errorf("registrar calls %d, want %d", n, domainCheckAccountBudget)
+ }
+}
+
+func TestBuyAdmissionCatalogRefusals(t *testing.T) {
+ for _, tc := range []struct {
+ name, key string
+ premium bool
+ cost int64
+ status int
+ }{
+ {"premium", "premium", true, 900, 422},
+ {"cost", "unavailable", false, 1001, 503},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ f := newPurchaseFixture(t)
+ f.reg.Available["shop.com"] = registrar.Availability{Available: true, Premium: tc.premium, Cost: registrar.Money{Currency: "usd", Minor: tc.cost}}
+ tok := buyToken(t, f)
+ browser := newBrowser(t)
+ f.setUser("owner" + f.acct.Sqid + "@example.com")
+ signUp(t, f.consoleFixture, browser, "catalog-refusal")
+ values := url.Values{"csrf": {sessionToken(t, f.consoleFixture, browser)}, "name": {"shop.com"}}
+ for _, field := range buyContactFields(purchaseContact()) {
+ values.Set(field.Name, field.Value)
+ }
+ for _, transport := range []string{"cli", "browser"} {
+ var wantBody string
+ for _, door := range []string{"check", "purchases"} {
+ var status int
+ var body string
+ if transport == "cli" {
+ payload := buyBody()
+ if door == "check" {
+ payload = `{"name":"shop.com"}`
+ }
+ status, body = buyRequest(t, f, "POST", buyAPIPath(f)+"/"+door, tok, payload)
+ } else {
+ path := door
+ if path == "purchases" {
+ path = "buy"
+ }
+ status, _, body = postForm(t, browser, f.ts.URL+"/a/"+f.acct.Sqid+"/apps/kass/domains/"+path, values)
+ }
+ if status != tc.status || !strings.Contains(body, domainBuyCopy[tc.key]) {
+ t.Errorf("%s/%s: status %d, want %d and %q", transport, door, status, tc.status, domainBuyCopy[tc.key])
+ }
+ if wantBody == "" {
+ wantBody = body
+ }
+ if body != wantBody {
+ t.Errorf("%s/%s: refusal bytes differ", transport, door)
+ }
+ }
+ }
+ })
+ }
+}