fix(19.7): RC-WR-01 a backend grant acts only as the administrator it was consented for

pact.MCPGrant gains BackendUserID, the backend account the consent page
showed. pier gives an OAuth identity a backend subject (on resolution and
after a link event) only while the user's link names that account; a link
to anyone else, or no recorded account, gives none, so a link changed after
the consent never re-targets the grant.
This commit is contained in:
2026-10-23 20:51:57 +02:00
parent 0b0cae5b53
commit c37c6ce50f
9 changed files with 112 additions and 43 deletions

View File

@@ -19,8 +19,8 @@ import (
// listFixture is the two-guard application of TestToolsList: the frontend
// guard "acme" (user 8 lacks acme.echo) with a write and a read tool, the
// "backend" guard with one read tool, the verifier's grants for users 7, 8,
// 9 (read only), 10 (backend, linked to admin 42) and 11 (backend, no
// link), and the hit counters of the write route and the backend route.
// 9 (read only), 10 (backend, linked to admin 42, plus grants consented for
// admin 43 and for none) and 11 (backend, no link), and the hit counters of the write route and the backend route.
type listFixture struct {
in *instance
echoHits *atomic.Int64
@@ -39,6 +39,10 @@ func newListFixture(t *testing.T) listFixture {
verifier := newFakeVerifier(map[string]pact.MCPGrant{
"oas_at_admin": grant(10, "c4", 4, "read", "write", "backend"),
"oas_at_nolink": grant(11, "c5", 5, "read", "write", "backend"),
// User 10's grants consented for another administrator and for
// none (RC-WR-01).
"oas_at_rebound": boundGrant(10, "c8", 8, 43, "read", "write", "backend"),
"oas_at_unbound": boundGrant(10, "c9", 9, 0, "read", "write", "backend"),
})
principalOf := func(w http.ResponseWriter, r *http.Request) {
user, _ := bouncer.User(r.Context())
@@ -74,8 +78,10 @@ func newListFixture(t *testing.T) listFixture {
// TestToolsList (SC-3, D-20, D-23): tools/list is computed per principal;
// a read-only grant sees only read-only tools; a tool whose guard has no
// subject for the caller is absent; calling a tool outside the caller's
// list is a JSON-RPC error that never reaches the route; an identity with
// both a frontend and a backend subject sees the union.
// list is a JSON-RPC error that never reaches the route; a backend grant
// whose consented administrator is not the linked one gets no backend
// subject (RC-WR-01); an identity with both a frontend and a backend
// subject sees the union.
func TestToolsList(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
@@ -113,6 +119,23 @@ func TestToolsList(t *testing.T) {
}
})
t.Run("a backend grant acts only as the administrator it was consented for", func(t *testing.T) {
before := f.officeHits.Load()
for _, token := range []string{"oas_at_rebound", "oas_at_unbound"} {
cs := sdkClient(t, ctx, f.in.url(), token, clientOpts{})
if got := listNames(t, ctx, cs); !slices.Equal(got, []string{"acme_echo", "acme_show"}) {
t.Fatalf("%s (user 10, linked to admin 42) lists %v, want the frontend tools only", token, got)
}
res, err := cs.CallTool(ctx, &mcp.CallToolParams{Name: "office_posts"})
if err == nil || !strings.Contains(err.Error(), "not available to this caller") {
t.Fatalf("%s calling office_posts: %+v, %v", token, res, err)
}
}
if f.officeHits.Load() != before {
t.Fatalf("a grant consented for another administrator reached the backend route")
}
})
t.Run("unlisted call never reaches the route", func(t *testing.T) {
before, office := f.echoHits.Load(), f.officeHits.Load()
for token, tool := range map[string]string{"oas_at_other": "acme_echo", "oas_at_read": "acme_echo", "oas_at_test": "office_posts"} {