carlos / platform Public

email: door checks every granted domain; caller-send gaps (review P3s)

- The switch door checks every granted domain the mailer would send
  from, not only the declared From's: each one's configuration-set ARN
  must be nameable and, for a tenant sender, its identity and set
  associated with the tenant.
- A named Send whose live check fails falls back to domain attribution,
  which records no caller class: it is now noted as a gap too.
- Tests: the gap is noted on a failed write and on the fallback, and
  flushed as an unrecorded row (kills the sesevents note mutant); the
  door refuses an unverified From domain; CheckFromDomain has its own
  table test in internal/mailwire.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev 0742bce5910c5b2f452623d4738599eb2ebd0c4e parent 4569c7a
5 files changed, +177 −0
  • internal/console/apisubmission.go +25 −0
  • internal/console/apisubmission_test.go +62 −0
  • internal/console/sesevents.go +5 −0
  • internal/console/sesevents_test.go +48 −0
  • internal/mailwire/mailwire_test.go +37 −0

CI failed — run details

diff --git a/internal/console/apisubmission.go b/internal/console/apisubmission.go
index 9501a2ee7..eac3d8176 100644
--- a/internal/console/apisubmission.go
+++ b/internal/console/apisubmission.go
@@ -208,6 +208,31 @@ func (s *Server) submissionPreconditions(ctx context.Context, acct, app string,
return conflict(fmt.Sprintf(submissionCopy.ConfigSetUnbuildable, domain))
}
identities, configSets := []string{st.IdentityARN}, []string{csARN}
+ // Every other granted domain the mailer would send from (review P3):
+ // a From the app changes at run time may name any of them, so each
+ // one's configuration set must be nameable and, below, associated
+ // with the tenant. A domain the mailer would refuse is not checked:
+ // nothing can be sent from it.
+ for _, d := range slices.Sorted(slices.Values(granted)) {
+ d = strings.ToLower(d)
+ if d == domain {
+ continue
+ }
+ odv, err := s.submissionDomainView(ctx, acct, d)
+ if err != nil {
+ log.Printf("console: email submission %s/%s: domain %s: %v", acct, app, d, err)
+ return &doorRefusal{Status: http.StatusBadGateway, Msg: submissionCopy.SwitchFailed}
+ }
+ if mailwire.CheckFromDomain(granted, d, app, region, odv) != mailwire.FromSendable {
+ continue
+ }
+ ost := odv.Regions[region]
+ oARN, ok := mail.ConfigSetARN(ost.IdentityARN, ost.ConfigSet)
+ if !ok {
+ return conflict(fmt.Sprintf(submissionCopy.ConfigSetUnbuildable, d))
+ }
+ identities, configSets = append(identities, ost.IdentityARN), append(configSets, oARN)
+ }
// 2. Every route runs a build that declares the transport, read from
// the boxes' fresh reports; a route no fresh box reports is refused,
diff --git a/internal/console/apisubmission_test.go b/internal/console/apisubmission_test.go
index 2841e3c9f..cfe243ed6 100644
--- a/internal/console/apisubmission_test.go
+++ b/internal/console/apisubmission_test.go
@@ -454,6 +454,10 @@ func TestSubmissionDoorMoreRefusals(t *testing.T) {
refused("an identity ARN no set can be named from", "Can't name the configuration set")
edit(func(raw map[string]any) { raw["apps"] = []string{"sheets"} })
refused("a domain record that does not list the app", "the box's mailer would refuse it")
+ edit(func(raw map[string]any) {
+ raw["regions"].(map[string]any)["eu-west-1"].(map[string]any)["status"] = mail.StatusPending
+ })
+ refused("a domain not verified", "the box's mailer would refuse it")
edit(func(map[string]any) {})
if status, out := w.submit(t, true); status != http.StatusOK {
t.Fatalf("restored: %d %s", status, out)
@@ -509,3 +513,61 @@ func TestReapWaitsOnAnUnrecordedSend(t *testing.T) {
}
}
}
+
+// Task 6 review P3: the mailer sends from any granted domain it accepts,
+// not only the declared From's, so the door checks each one: its
+// configuration set must be nameable and, for a tenant sender, associated
+// with the tenant. A granted domain the mailer would refuse is not checked.
+func TestSubmissionDoorChecksEveryGrantedDomain(t *testing.T) {
+ w := newSubmissionWorld(t, "code-submission-granted")
+ w.promote(t, "v2")
+ w.box(t, "box-1", "v2", true)
+ ctx := context.Background()
+ refused := func(name, want string) {
+ t.Helper()
+ status, out := w.submit(t, true)
+ if status != http.StatusConflict || !strings.Contains(out, want) {
+ t.Fatalf("%s: %d %s, want 409 containing %q", name, status, out, want)
+ }
+ }
+ const other = "news.example.com"
+ putOther := func(identity, configSet, status string) {
+ t.Helper()
+ key := mailwire.DomainKey("bab", other)
+ _, etag, _ := w.fx.rel.Get(ctx, key)
+ body, _ := json.Marshal(mail.DomainRecord{Domain: other, Kind: mail.KindCustom, Apps: []string{"titogo"},
+ Regions: map[string]*mail.RegionState{"eu-west-1": {IdentityARN: identity, ConfigSet: configSet, Status: status}}})
+ if _, err := w.fx.rel.Put(ctx, key, body, etag); err != nil {
+ t.Fatal(err)
+ }
+ }
+ setSenderField(t, w, func(raw map[string]any) {
+ g := raw["grant"].(map[string]any)
+ g["domains"] = append(g["domains"].([]any), other)
+ })
+ // Not verified: the mailer refuses it, so the door does not look.
+ putOther("not-an-arn", "carlos-news", mail.StatusPending)
+ if status, out := w.submit(t, true); status != http.StatusOK {
+ t.Fatalf("an unsendable granted domain: %d %s", status, out)
+ }
+ if status, out := w.submit(t, false); status != http.StatusOK {
+ t.Fatalf("off: %d %s", status, out)
+ }
+ putOther("not-an-arn", "carlos-news", mail.StatusVerified)
+ refused("a sendable granted domain with an unnameable set", "Can't name the configuration set of "+other)
+
+ identity := "arn:aws:ses:eu-west-1:1:identity/" + other
+ putOther(identity, "carlos-news", mail.StatusVerified)
+ setSenderField(t, w, func(raw map[string]any) { raw["tenant_arn"] = "arn:aws:ses:eu-west-1:1:tenant/carlos-bab/x" })
+ refused("a granted domain not associated with the tenant", identity)
+ p := w.fx.srv.cfg.MailProvisioner.(*mail.MemProvisioner)
+ csARN, _ := mail.ConfigSetARN(identity, "carlos-news")
+ for _, arn := range []string{identity, csARN} {
+ if err := p.AssociateTenantResource(ctx, mail.TenantName("bab"), arn); err != nil {
+ t.Fatal(err)
+ }
+ }
+ if status, out := w.submit(t, true); status != http.StatusOK {
+ t.Fatalf("every granted domain associated: %d %s", status, out)
+ }
+}
diff --git a/internal/console/sesevents.go b/internal/console/sesevents.go
index 9b0edd1cf..492fc8a54 100644
--- a/internal/console/sesevents.go
+++ b/internal/console/sesevents.go
@@ -526,6 +526,11 @@ func (s *Server) recordAndEnforceMail(ctx context.Context, ev mail.Event, notice
live, err := s.mailAppLiveErr(callerAcct, callerApp)
if err != nil {
log.Printf("console: ses events: checking %s/%s live: %v; falling back to domain attribution", callerAcct, callerApp, err)
+ // The fallback records no caller class, so this Send is a
+ // day the reap must not read as clean (Task 6 review P3).
+ if ev.Type == mail.EventSend {
+ s.mailCallerGaps.note(callerAcct, callerApp, day)
+ }
} else if !live {
return
} else {
diff --git a/internal/console/sesevents_test.go b/internal/console/sesevents_test.go
index 7715d79d0..3e9394a5a 100644
--- a/internal/console/sesevents_test.go
+++ b/internal/console/sesevents_test.go
@@ -1000,3 +1000,51 @@ func TestCallerSendRecordedWhenTheSenderCountFails(t *testing.T) {
t.Fatalf("caller sends = %v, want app_user:1 though the per-sender count failed", got)
}
}
+
+// Task 6 review P3: a named Send whose caller class cannot be recorded is
+// kept as a gap, whether the write fails or the live check does (the
+// domain fallback records no class), and written as an unrecorded day
+// once a write succeeds.
+func TestCallerSendGapNoted(t *testing.T) {
+ fx := newConsoleFixture(t)
+ ctx := context.Background()
+ now := time.Now()
+ day := now.UTC().Format("2006-01-02")
+ if _, err := fx.st.CreateAppIn(OpsAccountID, "docs", "bill@tito.io", now.Add(-time.Hour)); err != nil {
+ t.Fatal(err)
+ }
+ exec := func(q string) {
+ t.Helper()
+ if _, err := fx.st.db.Exec(q); err != nil {
+ t.Fatal(err)
+ }
+ }
+ send := func(msg string) {
+ t.Helper()
+ ev := mail.Event{Type: mail.EventSend, Domain: "lgc.li", MessageID: msg, At: now, SentAt: now,
+ Caller: mail.UserName(release.OpsAccount, "docs")}
+ fx.srv.recordAndEnforceMail(ctx, ev, "sns-"+msg)
+ }
+
+ exec(`ALTER TABLE mail_caller_sends RENAME TO mail_caller_sends_away`)
+ send("m-write-fails")
+ exec(`ALTER TABLE mail_caller_sends_away RENAME TO mail_caller_sends`)
+ if !fx.srv.mailCallerGaps.pending(release.OpsAccount, "docs", day) {
+ t.Fatal("a Send whose class write failed left no gap")
+ }
+ send("m-next")
+ if fx.srv.mailCallerGaps.pending(release.OpsAccount, "docs", day) {
+ t.Fatal("the next successful write did not flush the gap")
+ }
+ got, err := fx.st.MailCallerSends(ctx, release.OpsAccount, "docs", day)
+ if err != nil || got[mail.CallerClassUnrecorded] != 1 || got[mail.CallerClassAppUser] != 1 {
+ t.Fatalf("caller sends = %v %v, want app_user:1 and unrecorded:1", got, err)
+ }
+
+ exec(`ALTER TABLE apps RENAME TO apps_away`)
+ send("m-live-fails")
+ exec(`ALTER TABLE apps_away RENAME TO apps`)
+ if !fx.srv.mailCallerGaps.pending(release.OpsAccount, "docs", day) {
+ t.Fatal("a Send that fell back to the domain left no gap")
+ }
+}
diff --git a/internal/mailwire/mailwire_test.go b/internal/mailwire/mailwire_test.go
index dc69ff6e2..f28e182c0 100644
--- a/internal/mailwire/mailwire_test.go
+++ b/internal/mailwire/mailwire_test.go
@@ -104,3 +104,40 @@ func TestIsMailerCaller(t *testing.T) {
}
}
}
+
+// CheckFromDomain is the one rule the mailer and the console's switch door
+// both apply to a From domain (Task 6 review P3): each verdict, and the
+// order they are reached in.
+func TestCheckFromDomain(t *testing.T) {
+ const domain, app, region = "news.example.com", "docs", "eu-west-1"
+ good := func() *DomainView {
+ return &DomainView{Domain: domain, Apps: []string{"blog", app}, Regions: map[string]*DomainRegionView{
+ region: {IdentityARN: "arn:aws:ses:eu-west-1:1:identity/" + domain, ConfigSet: "carlos-news", Status: DomainStatusVerified},
+ }}
+ }
+ granted := []string{"other.example.com", domain}
+ for name, c := range map[string]struct {
+ granted []string
+ edit func(*DomainView) *DomainView
+ want FromVerdict
+ }{
+ "sendable": {granted, func(d *DomainView) *DomainView { return d }, FromSendable},
+ "not granted": {[]string{"other.example.com"}, func(d *DomainView) *DomainView { return d }, FromNotGranted},
+ "no record": {granted, func(*DomainView) *DomainView { return nil }, FromNoDomainRecord},
+ "another domain's record": {granted, func(d *DomainView) *DomainView { d.Domain = "other.example.com"; return d }, FromNoDomainRecord},
+ "app not listed": {granted, func(d *DomainView) *DomainView { d.Apps = []string{"blog"}; return d }, FromAppNotListed},
+ "no region": {granted, func(d *DomainView) *DomainView { d.Regions = nil; return d }, FromNotVerified},
+ "pending": {granted, func(d *DomainView) *DomainView { d.Regions[region].Status = "pending"; return d }, FromNotVerified},
+ "no identity": {granted, func(d *DomainView) *DomainView { d.Regions[region].IdentityARN = ""; return d }, FromNotVerified},
+ "no configuration set": {granted, func(d *DomainView) *DomainView { d.Regions[region].ConfigSet = ""; return d }, FromNoConfigSet},
+ "not granted wins": {nil, func(*DomainView) *DomainView { return nil }, FromNotGranted},
+ "unlisted before unverified": {granted, func(d *DomainView) *DomainView {
+ d.Apps, d.Regions[region].Status = nil, "pending"
+ return d
+ }, FromAppNotListed},
+ } {
+ if got := CheckFromDomain(c.granted, domain, app, region, c.edit(good())); got != c.want {
+ t.Errorf("%s: %v, want %v", name, got, c.want)
+ }
+ }
+}