fix(coderd): treat an empty query as a query when matching MCP OAuth2 resources

An advertised resource ending in "?" has an empty query, which RFC 3986
distinguishes from no query. It no longer covers server URLs with
another query or with no query.
This commit is contained in:
Michael Suchacz committed 2026-10-12 16:13:04 +00:00
1 parent 5c23fe00dc
commit 72c019ff5e
2 files changed
+24 -6

No files matched your search

+12 -6
View File
@@ -1905,11 +1905,11 @@ func mcpOAuth2ResourceBinding(resource string) string {
// resource indicator. As in the MCP Python SDK, a resource
// covers URLs on its origin under its path. Paths and queries compare in
// RFC 3986 normal form, where "%2F" does not end a segment. Unlike the
// SDK, a resource with a query covers only URLs with the same query,
// which can select a tenant.
// SDK, a resource with a query, even an empty one, covers only URLs with
// the same query, which can select a tenant.
func mcpOAuth2ResourceIndicator(serverURL *url.URL, advertised []string) string {
serverPath := withTrailingSlash(comparableURIPath(serverURL))
serverQuery := normalizePercentEncoding(serverURL.RawQuery)
serverQuery, serverHasQuery := comparableURIQuery(serverURL)
best := ""
bestPathLen := -1
bestHasQuery := false
@@ -1922,9 +1922,8 @@ func mcpOAuth2ResourceIndicator(serverURL *url.URL, advertised []string) string
if !sameOrigin(parsed, serverURL) {
continue
}
query := normalizePercentEncoding(parsed.RawQuery)
hasQuery := query != ""
if hasQuery && query != serverQuery {
query, hasQuery := comparableURIQuery(parsed)
if hasQuery && (!serverHasQuery || query != serverQuery) {
continue
}
entryPath := withTrailingSlash(comparableURIPath(parsed))
@@ -1967,6 +1966,13 @@ func comparableURIPath(u *url.URL) string {
return normalized.ResolveReference(&normalized).EscapedPath()
}
// comparableURIQuery returns u's query with percent-encodings normalized
// and whether u has a query. A trailing "?" is an empty query, which RFC
// 3986 distinguishes from no query.
func comparableURIQuery(u *url.URL) (string, bool) {
return normalizePercentEncoding(u.RawQuery), u.RawQuery != "" || u.ForceQuery
}
// sameOrigin reports whether two URLs have the same scheme, host, and
// port, where an omitted port is the scheme's default.
func sameOrigin(a, b *url.URL) bool {
+12
View File
@@ -345,6 +345,18 @@ func TestMCPOAuth2ResourceIndicator(t *testing.T) {
advertised: []string{"https://mcp.example.com/mcp"},
want: "https://mcp.example.com/mcp",
},
{
name: "EmptyQueryIsNotOtherQuery",
serverURL: "https://mcp.example.com/mcp?tenant=b",
advertised: []string{"https://mcp.example.com/mcp?"},
want: "https://mcp.example.com/mcp?tenant=b",
},
{
name: "EmptyQueryIsNotNoQuery",
serverURL: "https://mcp.example.com/mcp",
advertised: []string{"https://mcp.example.com/mcp?"},
want: "https://mcp.example.com/mcp",
},
{
name: "ExplicitDefaultPortOnServer",
serverURL: "https://mcp.example.com:443/mcp",