Skip to content

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

:lock: Block sensitive workspace files in MCP file tool https://github.com/siyuan-note/siyuan/security/advisories/GHSA-c8r8-95hg-mp34

Daniel3542530c505f
diff --git a/kernel/api/file.go b/kernel/api/file.go
index 9acaeab411..8ae3456630 100644
--- a/kernel/api/file.go
+++ b/kernel/api/file.go
@@ -24,7 +24,6 @@ import (
"net/http"
"os"
"path/filepath"
- "runtime"
"strconv"
"strings"
"time"
@@ -533,39 +532,9 @@ func getFile(c *gin.Context) {
}
func refuseToAccess(c *gin.Context, fileAbsPath string, ret *gulu.Result) bool {
- // 规范化并解析符号链接,防止通过大小写或符号链接绕过
- fileNorm := normalizeAndResolve(fileAbsPath)
-
- // 禁止访问配置文件 conf/conf.json
- confPath := normalizeAndResolve(filepath.Join(util.ConfDir, "conf.json"))
- if fileNorm == confPath {
- ret.Code = http.StatusForbidden
- ret.Msg = http.StatusText(http.StatusForbidden)
- c.JSON(http.StatusAccepted, ret)
- return true
- }
-
- // 禁止访问 data/snippets/conf.json
- snippetPath := normalizeAndResolve(filepath.Join(util.DataDir, "snippets", "conf.json"))
- if fileNorm == snippetPath {
- ret.Code = http.StatusForbidden
- ret.Msg = http.StatusText(http.StatusForbidden)
- c.JSON(http.StatusAccepted, ret)
- return true
- }
-
- // 禁止访问 data/templates 目录
- templatesBase := normalizeAndResolve(filepath.Join(util.DataDir, "templates"))
- if gulu.File.IsSubPath(templatesBase, fileNorm) {
- ret.Code = http.StatusForbidden
- ret.Msg = http.StatusText(http.StatusForbidden)
- c.JSON(http.StatusAccepted, ret)
- return true
- }
-
- // 禁止访问 data/.siyuan/publishAccess.json
- publishAccessPath := normalizeAndResolve(filepath.Join(util.DataDir, ".siyuan", "publishAccess.json"))
- if fileNorm == publishAccessPath {
+ // 禁止访问敏感文件(conf/conf.json、data/snippets/conf.json、data/templates、data/.siyuan/publishAccess.json),
+ // 规范化与符号链接解析见 util.NormalizeAndResolve,防止通过大小写或符号链接绕过
+ if util.IsForbiddenAbsPath(fileAbsPath) {
ret.Code = http.StatusForbidden
ret.Msg = http.StatusText(http.StatusForbidden)
c.JSON(http.StatusAccepted, ret)
@@ -584,22 +553,6 @@ func refuseToAccess(c *gin.Context, fileAbsPath string, ret *gulu.Result) bool {
return false
}
-// normalizeAndResolve 将路径转为绝对、解析符号链接并清理;在需要时转为小写以实现不区分大小写比较
-func normalizeAndResolve(p string) string {
- if abs, err := filepath.Abs(p); err == nil {
- p = abs
- }
- if eval, err := filepath.EvalSymlinks(p); err == nil {
- p = eval
- }
- p = filepath.Clean(p)
- // 在 Windows 和 macOS 上文件系统通常为不区分大小写,使用小写统一比较
- if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
- p = strings.ToLower(p)
- }
- return p
-}
-
func readDir(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)
diff --git a/kernel/mcp/tools/file.go b/kernel/mcp/tools/file.go
index 081ea8db12..d9160483a0 100644
--- a/kernel/mcp/tools/file.go
+++ b/kernel/mcp/tools/file.go
@@ -99,11 +99,10 @@ 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")
+ // 禁止访问敏感文件(conf/conf.json、data/snippets/conf.json、data/templates、data/.siyuan/publishAccess.json),
+ // 与 HTTP 文件 API 共用同一黑名单(见 kernel/util/path_guard.go 的 IsForbiddenAbsPath)
+ if util.IsForbiddenAbsPath(abs) {
+ return "", fmt.Errorf("access to sensitive workspace file is forbidden: %s", rel)
}
return abs, nil
}
diff --git a/kernel/util/path_guard.go b/kernel/util/path_guard.go
new file mode 100644
index 0000000000..ee354bec58
--- /dev/null
+++ b/kernel/util/path_guard.go
@@ -0,0 +1,73 @@
+// SiYuan - From thought to insight, with agents
+// 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 (
+ "path/filepath"
+ "runtime"
+ "strings"
+
+ "github.com/88250/gulu"
+)
+
+// IsForbiddenAbsPath 判断绝对路径是否为敏感路径,HTTP 文件 API(kernel/api/file.go 的 refuseToAccess)
+// 与 MCP 文件工具(kernel/mcp/tools/file.go 的 resolvePath)共用同一黑名单:
+// conf/conf.json、data/snippets/conf.json、data/templates 目录以及 data/.siyuan/publishAccess.json。
+func IsForbiddenAbsPath(abs string) bool {
+ fileNorm := NormalizeAndResolve(abs)
+
+ // 禁止访问配置文件 conf/conf.json(含 accessAuthCode/api.token/cookieKey 等明文凭据)
+ confPath := NormalizeAndResolve(filepath.Join(ConfDir, "conf.json"))
+ if fileNorm == confPath {
+ return true
+ }
+
+ // 禁止访问 data/snippets/conf.json
+ snippetPath := NormalizeAndResolve(filepath.Join(DataDir, "snippets", "conf.json"))
+ if fileNorm == snippetPath {
+ return true
+ }
+
+ // 禁止访问 data/templates 目录(含目录本身及其全部子路径)
+ templatesBase := NormalizeAndResolve(filepath.Join(DataDir, "templates"))
+ if fileNorm == templatesBase || gulu.File.IsSubPath(templatesBase, fileNorm) {
+ return true
+ }
+
+ // 禁止访问 data/.siyuan/publishAccess.json(含发布模式明文访问密码)
+ publishAccessPath := NormalizeAndResolve(filepath.Join(DataDir, ".siyuan", "publishAccess.json"))
+ if fileNorm == publishAccessPath {
+ return true
+ }
+ return false
+}
+
+// NormalizeAndResolve 将路径转为绝对、解析符号链接并清理;在需要时转为小写以实现不区分大小写比较
+func NormalizeAndResolve(p string) string {
+ if abs, err := filepath.Abs(p); err == nil {
+ p = abs
+ }
+ if eval, err := filepath.EvalSymlinks(p); err == nil {
+ p = eval
+ }
+ p = filepath.Clean(p)
+ // 在 Windows 和 macOS 上文件系统通常为不区分大小写,使用小写统一比较
+ if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
+ p = strings.ToLower(p)
+ }
+ return p
+}
diff --git a/kernel/util/path_guard_test.go b/kernel/util/path_guard_test.go
new file mode 100644
index 0000000000..01dbb4e1a2
--- /dev/null
+++ b/kernel/util/path_guard_test.go
@@ -0,0 +1,124 @@
+// SiYuan - From thought to insight, with agents
+// 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 (
+ "os"
+ "path/filepath"
+ "runtime"
+ "strings"
+ "testing"
+)
+
+// TestIsForbiddenAbsPath 覆盖 HTTP 文件 API 与 MCP 文件工具共用的敏感路径黑名单:
+// conf/conf.json、data/snippets/conf.json、data/templates 目录以及 data/.siyuan/publishAccess.json。
+func TestIsForbiddenAbsPath(t *testing.T) {
+ tmpWorkspace := t.TempDir()
+ origWorkspace, origConf, origData := WorkspaceDir, ConfDir, DataDir
+ WorkspaceDir = tmpWorkspace
+ ConfDir = filepath.Join(tmpWorkspace, "conf")
+ DataDir = filepath.Join(tmpWorkspace, "data")
+ t.Cleanup(func() { WorkspaceDir, ConfDir, DataDir = origWorkspace, origConf, origData })
+
+ cases := []struct {
+ name string
+ rel string // 相对工作空间的路径
+ }{
+ {"conf", filepath.Join("conf", "conf.json")},
+ {"snippets conf", filepath.Join("data", "snippets", "conf.json")},
+ {"templates dir", filepath.Join("data", "templates")},
+ {"templates file", filepath.Join("data", "templates", "a.txt")},
+ {"publish access", filepath.Join("data", ".siyuan", "publishAccess.json")},
+ }
+ for _, c := range cases {
+ abs := filepath.Join(tmpWorkspace, c.rel)
+ if got := IsForbiddenAbsPath(abs); !got {
+ t.Errorf("IsForbiddenAbsPath(%q) = false, want true [%s]", abs, c.name)
+ }
+ }
+
+ // 工作空间内的合法文件不应被误判。
+ allowed := []string{
+ filepath.Join(tmpWorkspace, "data", "assets", "image.png"),
+ filepath.Join(tmpWorkspace, "data", "snippets", "custom.css"),
+ filepath.Join(tmpWorkspace, "data", "plugins", "example", "main.js"),
+ filepath.Join(tmpWorkspace, "temp", "siyuan", "kernel.log"),
+ }
+ for _, p := range allowed {
+ if got := IsForbiddenAbsPath(p); got {
+ t.Errorf("IsForbiddenAbsPath(%q) = true, want false (workspace file)", p)
+ }
+ }
+}
+
+// TestIsForbiddenAbsPathCaseInsensitive 在大小写不敏感的文件系统上验证大小写变体无法绕过黑名单。
+func TestIsForbiddenAbsPathCaseInsensitive(t *testing.T) {
+ if runtime.GOOS != "windows" && runtime.GOOS != "darwin" {
+ t.Skip("case-insensitive comparison only on windows/darwin")
+ }
+ tmpWorkspace := t.TempDir()
+ origWorkspace, origConf, origData := WorkspaceDir, ConfDir, DataDir
+ WorkspaceDir = tmpWorkspace
+ ConfDir = filepath.Join(tmpWorkspace, "conf")
+ DataDir = filepath.Join(tmpWorkspace, "data")
+ t.Cleanup(func() { WorkspaceDir, ConfDir, DataDir = origWorkspace, origConf, origData })
+
+ // 模拟 MCP 工具输入的大小写变体路径(Windows 上 CONF.JSON 与 conf.json 指向同一文件)。
+ cases := []string{
+ filepath.Join(tmpWorkspace, "CONF", "CONF.JSON"),
+ filepath.Join(tmpWorkspace, "DATA", "SNIPPETS", "CONF.JSON"),
+ filepath.Join(tmpWorkspace, "DATA", ".SIYUAN", "PUBLISHACCESS.JSON"),
+ strings.ToUpper(filepath.Join(tmpWorkspace, "data", "templates")),
+ }
+ for _, p := range cases {
+ if got := IsForbiddenAbsPath(p); !got {
+ t.Errorf("IsForbiddenAbsPath(%q) = false, want true (case variant)", p)
+ }
+ }
+}
+
+// TestIsForbiddenAbsPathSymlinkBypass 验证通过符号链接指向敏感文件的路径会被拦截。
+func TestIsForbiddenAbsPathSymlinkBypass(t *testing.T) {
+ tmpWorkspace := t.TempDir()
+ origWorkspace, origConf, origData := WorkspaceDir, ConfDir, DataDir
+ WorkspaceDir = tmpWorkspace
+ ConfDir = filepath.Join(tmpWorkspace, "conf")
+ DataDir = filepath.Join(tmpWorkspace, "data")
+ t.Cleanup(func() { WorkspaceDir, ConfDir, DataDir = origWorkspace, origConf, origData })
+
+ target := filepath.Join(tmpWorkspace, "data", ".siyuan", "publishAccess.json")
+ if err := os.MkdirAll(filepath.Dir(target), 0755); err != nil {
+ t.Fatalf("mkdir: %v", err)
+ }
+ if err := os.WriteFile(target, []byte(`{"id":"","password":"secret"}`), 0644); err != nil {
+ t.Fatalf("write target: %v", err)
+ }
+
+ // 在 data/assets 下放一个指向 publishAccess.json 的符号链接。
+ link := filepath.Join(tmpWorkspace, "data", "assets", "leak.json")
+ if err := os.MkdirAll(filepath.Dir(link), 0755); err != nil {
+ t.Fatalf("mkdir: %v", err)
+ }
+ if err := os.Symlink(target, link); err != nil {
+ t.Skipf("symlink not supported on this platform: %v", err)
+ }
+
+ // 链接自身路径不命中黑名单,但解析后指向敏感文件,应被拒绝。
+ if got := IsForbiddenAbsPath(link); !got {
+ t.Errorf("IsForbiddenAbsPath(symlink -> publishAccess.json) = false, want true")
+ }
+}