Skip to content

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

:lock: Require admin role on MCP route and block Publish anon access to admin paths

Danieldf51c2bda696
diff --git a/kernel/mcp/server.go b/kernel/mcp/server.go
index 0a133a4836..b97570574c 100644
--- a/kernel/mcp/server.go
+++ b/kernel/mcp/server.go
@@ -26,11 +26,13 @@ import (
)
func Serve(ginServer *gin.Engine) {
- ginServer.POST("/mcp", model.CheckAuth, handlePost)
- ginServer.GET("/mcp", model.CheckAuth, func(c *gin.Context) {
+ // MCP 工具暴露任意工作区文件读写删、SQL、插件分发等管理级原语,必须要求管理员角色,
+ // 否则 Publish 匿名模式注入的 RoleReader JWT 可经此链路越权调用全部工具。
+ ginServer.POST("/mcp", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, handlePost)
+ ginServer.GET("/mcp", model.CheckAuth, model.CheckAdminRole, func(c *gin.Context) {
c.Status(http.StatusMethodNotAllowed)
})
- ginServer.DELETE("/mcp", model.CheckAuth, handleDelete)
+ ginServer.DELETE("/mcp", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, handleDelete)
}
func handlePost(c *gin.Context) {
diff --git a/kernel/mcp/tools/file.go b/kernel/mcp/tools/file.go
index fd5c590094..24b9029a5b 100644
--- a/kernel/mcp/tools/file.go
+++ b/kernel/mcp/tools/file.go
@@ -99,6 +99,12 @@ func resolvePath(rel string) (string, error) {
if resolved := util.ResolveLongestExistingParent(abs); resolved != abs && !gulu.File.IsSubPath(util.WorkspaceDir, resolved) {
return "", fmt.Errorf("symlink escapes workspace: %s", rel)
}
+ // 禁止访问配置文件 conf/conf.json(含 accessAuthCode/api.token/cookieKey 等明文凭据),
+ // 对齐 HTTP 文件 API 的既定黑名单(见 kernel/api/file.go 的 refuseToAccess)。
+ confPath := filepath.Join(util.ConfDir, "conf.json")
+ if abs == confPath {
+ return "", fmt.Errorf("access to conf.json is forbidden")
+ }
return abs, nil
}
diff --git a/kernel/server/proxy/publish.go b/kernel/server/proxy/publish.go
index 1a09130d51..97396238f1 100644
--- a/kernel/server/proxy/publish.go
+++ b/kernel/server/proxy/publish.go
@@ -24,6 +24,7 @@ import (
"net"
"net/http"
"net/http/httputil"
+ "strings"
"time"
"github.com/siyuan-note/logging"
@@ -226,7 +227,68 @@ func (PublishServiceTransport) RoundTrip(request *http.Request) (response *http.
return
}
+ // 匿名发布模式下,匿名访问者只能访问公开内容;管理类路径(MCP、配置、系统、导入导出、
+ // 同步、仓库、插件分发等)必须拒绝,否则会注入 RoleReader JWT 后被内核侧误判为合法调用。
+ if isPublishAdminPath(request.URL.Path) {
+ return &http.Response{
+ StatusCode: http.StatusUnauthorized,
+ Status: http.StatusText(http.StatusUnauthorized),
+ Proto: request.Proto,
+ ProtoMajor: request.ProtoMajor,
+ ProtoMinor: request.ProtoMinor,
+ Request: request,
+ Header: http.Header{},
+ Body: http.NoBody,
+ Close: false,
+ }, nil
+ }
+
request.Header.Set(model.XAuthTokenKey, model.GetBasicAuthAccount("").Token)
response, err = publishRoundTripper.RoundTrip(request)
return
}
+
+// isPublishAdminPath 判断请求路径是否属于管理类(写/敏感)路径,匿名发布模式下必须拒绝。
+// 公开浏览所需的读端点(/api/file/getFile、/api/filetree/getDoc、/assets/、/appearance/、/stage/ 等)
+// 不在此列表内,内核侧各 handler 已用 IsReadOnlyRoleContext 做角色级读过滤。
+func isPublishAdminPath(path string) bool {
+ // 精确匹配的管理路径前缀(带斜杠后缀,避免误伤 /api/system/version 等公开读端点)
+ for _, prefix := range []string{
+ "/mcp",
+ "/api/setting/",
+ "/api/system/",
+ "/api/import/",
+ "/api/export/",
+ "/api/archive/",
+ "/api/query/",
+ "/api/sqlite/",
+ "/api/bazaar/",
+ "/api/plugin/",
+ "/api/petal/",
+ "/api/repo/",
+ "/api/sync/",
+ "/api/history/",
+ "/api/ai/",
+ "/api/account/",
+ "/api/cloud/",
+ "/api/transactions",
+ "/webdav",
+ "/caldav",
+ "/carddav",
+ "/.well-known/caldav",
+ "/.well-known/carddav",
+ "/debug/pprof/",
+ "/plugin/private/",
+ } {
+ if strings.HasPrefix(path, prefix) {
+ return true
+ }
+ }
+ // /api/file/* 中仅写/敏感端点拒绝,保留 getFile 等读端点供公开渲染使用
+ switch path {
+ case "/api/file/putFile", "/api/file/copyFile", "/api/file/globalCopyFiles",
+ "/api/file/workspaceCopyFiles", "/api/file/removeFile", "/api/file/renameFile", "/api/file/readDir":
+ return true
+ }
+ return false
+}