carlos / platform Public

console: a service mint re-reads its minter's owner role (H4 r2)

The named-service insert (both doors) now reads the minter's membership
in its own transaction and refuses anyone no longer an owner: an owner
removed or demoted while a create request was in flight mints nothing.
The API answers the family's 404 (removed) or the owner gate's 403
(demoted); the browser door answers the owner gate's 404. Test fixtures
mint as an owner of their account.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 5ee201336aa89fcb7d25e4f2735ac13abf264ab8 parent 8250a47
11 files changed, +121 −3
  • internal/console/apibackups_test.go +1 −0
  • internal/console/apiservices.go +10 −0
  • internal/console/apiservices_test.go +2 −1
  • internal/console/deploycred_test.go +3 −0
  • internal/console/memberrevoke_test.go +47 −0
  • internal/console/planmembers_test.go +1 −1
  • internal/console/svcfixture_test.go +18 −0
  • internal/console/svcpage.go +6 −0
  • internal/console/svcprincipal.go +29 −0
  • internal/console/svcprincipal_run_test.go +1 −1
  • internal/console/svcprincipal_test.go +3 −0
diff --git a/internal/console/apibackups_test.go b/internal/console/apibackups_test.go
index 2a8966fc1..189d72971 100644
--- a/internal/console/apibackups_test.go
+++ b/internal/console/apibackups_test.go
@@ -159,6 +159,7 @@ func backupsToken(t *testing.T, st *Store, accountID int64, name, app string, ro
default:
return serviceToken(t, st, accountID, name, app, role)
}
+ mustMintingOwner(t, st, accountID, "paul@example.test")
tok, err := st.NewServicePrincipalScoped(accountID, name, app, role, rungs, commands, "paul@example.test", time.Now())
if err != nil {
t.Fatalf("mint %s %q: %v", role, name, err)
diff --git a/internal/console/apiservices.go b/internal/console/apiservices.go
index 011ac09a2..4cb832e53 100644
--- a/internal/console/apiservices.go
+++ b/internal/console/apiservices.go
@@ -292,6 +292,16 @@ func (s *Server) handleAPIServiceCreate(w http.ResponseWriter, r *http.Request)
// refused by the grammar rather than quietly repaired (teamscope.go).
secret, err := s.store.NewServicePrincipalWithScopeContext(r.Context(), acct.ID, name, app, role, req.Rungs, req.Commands, req.Scope, actor, time.Now())
if err != nil {
+ // Removed, or stepped down, while the request was in flight: the
+ // family's 404 and the owner gate's 403, as a fresh request gets.
+ if errors.Is(err, errMinterNotMember) {
+ http.NotFound(w, r)
+ return
+ }
+ if errors.Is(err, errMinterNotOwner) {
+ jsonError(w, http.StatusForbidden, "only an account owner can manage service credentials")
+ return
+ }
// The sentinel, not a string match: a name collision is the
// caller's to fix and a busy database is not, and the two must not
// answer alike.
diff --git a/internal/console/apiservices_test.go b/internal/console/apiservices_test.go
index eadb5b618..e7918c151 100644
--- a/internal/console/apiservices_test.go
+++ b/internal/console/apiservices_test.go
@@ -835,7 +835,8 @@ func TestServiceCredentialCannotBeAccountScoped(t *testing.T) {
t.Fatalf("account-scoped Service credential = %v, want ErrAccountScopedService", err)
}
- // Bound to an app, it mints.
+ // Bound to an app, it mints (as an owner: H4-M1 review round 2).
+ mustMintingOwner(t, fx.st, OpsAccountID, "op@keymail.dev")
if _, err := fx.st.NewServicePrincipal(OpsAccountID, "svc-app", "kass", svcrole.Service, "op@keymail.dev", now); err != nil {
t.Fatalf("app-scoped Service credential: %v", err)
}
diff --git a/internal/console/deploycred_test.go b/internal/console/deploycred_test.go
index 1e0079c84..ae1524418 100644
--- a/internal/console/deploycred_test.go
+++ b/internal/console/deploycred_test.go
@@ -150,5 +150,8 @@ func newDeployStore(t *testing.T) *Store {
t.Fatal(err)
}
t.Cleanup(func() { st.Close() })
+ // The tests here mint into account 1 as serviceMinter, and a service
+ // mint re-reads its minter's role (H4-M1 review round 2).
+ mustMintingOwner(t, st, 1, serviceMinter)
return st
}
diff --git a/internal/console/memberrevoke_test.go b/internal/console/memberrevoke_test.go
index 17597c218..b0f562201 100644
--- a/internal/console/memberrevoke_test.go
+++ b/internal/console/memberrevoke_test.go
@@ -318,3 +318,50 @@ func TestDemotedOwnersServiceLosesOwnerDoors(t *testing.T) {
t.Fatalf("a demoted owner's service at an owner door = %d %s, want 403", code, out)
}
}
+
+// TestServiceMintInFlightDuringRemovalLandsNothing (review round 2, Astra
+// P1-1 / Fable N1): an owner's `carlos services create` that passed the
+// owner gate, then waited on its body while the owner was removed (or
+// stepped down to member), writes no credential. The insert re-reads the
+// minter's role in its own transaction.
+func TestServiceMintInFlightDuringRemovalLandsNothing(t *testing.T) {
+ for _, change := range []string{"removed", "demoted"} {
+ t.Run(change, func(t *testing.T) {
+ fx, _ := instanceFixture(t, "code-svc-race-"+change)
+ const alice = "alice@keymail.dev"
+ if err := fx.st.AddMember(OpsAccountID, alice, RoleOwner, time.Now()); err != nil {
+ t.Fatal(err)
+ }
+ bearer := mintUserToken(t, fx.st, alice, time.Now())
+ changed := false
+ body := &pausedBody{body: strings.NewReader(`{"name":"ci","role":"publish","app":"titogo"}`), pause: func() {
+ var err error
+ if change == "removed" {
+ err = fx.st.RemoveMember(OpsAccountID, alice)
+ } else {
+ err = fx.st.AddMember(OpsAccountID, alice, RoleMember, time.Now())
+ }
+ if err != nil {
+ t.Error(err)
+ }
+ changed = true
+ }}
+ req := httptest.NewRequest(http.MethodPost, "/api/cli/services/bab", body)
+ req.Header.Set("Authorization", "Bearer "+bearer)
+ req.Header.Set("Content-Type", "application/json")
+ rec := httptest.NewRecorder()
+ fx.srv.ServeHTTP(rec, req)
+ if !changed {
+ t.Fatalf("the handler answered %d %s before reading the body; the race was not staged", rec.Code, rec.Body)
+ }
+ if rec.Code == http.StatusCreated {
+ t.Fatalf("service mint after the minter was %s = %d %s, want refusal", change, rec.Code, rec.Body)
+ }
+ var n int
+ if err := fx.st.db.QueryRow(`SELECT COUNT(*) FROM cli_tokens WHERE kind = 'service' AND address = ? AND revoked_at = 0`,
+ alice).Scan(&n); err != nil || n != 0 {
+ t.Fatalf("live service credentials minted by the %s owner = %d (%v), want 0", change, n, err)
+ }
+ })
+ }
+}
diff --git a/internal/console/planmembers_test.go b/internal/console/planmembers_test.go
index 14fb8d94d..1d84628bb 100644
--- a/internal/console/planmembers_test.go
+++ b/internal/console/planmembers_test.go
@@ -322,7 +322,7 @@ func TestMemberCountExcludesInvitesAndServices(t *testing.T) {
if _, err := fx.st.CreateInvite(acct.ID, "invited@example.com", "owner@example.com", planNow); err != nil {
t.Fatal(err)
}
- if _, err := fx.st.NewServicePrincipal(acct.ID, "svc", "", svcrole.Publish, "owner@example.com", planNow); err != nil {
+ if _, err := fx.st.NewServicePrincipal(acct.ID, "svc", "", svcrole.Publish, "owner"+acct.Sqid+"@example.com", planNow); err != nil {
t.Fatal(err)
}
if n := memberCount(t, fx, acct.ID); n != 2 {
diff --git a/internal/console/svcfixture_test.go b/internal/console/svcfixture_test.go
index c8a8ac9c2..89b014096 100644
--- a/internal/console/svcfixture_test.go
+++ b/internal/console/svcfixture_test.go
@@ -19,6 +19,7 @@ func serviceToken(t *testing.T, st *Store, accountID int64, name, app string, ro
// scope is "alta", which teamToken takes a caller's choice of.
return teamToken(t, st, accountID, name, app, "alta")
}
+ mustMintingOwner(t, st, accountID, "paul@example.test")
raw, err := st.NewServicePrincipal(accountID, name, app, role, "paul@example.test", time.Now())
if err != nil {
t.Fatalf("NewServicePrincipal(%q): %v", name, err)
@@ -30,6 +31,7 @@ func serviceToken(t *testing.T, st *Store, accountID int64, name, app string, ro
// its bearer: serviceToken for the one role that takes a scope.
func teamToken(t *testing.T, st *Store, accountID int64, name, app, scope string) string {
t.Helper()
+ mustMintingOwner(t, st, accountID, "paul@example.test")
raw, err := st.NewServicePrincipalWithScopeContext(context.Background(), accountID, name, app, svcrole.Team, nil, nil, scope, "paul@example.test", time.Now())
if err != nil {
t.Fatalf("NewServicePrincipalWithScope(%q): %v", name, err)
@@ -46,3 +48,19 @@ func ownerMintsServices(t *testing.T, fx *consoleFixture) {
t.Helper()
makeOwner(t, fx, "paul@example.test")
}
+
+// mustMintingOwner makes address an owner of accountID when it holds no
+// membership there: a service credential mint re-reads its minter's role
+// and refuses anyone but an owner (security hunt H4-M1, review round 2).
+// A member is left a member, so a test that mints as one meets the refusal.
+func mustMintingOwner(t *testing.T, s *Store, accountID int64, address string) {
+ t.Helper()
+ if role, err := s.Role(accountID, address); err != nil {
+ t.Fatal(err)
+ } else if role != "" {
+ return
+ }
+ if err := s.AddMember(accountID, address, RoleOwner, time.Now()); err != nil {
+ t.Fatal(err)
+ }
+}
diff --git a/internal/console/svcpage.go b/internal/console/svcpage.go
index 34f30f91e..481903607 100644
--- a/internal/console/svcpage.go
+++ b/internal/console/svcpage.go
@@ -373,6 +373,12 @@ func (s *Server) handleServiceCreatePage(w http.ResponseWriter, r *http.Request)
}
secret, err := s.store.NewServicePrincipalWithScopeContext(r.Context(), acct.ID, name, app, role, r.Form["rungs"], commands, teamScope, addr, time.Now())
if err != nil {
+ // Removed, or stepped down, while the request was in flight: the
+ // owner gate's 404, as a fresh request gets.
+ if errors.Is(err, errMinterNotMember) || errors.Is(err, errMinterNotOwner) {
+ http.NotFound(w, r)
+ return
+ }
// The sentinel, not a string match: a name collision is the owner's
// to fix and a busy database is not.
if errors.Is(err, ErrServiceNameTaken) {
diff --git a/internal/console/svcprincipal.go b/internal/console/svcprincipal.go
index 7730f6ec9..6dd9e4ef2 100644
--- a/internal/console/svcprincipal.go
+++ b/internal/console/svcprincipal.go
@@ -376,6 +376,28 @@ func (s *Store) claimRawNewServicePrincipal(accountID int64, name, app string, r
return s.NewServicePrincipalScoped(accountID, name, app, role, nil, nil, mintedBy, now)
}
+// minterIsOwnerTx is the service mint's re-check of its minter, inside
+// tx: errMinterNotMember when address holds no membership in accountID,
+// errMinterNotOwner when it holds one that is not owner.
+func minterIsOwnerTx(tx *sql.Tx, accountID int64, address string) error {
+ var role string
+ err := tx.QueryRow(`SELECT role FROM memberships WHERE account_id = ? AND address = ?`, accountID, address).Scan(&role)
+ if errors.Is(err, sql.ErrNoRows) {
+ return errMinterNotMember
+ }
+ if err != nil {
+ return err
+ }
+ if role != RoleOwner {
+ return errMinterNotOwner
+ }
+ return nil
+}
+
+// errMinterNotOwner is a service credential mint refused because its minter
+// is no longer an owner of the account by the time the row would be written.
+var errMinterNotOwner = errors.New("console: the minter is not an owner of the account")
+
// The team mint rules (design §2, §5), the deploy rules' twin: a team
// credential names one app and a canonical scope; a scope on any other
// role is refused.
@@ -478,6 +500,13 @@ func (s *Store) claimRawNewServicePrincipalScoped(accountID int64, name, app str
return "", err
}
defer tx.Rollback()
+ // Both mint doors are owner-only, so the minter's role is re-read here,
+ // in the insert's transaction (security hunt H4-M1, review round 2): a
+ // mint that passed the owner gate cannot land after its minter was
+ // removed or stepped down to member.
+ if err := minterIsOwnerTx(tx, accountID, mintedBy); err != nil {
+ return "", err
+ }
var count int
if err := tx.QueryRow(`SELECT COUNT(*) FROM cli_tokens WHERE kind = ? AND account_id = ? AND name = ? AND revoked_at = 0`,
cliKindService, accountID, name).Scan(&count); err != nil {
diff --git a/internal/console/svcprincipal_run_test.go b/internal/console/svcprincipal_run_test.go
index 327175bac..470aeede0 100644
--- a/internal/console/svcprincipal_run_test.go
+++ b/internal/console/svcprincipal_run_test.go
@@ -244,11 +244,11 @@ func TestCredentialIDBackfillIsIdempotent(t *testing.T) {
t.Fatal(err)
}
now := time.Now()
+ mustMintingOwner(t, st, 1, "paul@example.test") // a mint re-reads the minter's role (H4-M1)
svc, err := st.NewServicePrincipalScoped(1, "pub", "hello", svcrole.Publish, nil, nil, "paul@example.test", now)
if err != nil {
t.Fatal(err)
}
- mustMember(t, st, 1, "paul@example.test") // a mint re-reads the minter's membership (H4-M1)
app, err := st.NewAppToken(1, "hello", "paul@example.test", "router", now)
if err != nil {
t.Fatal(err)
diff --git a/internal/console/svcprincipal_test.go b/internal/console/svcprincipal_test.go
index a418ca038..631861d55 100644
--- a/internal/console/svcprincipal_test.go
+++ b/internal/console/svcprincipal_test.go
@@ -17,6 +17,9 @@ func mustAccount(t *testing.T, s *Store, name string) *Account {
if err := s.CreateAccount(id, name, name, "paul@keymail.dev", time.Now()); err != nil {
t.Fatalf("CreateAccount(%q): %v", name, err)
}
+ // The tests here mint as serviceMinter, and a service mint re-reads
+ // its minter's role (H4-M1 review round 2): make it an owner.
+ mustMintingOwner(t, s, id, serviceMinter)
return &Account{ID: id, Sqid: name, Name: name}
}