Skip to content

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

:bug: Reject moving documents into themselves or descendants https://github.com/siyuan-note/siyuan/issues/19548

Daniel9a9ce75b2e8b
diff --git a/kernel/model/file.go b/kernel/model/file.go
index 392841aff4..868dee2e83 100644
--- a/kernel/model/file.go
+++ b/kernel/model/file.go
@@ -1708,6 +1708,13 @@ func MoveDocs(fromPaths []string, toBoxID, toPath string, callback any) (err err
return
}
+ // 在移动任何文档前拒绝自身及后代目标,避免调用方将未执行的移动视为成功。
+ for _, fromPath := range fromPaths {
+ if fromBox := pathsBoxes[fromPath]; nil != fromBox && fromBox.ID == toBoxID &&
+ (toPath == fromPath || strings.HasPrefix(toPath, strings.TrimSuffix(fromPath, ".sy")+"/")) {
+ return errors.New(Conf.Language(87))
+ }
+ }
fromPaths = util.FilterMoveDocFromPaths(fromPaths, toPath)
if 1 > len(fromPaths) {
return
diff --git a/kernel/model/file_test.go b/kernel/model/file_test.go
index 9878f270ec..d98aa7ac1a 100644
--- a/kernel/model/file_test.go
+++ b/kernel/model/file_test.go
@@ -996,6 +996,38 @@ func TestMoveDocsRejectsInvalidPathsBeforeMoving(t *testing.T) {
}
}
+func TestMoveDocsRejectsSelfAndDescendantsBeforeMoving(t *testing.T) {
+ fixture := setupFileOperationTest(t)
+ childPath := strings.TrimSuffix(fixture.sourcePath, ".sy") + "/20260718000003-abcdefg.sy"
+ grandchildPath := strings.TrimSuffix(childPath, ".sy") + "/20260718000004-abcdefg.sy"
+ for _, docPath := range []string{childPath, grandchildPath} {
+ tree := treenode.NewTree(fixture.box.ID, docPath, "/Source/Descendant", "Descendant")
+ if _, err := filesys.WriteTree(tree); err != nil {
+ t.Fatal(err)
+ }
+ treenode.UpsertBlockTree(tree)
+ t.Cleanup(func() {
+ cache.RemoveTreeData(tree.ID)
+ cache.RemoveDocIAL(tree.Path)
+ })
+ }
+ for _, targetPath := range []string{fixture.sourcePath, childPath, grandchildPath} {
+ for _, sources := range [][]string{{fixture.sourcePath}, {fixture.targetPath, fixture.sourcePath}} {
+ if err := MoveDocs(sources, fixture.box.ID, targetPath, nil); err == nil || err.Error() != Conf.Language(87) {
+ t.Fatalf("expected invalid move target error for %v to %s, got %v", sources, targetPath, err)
+ }
+ for _, docPath := range []string{fixture.sourcePath, fixture.targetPath, childPath, grandchildPath} {
+ if !fixture.box.Exist(docPath) {
+ t.Fatalf("invalid move changed document %s", docPath)
+ }
+ if bt := treenode.GetBlockTree(util.GetTreeID(docPath)); bt == nil || bt.Path != docPath {
+ t.Fatalf("invalid move changed document index %s: %+v", docPath, bt)
+ }
+ }
+ }
+ }
+}
+
func TestMoveDocsRefreshDeduplicatesParentsAndNotebooks(t *testing.T) {
refresh := newMoveDocsRefresh()
parent1 := &parse.Tree{Box: "20260718000000-abcdefg", ID: "20260718000001-abcdefg"}