Skip to content

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

:lock: Fix case-sensitive publishAccess.json guard bypass on Linux https://github.com/siyuan-note/siyuan/security/advisories/GHSA-mmgw-3mx9-cfwp

Daniel332f0a420cf5
diff --git a/kernel/mcp/tools/file_test.go b/kernel/mcp/tools/file_test.go
new file mode 100644
index 0000000000..728828d81e
--- /dev/null
+++ b/kernel/mcp/tools/file_test.go
@@ -0,0 +1,68 @@
+// 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 tools
+
+import (
+ "os"
+ "path/filepath"
+ "testing"
+
+ "github.com/siyuan-note/siyuan/kernel/util"
+)
+
+// TestSecurityReproMCPReadsPublishAccessMetadata 验证 MCP 文件工具无法读取受保护的
+// data/.siyuan/publishAccess.json:真实路径、大小写变体以及符号链接父目录均会被拦截,
+// 修复 GHSA-mmgw-3mx9-cfwp 中 Linux 上大小写敏感比较导致的黑名单失效问题。
+func TestSecurityReproMCPReadsPublishAccessMetadata(t *testing.T) {
+ tmpWorkspace := t.TempDir()
+ origWorkspace, origData := util.WorkspaceDir, util.DataDir
+ util.WorkspaceDir = tmpWorkspace
+ util.DataDir = filepath.Join(tmpWorkspace, "data")
+ t.Cleanup(func() { util.WorkspaceDir, util.DataDir = origWorkspace, origData })
+
+ publishAccess := filepath.Join(util.DataDir, ".siyuan", "publishAccess.json")
+ if err := os.MkdirAll(filepath.Dir(publishAccess), 0755); err != nil {
+ t.Fatalf("mkdir: %v", err)
+ }
+ if err := os.WriteFile(publishAccess, []byte(`{"id":"","password":"secret"}`), 0644); err != nil {
+ t.Fatalf("write publishAccess.json: %v", err)
+ }
+
+ // 真实路径与大小写变体均应被拦截;Linux 上此前真实驼峰路径无法命中全小写黑名单条目。
+ cases := []string{
+ filepath.Join("data", ".siyuan", "publishAccess.json"),
+ filepath.Join("data", ".siyuan", "PUBLISHACCESS.JSON"),
+ filepath.Join("data", ".SIYUAN", "PublishAccess.json"),
+ }
+ for _, c := range cases {
+ if _, err := resolvePath(c); err == nil {
+ t.Errorf("resolvePath(%q) = nil error, want forbidden", c)
+ }
+ }
+
+ // 通过指向 .siyuan 目录的符号链接父目录访问同样应被拦截。
+ if err := os.MkdirAll(filepath.Join(util.DataDir, "assets"), 0755); err != nil {
+ t.Fatalf("mkdir: %v", err)
+ }
+ parentLink := filepath.Join(util.DataDir, "assets", "siyuan-link")
+ if err := os.Symlink(filepath.Join(util.DataDir, ".siyuan"), parentLink); err != nil {
+ t.Skipf("symlink not supported on this platform: %v", err)
+ }
+ if _, err := resolvePath(filepath.Join("data", "assets", "siyuan-link", "publishAccess.json")); err == nil {
+ t.Errorf("resolvePath(symlinked parent -> publishAccess.json) = nil error, want forbidden")
+ }
+}
diff --git a/kernel/util/path_guard.go b/kernel/util/path_guard.go
index 1945ac91cb..f79aec9384 100644
--- a/kernel/util/path_guard.go
+++ b/kernel/util/path_guard.go
@@ -83,8 +83,10 @@ func IsForbiddenDataRelPath(rel string) bool {
return true
}
- // 禁止访问 data/.siyuan/publishAccess.json(含发布模式明文访问密码)
- if rel == "/.siyuan/publishaccess.json" {
+ // 禁止访问 data/.siyuan/publishAccess.json(含发布模式明文访问密码)。
+ // 磁盘上的真实文件名为驼峰 publishAccess.json:Windows/macOS 上路径已在上方转为小写,与小写常量比较即可;
+ // Linux 等大小写敏感平台需与真实名称精确比较,并对大小写变体做小写兜底比较,防止变体路径绕过黑名单
+ if rel == "/.siyuan/publishAccess.json" || strings.ToLower(rel) == "/.siyuan/publishaccess.json" {
return true
}
return false
diff --git a/kernel/util/path_guard_test.go b/kernel/util/path_guard_test.go
index de00b31dfa..ae56912729 100644
--- a/kernel/util/path_guard_test.go
+++ b/kernel/util/path_guard_test.go
@@ -156,6 +156,15 @@ func TestIsForbiddenAbsPathSymlinkBypass(t *testing.T) {
if got := IsForbiddenAbsPath(tlsKeyLink); !got {
t.Errorf("IsForbiddenAbsPath(symlink -> key.pem) = false, want true")
}
+
+ // 符号链接指向的父目录同样无法绕过黑名单:经由链接目录访问 publishAccess.json 应被拦截。
+ parentLink := filepath.Join(tmpWorkspace, "data", "assets", "siyuan-link")
+ if err := os.Symlink(filepath.Join(DataDir, ".siyuan"), parentLink); err != nil {
+ t.Skipf("symlink not supported on this platform: %v", err)
+ }
+ if got := IsForbiddenAbsPath(filepath.Join(parentLink, "publishAccess.json")); !got {
+ t.Errorf("IsForbiddenAbsPath(symlinked parent -> publishAccess.json) = false, want true")
+ }
}
// TestIsForbiddenDataRelPath 覆盖 /history 与 /repo/diff 路由共用的数据相对路径片段匹配黑名单。
@@ -166,6 +175,8 @@ func TestIsForbiddenDataRelPath(t *testing.T) {
".siyuan/publishAccess.json",
"/.siyuan/publishAccess.json",
filepath.Join(".siyuan", "publishAccess.json"),
+ ".siyuan/PUBLISHACCESS.JSON",
+ filepath.Join(".SIYUAN", "PublishAccess.json"),
"templates",
"/templates",
filepath.Join("templates", "a.md"),