Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix(mcp): keep legacy grants refreshable and reject combined scope tiers

Vincent Huangbeb43a404304
diff --git a/backend/api/oauth2/grant_test.go b/backend/api/oauth2/grant_test.go
index 3898033854..62501c91b5 100644
--- a/backend/api/oauth2/grant_test.go
+++ b/backend/api/oauth2/grant_test.go
@@ -117,9 +117,9 @@ func TestResourceScopeGrantLifecycle(t *testing.T) {
}{
{"resource on another host", url.Values{"resource": {"https://evil.example.com/mcp"}}, "invalid_target"},
{"noncanonical resource", url.Values{"resource": {"https://bb.example.com:443/mcp"}}, "invalid_target"},
- {"multiple resources", url.Values{"resource": {testResource, testResource}}, "invalid_target"},
+ {"resource parameter repeated", url.Values{"resource": {testResource, testResource}}, "invalid_target"},
{"unknown scope", url.Values{"scope": {"mcp:admin"}}, "invalid_scope"},
- {"multiple scopes", url.Values{"scope": {"mcp:read-only", "mcp:read-write"}}, "invalid_scope"},
+ {"scope parameter repeated", url.Values{"scope": {"mcp:read-only", "mcp:read-write"}}, "invalid_scope"},
}
for _, tc := range rejections {
t.Run("consent rejected: "+tc.name, func(t *testing.T) {
@@ -265,24 +265,53 @@ func TestResourceScopeGrantLifecycle(t *testing.T) {
"the canonical form must survive rotation, not drift back to what the client sent")
})
- t.Run("token exchange rejects a resource the grant never consented to", func(t *testing.T) {
+ t.Run("a grant with no consented resource survives a resource-bearing exchange", func(t *testing.T) {
+ // This is the upgrade path. A code or refresh token issued before 3.21.5
+ // has no stored resource, and RFC 8707 clients keep sending `resource` on
+ // every exchange and refresh — so rejecting the parameter here would fail
+ // in-flight codes and every live session at its next refresh. The
+ // parameter is accepted and ignored, and the grant must stay UNBOUND:
+ // accepting a binding here would be a resource the user never consented
+ // to, and would let PR 3 mint an MCP-audience token off a legacy grant.
code := consentOK(t, configured, url.Values{})
- rec := postToken(t, configured, url.Values{
+ first := tokenOK(t, configured, url.Values{
"grant_type": {"authorization_code"},
"code": {code},
"redirect_uri": {testRedirectURI},
"code_verifier": {testCodeVerifier},
"client_id": {testClientID},
"resource": {testResource},
+ "scope": {"mcp:read-write"},
})
- require.Equal(t, http.StatusBadRequest, rec.Code)
- require.Equal(t, "invalid_target", errorCode(t, rec))
+ require.Empty(t, first.Scope, "an unbound grant must not adopt the scope the client asked for")
- // Rejected before consumption: the code survives for a correct retry.
- still, err := st.GetOAuth2AuthorizationCode(ctx, testClientID, code)
+ stored, err := st.GetOAuth2RefreshToken(ctx, testClientID, auth.HashToken(first.RefreshToken))
require.NoError(t, err)
- require.NotNil(t, still, "a rejected exchange must not burn the authorization code")
+ require.NotNil(t, stored)
+ require.Empty(t, stored.Resource, "the requested resource must be ignored, never bound to a grant that never consented to one")
+ require.Empty(t, stored.Scope)
+
+ // And the same on refresh, so a legacy session keeps working indefinitely
+ // until PR 3 retires it deliberately.
+ second := tokenOK(t, configured, url.Values{
+ "grant_type": {"refresh_token"},
+ "refresh_token": {first.RefreshToken},
+ "client_id": {testClientID},
+ "resource": {testResource},
+ })
+ rotated, err := st.GetOAuth2RefreshToken(ctx, testClientID, auth.HashToken(second.RefreshToken))
+ require.NoError(t, err)
+ require.NotNil(t, rotated)
+ require.Empty(t, rotated.Resource)
+ })
+
+ t.Run("consent rejects two scope tiers in one parameter", func(t *testing.T) {
+ // Distinct from the repeated-parameter case above: one `scope` value
+ // carrying both tiers. A grant binds to a single predefined set, so a
+ // combination names nothing resolvable.
+ redirect := consent(t, configured, url.Values{"scope": {"mcp:read-only mcp:read-write"}})
+ require.Equal(t, "invalid_scope", redirect.Query().Get("error"))
})
}
diff --git a/backend/api/oauth2/resource.go b/backend/api/oauth2/resource.go
index 2efbc5db72..1a7d50f2aa 100644
--- a/backend/api/oauth2/resource.go
+++ b/backend/api/oauth2/resource.go
@@ -121,13 +121,17 @@ func (s *Service) parseGrantParams(ctx context.Context, values url.Values) (gran
// checkConsentedResource verifies a token request's `resource` against the value
// the grant was consented for. Omitting it is allowed (the consented value
-// stands); naming a different one — or one that was never consented — is not,
-// because the token would then be bound somewhere the user never approved.
+// stands); naming a different one is not, because the token would then be bound
+// somewhere the user never approved.
//
// The bare origin is accepted here too, matching what validateResource accepts at
// consent time. The equivalence is derived from the stored value rather than from
// the configured external URL on purpose: comparing against live config would make
// every outstanding grant unusable the moment an admin changes the external URL.
+//
+// A grant with no stored resource has no constraint to check, so the parameter is
+// accepted and ignored rather than rejected — see the comment on the empty case
+// below.
func checkConsentedResource(values url.Values, consented string) *oauth2Failure {
requested, err := singleValue(values, "resource")
if err != nil {
@@ -140,6 +144,17 @@ func checkConsentedResource(values url.Values, consented string) *oauth2Failure
if err != nil {
return &oauth2Failure{code: "invalid_target", description: err.Error()}
}
+ // Codes and refresh tokens issued before 3.21.5 read back with no resource,
+ // and RFC 8707 clients send `resource` on every exchange *and* every refresh.
+ // Rejecting it would fail each in-flight code and every live session at its
+ // next refresh, for up to a refresh-token lifetime after upgrade. Accept it
+ // without binding: the stored value stays empty and is what gets carried
+ // forward, so an unbound grant cannot acquire a binding here. Retiring that
+ // population is PR 3's job (spec §"PR 3": legacy refresh grants require
+ // re-consent), and it needs the audience change to do it coherently.
+ if consented == "" {
+ return nil
+ }
if canonical != consented && canonical+mcpResourcePath != consented {
return &oauth2Failure{
code: "invalid_target",
@@ -154,12 +169,18 @@ func checkConsentedResource(values url.Values, consented string) *oauth2Failure
// match exactly. Narrowing is not supported here either: the grant record is the
// authority, and changing what a session may do happens on the grant page, never
// by asking for a different scope at the token endpoint.
+//
+// A grant with no stored scope is treated the same way as one with no stored
+// resource, and for the same upgrade reason: RFC 6749 §6 lets a client send
+// `scope` on a refresh, so a pre-3.21.5 grant would start failing its refreshes.
+// The requested value is ignored rather than adopted, so the grant keeps the
+// (empty) scope it was issued with.
func checkConsentedScope(values url.Values, consented string) *oauth2Failure {
requested, err := singleValue(values, "scope")
if err != nil {
return &oauth2Failure{code: "invalid_scope", description: err.Error()}
}
- if requested == "" || requested == consented {
+ if requested == "" || requested == consented || consented == "" {
return nil
}
return &oauth2Failure{
@@ -249,13 +270,24 @@ func canonicalizeResourceURI(resource string) (string, error) {
}
// validateScope accepts an empty scope (the common case today — no client asks
-// for one) and otherwise requires every space-delimited token to be a scope we
-// actually define.
+// for one) and otherwise requires exactly one token from the known vocabulary.
+//
+// Exactly one, not a set of them: a grant is bound to a single predefined
+// permission set (proposal v2 §4 — "consent binds the grant to a predefined
+// set", stored as one set reference). The scopes are a ladder, not additive
+// permissions, so `mcp:read-only mcp:read-write` names no set that can be
+// resolved. Accepting it would persist and echo a scope string with no
+// representation in the model P1b enforces against.
func validateScope(scope string) error {
- for token := range strings.FieldsSeq(scope) {
- if !slices.Contains(knownScopes, token) {
- return errors.Errorf("unknown scope %q; supported scopes are %s", token, strings.Join(knownScopes, " "))
- }
+ tokens := strings.Fields(scope)
+ if len(tokens) == 0 {
+ return nil
+ }
+ if len(tokens) > 1 {
+ return errors.Errorf("scope must name exactly one permission set, got %d tokens; supported scopes are %s", len(tokens), strings.Join(knownScopes, " "))
+ }
+ if !slices.Contains(knownScopes, tokens[0]) {
+ return errors.Errorf("unknown scope %q; supported scopes are %s", tokens[0], strings.Join(knownScopes, " "))
}
return nil
}
diff --git a/backend/api/oauth2/resource_test.go b/backend/api/oauth2/resource_test.go
index 3c5866f159..6b822fbc3d 100644
--- a/backend/api/oauth2/resource_test.go
+++ b/backend/api/oauth2/resource_test.go
@@ -125,6 +125,20 @@ func TestValidateScope(t *testing.T) {
"an unrecognized scope must be refused, not silently dropped — a client would otherwise believe it holds something we never granted")
})
}
+
+ // A grant is bound to ONE predefined set (proposal v2 §4). The scopes are a
+ // ladder, not additive permissions, so a combination names no resolvable set
+ // — and persisting one would hand P1b a scope string it cannot map.
+ for _, scope := range []string{
+ "mcp:read-only mcp:read-write",
+ "mcp:read-write mcp:read-only",
+ "mcp:read-only mcp:read-only",
+ } {
+ t.Run("combined tiers rejected: "+scope, func(t *testing.T) {
+ require.Error(t, validateScope(scope),
+ "two known tokens are still not a set we can resolve; the tier choice is single-valued")
+ })
+ }
}
func TestSingleValue(t *testing.T) {
@@ -168,9 +182,15 @@ func TestCheckConsentedResource(t *testing.T) {
{"noncanonical value that is not equivalent", url.Values{"resource": {"https://bb.example.com:443/mcp"}}, consented, "invalid_target"},
{"malformed value", url.Values{"resource": {"not a uri"}}, consented, "invalid_target"},
{"repeated value", url.Values{"resource": {consented, consented}}, consented, "invalid_target"},
- // A grant consented without a resource cannot acquire one at the token
- // endpoint — that would be a resource binding the user never approved.
- {"resource on a grant that consented none", url.Values{"resource": {consented}}, "", "invalid_target"},
+ // A grant with no stored resource predates the column (or the client never
+ // sent one). RFC 8707 clients keep sending `resource` on every exchange
+ // and refresh, so rejecting here would fail live sessions across upgrade.
+ // Accepted and ignored — an unbound grant stays unbound, which the
+ // lifecycle test asserts end to end.
+ {"resource on a grant that consented none is accepted, not bound", url.Values{"resource": {consented}}, "", ""},
+ {"any resource on an unbound grant is accepted", url.Values{"resource": {"https://evil.example.com/mcp"}}, "", ""},
+ // Shape is still checked, so a genuinely broken value is still reported.
+ {"malformed value on an unbound grant", url.Values{"resource": {"urn:nope"}}, "", "invalid_target"},
{"omitted on a grant that consented none", url.Values{}, "", ""},
}
for _, tc := range cases {
@@ -199,7 +219,10 @@ func TestCheckConsentedScope(t *testing.T) {
{"exact match", url.Values{"scope": {"mcp:read-only"}}, "mcp:read-only", ""},
{"widening is rejected", url.Values{"scope": {"mcp:read-write"}}, "mcp:read-only", "invalid_scope"},
{"narrowing is rejected too (the grant record is the authority)", url.Values{"scope": {"mcp:read-only"}}, "mcp:read-write", "invalid_scope"},
- {"scope on a grant that consented none", url.Values{"scope": {"mcp:read-only"}}, "", "invalid_scope"},
+ // Same upgrade path as the resource: RFC 6749 §6 permits `scope` on a
+ // refresh, so a grant issued before the column must not start failing.
+ // Ignored, not adopted — the grant keeps its empty scope.
+ {"scope on a grant that consented none is accepted, not adopted", url.Values{"scope": {"mcp:read-only"}}, "", ""},
{"repeated value", url.Values{"scope": {"mcp:read-only", "mcp:read-write"}}, "mcp:read-only", "invalid_scope"},
}
for _, tc := range cases {