Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix(mcp): store one canonical resource form for every accepted spelling
Vincent Huangd1aff0f0dc35
diff --git a/backend/api/oauth2/grant_test.go b/backend/api/oauth2/grant_test.goindex d8ec5ed1b0..3898033854 100644--- a/backend/api/oauth2/grant_test.go+++ b/backend/api/oauth2/grant_test.go@@ -89,6 +89,18 @@ func TestResourceScopeGrantLifecycle(t *testing.T) {"the token endpoint compares against this value, so it must already be canonical")})+ t.Run("a bare origin is consented as the canonical MCP resource", func(t *testing.T) {+ // Accepting the bare origin (our unsuffixed RFC 9728 document publishes+ // it) must not put a second spelling in the grant: PR 3 binds the token+ // audience to the stored value, and two spellings would mean two+ // audiences to honor at /mcp for as long as any grant lives.+ code := consentOK(t, configured, url.Values{"resource": {"https://bb.example.com"}})++ got, err := st.GetOAuth2AuthorizationCode(ctx, testClientID, code)+ require.NoError(t, err)+ require.Equal(t, testResource, got.Config.Resource)+ })+t.Run("no resource and no scope still consents (clients that predate both)", func(t *testing.T) {code := consentOK(t, configured, url.Values{})@@ -220,6 +232,39 @@ func TestResourceScopeGrantLifecycle(t *testing.T) {require.Equal(t, "mcp:read-write", rotated.Scope)})+ t.Run("a bare-origin grant refreshes as the canonical resource, named either way", func(t *testing.T) {+ // Consent with the bare origin, then drive the whole exchange naming the+ // bare origin at every step. The stored value stays the canonical MCP+ // URI throughout, and the same normalization applies at the token end so+ // the client is never told its own spelling is wrong.+ code := consentOK(t, configured, url.Values{"resource": {"https://bb.example.com/"}})++ first := tokenOK(t, configured, url.Values{+ "grant_type": {"authorization_code"},+ "code": {code},+ "redirect_uri": {testRedirectURI},+ "code_verifier": {testCodeVerifier},+ "client_id": {testClientID},+ "resource": {"https://bb.example.com"},+ })+ stored, err := st.GetOAuth2RefreshToken(ctx, testClientID, auth.HashToken(first.RefreshToken))+ require.NoError(t, err)+ require.NotNil(t, stored)+ require.Equal(t, testResource, stored.Resource)++ second := tokenOK(t, configured, url.Values{+ "grant_type": {"refresh_token"},+ "refresh_token": {first.RefreshToken},+ "client_id": {testClientID},+ "resource": {"https://bb.example.com"},+ })+ rotated, err := st.GetOAuth2RefreshToken(ctx, testClientID, auth.HashToken(second.RefreshToken))+ require.NoError(t, err)+ require.NotNil(t, rotated)+ require.Equal(t, testResource, rotated.Resource,+ "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) {code := consentOK(t, configured, url.Values{})diff --git a/backend/api/oauth2/resource.go b/backend/api/oauth2/resource.goindex 28594b9e00..2efbc5db72 100644--- a/backend/api/oauth2/resource.go+++ b/backend/api/oauth2/resource.go@@ -123,6 +123,11 @@ func (s *Service) parseGrantParams(ctx context.Context, values url.Values) (gran// 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.+//+// 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.func checkConsentedResource(values url.Values, consented string) *oauth2Failure {requested, err := singleValue(values, "resource")if err != nil {@@ -135,7 +140,7 @@ func checkConsentedResource(values url.Values, consented string) *oauth2Failureif err != nil {return &oauth2Failure{code: "invalid_target", description: err.Error()}}- if canonical != consented {+ if canonical != consented && canonical+mcpResourcePath != consented {return &oauth2Failure{code: "invalid_target",description: "resource does not match the resource this grant was authorized for",@@ -178,13 +183,19 @@ func singleValue(values url.Values, name string) (string, error) {return got[0], nil}-// validateResource canonicalizes a client-supplied resource indicator and checks-// that it names this server. trustedBaseURL must come from the configured-// external URL; an empty one means we cannot prove anything about the value and-// yields errExternalURLNotConfigured.+// validateResource checks that a client-supplied resource indicator names this+// server and returns the form to consent to. trustedBaseURL must come from the+// configured external URL; an empty one means we cannot prove anything about the+// value and yields errExternalURLNotConfigured.+//+// Accepted inputs: the canonical MCP resource URI (<base>/mcp) and the bare+// origin (<base>) — the two values our own RFC 9728 metadata documents publish,+// so a client that read either one must not be turned away.//-// Accepted: the canonical MCP resource URI (<base>/mcp) and the bare origin-// (<base>) — the two values our own RFC 9728 metadata documents publish.+// Stored form: always <base>/mcp, whichever of the two was sent. PR 3 binds the+// access token's audience to this stored value, and two accepted spellings of one+// resource would mean two audiences to accept at /mcp — forever, since grants are+// long-lived. Normalizing at the door keeps the audience single-valued.func validateResource(resource, trustedBaseURL string) (string, error) {if trustedBaseURL == "" {return "", errExternalURLNotConfigured@@ -193,14 +204,15 @@ func validateResource(resource, trustedBaseURL string) (string, error) {if err != nil {return "", errors.Wrapf(err, "configured external URL %q is not a usable absolute URL", trustedBaseURL)}+ mcpResource := base + mcpResourcePathcanonical, err := canonicalizeResourceURI(resource)if err != nil {return "", err}- if canonical != base && canonical != base+mcpResourcePath {- return "", errors.Errorf("resource %q is not this server's MCP resource; expected %q", canonical, base+mcpResourcePath)+ if canonical != base && canonical != mcpResource {+ return "", errors.Errorf("resource %q is not this server's MCP resource; expected %q", canonical, mcpResource)}- return canonical, nil+ return mcpResource, nil}// canonicalizeResourceURI normalizes the two differences a resource URI maydiff --git a/backend/api/oauth2/resource_test.go b/backend/api/oauth2/resource_test.goindex 8909e69bcf..3c5866f159 100644--- a/backend/api/oauth2/resource_test.go+++ b/backend/api/oauth2/resource_test.go@@ -22,6 +22,9 @@ func TestValidateResource(t *testing.T) {"callers key on this sentinel to return the actionable setup error instead of a generic OAuth failure")})+ // Every accepted input resolves to the same stored form. PR 3 binds the token+ // audience to what is stored, so a second accepted spelling would become a+ // second audience to honor at /mcp for the life of every grant.accepted := []struct {name stringin string@@ -32,9 +35,10 @@ func TestValidateResource(t *testing.T) {{"scheme and host case are normalized", "HTTPS://BB.Example.COM/mcp", "https://bb.example.com/mcp"},// Our own RFC 9728 document at the unsuffixed well-known path publishes// the bare origin as `resource`, so a client that read it must not be- // rejected for using the value we advertised.- {"bare origin", "https://bb.example.com", "https://bb.example.com"},- {"bare origin with trailing slash", "https://bb.example.com/", "https://bb.example.com"},+ // rejected for using the value we advertised — but it is stored as the+ // canonical MCP URI, not echoed back as the origin.+ {"bare origin normalizes to the MCP resource", "https://bb.example.com", "https://bb.example.com/mcp"},+ {"bare origin with trailing slash", "https://bb.example.com/", "https://bb.example.com/mcp"},}for _, tc := range accepted {t.Run("accepted: "+tc.name, func(t *testing.T) {@@ -86,6 +90,22 @@ func TestValidateResource(t *testing.T) {require.NoError(t, err)require.Equal(t, "https://bb.example.com/mcp", got)})++ t.Run("every accepted spelling collapses to one stored value", func(t *testing.T) {+ stored := map[string]struct{}{}+ for _, in := range []string{+ "https://bb.example.com/mcp",+ "https://bb.example.com/mcp/",+ "HTTPS://BB.Example.COM/mcp",+ "https://bb.example.com",+ "https://bb.example.com/",+ } {+ got, err := validateResource(in, testTrustedBase)+ require.NoError(t, err)+ stored[got] = struct{}{}+ }+ require.Len(t, stored, 1, "more than one stored spelling would force PR 3 to accept multiple audiences")+ })}func TestValidateScope(t *testing.T) {@@ -139,6 +159,11 @@ func TestCheckConsentedResource(t *testing.T) {{"omitted keeps the consented resource", url.Values{}, consented, ""},{"exact match", url.Values{"resource": {consented}}, consented, ""},{"noncanonical but equivalent match", url.Values{"resource": {"HTTPS://BB.example.com/mcp/"}}, consented, ""},+ // Same normalization at both ends: consent stores <base>/mcp for a bare+ // origin, so the token endpoint must accept a bare origin against it.+ {"bare origin matches a /mcp-stored grant", url.Values{"resource": {"https://bb.example.com"}}, consented, ""},+ {"bare origin with trailing slash matches", url.Values{"resource": {"https://bb.example.com/"}}, consented, ""},+ {"bare origin of a different host still fails", url.Values{"resource": {"https://evil.example.com"}}, consented, "invalid_target"},{"different resource", url.Values{"resource": {"https://evil.example.com/mcp"}}, consented, "invalid_target"},{"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"},