Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Fix MCP OAuth consent form origin https://github.com/siyuan-note/siyuan/issues/19867

Daniele2f0dbd08346
diff --git a/kernel/api/contract_mcp_oauth_test.go b/kernel/api/contract_mcp_oauth_test.go
index 3ed7f11f11..342bbda5a9 100644
--- a/kernel/api/contract_mcp_oauth_test.go
+++ b/kernel/api/contract_mcp_oauth_test.go
@@ -127,13 +127,16 @@ func TestAPIContractMCPOAuthFlow(t *testing.T) {
if page.Code != 200 || strings.Contains(page.Body.String(), "<script>") || page.Header().Get("X-Frame-Options") != "DENY" {
t.Fatal(page.Code, page.Body.String())
}
+ if policy := page.Header().Get("Referrer-Policy"); policy != "same-origin" {
+ t.Fatalf("consent form must retain its same-origin POST Origin, got policy %q", policy)
+ }
match := regexp.MustCompile(`name="ticket" value="([^"]+)"`).FindStringSubmatch(page.Body.String())
if len(match) != 2 {
t.Fatal(page.Body.String())
}
cookies = append(cookies, page.Result().Cookies()...)
consent := url.Values{"ticket": {match[1]}, "decision": {"approve"}}
- for _, origin := range []string{"https://evil.example", ""} {
+ for _, origin := range []string{"https://evil.example", "", "null"} {
res := call("POST", "/oauth/mcp/consent", consent.Encode(), "application/x-www-form-urlencoded", "", cookies, origin)
if res.Code != 400 {
t.Fatal("cross-origin consent accepted", res.Code)
@@ -143,6 +146,9 @@ func TestAPIContractMCPOAuthFlow(t *testing.T) {
if approved.Code != 302 {
t.Fatal(approved.Code, approved.Body.String())
}
+ if policy := approved.Header().Get("Referrer-Policy"); policy != "no-referrer" {
+ t.Fatalf("callback redirect must not disclose the authorization page URL, got policy %q", policy)
+ }
callback, _ := url.Parse(approved.Header().Get("Location"))
if callback.Query().Get("iss") != "https://note.example.com" || callback.Query().Get("state") != "original" {
t.Fatal(callback)
diff --git a/kernel/api/mcp_oauth.go b/kernel/api/mcp_oauth.go
index 1a00ff2fd9..d1108bcb1d 100644
--- a/kernel/api/mcp_oauth.go
+++ b/kernel/api/mcp_oauth.go
@@ -267,6 +267,10 @@ var mcpOAuthPage = template.Must(template.New("mcp-oauth").Parse(`<!doctype html
func mcpOAuthHTML(c *gin.Context, status int, ticket string, client apicontract.MCPOAuthClient) apicontract.Response[apicontract.BinaryContent] {
mcpOAuthHeaders(c)
+ if ticket != "" {
+ // 同源表单提交需要保留 Origin;跨站导航不发送 Referer。
+ c.Header("Referrer-Policy", "same-origin")
+ }
c.Header("Content-Security-Policy", "default-src 'none'; style-src 'self' 'unsafe-inline'; frame-ancestors 'none'; base-uri 'none'")
c.Header("X-Frame-Options", "DENY")
data := map[string]string{"Title": mcpOAuthLanguage("mcpOAuthServer"), "Workspace": util.WorkspaceName, "Name": client.Name, "Redirect": client.RedirectURI,