carlos / platform Public

console: per-customer deploy credentials end with their app

A per-customer Deploy credential is bound to its app by (account, name),
so it must never reach a later app given that name. App purge (forgetAppTx
and the claim purge) now deletes kind platform rows with the app and
service rows. Rename and move revoke them at the source name and at the
destination name (revokeCustomerCredentialsAt) instead of carrying them,
so a predecessor's leftover is never inherited either. The resolver also
requires a Deploy credential's app to be live, so a trashed app's
credential does not resolve.

The rung refusals are one helper (rungMintRefusal) shared by the services
door and the platform credential door. The never-reaches test probes the
email credentials door, and the paused-mint test demotes the person minter
to member mid-mint, so the membership read is the check that refuses.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev cc34dd4becef00ecef639c82c27b4661f0f4d842 parent 2011092
6 files changed, +201 −21
  • internal/console/apiplatformcredentials.go +5 −7
  • internal/console/apiservices.go +19 −7
  • internal/console/claim_app_identity.go +3 −1
  • internal/console/platform_credentials.go +28 −0
  • internal/console/platform_credentials_test.go +133 −0
  • internal/console/store.go +13 −6
diff --git a/internal/console/apiplatformcredentials.go b/internal/console/apiplatformcredentials.go
index 697321ff9..6991bdd75 100644
--- a/internal/console/apiplatformcredentials.go
+++ b/internal/console/apiplatformcredentials.go
@@ -142,19 +142,17 @@ func platformCredentialTTL(seconds int64) (time.Duration, bool) {
}
// platformCredentialRefused answers a refused or failed mint. The rung
-// refusals are the services door's words, byte for byte (apiservices.go),
-// so one rule reads the same at both mint doors.
+// refusals are the services door's (rungMintRefusal, apiservices.go).
func (s *Server) platformCredentialRefused(w http.ResponseWriter, r *http.Request, c platformCaller, err error) {
- var ue *UnknownRungError
+ if msg, ok := rungMintRefusal(err); ok {
+ jsonError(w, http.StatusBadRequest, msg)
+ return
+ }
switch {
case errors.Is(err, ErrPlatformCredentialRole):
jsonError(w, http.StatusBadRequest, platformCopy["cred.role"])
case errors.Is(err, ErrPlatformCredentialTTL):
jsonError(w, http.StatusBadRequest, platformCopy["cred.ttl"])
- case errors.Is(err, ErrStableNotDelegable):
- jsonError(w, http.StatusBadRequest, "stable cannot be delegated: moving it needs a passkey in the console")
- case errors.As(err, &ue):
- jsonError(w, http.StatusBadRequest, fmt.Sprintf("unknown rung %q: one of canary, edge, beta", ue.Rung))
case errors.Is(err, ErrPlatformAuthorityEnded):
jsonError(w, http.StatusForbidden, platformCopy["door.revoked"])
case errors.Is(err, errPlatformCredentialNoApp):
diff --git a/internal/console/apiservices.go b/internal/console/apiservices.go
index e0c017830..4cfe46d70 100644
--- a/internal/console/apiservices.go
+++ b/internal/console/apiservices.go
@@ -267,7 +267,10 @@ func (s *Server) handleAPIServiceCreate(w http.ResponseWriter, r *http.Request)
"and an account-wide one would reach every app in the account")
return
}
- var ue *UnknownRungError
+ if msg, ok := rungMintRefusal(err); ok {
+ jsonError(w, http.StatusBadRequest, msg)
+ return
+ }
switch {
case errors.Is(err, ErrAppScopedPlatform):
jsonError(w, http.StatusBadRequest, platformCopy["svc.platform_app"])
@@ -283,12 +286,6 @@ func (s *Server) handleAPIServiceCreate(w http.ResponseWriter, r *http.Request)
case errors.Is(err, ErrDeployNeedsRungs):
jsonError(w, http.StatusBadRequest, "a deploy credential must name its rungs (--rungs): canary, edge or beta")
return
- case errors.Is(err, ErrStableNotDelegable):
- jsonError(w, http.StatusBadRequest, "stable cannot be delegated: moving it needs a passkey in the console")
- return
- case errors.As(err, &ue):
- jsonError(w, http.StatusBadRequest, fmt.Sprintf("unknown rung %q: one of canary, edge, beta", ue.Rung))
- return
case errors.Is(err, ErrRungsNeedDeploy):
jsonError(w, http.StatusBadRequest, "only a deploy credential takes rungs")
return
@@ -376,6 +373,21 @@ func teamMintFlagHint(err error) string {
return ""
}
+// rungMintRefusal is the words for a deploy credential's rung refusals,
+// shared by the services door and the platform's per-customer credential
+// door (apiplatformcredentials.go), so one rule reads the same at both
+// mint doors. ok is false for an error that is not one of them.
+func rungMintRefusal(err error) (msg string, ok bool) {
+ var ue *UnknownRungError
+ switch {
+ case errors.Is(err, ErrStableNotDelegable):
+ return "stable cannot be delegated: moving it needs a passkey in the console", true
+ case errors.As(err, &ue):
+ return fmt.Sprintf("unknown rung %q: one of canary, edge, beta", ue.Rung), true
+ }
+ return "", false
+}
+
// runMintRefusal is the words for a run credential's mint refusals, shared
// by both mint doors so they cannot describe one rule two ways. ok is false
// for an error that is not one of them. The API door adds the flag to fix
diff --git a/internal/console/claim_app_identity.go b/internal/console/claim_app_identity.go
index fa5dc6fda..4130fe26a 100644
--- a/internal/console/claim_app_identity.go
+++ b/internal/console/claim_app_identity.go
@@ -179,7 +179,9 @@ func (s *Store) PurgeClaimAppRow(ctx context.Context, accountID int64, app, appI
if err = forgetMailApp(ctx, tx, sqid, app); err != nil {
return err
}
- if _, err = tx.ExecContext(ctx, `DELETE FROM cli_tokens WHERE kind IN (?,?) AND account_id=? AND app=?`, cliKindApp, cliKindService, accountID, app); err != nil {
+ // App, service and per-customer platform credentials alike: a later
+ // app of this name must not inherit any of them (store.go forgetAppTx).
+ if _, err = tx.ExecContext(ctx, `DELETE FROM cli_tokens WHERE kind IN (?,?,?) AND account_id=? AND app=?`, cliKindApp, cliKindService, cliKindPlatform, accountID, app); err != nil {
return err
}
if err = revokeClaimDoorKeys(tx, accountID, app); err != nil {
diff --git a/internal/console/platform_credentials.go b/internal/console/platform_credentials.go
index ce2323ad0..983da29e2 100644
--- a/internal/console/platform_credentials.go
+++ b/internal/console/platform_credentials.go
@@ -275,7 +275,35 @@ func (s *Store) platformCustomerPrincipal(ctx context.Context, addr string, acco
if !live {
return Principal{}, false
}
+ // A Deploy credential's app must be live too (defence in depth: app
+ // delete, rename and move already end it, revokeCustomerCredentialsAt
+ // and forgetAppTx). A trashed app's credential does not resolve.
+ if r == svcrole.Deploy {
+ if held, err := s.HasAppInContext(ctx, accountID, app); err != nil || !held {
+ return Principal{}, false
+ }
+ }
return Principal{Kind: PrincipalService, Address: addr, AccountID: accountID, App: app, Role: r, Name: name,
Rungs: stored, CredentialID: credID, PlatformAccountID: platformAccountID, Platform: sqid,
PlatformGeneration: generation}, true
}
+
+// revokeCustomerCredentialsAt revokes, in the caller's transaction, every
+// live per-customer credential (kind platform) bound to one of names. A
+// per-customer Deploy credential is bound to an app by its (account, name),
+// and it never follows an app to a new name or account: an app rename or
+// move revokes those at the source name, and those at the destination
+// name, where a live one can only be a predecessor's leftover that the
+// arriving app would otherwise answer to (discussion #219, item 1). The
+// holder asks the platform for a new one. Revoking only raises revoked_at,
+// which the claim gate allows on a platform row (store_gate.go RevokeFree).
+// The callers take no ctx (pre-flight C11).
+func revokeCustomerCredentialsAt(tx *sql.Tx, now time.Time, names ...appNameKey) error {
+ for _, n := range names {
+ if _, err := tx.ExecContext(context.Background(), `UPDATE cli_tokens SET revoked_at = ?
+ WHERE kind = ? AND account_id = ? AND app = ? AND revoked_at = 0`, now.Unix(), cliKindPlatform, n.accountID, n.app); err != nil {
+ return err
+ }
+ }
+ return nil
+}
diff --git a/internal/console/platform_credentials_test.go b/internal/console/platform_credentials_test.go
index 199f46e98..aecbe8c05 100644
--- a/internal/console/platform_credentials_test.go
+++ b/internal/console/platform_credentials_test.go
@@ -656,6 +656,28 @@ func TestCustomerCredentialMintPausedAcrossRevokeAndRegrant(t *testing.T) {
status, out, _ = pausedMint(t, fx, set, p.Sqid, cust.Sqid, owner, revokeAndRegrant)
wantNone("person owner across a revoke and re-grant", status, out)
+ // A person owner who stops being an owner while the mint waits: the
+ // grant and its generation are untouched, so the membership read in the
+ // insert transaction is the check that refuses (Fable minor 4). A
+ // second owner first, as the last owner cannot be demoted.
+ second := "second" + p.Sqid + "@example.com"
+ if err := fx.st.AddMember(p.ID, second, RoleOwner, time.Now()); err != nil {
+ t.Fatal(err)
+ }
+ g, err := fx.st.PlatformGrant(ctx, p.ID)
+ if err != nil || g == nil {
+ t.Fatalf("grant %+v %v", g, err)
+ }
+ status, out, _ = pausedMint(t, fx, set, p.Sqid, cust.Sqid, owner, func() {
+ if err := fx.st.AddMember(p.ID, "owner"+p.Sqid+"@example.com", RoleMember, time.Now()); err != nil {
+ t.Error(err)
+ }
+ })
+ wantNone("person demoted to member during the mint", status, out)
+ if after, err := fx.st.PlatformGrant(ctx, p.ID); err != nil || after.Generation != g.Generation || after.State != PlatformGrantActive {
+ t.Fatalf("grant after the demotion %+v %v, want generation %d still active", after, err, g.Generation)
+ }
+
// Only the minter's own credential revoked: the grant is untouched.
tok3 := platformToken(t, fx, p.ID, "cms-3")
status, out, _ = pausedMint(t, fx, set, p.Sqid, cust.Sqid, tok3, func() {
@@ -732,6 +754,7 @@ func TestCustomerCredentialNeverReachesPersonOrMintDoors(t *testing.T) {
{http.MethodGet, "/api/cli/services/" + cust.Sqid, ""},
{http.MethodPost, "/api/cli/apps/" + cust.Sqid + "/site/instance-token", `{}`},
{http.MethodPost, "/api/cli/apps/" + cust.Sqid + "/site/door-keys", `{}`},
+ {http.MethodPost, "/api/cli/apps/" + cust.Sqid + "/site/email/credentials", `{"name":"x"}`},
{http.MethodPost, "/api/cli/accounts/" + cust.Sqid + "/plan", `{"plan":"personal"}`},
{http.MethodPost, "/api/cli/accounts/" + cust.Sqid + "/domains/purchases", `{}`},
{http.MethodPost, "/api/cli/platform/" + cust.Sqid + "/customers", `{"name":"x"}`},
@@ -978,3 +1001,113 @@ func TestCustomerCredentialUnderTheGateIsSweptByARevoke(t *testing.T) {
t.Fatalf("swept customer credential resolved to %+v", pr)
}
}
+
+// deployOnSite is customerWorld with an app site in shop and a Deploy
+// per-customer credential on it, which reads site's channels.
+func deployOnSite(t *testing.T) (*consoleFixture, *Account, string, *Account, string) {
+ t.Helper()
+ fx, p, tok, cust := customerWorld(t, "free")
+ if _, err := fx.st.CreateAppIn(cust.ID, "site", "service:"+p.Sqid+"/cms", time.Now()); err != nil {
+ t.Fatal(err)
+ }
+ raw := tokenIn(t, customerCredential(t, fx, p.Sqid, cust.Sqid, tok,
+ map[string]any{"role": "deploy", "app": "site", "rungs": []string{"edge"}}))
+ if status := channelsStatus(t, fx, cust.Sqid, "site", raw); status != http.StatusOK {
+ t.Fatalf("deploy credential at site's channels = %d, want 200", status)
+ }
+ return fx, p, tok, cust, raw
+}
+
+func channelsStatus(t *testing.T, fx *consoleFixture, account, app, bearer string) int {
+ t.Helper()
+ status, _, _ := rawReq(t, http.MethodGet, fx.ts.URL+"/api/cli/apps/"+account+"/"+app+"/channels", bearer, "")
+ return status
+}
+
+// wantEnded fails unless the old Deploy credential is refused at app's
+// channels in account: the unknown token's 401, or the family's 404.
+func wantEnded(t *testing.T, fx *consoleFixture, what, account, app, raw string) {
+ t.Helper()
+ if status := channelsStatus(t, fx, account, app, raw); status != http.StatusUnauthorized && status != http.StatusNotFound {
+ t.Fatalf("%s: old deploy credential at %s/%s = %d, want 401 or 404", what, account, app, status)
+ }
+ if pr, ok := fx.st.CLIPrincipal(raw, time.Now()); ok {
+ t.Fatalf("%s: old deploy credential still resolves to %+v", what, pr)
+ }
+}
+
+// TestCustomerDeployCredentialEndsWithItsApp (Task 10 fix round 1): a
+// per-customer Deploy credential is bound to its app by name, and it never
+// reaches a later app given that name, in that account or another.
+func TestCustomerDeployCredentialEndsWithItsApp(t *testing.T) {
+ t.Run("delete and recreate", func(t *testing.T) {
+ fx, p, _, cust, raw := deployOnSite(t)
+ if err := fx.st.TrashAppRow(cust.ID, "site", time.Now()); err != nil {
+ t.Fatal(err)
+ }
+ if err := fx.st.PurgeAppRow(cust.ID, "site"); err != nil {
+ t.Fatal(err)
+ }
+ if n := customerCredentialRows(t, fx.st, cust.ID); n != 0 {
+ t.Fatalf("purging site left %d kind platform rows", n)
+ }
+ if _, err := fx.st.CreateAppIn(cust.ID, "site", "service:"+p.Sqid+"/cms", time.Now()); err != nil {
+ t.Fatal(err)
+ }
+ wantEnded(t, fx, "after delete and recreate", cust.Sqid, "site", raw)
+ })
+ t.Run("rename away and rename another in", func(t *testing.T) {
+ fx, p, _, cust, raw := deployOnSite(t)
+ // A live per-customer row left at the name another app is renamed
+ // to (a predecessor's leftover) is revoked too, never inherited.
+ insertTokenRow(t, fx.st, "leftover", cliKindPlatform, cust.ID, "next", string(svcrole.Deploy),
+ time.Now().Add(time.Hour).Unix(), p.ID, 1)
+ if _, err := fx.st.db.ExecContext(context.Background(), `UPDATE cli_tokens SET rungs = 'edge' WHERE token_hash = ?`,
+ hashToken("leftover")); err != nil {
+ t.Fatal(err)
+ }
+ for _, app := range []string{"other", "spare"} {
+ if _, err := fx.st.CreateAppIn(cust.ID, app, "service:"+p.Sqid+"/cms", time.Now()); err != nil {
+ t.Fatal(err)
+ }
+ }
+ if err := fx.st.RenameAppIn(cust.ID, "site", "old-site"); err != nil {
+ t.Fatal(err)
+ }
+ wantEnded(t, fx, "after the rename away", cust.Sqid, "old-site", raw)
+ if err := fx.st.RenameAppIn(cust.ID, "other", "site"); err != nil {
+ t.Fatal(err)
+ }
+ wantEnded(t, fx, "after another app took the name", cust.Sqid, "site", raw)
+ if err := fx.st.RenameAppIn(cust.ID, "spare", "next"); err != nil {
+ t.Fatal(err)
+ }
+ if at := tokenRevokedAt(t, fx.st, hashToken("leftover")); at == 0 {
+ t.Fatal("the leftover credential at the destination name is still live after the rename")
+ }
+ })
+ t.Run("move", func(t *testing.T) {
+ fx, p, tok, cust, raw := deployOnSite(t)
+ c2 := secondCustomer(t, fx, p, tok)
+ if err := fx.st.MoveApp("site", cust.ID, c2.ID); err != nil {
+ t.Fatal(err)
+ }
+ wantEnded(t, fx, "after the move, at the destination", c2.Sqid, "site", raw)
+ if _, err := fx.st.CreateAppIn(cust.ID, "site", "service:"+p.Sqid+"/cms", time.Now()); err != nil {
+ t.Fatal(err)
+ }
+ wantEnded(t, fx, "after the move, at a new site in the source", cust.Sqid, "site", raw)
+ })
+ t.Run("trashed", func(t *testing.T) {
+ // Defence in depth: a trashed app's credential does not resolve,
+ // whatever its row says.
+ fx, _, _, cust, raw := deployOnSite(t)
+ if err := fx.st.TrashAppRow(cust.ID, "site", time.Now()); err != nil {
+ t.Fatal(err)
+ }
+ if at := tokenRevokedAt(t, fx.st, hashToken(raw)); at != 0 {
+ t.Fatalf("trash revoked the row (revoked_at %d); the resolver's app check is the witness", at)
+ }
+ wantEnded(t, fx, "while site is trashed", cust.Sqid, "site", raw)
+ })
+}
diff --git a/internal/console/store.go b/internal/console/store.go
index b01611f9d..f5f082924 100644
--- a/internal/console/store.go
+++ b/internal/console/store.go
@@ -2318,15 +2318,16 @@ func forgetAppTx(ctx context.Context, tx *sql.Tx, accountID int64, app string) e
return err
}
// The same for the app's credentials (discussion #219, item 1). App
- // tokens of both kinds are bound (account, name), so a later app of
- // this name would inherit them unminted; they are DELETED rather than
- // revoked, because RenameAppIn and MoveApp refuse a name that still
- // carries any such row, revoked or not. Door keys are revoked rather
+ // tokens of both kinds, and a platform's per-customer Deploy
+ // credentials (kind platform), are bound (account, name), so a later
+ // app of this name would inherit them unminted; they are DELETED rather
+ // than revoked, because RenameAppIn and MoveApp refuse a name that still
+ // carries any app or service row, revoked or not. Door keys are revoked rather
// than deleted: the revision has to keep climbing past what boxes
// already admitted, and the re-sign pass publishes the now-empty
// record (apidoorkeys.go).
- if _, err := tx.ExecContext(ctx, `DELETE FROM cli_tokens WHERE kind IN (?, ?) AND account_id = ? AND app = ?`,
- cliKindApp, cliKindService, accountID, app); err != nil {
+ if _, err := tx.ExecContext(ctx, `DELETE FROM cli_tokens WHERE kind IN (?, ?, ?) AND account_id = ? AND app = ?`,
+ cliKindApp, cliKindService, cliKindPlatform, accountID, app); err != nil {
return err
}
if err := revokeAllDoorKeys(tx, accountID, app); err != nil {
@@ -2647,6 +2648,9 @@ LIMIT 1`, cliKindService, to, cliKindService, from, app).Scan(&collidingName)
to, cliKindApp, cliKindService, from, app); err != nil {
return err
}
+ if err := revokeCustomerCredentialsAt(tx, time.Now(), appNameKey{from, app}, appNameKey{to, app}); err != nil {
+ return err
+ }
// Door keys cannot follow the app: each key names its account's sqid
// (doorkey.Token), and the edge admits it only for routes of that account,
// so the moved app's processes need new keys either way. What the move
@@ -2775,6 +2779,9 @@ func (s *Store) renameAppIn(accountID int64, from, to string, undo bool) error {
to, cliKindApp, cliKindService, accountID, from); err != nil {
return err
}
+ if err := revokeCustomerCredentialsAt(tx, time.Now(), appNameKey{accountID, from}, appNameKey{accountID, to}); err != nil {
+ return err
+ }
var sqid string
if err := tx.QueryRow(`SELECT sqid FROM accounts WHERE id = ?`, accountID).Scan(&sqid); err != nil {
return err