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: Add ValidateBoxRelativePath to prevent path traversal across notebooks in filesys tree read/write, reject ../ and absolute paths in listDocTree API https://github.com/siyuan-note/siyuan/issues/18034
Danieldd0685eab28e
diff --git a/kernel/api/filetree.go b/kernel/api/filetree.goindex 5061026a63..7da1ea50a3 100644--- a/kernel/api/filetree.go+++ b/kernel/api/filetree.go@@ -78,8 +78,24 @@ func listDocTree(c *gin.Context) {p := arg["path"].(string)p = strings.TrimSuffix(p, ".sy")+ // 越界校验:拒绝 .. 和绝对路径,确保路径位于 <data>/<notebook>/ 内+ if idx := strings.Index(p, ".."); idx >= 0 {+ ret.Code = -1+ ret.Msg = "path must not contain '..'"+ return+ }+ if filepath.IsAbs(p) {+ ret.Code = -1+ ret.Msg = "path must be relative"+ return+ }var doctree []*DocFileroot := filepath.Join(util.WorkspaceDir, "data", notebook, p)+ if !gulu.File.IsSubPath(filepath.Join(util.WorkspaceDir, "data", notebook), root) {+ ret.Code = -1+ ret.Msg = "path escapes notebook directory"+ return+ }dir, err := os.ReadDir(root)if err != nil {ret.Code = -1diff --git a/kernel/filesys/tree.go b/kernel/filesys/tree.goindex f3a7a55f0a..5fe9cc9e12 100644--- a/kernel/filesys/tree.go+++ b/kernel/filesys/tree.go@@ -27,6 +27,7 @@ import ("strings""sync"+ "github.com/88250/gulu""github.com/88250/lute""github.com/88250/lute/ast""github.com/88250/lute/html"@@ -143,7 +144,29 @@ func batchLoadTrees(boxIDs, paths []string, luteEngine *lute.Lute) (ret []*parsereturn}+// ValidateBoxRelativePath 校验 box 内相对路径是否安全。+// 拒绝 ..、绝对路径、盘符,确保最终路径位于 <DataDir>/<boxID> 内。+func ValidateBoxRelativePath(boxID, p string) (string, error) {+ p = filepath.ToSlash(p)+ if strings.HasPrefix(p, "..") || strings.Contains(p, "/../") || strings.HasSuffix(p, "/..") || p == ".." {+ return "", fmt.Errorf("path [%s] must not contain '..'", p)+ }+ if strings.HasPrefix(p, "/") {+ return "", fmt.Errorf("path [%s] must be relative", p)+ }+ resolved := filepath.Join(util.DataDir, boxID, p)+ boxRoot := filepath.Join(util.DataDir, boxID)+ if !gulu.File.IsSubPath(boxRoot, resolved) {+ return "", fmt.Errorf("path [%s] escapes box directory", p)+ }+ return p, nil+}+func LoadTreeWithFix(boxID, p string, luteEngine *lute.Lute) (ret *parse.Tree, needFix bool, err error) {+ if _, err = ValidateBoxRelativePath(boxID, p); err != nil {+ logging.LogErrorf("invalid tree path [%s] for box [%s]: %s", p, boxID, err)+ return+ }rootID := util.GetTreeID(p)if raw, ok := cache.GetTreeData(rootID); ok {ret, err = LoadTreeByData(raw, boxID, p, luteEngine)@@ -381,6 +404,10 @@ func prepareWriteTree(tree *parse.Tree) (data []byte, filePath string, err errortreenode.UpgradeSpec(tree)+ if _, err = ValidateBoxRelativePath(tree.Box, tree.Path); err != nil {+ return+ }+filePath = filepath.Join(util.DataDir, tree.Box, tree.Path)tree.Root.SetIALAttr("type", "doc")renderer := render.NewJSONRenderer(tree, luteEngine.RenderOptions, luteEngine.ParseOptions)