From 72c019ff5e55f3424ca17892e46cd9459c4f3f8c Mon Sep 17 00:00:00 2001 From: Michael Suchacz <203725896+ibetitsmike@users.noreply.github.com> Date: Mon, 12 Oct 2026 16:13:04 +0000 Subject: [PATCH] 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. --- coderd/mcp.go | 18 ++++++++++++------ coderd/mcp_internal_test.go | 12 ++++++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/coderd/mcp.go b/coderd/mcp.go index 74939199c63..36621fdef11 100644 --- a/coderd/mcp.go +++ b/coderd/mcp.go @@ -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 { diff --git a/coderd/mcp_internal_test.go b/coderd/mcp_internal_test.go index ef67736a079..285f267c531 100644 --- a/coderd/mcp_internal_test.go +++ b/coderd/mcp_internal_test.go @@ -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",