go-sdk icon indicating copy to clipboard operation
go-sdk copied to clipboard

auth: OAuth client rejects protected resource metadata when the MCP server URL has a query string

Open dd-luckylanding opened this issue 2 weeks ago • 2 comments

Describe the bug

The OAuth client fails to authorize against an MCP server whose URL carries a query string, when the server's Protected Resource Metadata names the resource without that query.

AuthorizationCodeHandler passes req.URL.String(), query included, as the resource URL. oauthex.GetProtectedResourceMetadata then compares it to the metadata's resource with !=, so every candidate is refused and the handler falls back to treating the server root as the authorization server.

RFC 8707 section 2 says a resource indicator "SHOULD NOT include a query component", so a server that takes configuration in its URL (for example https://mcp.example.com/mcp?features=flags) publishes "resource": "https://mcp.example.com/mcp", and its WWW-Authenticate challenge points at metadata for that resource.

This is the same strict comparison as #886 (trailing slash), for the query component.

To Reproduce

func TestQueryInServerURL(t *testing.T) {
	var base string
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
		w.Header().Set("Content-Type", "application/json")
		_ = json.NewEncoder(w).Encode(map[string]any{
			"resource":              base + "/mcp",
			"authorization_servers": []string{"https://as.example"},
		})
	}))
	defer srv.Close()
	base = srv.URL
	meta := base + "/.well-known/oauth-protected-resource/mcp"

	if _, err := oauthex.GetProtectedResourceMetadata(context.Background(), meta, base+"/mcp", srv.Client()); err != nil {
		t.Fatal(err) // passes
	}
	if _, err := oauthex.GetProtectedResourceMetadata(context.Background(), meta, base+"/mcp?features=flags", srv.Client()); err != nil {
		t.Fatal(err) // got metadata resource ".../mcp", want ".../mcp?features=flags"
	}
}

Expected behavior

The server URL's query (and fragment) is dropped before the resource is compared and before it is sent as the resource parameter.

The other official SDKs accept this case:

  • TypeScript: checkResourceAllowed compares origin and path only.
  • Python: check_resource_allowed does the same, and uses the metadata's resource.
  • Rust: is_resource_identifier_valid notes "Query parameters may carry transport hints rather than resource identity."

Additional context

  • go-sdk v1.8.0; main has the same code (oauthex/resource_meta.go, auth/authorization_code.go).
  • protectedResourceMetadataURLs copies the URL with its query too, so the well-known candidates it builds also carry the query.

dd-luckylanding avatar Nov 12 '26 10:11 dd-luckylanding

I'd like to work on this issue.

beiryu avatar Nov 12 '26 16:11 beiryu

I would close as working as intended, following the same rationale as #886.

Per RFC 9728 §3.3, the resource value in Protected Resource Metadata MUST be identical (RFC 3986 §6.2.1 simple string comparison) to the resource/request URL, and RFC 9728 §3.1 preserves query components when constructing path-based .well-known URLs. While RFC 8707 §2 says a resource URI SHOULD NOT include a query component, that same sentence explicitly notes that query components are valid and necessary when used to scope requests to an application or tenant, so unconditionally stripping or ignoring query parameters in GetProtectedResourceMetadata would violate RFC 9728 §3.3 and weaken resource/audience isolation for query-scoped endpoints.

guglielmo-san avatar Nov 13 '26 08:11 guglielmo-san