carlos / platform Public

console: member removal works in claims mode (H4-M1 r1)

Review round 1 (Astra P1-2): with the host claim on, cli_tokens rows
that name an app and door_keys are gated, so the removal's plain
transaction was refused and the member stayed. Gated, the transaction
now revokes only what names no app (account-scoped service and link
credentials, links, invites) and queues the rest; the member_revocations
drain revokes each app's rows under that app's own SQL permit
(withAutoAppSQL, as other gated writes to these tables do), only rows
minted no later than the removal. Claims off, everything is still
revoked in the removal's transaction.

RemoveMemberWrite becomes a Store method so the box-side tool knows the
gate. TestClaimsModeRemovalRevokesAppCredentials (fails before: "claim:
app write not permitted", member kept). A cmd/console demo test mints as
a member for the mint only.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev b4e6bda3a402d134fd9b9be8e90dbc09210a1c5f parent 5b1b3f1
7 files changed, +195 −35
  • cmd/console/member.go +1 −1
  • cmd/console/member_demo_test.go +16 −0
  • internal/console/memberrevoke.go +117 −25
  • internal/console/memberrevoke_claim_test.go +44 −0
  • internal/console/memberrevoke_test.go +1 −1
  • internal/console/ownership.go +1 −1
  • internal/console/store.go +15 −7
diff --git a/cmd/console/member.go b/cmd/console/member.go
index 6d29063a4..ffa007fa4 100644
--- a/cmd/console/member.go
+++ b/cmd/console/member.go
@@ -200,7 +200,7 @@ func memberRemove(st *console.Store, acct *console.Account, address string) {
fmt.Printf("%s is not a member of %s (%s) — nothing to do\n", address, acct.Sqid, acct.Name)
return
}
- if err := memberTransition(st, acct, console.RemoveMemberWrite(acct.ID, address)); err != nil {
+ if err := memberTransition(st, acct, st.RemoveMemberWrite(acct.ID, address)); err != nil {
log.Fatal(err)
}
fmt.Printf("removed %s (%s) from %s (%s)\n", address, current, acct.Sqid, acct.Name)
diff --git a/cmd/console/member_demo_test.go b/cmd/console/member_demo_test.go
index 54abfc577..361bc6951 100644
--- a/cmd/console/member_demo_test.go
+++ b/cmd/console/member_demo_test.go
@@ -1,6 +1,7 @@
package main_test
import (
+ "database/sql"
"path/filepath"
"strings"
"testing"
@@ -55,10 +56,25 @@ func TestMemberDemoHoldingLastOwner(t *testing.T) {
}
}
// Tokens minted by someone else must be reported too. No implicit revocation.
+ // A mint re-reads its minter's membership (security hunt H4-M1), so the
+ // minter is a member for the mint only; the demo path revokes nothing.
+ if err := st.AddMember(42, "another@keymail.dev", console.RoleMember, now); err != nil {
+ t.Fatal(err)
+ }
secret, err := st.NewAppToken(42, "example", "another@keymail.dev", "demo credential", now)
if err != nil {
t.Fatal(err)
}
+ // The membership row goes by hand: RemoveMember would revoke the
+ // token, and this case is about tokens that outlive their minter.
+ raw, err := sql.Open("sqlite", dbPath)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if _, err := raw.Exec(`DELETE FROM memberships WHERE account_id = 42 AND address = 'another@keymail.dev'`); err != nil {
+ t.Fatal(err)
+ }
+ raw.Close()
toks, err := st.CLITokens("another@keymail.dev")
if err != nil || len(toks) != 1 {
t.Fatalf("tokens: %v %v", toks, err)
diff --git a/internal/console/memberrevoke.go b/internal/console/memberrevoke.go
index 7b449fc94..4b2d354bd 100644
--- a/internal/console/memberrevoke.go
+++ b/internal/console/memberrevoke.go
@@ -65,10 +65,23 @@ type memberRevocation struct {
// revokeMemberCredentialsTx revokes, inside tx, every credential address
// minted for accountID (the list above). It never commits.
-func revokeMemberCredentialsTx(tx *sql.Tx, accountID int64, address string, now time.Time) (memberRevocation, error) {
+//
+// With the host claim on (gated), cli_tokens rows that name an app and
+// door_keys are gated tables a plain transaction may not write. Then the
+// transaction revokes only what names no app (account-scoped service and
+// link credentials, links, invites) and queues the rest:
+// drainMemberRevocations revokes each app's rows under that app's own SQL
+// permit, the way every other gated write to those tables is made. Until
+// it has, the removed member's app-scoped credentials still resolve; the
+// drain runs at once after a browser removal and then every 10 seconds.
+func revokeMemberCredentialsTx(tx *sql.Tx, accountID int64, address string, now time.Time, gated bool) (memberRevocation, error) {
var out memberRevocation
+ appFree := ""
+ if gated {
+ appFree = " AND app = ''"
+ }
if _, err := tx.Exec(`UPDATE cli_tokens SET revoked_at = ?
-WHERE account_id = ? AND address = ? AND kind IN (?, ?, ?) AND revoked_at = 0`,
+WHERE account_id = ? AND address = ? AND kind IN (?, ?, ?) AND revoked_at = 0`+appFree,
now.Unix(), accountID, address, cliKindApp, cliKindService, cliKindLink); err != nil {
return out, err
}
@@ -87,33 +100,28 @@ WHERE account_id = ? AND address = ? AND kind IN (?, ?, ?) AND revoked_at = 0`,
}
// A link's credential row names its approver as address, rotation
// included (RotateLinkToken), so the cli_tokens update above has
- // already revoked it.
- // Door keys: the member's own, and every key of the account whose
- // minter is unknown (created_by '', a key minted before the column
- // whose audit row was missing). Unknown provenance fails safe: removing
- // any member ends those keys too (review round 1).
- apps, err := tx.Query(`SELECT DISTINCT app FROM door_keys
-WHERE account_id = ? AND created_by IN (?, '') AND revoked_at = 0 ORDER BY app`, accountID, address)
- if err != nil {
- return out, err
- }
- out.doorKeyApps, err = scanStrings(apps)
+ // already revoked it (a link credential names no app).
+ apps, err := memberCredentialApps(tx, accountID, address, now.Unix())
if err != nil {
return out, err
}
- for _, app := range out.doorKeyApps {
- if _, err := tx.Exec(`UPDATE door_keys SET revoked_at = ?
-WHERE account_id = ? AND app = ? AND created_by IN (?, '') AND revoked_at = 0`, now.UnixNano(), accountID, app, address); err != nil {
- return out, err
- }
- if _, err := bumpDoorKeyRevision(tx, accountID, app); err != nil {
- return out, err
+ queue := gated && len(apps) > 0
+ if !gated {
+ for _, app := range apps {
+ keys, err := revokeMemberAppCredentials(tx, accountID, app, address, now.Unix(), now)
+ if err != nil {
+ return out, err
+ }
+ if keys {
+ out.doorKeyApps = append(out.doorKeyApps, app)
+ }
}
+ queue = len(out.doorKeyApps) > 0
}
if _, err := tx.Exec(`DELETE FROM invites WHERE account_id = ? AND invited_by = ? AND accepted_at = 0`, accountID, address); err != nil {
return out, err
}
- if len(out.doorKeyApps) > 0 {
+ if queue {
if _, err := tx.Exec(`INSERT INTO member_revocations (account_id, address, created_at) VALUES (?, ?, ?)`,
accountID, address, now.Unix()); err != nil {
return out, err
@@ -122,6 +130,73 @@ WHERE account_id = ? AND app = ? AND created_by IN (?, '') AND revoked_at = 0`,
return out, nil
}
+// sqlExecQuery is what the per-app revocation needs from a transaction or
+// a bound view.
+type sqlExecQuery interface {
+ Exec(query string, args ...any) (sql.Result, error)
+ Query(query string, args ...any) (*sql.Rows, error)
+}
+
+// memberCredentialApps lists the apps where address still holds a live
+// app-scoped credential in accountID minted no later than before (Unix
+// seconds): instance tokens and app-bound service credentials it minted,
+// and door keys it minted or whose minter is unknown (an empty created_by,
+// which fails safe: removing any member ends them, review round 1).
+func memberCredentialApps(q sqlExecQuery, accountID int64, address string, before int64) ([]string, error) {
+ rows, err := q.Query(`SELECT app FROM cli_tokens
+WHERE account_id = ? AND address = ? AND kind IN (?, ?) AND app != '' AND revoked_at = 0 AND created_at <= ?
+UNION SELECT app FROM door_keys
+WHERE account_id = ? AND created_by IN (?, '') AND revoked_at = 0 AND created_at <= ?
+ORDER BY 1`, accountID, address, cliKindApp, cliKindService, before,
+ accountID, address, before*int64(time.Second)+int64(time.Second)-1)
+ if err != nil {
+ return nil, err
+ }
+ return scanStrings(rows)
+}
+
+// revokeMemberAppCredentials revokes, on q, address's app-scoped
+// credentials in one app minted no later than before (Unix seconds), and
+// the app's door keys with no recorded minter; it bumps the door-key
+// revision when a key went, and reports whether one did. Run inside the
+// removal's transaction, or inside the app's own permit by the drain.
+func revokeMemberAppCredentials(q sqlExecQuery, accountID int64, app, address string, before int64, now time.Time) (bool, error) {
+ if _, err := q.Exec(`UPDATE cli_tokens SET revoked_at = ?
+WHERE account_id = ? AND app = ? AND address = ? AND kind IN (?, ?) AND revoked_at = 0 AND created_at <= ?`,
+ now.Unix(), accountID, app, address, cliKindApp, cliKindService, before); err != nil {
+ return false, err
+ }
+ res, err := q.Exec(`UPDATE door_keys SET revoked_at = ?
+WHERE account_id = ? AND app = ? AND created_by IN (?, '') AND revoked_at = 0 AND created_at <= ?`,
+ now.UnixNano(), accountID, app, address, before*int64(time.Second)+int64(time.Second)-1)
+ if err != nil {
+ return false, err
+ }
+ n, err := res.RowsAffected()
+ if err != nil || n == 0 {
+ return false, err
+ }
+ if _, err := q.Exec(`INSERT INTO door_key_revisions (account_id, app, revision) VALUES (?, ?, 1)
+ON CONFLICT (account_id, app) DO UPDATE SET revision = revision + 1`, accountID, app); err != nil {
+ return false, err
+ }
+ return true, nil
+}
+
+// claimRawRevokeMemberAppCredentials is revokeMemberAppCredentials in one
+// transaction of its own, for the drain under the app's permit.
+func (s *Store) claimRawRevokeMemberAppCredentials(accountID int64, app, address string, before int64, now time.Time) error {
+ tx, err := s.db.Begin()
+ if err != nil {
+ return err
+ }
+ defer tx.Rollback()
+ if _, err := revokeMemberAppCredentials(tx, accountID, app, address, before, now); err != nil {
+ return err
+ }
+ return tx.Commit()
+}
+
// migrateMemberRevocations creates the follow-up queue. Additive and
// idempotent, called from Store.migrate.
func (s *Store) migrateMemberRevocations() error {
@@ -139,12 +214,13 @@ type memberRevocationRow struct {
ID int64
AccountID int64
Address string
+ CreatedAt int64 // Unix seconds: the removal's time
}
// pendingMemberRevocations lists the queue, oldest first; accountID 0 is
// every account.
func (s *Store) pendingMemberRevocations(accountID int64) ([]memberRevocationRow, error) {
- rows, err := s.db.Query(`SELECT id, account_id, address FROM member_revocations
+ rows, err := s.db.Query(`SELECT id, account_id, address, created_at FROM member_revocations
WHERE ? = 0 OR account_id = ? ORDER BY id`, accountID, accountID)
if err != nil {
return nil, err
@@ -153,7 +229,7 @@ WHERE ? = 0 OR account_id = ? ORDER BY id`, accountID, accountID)
var out []memberRevocationRow
for rows.Next() {
var r memberRevocationRow
- if err := rows.Scan(&r.ID, &r.AccountID, &r.Address); err != nil {
+ if err := rows.Scan(&r.ID, &r.AccountID, &r.Address, &r.CreatedAt); err != nil {
return nil, err
}
out = append(out, r)
@@ -200,14 +276,30 @@ func (s *Server) finishOneMemberRevocation(ctx context.Context, row memberRevoca
if acct == nil {
return nil
}
+ // With the host claim on, the removal could not revoke app-scoped rows
+ // in its own transaction; each app's go here, under that app's permit.
+ // Only rows minted no later than the removal: a member added back
+ // since keeps what they minted after rejoining.
+ apps, err := memberCredentialApps(s.store.db, row.AccountID, row.Address, row.CreatedAt)
+ if err != nil {
+ return err
+ }
+ for _, app := range apps {
+ err := s.store.withAutoAppSQL(ctx, row.AccountID, app, "sql.RevokeMemberCredentials", func(v *Store) error {
+ return v.claimRawRevokeMemberAppCredentials(row.AccountID, app, row.Address, row.CreatedAt, time.Now())
+ })
+ if err != nil {
+ return fmt.Errorf("%s: %w", app, err)
+ }
+ }
if len(s.cfg.Key) != ed25519.PrivateKeySize || s.cfg.Releases == nil {
return nil // nothing to sign with: this console publishes no record
}
- apps, err := s.store.DoorKeyApps()
+ keyApps, err := s.store.DoorKeyApps()
if err != nil {
return err
}
- for _, a := range apps {
+ for _, a := range keyApps {
if a.AccountID != row.AccountID {
continue
}
diff --git a/internal/console/memberrevoke_claim_test.go b/internal/console/memberrevoke_claim_test.go
new file mode 100644
index 000000000..a3e79e96f
--- /dev/null
+++ b/internal/console/memberrevoke_claim_test.go
@@ -0,0 +1,44 @@
+package console
+
+import (
+ "encoding/json"
+ "net/http"
+ "testing"
+ "time"
+)
+
+// TestClaimsModeRemovalRevokesAppCredentials (security hunt H4-M1, review
+// round 1): with the host claim on, cli_tokens and door_keys are gated
+// tables. Removing a member who minted an instance token and a door key
+// still succeeds, and both credentials stop working: the app-scoped
+// revocations take each app's own SQL permit.
+func TestClaimsModeRemovalRevokesAppCredentials(t *testing.T) {
+ cx := newClaimCase(t)
+ const kim = "kim@keymail.dev"
+ if err := cx.st.AddMember(OpsAccountID, kim, RoleMember, time.Now()); err != nil {
+ t.Fatal(err)
+ }
+ bearer := mintUserToken(t, cx.st, kim, time.Now())
+ status, _, out := rawReq(t, http.MethodPost, cx.ts.URL+"/api/cli/apps/bab/titogo/instance-token", bearer, `{}`)
+ if status != http.StatusCreated {
+ t.Fatalf("mint instance token = %d %s", status, out)
+ }
+ var minted struct{ Token string }
+ if err := json.Unmarshal([]byte(out), &minted); err != nil || minted.Token == "" {
+ t.Fatalf("instance token answer %s: %v", out, err)
+ }
+ status, _, out = rawReq(t, http.MethodPost, cx.ts.URL+"/api/cli/apps/bab/titogo/door-keys", bearer, `{"label":"router"}`)
+ if status != http.StatusCreated {
+ t.Fatalf("mint door key = %d %s", status, out)
+ }
+ cx.form(t, "/a/bab/members/remove", "address", kim)
+ if role, err := cx.st.Role(OpsAccountID, kim); err != nil || role != "" {
+ t.Fatalf("kim's role after removal = %q, %v; want removed", role, err)
+ }
+ if _, ok := cx.st.CLIPrincipal(minted.Token, time.Now()); ok {
+ t.Error("the removed member's instance token still resolves")
+ }
+ if keys, _, err := cx.st.DoorKeysLive(OpsAccountID, "titogo"); err != nil || len(keys) != 0 {
+ t.Errorf("live door keys after removal = %+v, %v; want none", keys, err)
+ }
+}
diff --git a/internal/console/memberrevoke_test.go b/internal/console/memberrevoke_test.go
index 3be20994c..97709e19f 100644
--- a/internal/console/memberrevoke_test.go
+++ b/internal/console/memberrevoke_test.go
@@ -270,7 +270,7 @@ func TestRemovalDoorKeyPublicationIsDurable(t *testing.T) {
if !readDoorRecord(t, fx).Admits(key) {
t.Fatal("test bug: the minted key is not published")
}
- if err := fx.st.MembershipTransition(RemoveMemberWrite(OpsAccountID, "alice@keymail.dev")); err != nil {
+ if err := fx.st.MembershipTransition(fx.st.RemoveMemberWrite(OpsAccountID, "alice@keymail.dev")); err != nil {
t.Fatal(err)
}
if rows, err := fx.st.pendingMemberRevocations(0); err != nil || len(rows) != 1 {
diff --git a/internal/console/ownership.go b/internal/console/ownership.go
index 5b211ee75..7d5135c80 100644
--- a/internal/console/ownership.go
+++ b/internal/console/ownership.go
@@ -52,7 +52,7 @@ func (s *Server) handleRemoveMember(w http.ResponseWriter, r *http.Request) {
var rev memberRevocation
err := s.membershipTransition(r.Context(), acct, time.Now(), func(tx *sql.Tx) error {
var err error
- rev, err = removeMemberRevokingTx(tx, acct.ID, target)
+ rev, err = removeMemberRevokingTx(tx, acct.ID, target, s.store.gate != nil)
return err
})
unlock()
diff --git a/internal/console/store.go b/internal/console/store.go
index 029e0cfef..500a895de 100644
--- a/internal/console/store.go
+++ b/internal/console/store.go
@@ -1611,7 +1611,7 @@ func (s *Store) RemoveMember(accountID int64, address string) error {
return err
}
defer tx.Rollback()
- if err := removeMemberTx(tx, accountID, address); err != nil {
+ if _, err := removeMemberRevokingTx(tx, accountID, address, s.gate != nil); err != nil {
return err
}
return tx.Commit()
@@ -1620,7 +1620,7 @@ func (s *Store) RemoveMember(accountID int64, address string) error {
// removeMemberTx is RemoveMember inside the caller's transaction (a
// MembershipTransition write). It never commits.
func removeMemberTx(tx *sql.Tx, accountID int64, address string) error {
- _, err := removeMemberRevokingTx(tx, accountID, address)
+ _, err := removeMemberRevokingTx(tx, accountID, address, false)
return err
}
@@ -1628,11 +1628,14 @@ func removeMemberTx(tx *sql.Tx, accountID int64, address string) error {
// credential the member minted for the account goes in the same
// transaction (revokeMemberCredentialsTx, security hunt H4-M1), and the
// caller publishes the door keys and link channels after the commit.
-func removeMemberRevokingTx(tx *sql.Tx, accountID int64, address string) (memberRevocation, error) {
+//
+// gated is the host claim's SQL gate being on (Store.gate): then the
+// app-scoped revocations are queued for the drain (revokeMemberCredentialsTx).
+func removeMemberRevokingTx(tx *sql.Tx, accountID int64, address string, gated bool) (memberRevocation, error) {
if err := lastOwnerGuard(tx, accountID, address); err != nil {
return memberRevocation{}, err
}
- rev, err := revokeMemberCredentialsTx(tx, accountID, address, time.Now())
+ rev, err := revokeMemberCredentialsTx(tx, accountID, address, time.Now(), gated)
if err != nil {
return memberRevocation{}, err
}
@@ -1659,9 +1662,14 @@ func AddMemberWrite(accountID int64, address, role string, now time.Time) func(*
}
// RemoveMemberWrite is removeMemberTx as a MembershipTransition write, for
-// the box-side membership tool (cmd/console).
-func RemoveMemberWrite(accountID int64, address string) func(*sql.Tx) error {
- return func(tx *sql.Tx) error { return removeMemberTx(tx, accountID, address) }
+// the box-side membership tool (cmd/console). A method so the write knows
+// whether s's host claim gate is on.
+func (s *Store) RemoveMemberWrite(accountID int64, address string) func(*sql.Tx) error {
+ gated := s.gate != nil
+ return func(tx *sql.Tx) error {
+ _, err := removeMemberRevokingTx(tx, accountID, address, gated)
+ return err
+ }
}
// lastOwnerGuard refuses a change that would leave an account with no owner.