fix(coderd): match query components when selecting the MCP OAuth2 resource

A Protected Resource Metadata resource with a query no longer covers a
server URL with a different query, and at equal path length a resource
with the server's query beats one without a query. A resource without a
query still covers server URLs with any query.
This commit is contained in:
Michael Suchacz committed 2026-10-12 14:27:41 +00:00
1 parent 4b54ffb5f9
commit bf0879560c
2 files changed
+32 -2

No files matched your search

+11 -2
View File
@@ -1827,11 +1827,15 @@ func mcpOAuth2ResourceBinding(resource string) string {
// mcpOAuth2ResourceIndicator picks the RFC 8707 resource indicator for
// an MCP server: the most specific advertised Protected Resource
// Metadata resource that covers the server URL, stored verbatim, else
// the server URL in canonical form. This matches the MCP Python SDK.
// the server URL in canonical form. As in the MCP Python SDK, a resource
// covers URLs on its origin under its path. Unlike the SDK, a resource
// with a query covers only URLs with the same query, which can select a
// tenant.
func mcpOAuth2ResourceIndicator(serverURL *url.URL, advertised []string) string {
serverPath := withTrailingSlash(serverURL.Path)
best := ""
bestPathLen := -1
bestHasQuery := false
for _, raw := range advertised {
candidate := strings.TrimSpace(raw)
parsed, err := parseMCPOAuth2Resource(candidate)
@@ -1841,13 +1845,18 @@ func mcpOAuth2ResourceIndicator(serverURL *url.URL, advertised []string) string
if !strings.EqualFold(parsed.Scheme, serverURL.Scheme) || !strings.EqualFold(parsed.Host, serverURL.Host) {
continue
}
hasQuery := parsed.RawQuery != ""
if hasQuery && parsed.RawQuery != serverURL.RawQuery {
continue
}
entryPath := withTrailingSlash(parsed.Path)
if !strings.HasPrefix(serverPath, entryPath) {
continue
}
if len(entryPath) > bestPathLen {
if len(entryPath) > bestPathLen || (len(entryPath) == bestPathLen && hasQuery && !bestHasQuery) {
best = candidate
bestPathLen = len(entryPath)
bestHasQuery = hasQuery
}
}
if best != "" {
+21
View File
@@ -324,6 +324,27 @@ func TestMCPOAuth2ResourceIndicator(t *testing.T) {
},
want: "https://mcp.example.com/a/b",
},
{
name: "QueryMustMatch",
serverURL: "https://mcp.example.com/mcp?tenant=b",
advertised: []string{
"https://mcp.example.com/mcp?tenant=a",
"https://mcp.example.com/mcp?tenant=b",
},
want: "https://mcp.example.com/mcp?tenant=b",
},
{
name: "MatchingQueryBeatsNoQuery",
serverURL: "https://mcp.example.com/mcp?tenant=b",
advertised: []string{"https://mcp.example.com/mcp", "https://mcp.example.com/mcp?tenant=b"},
want: "https://mcp.example.com/mcp?tenant=b",
},
{
name: "NoQueryCoversQuery",
serverURL: "https://mcp.example.com/mcp?tenant=a",
advertised: []string{"https://mcp.example.com/mcp"},
want: "https://mcp.example.com/mcp",
},
{
name: "NoEntriesFallsBack",
serverURL: "https://mcp.example.com/mcp",