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 Box.validateBoxPath to prevent path traversal across notebooks via listDocsByPath and all Box file operations (Ls/Stat/Exist/Mkdir/MkdirAll/Move/Remove/docIAL) https://github.com/siyuan-note/siyuan/issues/18034
Danielea8f16a43b28
diff --git a/kernel/api/filetree.go b/kernel/api/filetree.goindex 7da1ea50a3..416ec08262 100644--- a/kernel/api/filetree.go+++ b/kernel/api/filetree.go@@ -1076,6 +1076,14 @@ func listDocsByPath(c *gin.Context) {notebook := arg["notebook"].(string)p := arg["path"].(string)++ // 越界校验:拒绝 .. 和绝对路径,防止跨 box 遍历+ if strings.Contains(p, "..") || filepath.IsAbs(p) {+ ret.Code = -1+ ret.Msg = "path must not contain '..' and must be relative"+ return+ }+sortParam := arg["sort"]sortMode := util.SortModeUnassignedif nil != sortParam {diff --git a/kernel/model/box.go b/kernel/model/box.goindex 9e979891f8..aa849cbdf1 100644--- a/kernel/model/box.go+++ b/kernel/model/box.go@@ -269,7 +269,15 @@ func (box *Box) saveConf0(data []byte) error {return nil}+// validateBoxPath 校验 box 内相对路径,拒绝 .. 和绝对路径,确保最终路径在 <DataDir>/<boxID>/ 内。+func (box *Box) validateBoxPath(p string) (string, error) {+ return filesys.ValidateBoxRelativePath(box.ID, p)+}+func (box *Box) Ls(p string) (ret []*FileInfo, totals int, err error) {+ if _, err = box.validateBoxPath(p + "/placeholder"); err != nil {+ return+ }boxLocalPath := filepath.Join(util.DataDir, box.ID)if strings.HasSuffix(p, ".sy") {dir := strings.TrimSuffix(p, ".sy")@@ -324,6 +332,9 @@ func (box *Box) Ls(p string) (ret []*FileInfo, totals int, err error) {}func (box *Box) Stat(p string) (ret *FileInfo) {+ if _, err := box.validateBoxPath(p); err != nil {+ return+ }absPath := filepath.Join(util.DataDir, box.ID, p)info, err := os.Stat(absPath)if err != nil {@@ -342,10 +353,16 @@ func (box *Box) Stat(p string) (ret *FileInfo) {}func (box *Box) Exist(p string) bool {+ if _, err := box.validateBoxPath(p); err != nil {+ return false+ }return filelock.IsExist(filepath.Join(util.DataDir, box.ID, p))}func (box *Box) Mkdir(path string) error {+ if _, err := box.validateBoxPath(path); err != nil {+ return err+ }if err := os.Mkdir(filepath.Join(util.DataDir, box.ID, path), 0755); err != nil {msg := fmt.Sprintf(Conf.Language(6), box.Name, path, err)logging.LogErrorf("mkdir [path=%s] in box [%s] failed: %s", path, box.ID, err)@@ -356,6 +373,9 @@ func (box *Box) Mkdir(path string) error {}func (box *Box) MkdirAll(path string) error {+ if _, err := box.validateBoxPath(path); err != nil {+ return err+ }if err := os.MkdirAll(filepath.Join(util.DataDir, box.ID, path), 0755); err != nil {msg := fmt.Sprintf(Conf.Language(6), box.Name, path, err)logging.LogErrorf("mkdir all [path=%s] in box [%s] failed: %s", path, box.ID, err)@@ -366,6 +386,12 @@ func (box *Box) MkdirAll(path string) error {}func (box *Box) Move(oldPath, newPath string) error {+ if _, err := box.validateBoxPath(oldPath); err != nil {+ return err+ }+ if _, err := box.validateBoxPath(newPath); err != nil {+ return err+ }boxLocalPath := filepath.Join(util.DataDir, box.ID)fromPath := filepath.Join(boxLocalPath, oldPath)toPath := filepath.Join(boxLocalPath, newPath)@@ -387,6 +413,9 @@ func (box *Box) Move(oldPath, newPath string) error {}func (box *Box) Remove(path string) error {+ if _, err := box.validateBoxPath(path); err != nil {+ return err+ }boxLocalPath := filepath.Join(util.DataDir, box.ID)filePath := filepath.Join(boxLocalPath, path)if err := filelock.Remove(filePath); err != nil {@@ -399,6 +428,7 @@ func (box *Box) Remove(path string) error {}func (box *Box) ListFiles(path string) (ret []*FileInfo) {+ // ListFiles 委托给 Ls,后者已有 validateBoxPathfis, _, err := box.Ls(path)if err != nil {returndiff --git a/kernel/model/file.go b/kernel/model/file.goindex c222b579ac..2426212385 100644--- a/kernel/model/file.go+++ b/kernel/model/file.go@@ -111,6 +111,10 @@ func (box *Box) docIAL(p string) (ret map[string]string) {return nil}+ if _, err := box.validateBoxPath(p); err != nil {+ return nil+ }+ret = cache.GetDocIAL(p)if nil != ret {return ret