Skip to content

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

:bug: Reject invalid document paths in file-tree mutations https://github.com/siyuan-note/siyuan/issues/18279

Daniel8da64361ace5
diff --git a/kernel/model/box.go b/kernel/model/box.go
index 2cba39ff0f..0207ed3f4a 100644
--- a/kernel/model/box.go
+++ b/kernel/model/box.go
@@ -982,3 +982,46 @@ func getBoxesByPaths(paths []string) (ret map[string]*Box) {
}
return
}
+
+func getBoxesByPathsStrict(paths []string) (ret map[string]*Box, err error) {
+ if 1 > len(paths) {
+ return nil, ErrBlockNotFound
+ }
+
+ var ids []string
+ for _, p := range paths {
+ id := util.GetTreeID(p)
+ if !ast.IsNodeIDPattern(id) {
+ return nil, ErrBlockNotFound
+ }
+ ids = append(ids, id)
+ }
+
+ ret = map[string]*Box{}
+ bts := treenode.GetBlockTrees(ids)
+ for i, id := range ids {
+ bt := bts[id]
+ if nil == bt || "d" != bt.Type || bt.ID != bt.RootID {
+ return nil, ErrBlockNotFound
+ }
+
+ box := Conf.Box(bt.BoxID)
+ if nil == box {
+ return nil, ErrBoxNotFound
+ }
+
+ p := filepath.ToSlash(paths[i])
+ if !strings.HasPrefix(p, "/") {
+ p = "/" + p
+ }
+ if _, validateErr := filesys.ValidateBoxRelativePath(bt.BoxID, p); validateErr != nil {
+ return nil, ErrBlockNotFound
+ }
+ p = normalizeBoxDocPath(bt.BoxID, path.Clean(p))
+ if p != path.Clean(bt.Path) {
+ return nil, ErrBlockNotFound
+ }
+ ret[bt.Path] = box
+ }
+ return
+}
diff --git a/kernel/model/file.go b/kernel/model/file.go
index ac3425ebe5..d5974a4c63 100644
--- a/kernel/model/file.go
+++ b/kernel/model/file.go
@@ -1471,6 +1471,9 @@ func MoveDocs(fromPaths []string, toBoxID, toPath string, callback any) (err err
return
}
toPath = normalizeBoxDocTarget(toBoxID, toPath)
+ if _, err = getBoxesByPathsStrict(fromPaths); err != nil {
+ return
+ }
fromPaths = util.FilterMoveDocFromPaths(fromPaths, toPath)
if 1 > len(fromPaths) {
@@ -1715,7 +1718,10 @@ func RemoveDoc(boxID, p string) error {
FlushTxQueue()
luteEngine := util.NewLute()
- tree := removeDoc(box, p, luteEngine)
+ tree, err := removeDoc(box, p, luteEngine)
+ if err != nil {
+ return err
+ }
IncSync()
refreshParentDocInfo(tree)
@@ -1726,6 +1732,9 @@ func RemoveDocs(paths []string) error {
util.PushEndlessProgress(Conf.Language(116))
defer util.PushClearProgress()
+ if _, err := getBoxesByPathsStrict(paths); err != nil {
+ return err
+ }
paths = util.FilterSelfChildDocs(paths)
pathsBoxes := getBoxesByPaths(paths)
for p, box := range pathsBoxes {
@@ -1738,7 +1747,10 @@ func RemoveDocs(paths []string) error {
var trees []*parse.Tree
for p, box := range pathsBoxes {
- tree := removeDoc(box, p, luteEngine)
+ tree, removeErr := removeDoc(box, p, luteEngine)
+ if removeErr != nil {
+ return removeErr
+ }
trees = append(trees, tree)
}
@@ -1755,10 +1767,10 @@ func RemoveDocs(paths []string) error {
return nil
}
-func removeDoc(box *Box, p string, luteEngine *lute.Lute) (ret *parse.Tree) {
- ret, _ = filesys.LoadTree(box.ID, p, luteEngine)
- if nil == ret {
- return
+func removeDoc(box *Box, p string, luteEngine *lute.Lute) (ret *parse.Tree, err error) {
+ ret, err = filesys.LoadTree(box.ID, p, luteEngine)
+ if err != nil || nil == ret {
+ return nil, ErrBlockNotFound
}
historyDir, err := getHistoryDir(HistoryOpDelete)
diff --git a/kernel/model/file_test.go b/kernel/model/file_test.go
new file mode 100644
index 0000000000..b182496d45
--- /dev/null
+++ b/kernel/model/file_test.go
@@ -0,0 +1,153 @@
+// 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 (
+ "errors"
+ "path"
+ "path/filepath"
+ "strings"
+ "testing"
+
+ "github.com/88250/lute/parse"
+ "github.com/siyuan-note/siyuan/kernel/cache"
+ "github.com/siyuan-note/siyuan/kernel/conf"
+ "github.com/siyuan-note/siyuan/kernel/filesys"
+ "github.com/siyuan-note/siyuan/kernel/treenode"
+ "github.com/siyuan-note/siyuan/kernel/util"
+)
+
+type fileOperationTestFixture struct {
+ box *Box
+ sourcePath string
+ targetPath string
+ sourceID string
+ childID string
+}
+
+func setupFileOperationTest(t *testing.T) *fileOperationTestFixture {
+ originalConf := Conf
+ originalDataDir := util.DataDir
+ originalBlockTreeDBPath := util.BlockTreeDBPath
+ tempDir := t.TempDir()
+ util.DataDir = filepath.Join(tempDir, "data")
+ util.BlockTreeDBPath = filepath.Join(tempDir, "blocktree.db")
+ Conf = NewAppConf()
+ Conf.FileTree = conf.NewFileTree()
+ Conf.NotebookCrypto = conf.NewNotebookCrypto()
+
+ box := &Box{ID: "20260718000000-abcdefg"}
+ boxConf := conf.NewBoxConf()
+ boxConf.Name = "File operation test"
+ boxConf.Closed = false
+ if err := box.SaveConf(boxConf); err != nil {
+ t.Fatalf("save test notebook conf failed: %v", err)
+ }
+
+ treenode.InitBlockTree(true)
+ sourcePath := "/20260718000001-abcdefg.sy"
+ targetPath := "/20260718000002-abcdefg.sy"
+ sourceTree := treenode.NewTree(box.ID, sourcePath, "/Source", "Source")
+ targetTree := treenode.NewTree(box.ID, targetPath, "/Target", "Target")
+ for _, tree := range []*parse.Tree{sourceTree, targetTree} {
+ if _, err := filesys.WriteTree(tree); err != nil {
+ t.Fatalf("write test tree failed: %v", err)
+ }
+ treenode.UpsertBlockTree(tree)
+ }
+
+ t.Cleanup(func() {
+ cache.RemoveTreeData(sourceTree.ID)
+ cache.RemoveTreeData(targetTree.ID)
+ cache.RemoveDocIAL(sourceTree.Path)
+ cache.RemoveDocIAL(targetTree.Path)
+ treenode.CloseDatabase()
+ Conf = originalConf
+ util.DataDir = originalDataDir
+ util.BlockTreeDBPath = originalBlockTreeDBPath
+ if "" != originalBlockTreeDBPath {
+ treenode.InitBlockTree(false)
+ }
+ })
+
+ return &fileOperationTestFixture{
+ box: box,
+ sourcePath: sourcePath,
+ targetPath: targetPath,
+ sourceID: sourceTree.ID,
+ childID: sourceTree.Root.FirstChild.ID,
+ }
+}
+
+func TestRemoveDocRejectsInvalidPath(t *testing.T) {
+ fixture := setupFileOperationTest(t)
+
+ if err := RemoveDoc(fixture.box.ID, "/_REPRO_FLAT"); !errors.Is(err, ErrBlockNotFound) {
+ t.Fatalf("expected invalid document path to return ErrBlockNotFound, got [%v]", err)
+ }
+}
+
+func TestGetBoxesByPathsStrictRejectsInvalidPaths(t *testing.T) {
+ fixture := setupFileOperationTest(t)
+ tests := []struct {
+ name string
+ paths []string
+ }{
+ {name: "empty", paths: nil},
+ {name: "hpath", paths: []string{"/_REPRO_TEST/Sub_Note"}},
+ {name: "hpath with extension", paths: []string{"/_REPRO_FLAT.sy"}},
+ {name: "wrong parent", paths: []string{"/20260718000003-abcdefg/" + fixture.sourceID + ".sy"}},
+ {name: "parent traversal", paths: []string{"/../" + fixture.sourceID + ".sy"}},
+ {name: "child block", paths: []string{"/" + fixture.childID + ".sy"}},
+ }
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ if _, err := getBoxesByPathsStrict(test.paths); !errors.Is(err, ErrBlockNotFound) {
+ t.Fatalf("expected invalid document paths [%v] to return ErrBlockNotFound, got [%v]", test.paths, err)
+ }
+ })
+ }
+
+ if _, err := getBoxesByPathsStrict([]string{strings.TrimPrefix(fixture.sourcePath, "/")}); err != nil {
+ t.Fatalf("expected document path without leading slash to remain supported, got [%v]", err)
+ }
+}
+
+func TestMoveDocsRejectsInvalidPathsBeforeMoving(t *testing.T) {
+ fixture := setupFileOperationTest(t)
+ newPath := path.Join(strings.TrimSuffix(fixture.targetPath, ".sy"), fixture.sourceID+".sy")
+ tests := []struct {
+ name string
+ fromPaths []string
+ }{
+ {name: "hpath", fromPaths: []string{"/_REPRO_TEST/Sub_Note"}},
+ {name: "mixed", fromPaths: []string{fixture.sourcePath, "/_REPRO_TEST/Sub_Note"}},
+ }
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ if err := MoveDocs(test.fromPaths, fixture.box.ID, fixture.targetPath, nil); !errors.Is(err, ErrBlockNotFound) {
+ t.Fatalf("expected invalid source paths [%v] to return ErrBlockNotFound, got [%v]", test.fromPaths, err)
+ }
+ if !fixture.box.Exist(fixture.sourcePath) {
+ t.Fatalf("source document was moved for invalid source paths [%v]", test.fromPaths)
+ }
+ if fixture.box.Exist(newPath) {
+ t.Fatalf("target document was created for invalid source paths [%v]", test.fromPaths)
+ }
+ })
+ }
+}