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

未关闭
#1,519 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@beiryu 已经在做这个了。

开始于 2026年11月12日。

  • #1525 来自 @beiryu —— 已关闭,未合并

评估

难度
3/5
预计耗时
1-2 天
新手友好度
74/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
go

调研方向

Start with oauthex/resource_meta.go and auth/authorization_code.go, which the issue identifies as containing the URL handling and strict resource comparison. Trace how query and fragment components reach metadata candidate URLs and the resource parameter, then check the OAuth metadata tests. Done when a server URL with a query or fragment matches metadata naming the same URL without those components, and the resource parameter uses that normalized URL.

由索引模型根据 Issue 内容生成。

描述

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.
主要语言
Go
星标
5.3k
派生
614
平均合并
23 小时 55 分钟
30 天内合并 PR
62

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

modelcontextprotocol/go-sdk 的其他 Issue

查看 modelcontextprotocol/go-sdk 的全部 Issue

相似的 Issue

更多 Go Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。