instance: audit budget, Resume and confirmations; authority tests
Security round findings 8 and 9: budget changes, Resume and the add screen's confirmations append a credential-free audit event in their transaction; tests show another person's or a removed person's model pick, budget change and Resume change nothing.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+189
−1
internal/equipo/chat/metered.go+24 −1internal/equipo/chat/metered_store_test.go+127 −0internal/thintest/metered_contract_test.go+38 −0
diff --git a/internal/equipo/chat/metered.go b/internal/equipo/chat/metered.go| index e5b5eef4..96ce3dd8 100644 |
| --- a/internal/equipo/chat/metered.go |
| +++ b/internal/equipo/chat/metered.go |
| @@ -5,6 +5,7 @@ import ( |
| "encoding/json" |
| "errors" |
| "slices" |
| + "strconv" |
| "amadan.net/oficina/equipo/internal/wire" |
| "gorm.io/gorm" |
| @@ -98,7 +99,14 @@ func (s *Store) startAPIKey(ctx context.Context, person string, in wire.Connect) |
| return err |
| } |
| // The confirmations are kept on the record (saveConnection writes |
| - // only what a later change may change). |
| + // only what a later change may change), and audited. |
| + for kind, confirmed := range map[string]bool{"connection.limit_confirmed": in.LimitConfirmed, "connection.payg_off_confirmed": in.PaygOffConfirmed} { |
| + if confirmed { |
| + if err := t.audit(person, kind, r.ID+"@"+r.Provider); err != nil { |
| + return err |
| + } |
| + } |
| + } |
| if err := t.db.Model(&connectionRecord{}).Where("id = ?", r.ID).Updates(map[string]any{"limit_confirmed_at": r.LimitConfirmedAt, "payg_off_confirmed_at": r.PaygOffConfirmedAt}).Error; err != nil { |
| return err |
| } |
| @@ -108,6 +116,15 @@ func (s *Store) startAPIKey(ctx context.Context, person string, in wire.Connect) |
| return out, err |
| } |
| +// audit appends a credential-free audit event in t: the verified actor, |
| +// what was done and the record (a connection id and a revision). |
| +func (t *txn) audit(person, kind, record string) error { |
| + if t.s.authority.Audit == nil { |
| + return nil |
| + } |
| + return t.s.authority.Audit(t.db, person, kind, record) |
| +} |
| + |
| // ownConnection reads one of the person's connections in t. |
| func (t *txn) ownConnection(person, id string) (connectionRecord, error) { |
| var r connectionRecord |
| @@ -191,6 +208,9 @@ func (s *Store) SetBudget(ctx context.Context, person, id string, in wire.SetBud |
| if err := t.saveConnection(&r, false); err != nil { |
| return err |
| } |
| + if err := t.audit(person, "connection.budget", r.ID+"@"+strconv.FormatInt(next.Revision, 10)); err != nil { |
| + return err |
| + } |
| out = connectionView(r) |
| return t.commandTo(r.RunnerID, person, wire.FrameBudget, wire.BudgetCommand{Connection: r.ID, Person: person, LocalID: r.LocalID, Budget: next}) |
| }) |
| @@ -214,6 +234,9 @@ func (s *Store) ResumeSpend(ctx context.Context, person, id string, in wire.Resu |
| if r.Spend == "" || json.Unmarshal([]byte(r.Spend), &spend) != nil || !spend.Unknown || spend.PauseRevision != in.PauseRevision { |
| return ErrConflict |
| } |
| + if err := t.audit(person, "connection.resume", r.ID+"@"+strconv.FormatInt(in.PauseRevision, 10)); err != nil { |
| + return err |
| + } |
| return t.commandTo(r.RunnerID, person, wire.FrameResumeSpend, wire.ResumeSpendCommand{Connection: r.ID, Person: person, LocalID: r.LocalID, PauseRevision: in.PauseRevision}) |
| }) |
| } |
diff --git a/internal/equipo/chat/metered_store_test.go b/internal/equipo/chat/metered_store_test.go| new file mode 100644 |
| index 00000000..feae99c5 |
| --- /dev/null |
| +++ b/internal/equipo/chat/metered_store_test.go |
| @@ -0,0 +1,127 @@ |
| +package chat |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "io" |
| + "log/slog" |
| + "os" |
| + "path/filepath" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + "amadan.net/oficina/equipo/internal/wire" |
| + "amadan.net/rastrillo/rastrillo/db" |
| + "gorm.io/gorm" |
| +) |
| + |
| +// meteredRig is a store with one metered connection of owner's, a |
| +// connecting one waiting for its model, and an authority whose roster |
| +// check can fail between authentication and the write (removed). |
| +type meteredRig struct { |
| + t *testing.T |
| + s *Store |
| + removed bool |
| + audits []string |
| +} |
| + |
| +func newMeteredRig(t *testing.T) *meteredRig { |
| + src, err := hkTemplatePath() |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + raw, err := os.ReadFile(src) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + path := filepath.Join(t.TempDir(), "m.db") |
| + if err := os.WriteFile(path, raw, 0o600); err != nil { |
| + t.Fatal(err) |
| + } |
| + d, err := db.Open(path, slog.New(slog.NewTextHandler(io.Discard, nil))) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(func() { _ = d.Close() }) |
| + r := &meteredRig{t: t} |
| + r.s = NewStore(d, Authority{ |
| + CurrentPerson: func(context.Context, *gorm.DB) error { |
| + if r.removed { |
| + return ErrForbidden |
| + } |
| + return nil |
| + }, |
| + Me: func(*gorm.DB, string) (wire.Me, error) { return wire.Me{Role: "owner"}, nil }, |
| + PrimaryRunner: func(*gorm.DB, string) (string, error) { return "RUNNER1", nil }, |
| + Copy: func(k string) string { return k }, |
| + Audit: func(_ *gorm.DB, actor, kind, record string) error { |
| + r.audits = append(r.audits, actor+" "+kind+" "+record) |
| + return nil |
| + }, |
| + }, nil) |
| + now := time.Date(2026, 11, 12, 12, 0, 0, 0, time.UTC) |
| + r.s.now = func() time.Time { return now } |
| + for _, c := range []connectionRecord{ |
| + {ID: "cn_made", Person: "owner@example.com", RunnerID: "RUNNER1", LocalID: "tensorx-abc", Provider: wire.ProviderTensorX, Kind: wire.KindAPIKey, State: wire.ConnHealthy, Billing: wire.BillingMetered, Budget: `{"day":1000000,"month":10000000,"currency":"USD","revision":1}`, BudgetApplied: 1, Spend: `{"day":0,"month":0,"currency":"USD","unknown":true,"pause_revision":3,"running":false}`, Revision: 1, CreatedAt: now, UpdatedAt: now}, |
| + {ID: "cn_new", Person: "owner@example.com", RunnerID: "RUNNER1", Provider: wire.ProviderTensorX, Kind: wire.KindAPIKey, State: wire.ConnConnecting, Prompt: `{"kind":"model","url":"","code":"","models":["z-ai/glm-5.3"]}`, Revision: 1, CreatedAt: now, UpdatedAt: now}, |
| + } { |
| + if err := d.G.Create(&c).Error; err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + return r |
| +} |
| + |
| +// unchanged checks no command was queued and no record moved. |
| +func (r *meteredRig) unchanged(what string) { |
| + r.t.Helper() |
| + var commands, revisions int64 |
| + r.s.db.G.Model(&commandRecord{}).Count(&commands) |
| + r.s.db.G.Raw("SELECT sum(revision) FROM chat_connections").Scan(&revisions) |
| + if commands != 0 || revisions != 2 || len(r.audits) != 0 { |
| + r.t.Fatalf("%s changed something: %d commands, revisions %d, audits %v", what, commands, revisions, r.audits) |
| + } |
| +} |
| + |
| +// TestMeteredWritesCheckPersonAndRoster (security round, findings 8 and |
| +// 9): another person's model pick, budget change or Resume finds |
| +// nothing; a person removed between authentication and the write |
| +// changes nothing; the owner's budget change and Resume each append a |
| +// credential-free audit event in their transaction. |
| +func TestMeteredWritesCheckPersonAndRoster(t *testing.T) { |
| + ctx := context.Background() |
| + r := newMeteredRig(t) |
| + writes := map[string]func(person string) error{ |
| + "model": func(p string) error { |
| + _, err := r.s.ChooseModel(ctx, p, "cn_new", wire.ChooseModel{Model: "z-ai/glm-5.3"}) |
| + return err |
| + }, |
| + "budget": func(p string) error { |
| + _, err := r.s.SetBudget(ctx, p, "cn_made", wire.SetBudget{Day: 2_000_000, Month: 10_000_000, Revision: 1}) |
| + return err |
| + }, |
| + "resume": func(p string) error { return r.s.ResumeSpend(ctx, p, "cn_made", wire.ResumeSpend{PauseRevision: 3}) }, |
| + } |
| + for name, write := range writes { |
| + if err := write("member@example.com"); !errors.Is(err, ErrNotFound) { |
| + t.Fatalf("another person's %s: %v", name, err) |
| + } |
| + r.unchanged("another person's " + name) |
| + r.removed = true |
| + if err := write("owner@example.com"); !errors.Is(err, ErrForbidden) { |
| + t.Fatalf("a removed person's %s: %v", name, err) |
| + } |
| + r.unchanged("a removed person's " + name) |
| + r.removed = false |
| + } |
| + for _, name := range []string{"budget", "resume"} { |
| + if err := writes[name]("owner@example.com"); err != nil { |
| + t.Fatalf("%s: %v", name, err) |
| + } |
| + } |
| + want := []string{"owner@example.com connection.budget cn_made@2", "owner@example.com connection.resume cn_made@3"} |
| + if strings.Join(r.audits, "|") != strings.Join(want, "|") { |
| + t.Fatalf("audits %v", r.audits) |
| + } |
| +} |
diff --git a/internal/thintest/metered_contract_test.go b/internal/thintest/metered_contract_test.go| index dfe92374..3d57122b 100644 |
| --- a/internal/thintest/metered_contract_test.go |
| +++ b/internal/thintest/metered_contract_test.go |
| @@ -8,6 +8,7 @@ import ( |
| "testing" |
| "time" |
| + "amadan.net/oficina/equipo/internal/equipo" |
| "amadan.net/oficina/equipo/internal/sealbox" |
| "amadan.net/oficina/equipo/internal/wire" |
| ) |
| @@ -261,6 +262,43 @@ func TestContractMeteredConnection(t *testing.T) { |
| t.Fatalf("resume frame %+v", cmd) |
| } |
| + // Security round finding 8: a teammate's model pick, budget change |
| + // or Resume on this connection finds nothing; once the owner is off |
| + // the roster, theirs changes nothing either. |
| + commands := func() int { |
| + var n int |
| + r.inst.DB.Writer().QueryRow("SELECT count(*) FROM runner_commands WHERE person = ?", Owner).Scan(&n) |
| + return n |
| + } |
| + before, rev := commands(), r.conv.Connections[attempt.ID].Revision |
| + mate := personToolRig(t, pages, "mate@example.com", equipo.RoleOwner) |
| + if _, err := mate.client.ChooseModel(r.ctx, attempt.ID, "z-ai/glm-5.3"); Status(err) != 404 { |
| + t.Fatalf("a teammate's model pick: %v", err) |
| + } |
| + if _, err := mate.client.SetBudget(r.ctx, attempt.ID, wire.SetBudget{Day: 1, Month: 1, Revision: 2}); Status(err) != 404 { |
| + t.Fatalf("a teammate's budget: %v", err) |
| + } |
| + if err := mate.client.ResumeSpend(r.ctx, attempt.ID, 3); Status(err) != 404 { |
| + t.Fatalf("a teammate's resume: %v", err) |
| + } |
| + if _, err := r.inst.DB.Writer().Exec("UPDATE members SET active = 0 WHERE address = ?", Owner); err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := r.client.SetBudget(r.ctx, attempt.ID, wire.SetBudget{Day: 3_000_000, Month: 10_000_000, Revision: 2}); err == nil { |
| + t.Fatal("a removed person's budget change went through") |
| + } |
| + if err := r.client.ResumeSpend(r.ctx, attempt.ID, 3); err == nil { |
| + t.Fatal("a removed person's resume went through") |
| + } |
| + var after int64 |
| + r.inst.DB.Writer().QueryRow("SELECT revision FROM chat_connections WHERE id = ?", attempt.ID).Scan(&after) |
| + if commands() != before || after != rev { |
| + t.Fatalf("the owner's commands %d (was %d), revision %d (was %d)", commands(), before, after, rev) |
| + } |
| + if _, err := r.inst.DB.Writer().Exec("UPDATE members SET active = 1 WHERE address = ?", Owner); err != nil { |
| + t.Fatal(err) |
| + } |
| + |
| // The key is nowhere on the instance. |
| var found int |
| for _, q := range []string{"SELECT count(*) FROM runner_commands WHERE body LIKE ?", "SELECT count(*) FROM chat_connections WHERE coalesce(prompt,'') || coalesce(budget,'') || coalesce(spend,'') LIKE ?", "SELECT count(*) FROM chat_events WHERE body LIKE ?"} { |