diff --git a/coderd/mcp.go b/coderd/mcp.go index 36621fdef11..bf7035fc6a9 100644 --- a/coderd/mcp.go +++ b/coderd/mcp.go @@ -11,6 +11,7 @@ import ( "fmt" "io" "net/http" + "net/netip" "net/url" "slices" "strings" @@ -1792,9 +1793,12 @@ func parseMCPOAuth2Resource(resource string) (*url.URL, error) { return nil, xerrors.New("oauth2_resource must be a valid URI") } // url.Parse validates an IP literal host, but it also accepts "[" and - // "]" elsewhere, where RFC 3986 does not allow them. + // "]" elsewhere and a zone identifier in an IP literal, neither of + // which RFC 3986 allows. + ipLiteral := strings.HasPrefix(parsed.Host, "[") if strings.ContainsAny(parsed.Opaque+parsed.EscapedPath()+parsed.RawQuery, "[]") || - (!strings.HasPrefix(parsed.Host, "[") && strings.ContainsAny(parsed.Host, "[]")) { + (!ipLiteral && strings.ContainsAny(parsed.Host, "[]")) || + (ipLiteral && strings.Contains(parsed.Host, "%")) { return nil, xerrors.New("oauth2_resource must be a valid URI") } if !parsed.IsAbs() { @@ -1902,13 +1906,14 @@ func mcpOAuth2ResourceBinding(resource string) string { // 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, or "" when that is not a valid -// 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, even an empty one, covers only URLs with -// the same query, which can select a tenant. +// resource indicator. As in the MCP SDKs, a resource covers URLs on its +// origin at or under its path, so "/mcp" covers "/mcp/" but "/mcp/" does +// not cover "/mcp". Paths and queries compare in RFC 3986 normal form, +// where "%2F" does not end a segment, and IP addresses compare by value. +// Unlike the SDKs, 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)) + serverPath := comparableURIPath(serverURL) serverQuery, serverHasQuery := comparableURIQuery(serverURL) best := "" bestPathLen := -1 @@ -1926,8 +1931,8 @@ func mcpOAuth2ResourceIndicator(serverURL *url.URL, advertised []string) string if hasQuery && (!serverHasQuery || query != serverQuery) { continue } - entryPath := withTrailingSlash(comparableURIPath(parsed)) - if !strings.HasPrefix(serverPath, entryPath) { + entryPath := comparableURIPath(parsed) + if entryPath != serverPath && !strings.HasPrefix(serverPath, withTrailingSlash(entryPath)) { continue } if len(entryPath) > bestPathLen || (len(entryPath) == bestPathLen && hasQuery && !bestHasQuery) { @@ -1957,13 +1962,18 @@ func mcpOAuth2ResourceIndicator(serverURL *url.URL, advertised []string) string // comparableURIPath returns u's escaped path with percent-encodings // normalized and then dot segments removed, so "%2E%2E" is a dot segment. +// An empty path after an authority is "/" (RFC 3986 section 6.2.3). func comparableURIPath(u *url.URL) string { normalized := *u // Normalizing escapes leaves the unescaped Path unchanged, so this // RawPath stays a valid encoding of it. normalized.RawPath = normalizePercentEncoding(u.EscapedPath()) // Resolving a URL against itself removes dot segments and keeps escapes. - return normalized.ResolveReference(&normalized).EscapedPath() + path := normalized.ResolveReference(&normalized).EscapedPath() + if path == "" && u.Host != "" { + return "/" + } + return path } // comparableURIQuery returns u's query with percent-encodings normalized @@ -1977,10 +1987,21 @@ func comparableURIQuery(u *url.URL) (string, bool) { // port, where an omitted port is the scheme's default. func sameOrigin(a, b *url.URL) bool { return strings.EqualFold(a.Scheme, b.Scheme) && - strings.EqualFold(a.Hostname(), b.Hostname()) && + sameHost(a.Hostname(), b.Hostname()) && portOrDefault(a) == portOrDefault(b) } +// sameHost compares IP addresses by value, so "::1" equals +// "0:0:0:0:0:0:0:1", and other hosts case-insensitively. +func sameHost(a, b string) bool { + addrA, errA := netip.ParseAddr(a) + addrB, errB := netip.ParseAddr(b) + if errA == nil && errB == nil { + return addrA == addrB + } + return strings.EqualFold(a, b) +} + func portOrDefault(u *url.URL) string { if port := u.Port(); port != "" { return port diff --git a/coderd/mcp_internal_test.go b/coderd/mcp_internal_test.go index 285f267c531..491d939ebfb 100644 --- a/coderd/mcp_internal_test.go +++ b/coderd/mcp_internal_test.go @@ -302,6 +302,18 @@ func TestMCPOAuth2ResourceIndicator(t *testing.T) { advertised: []string{"https://mcp.example.com/mcp"}, want: "https://mcp.example.com/mcp", }, + { + name: "TrailingSlashResourceDoesNotCoverBarePath", + serverURL: "https://mcp.example.com/mcp", + advertised: []string{"https://mcp.example.com/mcp/"}, + want: "https://mcp.example.com/mcp", + }, + { + name: "EmptyPathEqualsSlash", + serverURL: "https://mcp.example.com", + advertised: []string{"https://mcp.example.com/"}, + want: "https://mcp.example.com/", + }, { name: "PathPrefixIsNotSegment", serverURL: "https://mcp.example.com/mcp2", @@ -416,6 +428,12 @@ func TestMCPOAuth2ResourceIndicator(t *testing.T) { advertised: []string{"https://[::1]:8443/"}, want: "https://[::1]:8443/", }, + { + name: "EquivalentIPv6Literal", + serverURL: "https://[0:0:0:0:0:0:0:1]/mcp", + advertised: []string{"https://[::1]/mcp"}, + want: "https://[::1]/mcp", + }, { name: "NoEntriesFallsBack", serverURL: "https://mcp.example.com/mcp", diff --git a/coderd/mcp_test.go b/coderd/mcp_test.go index 479e4757b15..7366407c323 100644 --- a/coderd/mcp_test.go +++ b/coderd/mcp_test.go @@ -1377,6 +1377,9 @@ func TestMCPServerConfigsUserOIDCClearsFields(t *testing.T) { "https://mcp.example.com/mcp?tenant=%zz", "https://mcp.example.com/a[b]", "https://a]b.example.com/mcp", + "https://[x]/mcp", + "https://[::1][x]/mcp", + "https://[fe80::1%25en0]/mcp", "https://user@mcp.example.com/mcp", } { _, err = client.UpdateMCPServerConfig(ctx, created.OrganizationID, created.ID, codersdk.UpdateMCPServerConfigRequest{ diff --git a/docs/ai-coder/agents/platform-controls/mcp-servers.md b/docs/ai-coder/agents/platform-controls/mcp-servers.md index 011d77f8f00..c47c7a4446c 100644 --- a/docs/ai-coder/agents/platform-controls/mcp-servers.md +++ b/docs/ai-coder/agents/platform-controls/mcp-servers.md @@ -94,7 +94,7 @@ The revocation endpoint must use HTTPS. Loopback URLs may use HTTP for local development and tests. When `oauth2_resource` is set, Coder sends it as the RFC 8707 `resource` parameter on authorization, token, and refresh requests. -The value must be an absolute URI without a fragment. +The value must be an absolute URI without a fragment or user information. A manual configuration sends no `resource` parameter unless you set this field. Leave it empty for providers that reject the parameter, such as Microsoft Entra ID.