Skip to content

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

:bug: Reject document IDs as insertBlock sibling anchors https://github.com/siyuan-note/siyuan/issues/20139

neariot625affee65b2
diff --git a/kernel/api/contract_block_transaction_test.go b/kernel/api/contract_block_transaction_test.go
index 514600601a..bc16ba254c 100644
--- a/kernel/api/contract_block_transaction_test.go
+++ b/kernel/api/contract_block_transaction_test.go
@@ -106,6 +106,7 @@ func TestAPIContractHeadingTransactions(t *testing.T) {
t.Fatal(err)
}
treenode.UpsertBlockTree(tree)
+ testAPIContractInsertBlockAnchors(t, box.ID, docID, paragraphID)
engine := gin.New()
engine.POST("/api/block/getBlockTreeInfos", getBlockTreeInfos)
infoRecorder := httptest.NewRecorder()
diff --git a/kernel/api/contract_insert_block_anchor_test.go b/kernel/api/contract_insert_block_anchor_test.go
new file mode 100644
index 0000000000..f9b9589090
--- /dev/null
+++ b/kernel/api/contract_insert_block_anchor_test.go
@@ -0,0 +1,132 @@
+// SiYuan - From thought to insight, with agents
+// 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.
+
+package api
+
+import (
+ "bytes"
+ "encoding/json"
+ "net/http/httptest"
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+
+ "github.com/gin-gonic/gin"
+ "github.com/siyuan-note/siyuan/kernel/apicontract"
+ "github.com/siyuan-note/siyuan/kernel/cache"
+ "github.com/siyuan-note/siyuan/kernel/model"
+ "github.com/siyuan-note/siyuan/kernel/treenode"
+ "github.com/siyuan-note/siyuan/kernel/util"
+)
+
+// testAPIContractInsertBlockAnchors 复用隔离进程中的真实文档,验证参数错误、定位优先级和落盘位置。
+func testAPIContractInsertBlockAnchors(t *testing.T, boxID, docID, paragraphID string) {
+ engine := gin.New()
+ const path = "/api/block/insertBlock"
+ engine.POST(path, insertBlock)
+ post := func(t *testing.T, request map[string]any) (response struct {
+ Code int `json:"code"`
+ Msg string `json:"msg"`
+ Data []*apicontract.BlockTransaction `json:"data"`
+ }) {
+ t.Helper()
+ body, err := json.Marshal(request)
+ if err != nil {
+ t.Fatal(err)
+ }
+ recorder := httptest.NewRecorder()
+ engine.ServeHTTP(recorder, httptest.NewRequest("POST", path, bytes.NewReader(body)))
+ requireAPIContract(t, "POST", path, recorder)
+ if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil {
+ t.Fatal(err)
+ }
+ return
+ }
+ readDocument := func(t *testing.T) []byte {
+ t.Helper()
+ data, err := os.ReadFile(filepath.Join(util.DataDir, boxID, docID+".sy"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ return data
+ }
+ const missingID = "20260908000000-missing"
+ for _, dataType := range []string{"markdown", "dom"} {
+ data := "anchor contract test"
+ if dataType == "dom" {
+ data = util.NewLute().Md2BlockDOM(data, false)
+ }
+ for _, field := range []string{"previousID", "nextID"} {
+ for _, anchor := range []struct{ name, id, message string }{
+ {"document", docID, "`" + field + "` cannot be the ID of a document"},
+ {"missing", missingID, "not found"},
+ } {
+ t.Run("insert-anchor/"+dataType+"/"+field+"/"+anchor.name, func(t *testing.T) {
+ before := readDocument(t)
+ response := post(t, map[string]any{
+ "data": data, "dataType": dataType, "parentID": docID, field: anchor.id,
+ })
+ if response.Code != -1 || response.Data != nil ||
+ !strings.Contains(response.Msg, anchor.message) || strings.Contains(response.Msg, "panic") {
+ t.Fatalf("expected explicit anchor error, got %+v", response)
+ }
+ if !bytes.Equal(before, readDocument(t)) {
+ t.Fatal("rejected insert changed the document")
+ }
+ })
+ }
+ }
+ for _, test := range []struct {
+ name, previousID, nextID, position string
+ }{
+ {"previous-block", paragraphID, "", "after"},
+ {"next-block", "", paragraphID, "before"},
+ {"document-parent", "", "", "first"},
+ {"next-priority-document-previous", docID, paragraphID, "before"},
+ {"next-priority-missing-previous", missingID, paragraphID, "before"},
+ } {
+ t.Run("insert-anchor/"+dataType+"/"+test.name, func(t *testing.T) {
+ response := post(t, map[string]any{
+ "data": data, "dataType": dataType, "parentID": docID,
+ "previousID": test.previousID, "nextID": test.nextID,
+ })
+ if response.Code != 0 || len(response.Data) != 1 || len(response.Data[0].DoOperations) != 1 {
+ t.Fatalf("expected successful insert, got %+v", response)
+ }
+ operation := response.Data[0].DoOperations[0]
+ cache.RemoveTreeData(docID)
+ tree, err := model.LoadTreeByBlockID(docID)
+ if err != nil {
+ t.Fatal(err)
+ }
+ node := treenode.GetNodeInTree(tree, operation.ID)
+ if node == nil || node.Parent != tree.Root || operation.ParentID != docID {
+ t.Fatalf("inserted block is not persisted under the document: %+v", operation)
+ }
+ switch test.position {
+ case "after":
+ if node.Previous == nil || node.Previous.ID != paragraphID {
+ t.Fatal("block was not inserted after previousID")
+ }
+ case "before":
+ if node.Next == nil || node.Next.ID != paragraphID {
+ t.Fatal("block was not inserted before nextID")
+ }
+ case "first":
+ if tree.Root.FirstChild != node {
+ t.Fatal("document parent must insert at the beginning")
+ }
+ }
+ if _, err := model.PerformBlockOperation(&model.Operation{Action: "delete", ID: operation.ID}); err != nil {
+ t.Fatal(err)
+ }
+ })
+ }
+ }
+}
diff --git a/kernel/apicontract/block_inputs.go b/kernel/apicontract/block_inputs.go
index c95d4c22f4..cae85a860e 100644
--- a/kernel/apicontract/block_inputs.go
+++ b/kernel/apicontract/block_inputs.go
@@ -31,6 +31,9 @@ type BatchParentBlockRequest struct {
Blocks []PrependBlockRequest `json:"blocks"`
}
+// InsertBlockRequest 按 nextID、previousID、parentID 的顺序选择插入位置。
+// 生效的 nextID 或 previousID 必须指向非文档块,未使用的定位参数不参与节点类型校验。
+// parentID 指向文档时插入到文档开头;成功返回已落盘的操作,目标非法时返回 code=-1、data=null。
type InsertBlockRequest struct {
Data string `json:"data"`
DataType string `json:"dataType" api:"trim"`
diff --git a/kernel/model/block_operation_test.go b/kernel/model/block_operation_test.go
index 98dd6fd490..0c23b3d445 100644
--- a/kernel/model/block_operation_test.go
+++ b/kernel/model/block_operation_test.go
@@ -11,8 +11,12 @@
package model
import (
+ "bytes"
+ "os"
+ "path/filepath"
"testing"
+ "github.com/88250/lute/ast"
"github.com/siyuan-note/siyuan/kernel/cache"
"github.com/siyuan-note/siyuan/kernel/treenode"
"github.com/siyuan-note/siyuan/kernel/util"
@@ -91,3 +95,124 @@ func TestPerformBlockOperationReturnsErrors(t *testing.T) {
}
}
}
+
+func TestInsertDocumentSiblingAnchorRollsBackEarlierOperations(t *testing.T) {
+ for _, test := range []struct {
+ name string
+ count int
+ }{{"small", 2}, {"large", 33}} {
+ t.Run(test.name, func(t *testing.T) {
+ fixture := setupStructureTransactionTest(t)
+ setupFoldTransactionDatabase(t, fixture)
+ path := filepath.Join(util.DataDir, fixture.box.ID, filepath.Base(fixture.sourcePath))
+ before, err := os.ReadFile(path)
+ if err != nil {
+ t.Fatal(err)
+ }
+ tx := &Transaction{}
+ var insertedIDs []string
+ for index := 0; index < test.count-1; index++ {
+ id := ast.NewNodeID()
+ insertedIDs = append(insertedIDs, id)
+ dom := util.NewLute().Md2BlockDOM("inserted\n{: id=\""+id+"\"}", false)
+ tx.DoOperations = append(tx.DoOperations, &Operation{Action: "insert", PreviousID: fixture.childID, Data: dom})
+ }
+ tx.DoOperations = append(tx.DoOperations, &Operation{
+ Action: "insert", NextID: fixture.sourceID, Data: util.NewLute().Md2BlockDOM("invalid", false),
+ })
+ err = PerformTxSync(tx)
+ requireStructureTransactionError(t, err)
+ if err.Error() != "`nextID` cannot be the ID of a document" {
+ t.Fatalf("expected document anchor error, got %v", err)
+ }
+ after, err := os.ReadFile(path)
+ if err != nil || !bytes.Equal(before, after) {
+ t.Fatalf("rejected transaction changed the source document: %v", err)
+ }
+ cache.RemoveTreeData(fixture.sourceID)
+ tree, err := LoadTreeByBlockID(fixture.sourceID)
+ if err != nil {
+ t.Fatal(err)
+ }
+ for _, id := range insertedIDs {
+ if treenode.GetNodeInTree(tree, id) != nil {
+ t.Fatal("rejected transaction persisted an earlier insertion")
+ }
+ }
+ })
+ }
+}
+
+func TestPerformBlockOperationInsertAfterQueuedAppend(t *testing.T) {
+ fixture := setupStructureTransactionTest(t)
+ setupFoldTransactionDatabase(t, fixture)
+ const queuedID = "20261006000000-queued1"
+ const insertedID = "20261006000000-insert1"
+ dom := func(id string) string {
+ return util.NewLute().Md2BlockDOM("queued\n{: id=\""+id+"\"}", false)
+ }
+ queued := []*Transaction{{DoOperations: []*Operation{{
+ Action: "appendInsert", ParentID: fixture.sourceID, Data: dom(queuedID),
+ }}}}
+ PerformTransactions(&queued)
+ if _, err := PerformBlockOperation(&Operation{
+ Action: "insert", PreviousID: queuedID, Data: dom(insertedID),
+ }); err != nil {
+ t.Fatalf("insert after queued append failed: %v", err)
+ }
+ cache.RemoveTreeData(fixture.sourceID)
+ tree, err := LoadTreeByBlockID(fixture.sourceID)
+ if err != nil {
+ t.Fatal(err)
+ }
+ node := treenode.GetNodeInTree(tree, insertedID)
+ if node == nil || node.Previous == nil || node.Previous.ID != queuedID {
+ t.Fatal("insert did not use the queued append as its sibling anchor")
+ }
+}
+
+func TestLargeInsertSiblingAnchors(t *testing.T) {
+ for _, field := range []string{"previousID", "nextID"} {
+ t.Run(field, func(t *testing.T) {
+ fixture := setupStructureTransactionTest(t)
+ setupFoldTransactionDatabase(t, fixture)
+ tx := &Transaction{}
+ var ids []string
+ for index := 0; index < 32; index++ {
+ id := ast.NewNodeID()
+ ids = append(ids, id)
+ dom := util.NewLute().Md2BlockDOM("inserted\n{: id=\""+id+"\"}", false)
+ operation := &Operation{Action: "insert", Data: dom}
+ if field == "previousID" {
+ operation.PreviousID = fixture.childID
+ } else {
+ operation.NextID, operation.PreviousID = fixture.childID, fixture.sourceID
+ }
+ tx.DoOperations = append(tx.DoOperations, operation)
+ }
+ if err := PerformTxSync(tx); err != nil {
+ t.Fatalf("valid bulk insert failed: %v", err)
+ }
+ cache.RemoveTreeData(fixture.sourceID)
+ tree, err := LoadTreeByBlockID(fixture.sourceID)
+ if err != nil {
+ t.Fatal(err)
+ }
+ anchor := treenode.GetNodeInTree(tree, fixture.childID)
+ for index, id := range ids {
+ node := treenode.GetNodeInTree(tree, id)
+ if node == nil || node.Parent != tree.Root {
+ t.Fatalf("bulk insert did not persist block %s", id)
+ }
+ if field == "previousID" && (index == len(ids)-1 && node.Previous != anchor ||
+ index < len(ids)-1 && (node.Previous == nil || node.Previous.ID != ids[index+1])) {
+ t.Fatal("bulk insert did not preserve previousID placement")
+ }
+ if field == "nextID" && (index == len(ids)-1 && node.Next != anchor ||
+ index < len(ids)-1 && (node.Next == nil || node.Next.ID != ids[index+1])) {
+ t.Fatal("bulk insert did not preserve nextID placement")
+ }
+ }
+ })
+ }
+}
diff --git a/kernel/model/transaction.go b/kernel/model/transaction.go
index 8b34140909..1061fbb723 100644
--- a/kernel/model/transaction.go
+++ b/kernel/model/transaction.go
@@ -264,7 +264,11 @@ func performTx(tx *Transaction) (ret *TxErr) {
return &TxErr{code: TxErrHandleAttributeView, msg: err.Error()}
}
diagnostic.Stage("process bulk operations")
- isLargeInsert := tx.processLargeInsert()
+ isLargeInsert, ret := tx.processLargeInsert()
+ if ret != nil {
+ tx.rollback()
+ return ret
+ }
isLargeDelete := tx.processLargeDelete()
if !isLargeInsert {
for operationIndex := 0; operationIndex < len(tx.DoOperations); operationIndex++ {
@@ -570,10 +574,10 @@ func (tx *Transaction) processLargeDelete() bool {
return true
}
-func (tx *Transaction) processLargeInsert() bool {
+func (tx *Transaction) processLargeInsert() (bool, *TxErr) {
opSize := len(tx.DoOperations)
if 32 > opSize {
- return false
+ return false, nil
}
var insertOps []*Operation
@@ -581,10 +585,10 @@ func (tx *Transaction) processLargeInsert() bool {
for i, op := range tx.DoOperations {
if "insert" != op.Action {
if "delete" != op.Action {
- return false
+ return false, nil
}
if 0 != i && i != opSize-1 {
- return false
+ return false, nil
}
if "delete" == op.Action {
@@ -601,19 +605,25 @@ func (tx *Transaction) processLargeInsert() bool {
}
if 1 > len(insertOps) {
- return false
+ return false, nil
}
if nil != firstDeleteOp {
- tx.doDelete(firstDeleteOp)
+ if txErr := tx.doDelete(firstDeleteOp); txErr != nil {
+ return true, txErr
+ }
tx.flushDeletedAttributeViewBlocks()
}
- tx.doLargeInsert(insertOps)
+ if txErr := tx.doLargeInsert(insertOps); txErr != nil {
+ return true, txErr
+ }
if nil != lastDeleteOp {
- tx.doDelete(lastDeleteOp)
+ if txErr := tx.doDelete(lastDeleteOp); txErr != nil {
+ return true, txErr
+ }
tx.flushDeletedAttributeViewBlocks()
}
- return true
+ return true, nil
}
func (tx *Transaction) doMove(operation *Operation) (ret *TxErr) {
@@ -1598,7 +1608,7 @@ func removeAttributeViewBoundItems(attrView *av.AttributeView, deletedBlockIDs m
return
}
-func (tx *Transaction) doLargeInsert(operations []*Operation) {
+func (tx *Transaction) doLargeInsert(operations []*Operation) *TxErr {
tree, _ := tx.loadTree(operations[0].ID)
if nil == tree {
tree, _ = tx.loadTree(operations[0].PreviousID)
@@ -1612,16 +1622,17 @@ func (tx *Transaction) doLargeInsert(operations []*Operation) {
if nil == tree {
logging.LogErrorf("load tree [%s] failed", operations[0].ID)
- return
+ return &TxErr{code: TxErrCodeReloadUI, msg: "insertion target block not found"}
}
for _, operation := range operations {
if txErr := tx.doInsert0(operation, tree); nil != txErr {
- return
+ return txErr
}
}
tx.writeTree(tree)
+ return nil
}
func (tx *Transaction) doInsert(operation *Operation) (ret *TxErr) {
@@ -1669,6 +1680,24 @@ func (tx *Transaction) doInsert(operation *Operation) (ret *TxErr) {
}
func (tx *Transaction) doInsert0(operation *Operation, tree *parse.Tree) (ret *TxErr) {
+ var node *ast.Node
+ nextID, previousID := operation.NextID, operation.PreviousID
+ anchorID, anchorName := nextID, "nextID"
+ if anchorID == "" {
+ anchorID, anchorName = previousID, "previousID"
+ }
+ // 按插入优先级检查实际生效的同级锚点,文档根没有父节点,不能在其前后插入块。
+ if anchorID != "" {
+ node = treenode.GetNodeInTree(tree, anchorID)
+ if node == nil {
+ logging.LogErrorf("get node [%s] in tree [%s] failed", anchorID, tree.Root.ID)
+ return &TxErr{code: TxErrCodeBlockNotFound, id: anchorID, msg: "insertion target block not found"}
+ }
+ if node.Parent == nil {
+ return &TxErr{code: TxErrCodeReloadUI, msg: fmt.Sprintf("`%s` cannot be the ID of a document", anchorName)}
+ }
+ }
+
data := strings.ReplaceAll(operation.Data.(string), editor.FrontEndCaret, "")
subTree := tx.luteEngine.BlockDOM2Tree(data)
subTree.Box, subTree.Path = tree.Box, tree.Path
@@ -1706,16 +1735,7 @@ func (tx *Transaction) doInsert0(operation *Operation, tree *parse.Tree) (ret *T
operation.Data = tx.luteEngine.Tree2BlockDOM(subTree, tx.luteEngine.RenderOptions, tx.luteEngine.ParseOptions)
}
- var node *ast.Node
- nextID := operation.NextID
- previousID := operation.PreviousID
if "" != nextID {
- node = treenode.GetNodeInTree(tree, nextID)
- if nil == node {
- logging.LogErrorf("get node [%s] in tree [%s] failed", nextID, tree.Root.ID)
- return &TxErr{code: TxErrCodeBlockNotFound, id: nextID}
- }
-
if ast.NodeList == insertedNode.Type && nil != node.Parent && ast.NodeList == node.Parent.Type {
insertedNode = insertedNode.FirstChild
}
@@ -1724,12 +1744,6 @@ func (tx *Transaction) doInsert0(operation *Operation, tree *parse.Tree) (ret *T
node.InsertBefore(remain)
}
} else if "" != previousID {
- node = treenode.GetNodeInTree(tree, previousID)
- if nil == node {
- logging.LogErrorf("get node [%s] in tree [%s] failed", previousID, tree.Root.ID)
- return &TxErr{code: TxErrCodeBlockNotFound, id: previousID}
- }
-
if ast.NodeHeading == node.Type && treenode.IsSelfFolded(node) {
children := treenode.HeadingChildren(node)
if l := len(children); 0 < l {
diff --git a/kernel/model/transaction_list_conversion_sqlcipher_test.go b/kernel/model/transaction_list_conversion_sqlcipher_test.go
index 6634abc4aa..1c2fc076eb 100644
--- a/kernel/model/transaction_list_conversion_sqlcipher_test.go
+++ b/kernel/model/transaction_list_conversion_sqlcipher_test.go
@@ -67,6 +67,7 @@ func TestListConversionEncryptedReplayAndHistory(t *testing.T) {
if err = os.Remove(plainPath); err != nil {
t.Fatal(err)
}
+ testEncryptedInsertBlockAnchors(t, boxID, tree.ID, list.ID)
tx := listConversionTestTx(tree.ID, []string{list.ID}, "heading", false)
if err = PerformTxSync(tx); err != nil {
t.Fatal(err)
@@ -112,3 +113,50 @@ func TestListConversionEncryptedReplayAndHistory(t *testing.T) {
t.Fatal("failed authentication changed document")
}
}
+
+// testEncryptedInsertBlockAnchors 验证文档锚点拒绝时保留源密文,合法定位保持加密落盘。
+func testEncryptedInsertBlockAnchors(t *testing.T, boxID, docID, anchorID string) {
+ t.Helper()
+ path := filepath.Join(util.DataDir, boxID, docID+".sy")
+ before, err := os.ReadFile(path)
+ if err != nil || !util.IsCiphertext(before) {
+ t.Fatalf("expected encrypted source document: %v", err)
+ }
+ dom := util.NewLute().Md2BlockDOM("encrypted anchor test", false)
+ for _, field := range []string{"previousID", "nextID"} {
+ operation := &Operation{Action: "insert", Data: dom}
+ if field == "previousID" {
+ operation.PreviousID = docID
+ } else {
+ operation.NextID = docID
+ }
+ _, err := PerformBlockOperation(operation)
+ if err == nil || err.Error() != "`"+field+"` cannot be the ID of a document" {
+ t.Fatalf("expected encrypted document anchor rejection, got %v", err)
+ }
+ after, err := os.ReadFile(path)
+ if err != nil || !bytes.Equal(before, after) {
+ t.Fatalf("rejected insert changed source ciphertext: %v", err)
+ }
+ }
+ operation := &Operation{Action: "insert", NextID: anchorID, PreviousID: docID, ParentID: docID, Data: dom}
+ if _, err := PerformBlockOperation(operation); err != nil {
+ t.Fatalf("valid nextID must ignore the document previousID: %v", err)
+ }
+ cache.RemoveTreeDataInBox(docID, boxID)
+ tree, err := LoadTreeByBlockID(docID)
+ if err != nil {
+ t.Fatal(err)
+ }
+ node := treenode.GetNodeInTree(tree, operation.ID)
+ if node == nil || node.Next == nil || node.Next.ID != anchorID {
+ t.Fatal("encrypted insertion did not preserve nextID precedence")
+ }
+ after, err := os.ReadFile(path)
+ if err != nil || !util.IsCiphertext(after) {
+ t.Fatalf("successful insert did not preserve ciphertext storage: %v", err)
+ }
+ if _, err := PerformBlockOperation(&Operation{Action: "delete", ID: operation.ID}); err != nil {
+ t.Fatal(err)
+ }
+}
diff --git a/kernel/model/transaction_structure_test.go b/kernel/model/transaction_structure_test.go
index 682b0ebfe7..219ed60675 100644
--- a/kernel/model/transaction_structure_test.go
+++ b/kernel/model/transaction_structure_test.go
@@ -108,6 +108,33 @@ func TestInsertMissingTargetReturnsTransactionError(t *testing.T) {
requireStructureTransactionError(t, PerformTxSync(tx))
}
+func TestInsertRejectsDocumentSiblingAnchor(t *testing.T) {
+ for _, field := range []string{"previousID", "nextID"} {
+ t.Run(field, func(t *testing.T) {
+ fixture := setupStructureTransactionTest(t)
+ dom := util.NewLute().Md2BlockDOM("inserted", false)
+ operation := &Operation{Action: "insert", Data: dom}
+ if field == "previousID" {
+ operation.PreviousID = fixture.sourceID
+ } else {
+ operation.NextID = fixture.sourceID
+ }
+ err := PerformTxSync(&Transaction{DoOperations: []*Operation{operation}})
+ requireStructureTransactionError(t, err)
+ if err.Error() != "`"+field+"` cannot be the ID of a document" {
+ t.Fatalf("expected explicit document anchor error, got %v", err)
+ }
+ tree, err := LoadTreeByBlockID(fixture.sourceID)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if tree.Root.FirstChild.ID != fixture.childID || tree.Root.FirstChild != tree.Root.LastChild {
+ t.Fatal("rejected anchor changed the document")
+ }
+ })
+ }
+}
+
func TestMoveRejectsParagraphDirectlyUnderList(t *testing.T) {
tests := []struct {
name string