Bind delegated meeting retries to live authority
12 files changed,
+451
−29
docs/superpowers/specs/2026-10-25-delegation-hardening.md+24 −0go.mod+1 −1go.sum+0 −2internal/calls/app.go+13 −12internal/calls/delegated.go+17 −5internal/calls/delegated_attribution_test.go+50 −0internal/calls/delegated_authority.go+85 −0internal/calls/delegated_authority_test.go+151 −0internal/calls/delegated_continuity_test.go+85 −0internal/calls/delegated_create.go+19 −7internal/calls/delegated_migrations/migrations/0002_authority.sql+4 −0internal/calls/delegated_test.go+2 −2
CI passed — run details
diff --git a/docs/superpowers/specs/2026-10-25-delegation-hardening.md b/docs/superpowers/specs/2026-10-25-delegation-hardening.md| new file mode 100644 |
| index 0000000..4b3432e |
| --- /dev/null |
| +++ b/docs/superpowers/specs/2026-10-25-delegation-hardening.md |
| @@ -0,0 +1,24 @@ |
| +# Delegated Calls mutation authority |
| + |
| +Status: bounded design approved by GPT-6 Astra on 2026-10-25 in the joint |
| +Memoria/Calendar/Calls review. Implementation review, CI and canary remain. |
| + |
| +Bind the receipt key, exact Home issuer, delegation issuer and instance team in |
| +a keyed singleton marker. Adopt only under writer reservation when both receipt |
| +and connection-attribution tables are empty. Check at enablement and before each |
| +mutation's receipt lookup; changed or missing binding requires explicit migration. |
| +Both issuers enter receipt and attribution coordinates. |
| + |
| +Retain a private request callback to the original credential's underlying |
| +verifier. Compare subject, client, grant, team and calls:write. Never call local |
| +DB or rate-limit wrappers inside the writer transaction. Check delegation and |
| +the transaction-local Home link/person/current membership after reserving the |
| +writer and again immediately before commit, including completed retries. Missing |
| +callback refuses. Meeting, invitation, attribution and receipt remain atomic. |
| +Malformed Unicode fails before lossy JSON decoding. |
| + |
| +Tests must distinguish initial versus final checks, completed retry checks, |
| +post-mount marker tampering, changed verifier principals, current unlink and |
| +membership loss, and failure rollback. Test reopened databases and immutable |
| +configuration, then run adverse mutations and full CI. Existing direct |
| +Registry.HTTP read emission remains a separate response-revalidation gap. |
diff --git a/go.mod b/go.mod| index 779923c..75cfee9 100644 |
| --- a/go.mod |
| +++ b/go.mod |
| @@ -9,6 +9,7 @@ require ( |
| amadan.net/oficina/home/handoff v0.0.0-20260908102137-481d2d57f489 |
| amadan.net/rastrillo/rastrillo v0.27.1-0.20261006204347-fb806a287060 |
| github.com/go-chi/chi/v5 v5.3.2 |
| + github.com/go-json-experiment/json v0.0.0-20250725192818-e39067aee2d2 |
| github.com/google/uuid v1.6.0 |
| github.com/keymaildev/signin v0.1.1 |
| golang.org/x/crypto v0.56.0 |
| @@ -20,7 +21,6 @@ require ( |
| github.com/BurntSushi/toml v1.6.0 // indirect |
| github.com/carlosframework/rastrillo v0.23.0 // indirect |
| github.com/dustin/go-humanize v1.0.1 // indirect |
| - github.com/go-json-experiment/json v0.0.0-20250725192818-e39067aee2d2 // indirect |
| github.com/google/jsonschema-go v0.4.3 // indirect |
| github.com/jinzhu/inflection v1.0.0 // indirect |
| github.com/jinzhu/now v1.1.5 // indirect |
diff --git a/go.sum b/go.sum| index 386eebc..69d2119 100644 |
| --- a/go.sum |
| +++ b/go.sum |
| @@ -1,5 +1,3 @@ |
| -amadan.net/oficina/agent-access.git v0.0.0-20261024224328-3966c68144d1 h1:GknXgICfFlRNJTwXrZRdeTRS6DtQ37b3e7COQX/IxAs= |
| -amadan.net/oficina/agent-access.git v0.0.0-20261024224328-3966c68144d1/go.mod h1:Ph9PIa3VE1IEaUzZiQH6D+IQ6HThWWGfVr0ecMMgrFA= |
| amadan.net/oficina/agent-access.git v0.0.0-20261024232549-8ed8ba826000 h1:L07nc+dmLI7c1PBkSkbnrjOK5f8p6OBbA/yTxznxCAw= |
| amadan.net/oficina/agent-access.git v0.0.0-20261024232549-8ed8ba826000/go.mod h1:Ph9PIa3VE1IEaUzZiQH6D+IQ6HThWWGfVr0ecMMgrFA= |
| amadan.net/oficina/estilo.git v0.0.0-20261001093750-530cb932e768 h1:bVscoa9fH5LdLDjlyecZhrX7JzWPT0blb57MH0FIm2U= |
diff --git a/internal/calls/app.go b/internal/calls/app.go| index c77adc9..7195a81 100644 |
| --- a/internal/calls/app.go |
| +++ b/internal/calls/app.go |
| @@ -30,18 +30,19 @@ import ( |
| // app holds what every handler needs. |
| type app struct { |
| // signinMail is the queue sign-in links go through (signin_mail.go). |
| - signinMail *mailQueue |
| - db *db.DB |
| - cfg resolved |
| - sealer *seal.Sealer |
| - store *store |
| - logger *slog.Logger |
| - auth *rauth.Auth |
| - home *homeIntegration |
| - delegated http.Handler |
| - nativeAuth *nativeAuthorization |
| - linkBudget *budget |
| - outsiderBudget *budget |
| + signinMail *mailQueue |
| + db *db.DB |
| + cfg resolved |
| + sealer *seal.Sealer |
| + store *store |
| + logger *slog.Logger |
| + auth *rauth.Auth |
| + home *homeIntegration |
| + delegated http.Handler |
| + agentIssuer, agentTeam string |
| + nativeAuth *nativeAuthorization |
| + linkBudget *budget |
| + outsiderBudget *budget |
| // overBudget counts outsider sign-in attempts the budgets refused, |
| // for sweepHourly to report: never logged on the response path. |
| overBudget atomic.Int64 |
diff --git a/internal/calls/delegated.go b/internal/calls/delegated.go| index bee6ee8..0951a75 100644 |
| --- a/internal/calls/delegated.go |
| +++ b/internal/calls/delegated.go |
| @@ -5,6 +5,8 @@ import ( |
| "embed" |
| "encoding/json" |
| "errors" |
| + strictjson "github.com/go-json-experiment/json" |
| + "gorm.io/gorm" |
| "io/fs" |
| access "amadan.net/oficina/agent-access.git" |
| @@ -37,17 +39,24 @@ func (a *app) wireDelegated() error { |
| if err != nil { |
| return err |
| } |
| - return a.mountDelegated(v, a.cfg.AgentTeam) |
| + return a.mountDelegated(v, a.cfg.AgentIssuer, a.cfg.AgentTeam) |
| } |
| -func (a *app) mountDelegated(v access.Verifier, team string) error { |
| +func (a *app) mountDelegated(v access.Verifier, issuer, team string) error { |
| if _, err := migrate.Apply(context.Background(), a.db, delegatedSchema); err != nil { |
| return err |
| } |
| + a.agentIssuer, a.agentTeam = issuer, team |
| + if a.cfg.Home == nil || issuer == "" { |
| + return errors.New("calls: agent authority unavailable") |
| + } |
| + if err := a.db.G.Transaction(func(tx *gorm.DB) error { return a.bindDelegated(tx) }); err != nil { |
| + return err |
| + } |
| reg, err := access.NewRegistry(a.delegatedOperations(team)...) |
| if err != nil { |
| return err |
| } |
| - a.delegated = reg.HTTP(access.VerifyFunc(func(ctx context.Context, token string) (access.Principal, error) { |
| + a.delegated = a.delegatedBoundary(v, reg.HTTP(access.VerifyFunc(func(ctx context.Context, token string) (access.Principal, error) { |
| p, err := v.Verify(ctx, token) |
| if err != nil { |
| return access.Principal{}, err |
| @@ -64,7 +73,7 @@ func (a *app) mountDelegated(v access.Verifier, team string) error { |
| return access.Principal{}, access.Fail("rate_limited", "Calls request limit reached. Try again later.") |
| } |
| return p, nil |
| - })) |
| + }))) |
| return nil |
| } |
| func (a *app) delegatedPerson(ctx context.Context, p access.Principal, team string) (Person, error) { |
| @@ -106,7 +115,10 @@ func (a *app) delegatedGuard(team, scope string, fn func(context.Context, access |
| var in struct { |
| Team string `json:"team"` |
| } |
| - if json.Unmarshal(raw, &in) != nil || in.Team != team || !p.Allows(team, scope) { |
| + if strictjson.Unmarshal(raw, &in) != nil { |
| + return nil, access.Fail("invalid", "Send valid Unicode JSON.") |
| + } |
| + if in.Team != team || !p.Allows(team, scope) { |
| return nil, access.Fail("forbidden", "This connection cannot access that team.") |
| } |
| person, err := a.delegatedPerson(ctx, p, team) |
diff --git a/internal/calls/delegated_attribution_test.go b/internal/calls/delegated_attribution_test.go| new file mode 100644 |
| index 0000000..0642b59 |
| --- /dev/null |
| +++ b/internal/calls/delegated_attribution_test.go |
| @@ -0,0 +1,50 @@ |
| +package calls |
| + |
| +import ( |
| + "context" |
| + "encoding/json" |
| + "strconv" |
| + "sync/atomic" |
| + "testing" |
| + "time" |
| + |
| + access "amadan.net/oficina/agent-access.git" |
| +) |
| + |
| +func TestDelegatedActorNamespaceAndSingleRateCharge(t *testing.T) { |
| + f, p := linkedDelegated(t) |
| + var checks atomic.Int64 |
| + if e := f.app.mountDelegated(access.VerifyFunc(func(context.Context, string) (access.Principal, error) { checks.Add(1); return *p, nil }), f.app.agentIssuer, "acme"); e != nil { |
| + t.Fatal(e) |
| + } |
| + now := time.Now() |
| + f.app.store.clk.set(func() time.Time { return now }) |
| + if rec := delegatedCall(f.app, "calls_create_meeting", delegatedInputForTest()); rec.Code != 200 { |
| + t.Fatal(rec.Code, rec.Body.String()) |
| + } |
| + if checks.Load() < 3 { |
| + t.Fatal("write did not reverify delegation") |
| + } |
| + person, e := f.app.delegatedPerson(t.Context(), *p, "acme") |
| + if e != nil { |
| + t.Fatal(e) |
| + } |
| + raw, _ := json.Marshal([]string{f.app.cfg.Home.Issuer, f.app.agentIssuer, p.Subject, p.Client, "acme", strconv.FormatInt(person.ID, 10)}) |
| + want, e := f.app.sealer.Fingerprint("connection.actor.v1", string(raw)) |
| + if e != nil { |
| + t.Fatal(e) |
| + } |
| + var got string |
| + if e = f.app.db.Writer().QueryRow("SELECT coordinate FROM calls_connections").Scan(&got); e != nil || got != want { |
| + t.Fatal("actor namespace mismatch", e) |
| + } |
| + // One creation plus 59 reads exhaust exactly the 60-request allowance. |
| + for i := 0; i < 59; i++ { |
| + if rec := delegatedCall(f.app, "calls_list_meetings", map[string]any{"team": "acme"}); rec.Code != 200 { |
| + t.Fatal("one request charged repeatedly", i, rec.Code) |
| + } |
| + } |
| + if rec := delegatedCall(f.app, "calls_list_meetings", map[string]any{"team": "acme"}); rec.Code != 429 { |
| + t.Fatal("request not charged", rec.Code) |
| + } |
| +} |
diff --git a/internal/calls/delegated_authority.go b/internal/calls/delegated_authority.go| new file mode 100644 |
| index 0000000..8e23dc4 |
| --- /dev/null |
| +++ b/internal/calls/delegated_authority.go |
| @@ -0,0 +1,85 @@ |
| +package calls |
| + |
| +import ( |
| + "context" |
| + "crypto/hmac" |
| + "database/sql" |
| + "encoding/json" |
| + "errors" |
| + "net/http" |
| + "strings" |
| + |
| + access "amadan.net/oficina/agent-access.git" |
| + "gorm.io/gorm" |
| +) |
| + |
| +type delegatedBoundaryKey struct{} |
| +type delegatedRecheck func(context.Context, access.Principal) error |
| + |
| +func (a *app) delegatedBoundary(verifier access.Verifier, next http.Handler) http.Handler { |
| + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| + parts := strings.Fields(r.Header.Get("Authorization")) |
| + token := "" |
| + if len(parts) == 2 { |
| + token = parts[1] |
| + } |
| + check := delegatedRecheck(func(ctx context.Context, p access.Principal) error { |
| + current, e := verifier.Verify(ctx, token) |
| + if e != nil { |
| + return e |
| + } |
| + if current.Subject != p.Subject || current.Client != p.Client || current.Grant != p.Grant || !current.Allows(a.agentTeam, "calls:write") { |
| + return access.Fail("forbidden", "This connection is no longer authorized.") |
| + } |
| + return nil |
| + }) |
| + next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), delegatedBoundaryKey{}, check))) |
| + }) |
| +} |
| +func (a *app) bindDelegated(tx *gorm.DB) error { |
| + raw, _ := json.Marshal([]string{a.cfg.Home.Issuer, a.agentIssuer, a.agentTeam}) |
| + want, e := a.sealer.Fingerprint("connection.authority.v1", string(raw)) |
| + if e != nil { |
| + return e |
| + } |
| + if e = tx.Exec("INSERT INTO calls_agent_state(id,keycheck) SELECT 1,? WHERE NOT EXISTS(SELECT 1 FROM calls_agent_meetings) AND NOT EXISTS(SELECT 1 FROM calls_connections) ON CONFLICT(id) DO NOTHING", want).Error; e != nil { |
| + return e |
| + } |
| + var got string |
| + if e = tx.Raw("SELECT keycheck FROM calls_agent_state WHERE id=1").Scan(&got).Error; e != nil { |
| + return e |
| + } |
| + if !hmac.Equal([]byte(got), []byte(want)) { |
| + return access.Fail("unavailable", "Agent receipt authority requires an explicit migration.") |
| + } |
| + return nil |
| +} |
| +func (a *app) checkDelegatedWriter(ctx context.Context, tx *gorm.DB, p access.Principal, expected Person) error { |
| + check, ok := ctx.Value(delegatedBoundaryKey{}).(delegatedRecheck) |
| + if !ok || check == nil { |
| + return access.Fail("forbidden", "Agent authority is unavailable.") |
| + } |
| + if e := check(ctx, p); e != nil { |
| + return e |
| + } |
| + var id int64 |
| + if e := tx.Raw("SELECT person_id FROM home_identity_links WHERE issuer=? AND subject=?", a.cfg.Home.Issuer, p.Subject).Row().Scan(&id); e != nil { |
| + if errors.Is(e, sql.ErrNoRows) { |
| + return access.Fail("forbidden", "The linked identity is unavailable.") |
| + } |
| + return e |
| + } |
| + if id != expected.ID { |
| + return access.Fail("forbidden", "The linked identity changed.") |
| + } |
| + currentStore := *a.store |
| + currentStore.g = tx |
| + current, exists, e := currentStore.personByID(ctx, id) |
| + if e != nil { |
| + return e |
| + } |
| + if !exists || current.Email != expected.Email || !a.isMember(current) { |
| + return access.Fail("forbidden", "This account is no longer admitted.") |
| + } |
| + return nil |
| +} |
diff --git a/internal/calls/delegated_authority_test.go b/internal/calls/delegated_authority_test.go| new file mode 100644 |
| index 0000000..2440639 |
| --- /dev/null |
| +++ b/internal/calls/delegated_authority_test.go |
| @@ -0,0 +1,151 @@ |
| +package calls |
| + |
| +import ( |
| + "bytes" |
| + "context" |
| + "encoding/json" |
| + "net/http/httptest" |
| + "strings" |
| + "sync/atomic" |
| + "testing" |
| + |
| + access "amadan.net/oficina/agent-access.git" |
| + "amadan.net/oficina/calls/internal/calls/seal" |
| + "amadan.net/rastrillo/rastrillo/db" |
| + "gorm.io/gorm" |
| +) |
| + |
| +func TestDelegatedPrecommitAuthority(t *testing.T) { |
| + for _, mode := range []string{"reserved-revoke", "final-revoke", "unlink", "member"} { |
| + t.Run(mode, func(t *testing.T) { |
| + f, p := linkedDelegated(t) |
| + var checks atomic.Int64 |
| + if e := f.app.mountDelegated(access.VerifyFunc(func(context.Context, string) (access.Principal, error) { |
| + n := checks.Add(1) |
| + if mode == "reserved-revoke" && n >= 2 || mode == "final-revoke" && n >= 3 { |
| + return access.Principal{}, access.Fail("unauthenticated", "Revoked.") |
| + } |
| + return *p, nil |
| + }), f.app.agentIssuer, "acme"); e != nil { |
| + t.Fatal(e) |
| + } |
| + trigger := "" |
| + switch mode { |
| + case "reserved-revoke": |
| + trigger = "CREATE TRIGGER body_must_not_run BEFORE INSERT ON meetings BEGIN SELECT RAISE(ABORT,'body attempted'); END" |
| + case "unlink": |
| + trigger = "CREATE TRIGGER lose_authority AFTER UPDATE OF meeting_id ON calls_agent_meetings BEGIN DELETE FROM home_identity_links; END" |
| + case "member": |
| + trigger = "CREATE TRIGGER lose_authority AFTER UPDATE OF meeting_id ON calls_agent_meetings BEGIN UPDATE persons SET anonymous=1; END" |
| + } |
| + if trigger != "" { |
| + if _, e := f.app.db.Writer().Exec(trigger); e != nil { |
| + t.Fatal(e) |
| + } |
| + } |
| + rec := delegatedCall(f.app, "calls_create_meeting", delegatedInputForTest()) |
| + if rec.Code != 401 && rec.Code != 403 { |
| + t.Fatal("lost authority committed", mode, rec.Code, rec.Body.String()) |
| + } |
| + for _, table := range []string{"meetings", "meeting_links", "calls_connections", "calls_agent_meetings"} { |
| + var n int |
| + if e := f.app.db.Writer().QueryRow("SELECT COUNT(*) FROM " + table).Scan(&n); e != nil || n != 0 { |
| + t.Fatal("partial meeting", table, n, e) |
| + } |
| + } |
| + }) |
| + } |
| +} |
| +func TestDelegatedAuthorityBindingRestart(t *testing.T) { |
| + f, p := linkedDelegated(t) |
| + input := delegatedInputForTest() |
| + first := delegatedCall(f.app, "calls_create_meeting", input) |
| + if first.Code != 200 { |
| + t.Fatal(first.Code) |
| + } |
| + var path string |
| + if e := f.app.db.Writer().QueryRow("SELECT file FROM pragma_database_list WHERE name='main'").Scan(&path); e != nil { |
| + t.Fatal(e) |
| + } |
| + f.app.signinMail.close() |
| + f.app.db.Close() |
| + d, e := db.Open(path, f.app.logger) |
| + if e != nil { |
| + t.Fatal(e) |
| + } |
| + defer d.Close() |
| + restarted := newHomeRouterFixtureWith(t, d, f.app.logger, f.key, f.app.cfg.Home) |
| + if e = restarted.app.mountDelegated(access.VerifyFunc(func(context.Context, string) (access.Principal, error) { return *p, nil }), f.app.agentIssuer, "acme"); e != nil { |
| + t.Fatal(e) |
| + } |
| + rec := delegatedCall(restarted.app, "calls_create_meeting", input) |
| + if rec.Code != 200 || rec.Body.String() != first.Body.String() { |
| + t.Fatal("restart changed receipt", rec.Code, rec.Body.String()) |
| + } |
| + a := restarted.app |
| + for _, kind := range []string{"issuer", "home", "team", "key"} { |
| + t.Run(kind, func(t *testing.T) { |
| + oldIssuer, oldHome, oldTeam, oldSeal := a.agentIssuer, a.cfg.Home.Issuer, a.agentTeam, a.sealer |
| + defer func() { |
| + a.agentIssuer = oldIssuer |
| + a.cfg.Home.Issuer = oldHome |
| + a.agentTeam = oldTeam |
| + a.sealer = oldSeal |
| + }() |
| + switch kind { |
| + case "issuer": |
| + a.agentIssuer = "https://other-connect.test" |
| + case "home": |
| + a.cfg.Home.Issuer = "https://other-home.test" |
| + case "team": |
| + a.agentTeam = "green" |
| + case "key": |
| + a.sealer, _ = seal.New(bytes.Repeat([]byte{29}, 32)) |
| + } |
| + if e := a.db.G.Transaction(func(tx *gorm.DB) error { return a.bindDelegated(tx) }); e == nil { |
| + t.Fatal("authority change accepted") |
| + } |
| + }) |
| + } |
| + if _, e = a.db.Writer().Exec("DELETE FROM calls_agent_state"); e != nil { |
| + t.Fatal(e) |
| + } |
| + if e = a.db.G.Transaction(func(tx *gorm.DB) error { return a.bindDelegated(tx) }); e == nil { |
| + t.Fatal("existing receipts adopted without marker") |
| + } |
| + // Test each preexisting table independently, so either missing predicate fails. |
| + if e = a.db.G.Transaction(func(tx *gorm.DB) error { |
| + if e := tx.Exec("DELETE FROM calls_connections").Error; e != nil { |
| + return e |
| + } |
| + if e := a.bindDelegated(tx); e == nil { |
| + t.Fatal("receipt-only state adopted without marker") |
| + } |
| + return gorm.ErrInvalidTransaction // restore attribution for the next case |
| + }); e != gorm.ErrInvalidTransaction { |
| + t.Fatal(e) |
| + } |
| + // Attribution alone must also prevent marker adoption. |
| + if _, e = a.db.Writer().Exec("DELETE FROM calls_agent_meetings"); e != nil { |
| + t.Fatal(e) |
| + } |
| + if e = a.db.G.Transaction(func(tx *gorm.DB) error { return a.bindDelegated(tx) }); e == nil { |
| + t.Fatal("existing attribution adopted without marker") |
| + } |
| +} |
| +func TestDelegatedRejectsMalformedUnicode(t *testing.T) { |
| + f, _ := linkedDelegated(t) |
| + input := delegatedInputForTest() |
| + input["title"] = "REPLACEME" |
| + raw, _ := json.Marshal(input) |
| + for _, bad := range []string{`\ud800`, string([]byte{255})} { |
| + r := httptest.NewRequest("POST", homeTestOrigin+"/api/v1/operations/calls_create_meeting", strings.NewReader(strings.Replace(string(raw), "REPLACEME", bad, 1))) |
| + r.Header.Set("Content-Type", "application/json") |
| + r.Header.Set("Authorization", "Bearer fixture") |
| + w := httptest.NewRecorder() |
| + f.app.delegated.ServeHTTP(w, r) |
| + if w.Code != 422 { |
| + t.Fatal("malformed meeting Unicode", w.Code) |
| + } |
| + } |
| +} |
diff --git a/internal/calls/delegated_continuity_test.go b/internal/calls/delegated_continuity_test.go| new file mode 100644 |
| index 0000000..b417a7b |
| --- /dev/null |
| +++ b/internal/calls/delegated_continuity_test.go |
| @@ -0,0 +1,85 @@ |
| +package calls |
| + |
| +import ( |
| + "context" |
| + "sync/atomic" |
| + "testing" |
| + |
| + access "amadan.net/oficina/agent-access.git" |
| +) |
| + |
| +func TestDelegatedMeetingRetryFinalCheckAndLiveMarker(t *testing.T) { |
| + for _, mode := range []string{"retry-final", "marker-create", "marker-retry"} { |
| + t.Run(mode, func(t *testing.T) { |
| + f, p := linkedDelegated(t) |
| + input := delegatedInputForTest() |
| + if rec := delegatedCall(f.app, "calls_create_meeting", input); rec.Code != 200 { |
| + t.Fatal(rec.Code) |
| + } |
| + want := 503 |
| + if mode == "retry-final" { |
| + want = 401 |
| + var checks atomic.Int64 |
| + if e := f.app.mountDelegated(access.VerifyFunc(func(context.Context, string) (access.Principal, error) { |
| + if checks.Add(1) >= 3 { |
| + return access.Principal{}, access.Fail("unauthenticated", "Revoked.") |
| + } |
| + return *p, nil |
| + }), f.app.agentIssuer, "acme"); e != nil { |
| + t.Fatal(e) |
| + } |
| + } else { |
| + if _, e := f.app.db.Writer().Exec("UPDATE calls_agent_state SET keycheck='damaged'"); e != nil { |
| + t.Fatal(e) |
| + } |
| + if mode == "marker-create" { |
| + input["client_id"] = "15bb7913-1316-4b96-bc3a-4b4cded1ded4" |
| + } |
| + } |
| + if rec := delegatedCall(f.app, "calls_create_meeting", input); rec.Code != want { |
| + t.Fatal("boundary ignored", mode, rec.Code, rec.Body.String()) |
| + } |
| + for _, table := range []string{"meetings", "calls_agent_meetings"} { |
| + var n int |
| + if e := f.app.db.Writer().QueryRow("SELECT COUNT(*) FROM " + table).Scan(&n); e != nil || n != 1 { |
| + t.Fatal("retry wrote extra effect", n, e) |
| + } |
| + } |
| + }) |
| + } |
| +} |
| +func TestDelegatedMeetingVerifierContinuity(t *testing.T) { |
| + for _, field := range []string{"subject", "client", "grant", "team", "scope"} { |
| + t.Run(field, func(t *testing.T) { |
| + f, p := linkedDelegated(t) |
| + var checks atomic.Int64 |
| + if e := f.app.mountDelegated(access.VerifyFunc(func(context.Context, string) (access.Principal, error) { |
| + current := *p |
| + if checks.Add(1) > 1 { |
| + switch field { |
| + case "subject": |
| + current.Subject = "different" |
| + case "client": |
| + current.Client = "different" |
| + case "grant": |
| + current.Grant = "different" |
| + case "team": |
| + current.Teams = []string{"green"} |
| + case "scope": |
| + current.Scopes = []string{"calls:read"} |
| + } |
| + } |
| + return current, nil |
| + }), f.app.agentIssuer, "acme"); e != nil { |
| + t.Fatal(e) |
| + } |
| + if rec := delegatedCall(f.app, "calls_create_meeting", delegatedInputForTest()); rec.Code != 403 { |
| + t.Fatal("changed principal accepted", field, rec.Code) |
| + } |
| + var n int |
| + if e := f.app.db.Writer().QueryRow("SELECT COUNT(*) FROM meetings").Scan(&n); e != nil || n != 0 { |
| + t.Fatal("changed principal wrote", n, e) |
| + } |
| + }) |
| + } |
| +} |
diff --git a/internal/calls/delegated_create.go b/internal/calls/delegated_create.go| index 397b936..364ed3e 100644 |
| --- a/internal/calls/delegated_create.go |
| +++ b/internal/calls/delegated_create.go |
| @@ -26,7 +26,7 @@ func (a *app) delegatedCreate(ctx context.Context, p access.Principal, person Pe |
| return nil, access.Fail("invalid", "Invalid meeting input.") |
| } |
| id, err := uuid.Parse(in.ClientID) |
| - if err != nil || id == uuid.Nil || len(in.ClientID) != 36 { |
| + if err != nil || id == uuid.Nil || id.String() != in.ClientID { |
| return nil, access.Fail("invalid", "Supply a nonzero UUID client_id.") |
| } |
| if strings.TrimSpace(in.Title) == "" || titleTooLong(in.Title) { |
| @@ -42,7 +42,7 @@ func (a *app) delegatedCreate(ctx context.Context, p access.Principal, person Pe |
| starts = &v |
| } |
| in.ClientID = id.String() |
| - coordinates, _ := json.Marshal([]string{p.Subject, p.Client, in.Team, in.ClientID}) |
| + coordinates, _ := json.Marshal([]string{a.cfg.Home.Issuer, a.agentIssuer, p.Subject, p.Client, in.Team, in.ClientID}) |
| payload, _ := json.Marshal(in) |
| key, err := a.sealer.Fingerprint("connection.receipt.v1", string(coordinates)) |
| if err != nil { |
| @@ -54,6 +54,12 @@ func (a *app) delegatedCreate(ctx context.Context, p access.Principal, person Pe |
| } |
| var meeting Meeting |
| err = a.store.g.WithContext(ctx).Transaction(func(tx *gorm.DB) error { |
| + if e := a.bindDelegated(tx); e != nil { |
| + return e |
| + } |
| + if e := a.checkDelegatedWriter(ctx, tx, p, person); e != nil { |
| + return e |
| + } |
| if err := tx.Exec("INSERT INTO calls_agent_meetings(coordinate,content) VALUES(?,?) ON CONFLICT(coordinate) DO NOTHING", key, content).Error; err != nil { |
| return err |
| } |
| @@ -78,9 +84,12 @@ func (a *app) delegatedCreate(ctx context.Context, p access.Principal, person Pe |
| if err = tx.Where("id=?", mid.Int64).Take(&meeting).Error; err == gorm.ErrRecordNotFound { |
| return access.Fail("not_found", "This meeting is unavailable.") |
| } |
| - return err |
| + if err != nil { |
| + return err |
| + } |
| + return a.checkDelegatedWriter(ctx, tx, p, person) |
| } |
| - actor, err := current.delegatedActor(ctx, p, in.Team, operator) |
| + actor, err := current.delegatedActor(ctx, p, a.cfg.Home.Issuer, a.agentIssuer, in.Team, operator) |
| if err != nil { |
| return err |
| } |
| @@ -88,15 +97,18 @@ func (a *app) delegatedCreate(ctx context.Context, p access.Principal, person Pe |
| if err != nil { |
| return err |
| } |
| - return tx.Exec("UPDATE calls_agent_meetings SET meeting_id=? WHERE coordinate=?", meeting.ID, key).Error |
| + if e := tx.Exec("UPDATE calls_agent_meetings SET meeting_id=? WHERE coordinate=?", meeting.ID, key).Error; e != nil { |
| + return e |
| + } |
| + return a.checkDelegatedWriter(ctx, tx, p, person) |
| }) |
| if err != nil { |
| return nil, err |
| } |
| return map[string]any{"sqid": meeting.Sqid, "url": a.cfg.Origin + "/m/" + meeting.Sqid, "invitations_sent": false}, nil |
| } |
| -func (s *store) delegatedActor(ctx context.Context, p access.Principal, team string, operator Person) (string, error) { |
| - raw, _ := json.Marshal([]string{p.Subject, p.Client, team, strconv.FormatInt(operator.ID, 10)}) |
| +func (s *store) delegatedActor(ctx context.Context, p access.Principal, homeIssuer, issuer, team string, operator Person) (string, error) { |
| + raw, _ := json.Marshal([]string{homeIssuer, issuer, p.Subject, p.Client, team, strconv.FormatInt(operator.ID, 10)}) |
| key, err := s.sealer.Fingerprint("connection.actor.v1", string(raw)) |
| if err != nil { |
| return "", err |
diff --git a/internal/calls/delegated_migrations/migrations/0002_authority.sql b/internal/calls/delegated_migrations/migrations/0002_authority.sql| new file mode 100644 |
| index 0000000..72727e2 |
| --- /dev/null |
| +++ b/internal/calls/delegated_migrations/migrations/0002_authority.sql |
| @@ -0,0 +1,4 @@ |
| +CREATE TABLE calls_agent_state ( |
| + id INTEGER PRIMARY KEY CHECK(id=1), |
| + keycheck TEXT NOT NULL |
| +); |
diff --git a/internal/calls/delegated_test.go b/internal/calls/delegated_test.go| index 07a83f9..35d9af2 100644 |
| --- a/internal/calls/delegated_test.go |
| +++ b/internal/calls/delegated_test.go |
| @@ -19,7 +19,7 @@ func linkedDelegated(t *testing.T) (*homeRouterFixture, *access.Principal) { |
| f := newHomeRouterFixture(t) |
| f.link(t) |
| p := &access.Principal{Subject: "opaque-home-alice", Client: "integration-agent-251a75", Grant: "grant-one", Teams: []string{"acme"}, Scopes: []string{"calls:read", "calls:write", "calls:share"}} |
| - if err := f.app.mountDelegated(access.VerifyFunc(func(context.Context, string) (access.Principal, error) { return *p, nil }), "acme"); err != nil { |
| + if err := f.app.mountDelegated(access.VerifyFunc(func(context.Context, string) (access.Principal, error) { return *p, nil }), "https://connect.example.test", "acme"); err != nil { |
| t.Fatal(err) |
| } |
| return f, p |
| @@ -92,7 +92,7 @@ func TestDelegatedMeetingAtomicRetryAndSealing(t *testing.T) { |
| if _, _, ok, err := f.app.store.liveLink(t.Context(), token); err != nil || !ok { |
| t.Fatal("invalid invitation", err) |
| } |
| - coords, _ := json.Marshal([]string{p.Subject, p.Client, "acme", input["client_id"].(string)}) |
| + coords, _ := json.Marshal([]string{f.app.cfg.Home.Issuer, f.app.agentIssuer, p.Subject, p.Client, "acme", input["client_id"].(string)}) |
| want, err := f.app.sealer.Fingerprint("connection.receipt.v1", string(coords)) |
| if err != nil { |
| t.Fatal(err) |