An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:art: Unify OAuth callback pages https://github.com/siyuan-note/siyuan/issues/18542
Daniel173e3859a17b
diff --git a/kernel/api/ai.go b/kernel/api/ai.goindex 67617a1905..13e96645a7 100644--- a/kernel/api/ai.go+++ b/kernel/api/ai.go@@ -18,7 +18,6 @@ package apiimport ("errors"- "html""net/http""strings"@@ -307,35 +306,19 @@ func mcpOAuthCallback(c *gin.Context) {c.Header("Content-Security-Policy", "default-src 'none'; style-src 'unsafe-inline'; base-uri 'none'; frame-ancestors 'none'")callbackError := c.Query("error")if err := mcpclient.CompleteMCPOAuth(c.Param("flowID"), c.Query("code"), c.Query("state"), callbackError, c.Query("iss")); err != nil {- c.Data(http.StatusBadRequest, "text/html; charset=utf-8", renderMCPOAuthCallbackPage(+ c.Data(http.StatusBadRequest, "text/html; charset=utf-8", util.RenderOAuthCallbackPage(util.LangToBCP47(model.Conf.Lang), model.Conf.Language(327), model.Conf.Language(328), false))return}if callbackError != "" {- c.Data(http.StatusOK, "text/html; charset=utf-8", renderMCPOAuthCallbackPage(+ c.Data(http.StatusOK, "text/html; charset=utf-8", util.RenderOAuthCallbackPage(util.LangToBCP47(model.Conf.Lang), model.Conf.Language(327), model.Conf.Language(328), false))return}- c.Data(http.StatusOK, "text/html; charset=utf-8", renderMCPOAuthCallbackPage(+ c.Data(http.StatusOK, "text/html; charset=utf-8", util.RenderOAuthCallbackPage(util.LangToBCP47(model.Conf.Lang), model.Conf.Language(325), model.Conf.Language(326), true))}-func renderMCPOAuthCallbackPage(lang, title, message string, success bool) []byte {- markClass, mark := " mark--error", "!"- if success {- markClass, mark = "", "✓"- }- return []byte(`<!doctype html><html lang="` + html.EscapeString(lang) + `"><head><meta charset="utf-8">` +- `<meta name="viewport" content="width=device-width,initial-scale=1"><title>` + html.EscapeString(title) + `</title>` +- `<style>body{margin:0;min-height:100vh;display:grid;place-items:center;background:#f5f5f5;color:#202124;font-family:system-ui,-apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif}` +- `main{box-sizing:border-box;width:min(480px,calc(100vw - 32px));padding:40px 32px;text-align:center;background:#fff;border:1px solid #ddd;border-radius:12px;box-shadow:0 8px 24px #00000014}` +- `.mark{display:grid;place-items:center;width:56px;height:56px;margin:0 auto 24px;border-radius:50%;background:#2e7d32;color:#fff;font-size:32px}.mark--error{background:#c62828}` +- `h1{margin:0 0 12px;font-size:24px;font-weight:600}p{margin:0;color:#5f6368;font-size:15px;line-height:1.7}` +- `@media(prefers-color-scheme:dark){body{background:#171717;color:#eee}main{background:#242424;border-color:#3c3c3c;box-shadow:none}p{color:#bbb}}</style>` +- `</head><body><main><div class="mark` + markClass + `" aria-hidden="true">` + mark + `</div><h1>` + html.EscapeString(title) +- `</h1><p>` + html.EscapeString(message) + `</p></main></body></html>`)-}-// reindexEmbedding 清空嵌入向量表并触发后台索引器重新计算所有块,异步执行。func reindexEmbedding(c *gin.Context) {ret := gulu.Ret.NewResult()diff --git a/kernel/api/mcp_oauth_test.go b/kernel/api/mcp_oauth_test.goindex 31a7f6ac27..c001419b9a 100644--- a/kernel/api/mcp_oauth_test.go+++ b/kernel/api/mcp_oauth_test.go@@ -19,7 +19,6 @@ package apiimport ("net/http""net/http/httptest"- "strings""testing""github.com/gin-gonic/gin"@@ -42,22 +41,6 @@ func TestPreserveMCPServerIDsForOlderFrontend(t *testing.T) {}}-func TestRenderMCPOAuthCallbackPage(t *testing.T) {- page := string(renderMCPOAuthCallbackPage("zh-CN", "已收到<script>", "返回思源 & 查看状态", true))- for _, expected := range []string{`lang="zh-CN"`, "已收到<script>", "返回思源 & 查看状态"} {- if !strings.Contains(page, expected) {- t.Fatalf("OAuth callback page does not contain %q: %s", expected, page)- }- }- if strings.Contains(page, "window.close") || strings.Contains(page, "已收到<script>") {- t.Fatalf("OAuth callback page contains unsafe or auto-close content: %s", page)- }- failurePage := string(renderMCPOAuthCallbackPage("en", "Authorization failed", "Try again", false))- if !strings.Contains(failurePage, `class="mark mark--error"`) {- t.Fatalf("OAuth failure callback page does not use the error state: %s", failurePage)- }-}-func TestMCPOAuthCallbackRejectsRemoteClientThroughLocalProxy(t *testing.T) {engine := gin.New()if err := engine.SetTrustedProxies([]string{"127.0.0.1", "::1"}); err != nil {diff --git a/kernel/model/oidc.go b/kernel/model/oidc.goindex 47e6ee7828..955772bccc 100644--- a/kernel/model/oidc.go+++ b/kernel/model/oidc.go@@ -15,7 +15,6 @@ import ("encoding/json""errors""fmt"- "html""net/http""net/url""strings"@@ -783,12 +782,13 @@ func writeOIDCCallbackPage(c *gin.Context, success bool, message string) {if success {title = oidcLanguage(366, "OIDC login completed")}- c.Header("Content-Type", "text/html; charset=utf-8")c.Header("Cache-Control", "no-store")- c.Header("Content-Security-Policy", "default-src 'none'; style-src 'unsafe-inline'")+ c.Header("Content-Security-Policy", "default-src 'none'; style-src 'unsafe-inline'; base-uri 'none'; frame-ancestors 'none'")c.Header("Referrer-Policy", "no-referrer")c.Header("X-Content-Type-Options", "nosniff")- c.String(http.StatusOK, "<!doctype html><html><head><meta charset=\"utf-8\"><meta name=\"viewport\" "+- "content=\"width=device-width,initial-scale=1\"><title>%s</title></head><body><main><h1>%s</h1><p>%s</p></main></body></html>",- html.EscapeString(title), html.EscapeString(title), html.EscapeString(message))+ lang := "en"+ if Conf != nil {+ lang = util.LangToBCP47(Conf.Lang)+ }+ c.Data(http.StatusOK, "text/html; charset=utf-8", util.RenderOAuthCallbackPage(lang, title, message, success))}diff --git a/kernel/model/oidc_test.go b/kernel/model/oidc_test.goindex 99dad99a23..617e91a94b 100644--- a/kernel/model/oidc_test.go+++ b/kernel/model/oidc_test.go@@ -305,6 +305,26 @@ func TestOIDCTransactionExpiryAndSafeRedirect(t *testing.T) {}}+func TestWriteOIDCCallbackPageUsesSharedOAuthStyle(t *testing.T) {+ setupOIDCTest(t)+ Conf.Lang = "zh_CN"+ recorder := httptest.NewRecorder()+ context, _ := gin.CreateTestContext(recorder)++ writeOIDCCallbackPage(context, false, "登录失败<script>")++ page := recorder.Body.String()+ for _, expected := range []string{`lang="zh-CN"`, `class="mark mark--error"`, "登录失败<script>"} {+ if !strings.Contains(page, expected) {+ t.Fatalf("OIDC callback page does not contain %q: %s", expected, page)+ }+ }+ if !strings.Contains(recorder.Header().Get("Content-Security-Policy"), "frame-ancestors 'none'") {+ t.Fatalf("OIDC callback page has an incomplete content security policy: %s",+ recorder.Header().Get("Content-Security-Policy"))+ }+}+func TestOIDCTransactionLimitsRejectWithoutEviction(t *testing.T) {setupOIDCTest(t)newTransaction := func(state, binding, clientIP string) *oidcTransaction {diff --git a/kernel/util/oauth_callback.go b/kernel/util/oauth_callback.gonew file mode 100644index 0000000000..cea69876da--- /dev/null+++ b/kernel/util/oauth_callback.go@@ -0,0 +1,36 @@+// SiYuan - Refactor your thinking+// Copyright (c) 2020-present, b3log.org+//+// This program is free software: you can redistribute it and/or modify+// it under the terms of the GNU Affero General Public License as published by+// the Free Software Foundation, either version 3 of the License, or+// (at your option) any later version.+//+// This program is distributed in the hope that it will be useful,+// but WITHOUT ANY WARRANTY; without even the implied warranty of+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+// GNU Affero General Public License for more details.+//+// You should have received a copy of the GNU Affero General Public License+// along with this program. If not, see <https://www.gnu.org/licenses/>.++package util++import "html"++// RenderOAuthCallbackPage 渲染 OAuth 和 OIDC 共用的回调结果页面。+func RenderOAuthCallbackPage(lang, title, message string, success bool) []byte {+ markClass, mark := " mark--error", "!"+ if success {+ markClass, mark = "", "✓"+ }+ return []byte(`<!doctype html><html lang="` + html.EscapeString(lang) + `"><head><meta charset="utf-8">` ++ `<meta name="viewport" content="width=device-width,initial-scale=1"><title>` + html.EscapeString(title) + `</title>` ++ `<style>body{margin:0;min-height:100vh;display:grid;place-items:center;background:#f5f5f5;color:#202124;font-family:system-ui,-apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif}` ++ `main{box-sizing:border-box;width:min(480px,calc(100vw - 32px));padding:40px 32px;text-align:center;background:#fff;border:1px solid #ddd;border-radius:12px;box-shadow:0 8px 24px #00000014}` ++ `.mark{display:grid;place-items:center;width:56px;height:56px;margin:0 auto 24px;border-radius:50%;background:#2e7d32;color:#fff;font-size:32px}.mark--error{background:#c62828}` ++ `h1{margin:0 0 12px;font-size:24px;font-weight:600}p{margin:0;color:#5f6368;font-size:15px;line-height:1.7}` ++ `@media(prefers-color-scheme:dark){body{background:#171717;color:#eee}main{background:#242424;border-color:#3c3c3c;box-shadow:none}p{color:#bbb}}</style>` ++ `</head><body><main><div class="mark` + markClass + `" aria-hidden="true">` + mark + `</div><h1>` + html.EscapeString(title) ++ `</h1><p>` + html.EscapeString(message) + `</p></main></body></html>`)+}diff --git a/kernel/util/oauth_callback_test.go b/kernel/util/oauth_callback_test.gonew file mode 100644index 0000000000..b10b8b89ec--- /dev/null+++ b/kernel/util/oauth_callback_test.go@@ -0,0 +1,38 @@+// SiYuan - Refactor your thinking+// Copyright (c) 2020-present, b3log.org+//+// This program is free software: you can redistribute it and/or modify+// it under the terms of the GNU Affero General Public License as published by+// the Free Software Foundation, either version 3 of the License, or+// (at your option) any later version.+//+// This program is distributed in the hope that it will be useful,+// but WITHOUT ANY WARRANTY; without even the implied warranty of+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+// GNU Affero General Public License for more details.+//+// You should have received a copy of the GNU Affero General Public License+// along with this program. If not, see <https://www.gnu.org/licenses/>.++package util++import (+ "strings"+ "testing"+)++func TestRenderOAuthCallbackPage(t *testing.T) {+ page := string(RenderOAuthCallbackPage("zh-CN", "已收到<script>", "返回思源 & 查看状态", true))+ for _, expected := range []string{`lang="zh-CN"`, "已收到<script>", "返回思源 & 查看状态", `class="mark"`} {+ if !strings.Contains(page, expected) {+ t.Fatalf("OAuth callback page does not contain %q: %s", expected, page)+ }+ }+ if strings.Contains(page, "window.close") || strings.Contains(page, "已收到<script>") {+ t.Fatalf("OAuth callback page contains unsafe or auto-close content: %s", page)+ }+ failurePage := string(RenderOAuthCallbackPage("en", "Authorization failed", "Try again", false))+ if !strings.Contains(failurePage, `class="mark mark--error"`) {+ t.Fatalf("OAuth failure callback page does not use the error state: %s", failurePage)+ }+}