carlos / platform Public

console: materialize the platform link at an account's birth only

MaterializeAccounts now births a manifest that carries a platform link
through EnsureLinkedAccount, writing platform, platform_ref and the
manifest's platform_mint (pre-flight C15). The link must pass
domains.ValidPlatformLink, now exported and shared with
MintLinkedAccount, and must not name the account itself; a link that
fails is logged and the account is born unlinked. A known account is
never linked afterwards, for the same reason the owner is adopted once.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 785331a2619166a0fae4394e1280a2ad2f643301 parent 76e4e00
3 files changed, +223 −3
  • internal/console/materialize.go +25 −2
  • internal/console/materialize_test.go +188 −0
  • internal/domains/mint.go +10 −1
diff --git a/internal/console/materialize.go b/internal/console/materialize.go
index 0068949e9..da5843591 100644
--- a/internal/console/materialize.go
+++ b/internal/console/materialize.go
@@ -140,7 +140,8 @@ func materializeAccounts(ctx context.Context, s release.Store, st *Store, unlink
case a != nil:
// Known and live: settled. INSERT-only, and — the security
// boundary — owner adoption is never revisited for an account the
- // console has seen before.
+ // console has seen before. Nor is the platform link: a manifest
+ // that gains one after birth links nothing.
continue
case m.Deleted():
@@ -160,7 +161,24 @@ func materializeAccounts(ctx context.Context, s release.Store, st *Store, unlink
if created.IsZero() {
created = time.Now()
}
- added, err := st.EnsureAccount(m.ID, m.Sqid, m.Name, created)
+ // A customer account's platform link is adopted here and only here,
+ // under the same check MintLinkedAccount applies before it writes,
+ // plus the one only the account knows: it is not its own platform. A
+ // link that fails is dropped, not the account: the row is born
+ // unlinked and, as customers are minted with no owner, unreachable,
+ // which is the safe side.
+ link := m.Platform != ""
+ if link && (!domains.ValidPlatformLink(m.Platform, m.PlatformMint) || m.Platform == m.Sqid) {
+ log.Printf("console: %s names platform %q with mint id %q, which is not a link it can have; "+
+ "materializing the account unlinked", key, m.Platform, m.PlatformMint)
+ link = false
+ }
+ var added bool
+ if link {
+ added, err = st.EnsureLinkedAccount(m.ID, m.Sqid, m.Name, m.Platform, m.PlatformRef, m.PlatformMint, created)
+ } else {
+ added, err = st.EnsureAccount(m.ID, m.Sqid, m.Name, created)
+ }
if err != nil {
return n, err
}
@@ -239,6 +257,11 @@ func materializeAccounts(ctx context.Context, s release.Store, st *Store, unlink
// reason. The same applies to an account that materialized ownerless from a
// pre-`owner` manifest — it stays ownerless until an operator says otherwise.
//
+// The platform link (platform, platform_ref, platform_mint) follows the same
+// rule for the same reason: it is adopted on the pass that creates the row and
+// never after, so rewriting a live account's manifest cannot hand it to a
+// platform.
+//
// Identity and authority stay SEPARATE decisions, which is why this runs after
// the account row rather than inside EnsureAccount: a manifest with an owner
// keymail could never authenticate still yields a perfectly good account. One
diff --git a/internal/console/materialize_test.go b/internal/console/materialize_test.go
index 09dadf8e7..51248d8e0 100644
--- a/internal/console/materialize_test.go
+++ b/internal/console/materialize_test.go
@@ -4,7 +4,9 @@ import (
"bytes"
"context"
"encoding/json"
+ "errors"
"log"
+ "net/http"
"os"
"strings"
"testing"
@@ -728,3 +730,189 @@ func TestMaterializeWithNoStoreIsANoOp(t *testing.T) {
t.Errorf("MaterializeApps(nil store) = %v, want nil", err)
}
}
+
+// The platform-link tests' fixed sqids: bdf is the customer, bcd its
+// platform. linkMint is a mint id of the shape the console reserves.
+const (
+ linkCustomerID = int64(74) // Sqid(74) == "bdf"
+ linkPlatform = "bcd"
+ linkMint = "0123456789abcdef0123456789abcdef"
+)
+
+// putLinkManifest writes the customer manifest bdf, linked to platform with
+// ref and mint and no owner, over whatever is there.
+func putLinkManifest(t *testing.T, rel release.Store, platform, ref, mint string) {
+ t.Helper()
+ ctx := t.Context()
+ sqid := domains.Sqid(linkCustomerID)
+ if sqid != "bdf" {
+ t.Fatalf("Sqid(%d) = %q, want bdf", linkCustomerID, sqid)
+ }
+ key := domains.AccountManifestKey(sqid)
+ _, etag, err := rel.Get(ctx, key)
+ if err != nil && !errors.Is(err, os.ErrNotExist) {
+ t.Fatal(err)
+ }
+ body, err := json.Marshal(domains.AccountManifest{ID: linkCustomerID, Sqid: sqid, Name: "Shop",
+ Platform: platform, PlatformRef: ref, PlatformMint: mint, CreatedAt: time.Unix(1700000000, 0).UTC()})
+ if err != nil {
+ t.Fatal(err)
+ }
+ if _, err := rel.Put(ctx, key, body, etag); err != nil {
+ t.Fatal(err)
+ }
+}
+
+// accountPlatformOf is AccountPlatform, failing the test on error.
+func accountPlatformOf(t *testing.T, st *Store, id int64) (string, string) {
+ t.Helper()
+ p, ref, err := st.AccountPlatform(t.Context(), id)
+ if err != nil {
+ t.Fatal(err)
+ }
+ return p, ref
+}
+
+// A customer manifest carries its link from the create-only mint, and the
+// console's row is born with it: that is how the link survives a console
+// rebuild (spec §5.2). A customer has no owner, and materialization does
+// not invent one.
+func TestMaterializeCarriesThePlatformLinkAtBirth(t *testing.T) {
+ st := openTestStore(t)
+ rel := release.NewDirStore(t.TempDir())
+ putLinkManifest(t, rel, linkPlatform, "4812", linkMint)
+
+ if n, err := MaterializeAccounts(t.Context(), rel, st); err != nil || n != 1 {
+ t.Fatalf("materialized %d (%v), want 1", n, err)
+ }
+ if p, ref := accountPlatformOf(t, st, linkCustomerID); p != linkPlatform || ref != "4812" {
+ t.Errorf("AccountPlatform(bdf) = %q, %q; want bcd, 4812", p, ref)
+ }
+ if members, _ := st.Members(linkCustomerID); len(members) != 0 {
+ t.Errorf("Members(bdf) = %+v, want none: a customer is born held by its platform", members)
+ }
+}
+
+// The link is adopted at birth only, for the reason the owner is: the
+// manifest is not write-once, so adopting a link on a later pass would let
+// anyone with bucket write access hand a live account to a platform.
+func TestMaterializeNeverLinksAnAccountAfterBirth(t *testing.T) {
+ st := openTestStore(t)
+ rel := release.NewDirStore(t.TempDir())
+ putLinkManifest(t, rel, "", "", "")
+ if n, err := MaterializeAccounts(t.Context(), rel, st); err != nil || n != 1 {
+ t.Fatalf("materialized %d (%v), want 1", n, err)
+ }
+
+ putLinkManifest(t, rel, linkPlatform, "4812", linkMint)
+ if n, err := MaterializeAccounts(t.Context(), rel, st); err != nil || n != 0 {
+ t.Fatalf("second pass materialized %d (%v), want 0", n, err)
+ }
+ if p, ref := accountPlatformOf(t, st, linkCustomerID); p != "" || ref != "" {
+ t.Errorf("AccountPlatform(bdf) = %q, %q after a rewritten manifest; want unlinked", p, ref)
+ }
+ if cs, err := st.LinkedCustomers(t.Context(), linkPlatform); err != nil || len(cs) != 0 {
+ t.Errorf("LinkedCustomers(bcd) = %+v (%v), want none", cs, err)
+ }
+}
+
+// A link MintLinkedAccount would refuse, or one naming the account itself,
+// is not adopted. The account is still born (one bad field must not hide
+// it), unlinked: with no owner that is unreachable, the safe side.
+func TestMaterializeRefusesASelfOrOpsLink(t *testing.T) {
+ cases := []struct{ name, platform, mint string }{
+ {"itself", "bdf", linkMint},
+ {"the ops account", domains.Sqid(domains.ReservedAccountID), linkMint},
+ {"a name, not a sqid", "ops", linkMint},
+ {"not a sqid", "not a sqid!", linkMint},
+ {"no mint id", linkPlatform, ""},
+ {"an uppercase mint id", linkPlatform, strings.ToUpper(linkMint)},
+ }
+ for _, c := range cases {
+ t.Run(c.name, func(t *testing.T) {
+ var logged bytes.Buffer
+ log.SetOutput(&logged)
+ t.Cleanup(func() { log.SetOutput(os.Stderr) })
+ st := openTestStore(t)
+ rel := release.NewDirStore(t.TempDir())
+ putLinkManifest(t, rel, c.platform, "4812", c.mint)
+
+ if n, err := MaterializeAccounts(t.Context(), rel, st); err != nil || n != 1 {
+ t.Fatalf("materialized %d (%v), want the account itself", n, err)
+ }
+ if a, err := st.AccountBySqid("bdf"); err != nil || a == nil {
+ t.Fatalf("AccountBySqid(bdf) = %v %v, want the row", a, err)
+ }
+ if p, ref := accountPlatformOf(t, st, linkCustomerID); p != "" || ref != "" {
+ t.Errorf("AccountPlatform(bdf) = %q, %q; want unlinked", p, ref)
+ }
+ if !strings.Contains(logged.String(), domains.AccountManifestKey("bdf")) {
+ t.Errorf("no log line names the manifest; logged:\n%s", logged.String())
+ }
+ })
+ }
+}
+
+// A customer minted in the bucket whose console row was never written (a
+// create that failed after the mint, or a rebuilt console) is adopted, and
+// its row carries the manifest's mint id, so the platform's pending mint
+// and the account count once (pre-flight C15).
+func TestMaterializeAdoptsAStrandedLinkedMint(t *testing.T) {
+ st := openTestStore(t)
+ rel := release.NewDirStore(t.TempDir())
+ ctx := t.Context()
+ newPlatform(t, st, linkPlatform)
+ reserveMint(t, st, linkPlatform, linkMint, "4812")
+ id, sqid, existing, err := domains.MintLinkedAccount(ctx, rel, "Shop", linkPlatform, "4812", linkMint, platformNow)
+ if err != nil || existing {
+ t.Fatalf("MintLinkedAccount = %d %q %v %v", id, sqid, existing, err)
+ }
+
+ if n, err := MaterializeAccounts(ctx, rel, st); err != nil || n != 1 {
+ t.Fatalf("materialized %d (%v), want 1", n, err)
+ }
+ cs, err := st.LinkedCustomers(ctx, linkPlatform)
+ if err != nil || len(cs) != 1 || cs[0].Sqid != sqid || cs[0].Ref != "4812" {
+ t.Fatalf("LinkedCustomers(bcd) = %+v (%v), want %s with ref 4812", cs, err, sqid)
+ }
+ var mint string
+ if err := st.db.QueryRowContext(ctx, `SELECT platform_mint FROM accounts WHERE id = ?`, id).Scan(&mint); err != nil {
+ t.Fatal(err)
+ }
+ if mint != linkMint {
+ t.Errorf("platform_mint = %q, want the manifest's %q", mint, linkMint)
+ }
+ if n := linkedCount(t, st, linkPlatform); n != 1 {
+ t.Errorf("LinkedCustomerCount(bcd) = %d, want 1: the pending mint is this account", n)
+ }
+}
+
+// Ownerless while linked is a supported state (spec §5.3), and membership is
+// authorization: nobody signed in reaches the customer through the person
+// doors. The platform's own doors are later tasks'.
+func TestLinkedOwnerlessAccountReachesNobody(t *testing.T) {
+ fx := newConsoleFixture(t)
+ putLinkManifest(t, fx.rel, linkPlatform, "4812", linkMint)
+ if _, err := MaterializeAccounts(t.Context(), fx.rel, fx.st); err != nil {
+ t.Fatal(err)
+ }
+ if p, _ := accountPlatformOf(t, fx.st, linkCustomerID); p != linkPlatform {
+ t.Fatalf("AccountPlatform(bdf) = %q, want bcd", p)
+ }
+ browser := signInPaul(t, fx)
+ for _, addr := range []string{"paul@keymail.dev", "owner-bcd@example.com", "someone@example.com"} {
+ accts, err := fx.st.AccountsFor(addr)
+ if err != nil {
+ t.Fatal(err)
+ }
+ for _, a := range accts {
+ if a.ID == linkCustomerID {
+ t.Errorf("AccountsFor(%s) includes the ownerless customer bdf", addr)
+ }
+ }
+ }
+ status, _, body := get(t, browser, fx.ts.URL+"/a/bdf/")
+ if status != http.StatusNotFound || body != "404 page not found\n" {
+ t.Errorf("GET /a/bdf/ signed in = %d %q, want the stdlib 404", status, body)
+ }
+}
diff --git a/internal/domains/mint.go b/internal/domains/mint.go
index a913f7282..b03006ef3 100644
--- a/internal/domains/mint.go
+++ b/internal/domains/mint.go
@@ -280,7 +280,7 @@ var ErrBadPlatformLink = errors.New("domains: bad platform link")
// The link is checked before the first listing, so a refused mint writes
// nothing. The ref is the console's to validate, not this function's.
func MintLinkedAccount(ctx context.Context, s release.Store, name, platformSqid, ref, mintID string, created time.Time) (id int64, sqid string, existing bool, err error) {
- if !ValidSqid(platformSqid) || platformSqid == Sqid(ReservedAccountID) || !validMintID(mintID) {
+ if !ValidPlatformLink(platformSqid, mintID) {
return 0, "", false, fmt.Errorf("%w: platform %q, mint id %q", ErrBadPlatformLink, platformSqid, mintID)
}
template := AccountManifest{Name: name, Platform: platformSqid, PlatformRef: ref, PlatformMint: mintID}
@@ -289,6 +289,15 @@ func MintLinkedAccount(ctx context.Context, s release.Store, name, platformSqid,
})
}
+// ValidPlatformLink is the one check on a customer account's link, shared by
+// MintLinkedAccount (before it writes) and console materialization (before it
+// adopts a manifest's link at birth): the platform is an account sqid and not
+// the ops account's, and the mint id is 32 lowercase hex. Whether the platform
+// is the linked account itself is the caller's to ask: only it knows the sqid.
+func ValidPlatformLink(platformSqid, mintID string) bool {
+ return ValidSqid(platformSqid) && platformSqid != Sqid(ReservedAccountID) && validMintID(mintID)
+}
+
// validMintID reports whether id is 32 lowercase hex digits, the shape the
// console's pending-mint ids take (16 random bytes, hex encoded).
func validMintID(id string) bool {