An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:lock: Unify encrypted raw path guards #18034
Daniele6e40f9d3041
diff --git a/kernel/api/file.go b/kernel/api/file.goindex db1f4f7aeb..bbfdfb7b18 100644--- a/kernel/api/file.go+++ b/kernel/api/file.go@@ -47,20 +47,7 @@ const errMsgSeeKernelLog = ". For details, see the SiYuan kernel log."// 避免密文泄漏给插件或明文破坏加密格式。// 防止 symlink 绕过:找到最长已存在的父路径,解析 symlink 后拼回剩余路径,再检查是否落入加密 box。func rejectEncryptedBoxPath(absPath string) bool {- // 先检查字面路径- boxID := model.ExtractBoxIDFromAssetsPath(absPath)- if boxID != "" && model.IsEncryptedBox(boxID) {- return true- }- // 找最长已存在的父路径,解析 symlink 后拼回剩余路径- resolved := util.ResolveLongestExistingParent(absPath)- if resolved != absPath {- boxID = model.ExtractBoxIDFromAssetsPath(resolved)- if boxID != "" && model.IsEncryptedBox(boxID) {- return true- }- }- return false+ return model.EncryptedRawPathBoxID(absPath) != ""}// copyDecryptedAsset 将加密 asset 解密后复制到目标路径(dest 必须在工作区外)。diff --git a/kernel/cli/cmd/file.go b/kernel/cli/cmd/file.goindex 58888a9bde..b1c1dd5b32 100644--- a/kernel/cli/cmd/file.go+++ b/kernel/cli/cmd/file.go@@ -26,6 +26,7 @@ import ("text/tabwriter""github.com/88250/gulu"+ "github.com/siyuan-note/siyuan/kernel/model""github.com/siyuan-note/siyuan/kernel/util""github.com/spf13/cobra"@@ -42,6 +43,9 @@ func absPath(rel string) (string, error) {if !gulu.File.IsSubPath(util.WorkspaceDir, abs) {return "", fmt.Errorf("path escapes workspace: %s", rel)}+ if boxID := model.EncryptedRawPathBoxID(abs); boxID != "" {+ return "", fmt.Errorf("path belongs to encrypted notebook [%s]: %s", boxID, rel)+ }return abs, nil}diff --git a/kernel/mcp/tools/file.go b/kernel/mcp/tools/file.goindex 83338dc773..718530ac3c 100644--- a/kernel/mcp/tools/file.go+++ b/kernel/mcp/tools/file.go@@ -104,18 +104,8 @@ func resolvePath(rel string) (string, error) {// rejectEncryptedPath 检查路径是否属于加密笔记本(含 symlink 绕过),返回 boxID 和是否为加密 box。func rejectEncryptedPath(absPath string) (boxID string, encrypted bool) {- boxID = model.ExtractBoxIDFromAssetsPath(absPath)- if boxID != "" && model.IsEncryptedBox(boxID) {- return boxID, true- }- // 防止 symlink 绕过:解析最长已存在的父路径,再检查- if resolved := util.ResolveLongestExistingParent(absPath); resolved != absPath {- boxID = model.ExtractBoxIDFromAssetsPath(resolved)- if boxID != "" && model.IsEncryptedBox(boxID) {- return boxID, true- }- }- return "", false+ boxID = model.EncryptedRawPathBoxID(absPath)+ return boxID, boxID != ""}func fileList(args map[string]interface{}) (CallToolResult, error) {diff --git a/kernel/model/raw_path_guard.go b/kernel/model/raw_path_guard.gonew file mode 100644index 0000000000..74708290e8--- /dev/null+++ b/kernel/model/raw_path_guard.go@@ -0,0 +1,41 @@+// 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 model++import (+ "path/filepath"++ "github.com/siyuan-note/siyuan/kernel/util"+)++// EncryptedRawPathBoxID 返回原始文件路径所属的加密笔记本 ID。+// 同时检查最长已存在父目录解析后的路径,防止通过符号链接绕过加密边界。+func EncryptedRawPathBoxID(absPath string) string {+ if normalized, err := filepath.Abs(absPath); err == nil {+ absPath = normalized+ }+ if boxID := ExtractBoxIDFromAssetsPath(absPath); boxID != "" && IsEncryptedBox(boxID) {+ return boxID+ }+ resolved := util.ResolveLongestExistingParent(absPath)+ if resolved != absPath {+ if boxID := ExtractBoxIDFromAssetsPath(resolved); boxID != "" && IsEncryptedBox(boxID) {+ return boxID+ }+ }+ return ""+}