Skip to content

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

:bug: Close the remaining cross-boundary and reporting gaps

Danielbcfb8e569f50
diff --git a/kernel/model/import.go b/kernel/model/import.go
index 835bac1c9a..a3c0c6353d 100644
--- a/kernel/model/import.go
+++ b/kernel/model/import.go
@@ -645,6 +645,15 @@ func importSY0(zipPath, boxID, toPath string, createNotebook, autoDetect bool, s
if err = checkEncryptedImportDeck(unzipRootPath, trees, blockIDs); nil != err {
return
}
+ // 兜底校验:禁止跨加密边界块引。包内文档尚未入库,块树查不到,因此把本次导入的全部块 ID
+ // 一并放行,既拦住包外引用(普通笔记本或其它加密笔记本的块),又不误伤包内跨文档引用。
+ importedBlockIDs := make(map[string]bool, len(blockIDs))
+ for _, newID := range blockIDs {
+ importedBlockIDs[newID] = true
+ }
+ for _, tree := range trees {
+ degradeCrossBoundaryBlockRefsWithAllowed(tree.Root, tree.Box, nil, importedBlockIDs)
+ }
}
if importedBoxDoc {
if err = writeBoxDocID(boxID); err != nil {
@@ -936,9 +945,10 @@ func importSY0(zipPath, boxID, toPath string, createNotebook, autoDetect bool, s
newSyPath := filepath.Join(filepath.Dir(syPath), finalSyName)
if err = writeImportedTree(boxID, syPath, newSyPath, finalRelPath, data); err != nil {
logging.LogErrorf("write imported .sy [%s] failed: %s", syPath, err)
- // 只有"目标笔记本未解锁导致拒绝写盘"才替换为提示解锁的文案,其余写盘错误原样上抛,避免归因错误
+ // 只有"目标笔记本未解锁导致拒绝写盘"才替换为提示解锁的文案,其余写盘错误原样上抛,避免归因错误。
+ // 相对路径的父目录名来自导入包,需与文档标题一样转义后再进入错误消息。
if errors.Is(err, errImportedTreeBoxLocked) {
- err = errors.New(fmt.Sprintf(Conf.Language(388), finalRelPath))
+ err = errors.New(fmt.Sprintf(Conf.Language(388), htmlstd.EscapeString(finalRelPath)))
}
return
}
diff --git a/kernel/model/import_ref_degrade_test.go b/kernel/model/import_ref_degrade_test.go
new file mode 100644
index 0000000000..3f8543bbf0
--- /dev/null
+++ b/kernel/model/import_ref_degrade_test.go
@@ -0,0 +1,74 @@
+// 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.
+//
+// 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 (
+ "testing"
+
+ "github.com/88250/lute/ast"
+ "github.com/88250/lute/parse"
+)
+
+// newEncryptedImportRefFixture 构造一棵含两个块引的加密笔记本导入文档:
+// inPackageRef 指向包内块,externalRef 指向包外块。
+func newEncryptedImportRefFixture(t *testing.T, boxID, inPackageRef, externalRef string) (tree *parse.Tree, refs []*ast.Node) {
+ t.Helper()
+
+ root := &ast.Node{Type: ast.NodeDocument, ID: ast.NewNodeID()}
+ paragraph := &ast.Node{Type: ast.NodeParagraph, ID: ast.NewNodeID()}
+ root.AppendChild(paragraph)
+ for _, target := range []string{inPackageRef, externalRef} {
+ ref := &ast.Node{Type: ast.NodeTextMark, TextMarkType: "block-ref"}
+ ref.TextMarkBlockRefID = target
+ ref.TextMarkBlockRefSubtype = "s"
+ ref.TextMarkTextContent = "anchor"
+ paragraph.AppendChild(ref)
+ refs = append(refs, ref)
+ }
+ return &parse.Tree{ID: root.ID, Box: boxID, Root: root}, refs
+}
+
+func TestDegradeCrossBoundaryBlockRefsWithAllowed(t *testing.T) {
+ const (
+ encryptedBoxID = "20260911150000-encbox0"
+ inPackageID = "20260911150001-inpack1"
+ externalID = "20260911150002-outpk01"
+ )
+ tree, refs := newEncryptedImportRefFixture(t, encryptedBoxID, inPackageID, externalID)
+
+ // 模拟导入时的块 ID 重建:包内块 ID 已知但尚未入库
+ allowed := map[string]bool{inPackageID: true}
+ if degraded := degradeCrossBoundaryBlockRefsWithAllowed(tree.Root, tree.Box, nil, allowed); 1 != degraded {
+ t.Fatalf("expected exactly one degraded reference, got %d", degraded)
+ }
+ if refs[0].TextMarkBlockRefID != inPackageID || "s" != refs[0].TextMarkBlockRefSubtype {
+ t.Fatalf("reference inside the archive must survive, got id=%q subtype=%q",
+ refs[0].TextMarkBlockRefID, refs[0].TextMarkBlockRefSubtype)
+ }
+ if refs[1].TextMarkBlockRefID != "" || "" != refs[1].TextMarkBlockRefSubtype {
+ t.Fatalf("reference outside the archive must degrade to plain text, got id=%q", refs[1].TextMarkBlockRefID)
+ }
+ if "anchor" != refs[1].TextMarkTextContent {
+ t.Fatalf("degraded reference must keep its anchor text, got %q", refs[1].TextMarkTextContent)
+ }
+
+ // 不放行任何块时,包内引用同样按跨边界处理(回到原有语义)
+ tree2, _ := newEncryptedImportRefFixture(t, encryptedBoxID, inPackageID, externalID)
+ if degraded := degradeCrossBoundaryBlockRefsWithAllowed(tree2.Root, tree2.Box, nil, nil); 2 != degraded {
+ t.Fatalf("without allowed block IDs both references should degrade, got %d", degraded)
+ }
+}
diff --git a/kernel/model/mount.go b/kernel/model/mount.go
index a0cc78cdcf..696495551c 100644
--- a/kernel/model/mount.go
+++ b/kernel/model/mount.go
@@ -191,8 +191,9 @@ func removeBoxDir(p string) (err error) {
return
}
- // 目录已不存在或已被删除视为成功
- if !gulu.File.IsExist(p) {
+ // 目录确实已不存在才算删除成功;无法访问(权限、IO 错误)时不能误判为已删除,
+ // 否则调用方会继续清理数据库与运行态,留下"索引清空但目录还在"的笔记本。
+ if _, statErr := os.Stat(p); nil != statErr && os.IsNotExist(statErr) {
return nil
}
diff --git a/kernel/model/transaction.go b/kernel/model/transaction.go
index 56b564ec28..406cb1b75d 100644
--- a/kernel/model/transaction.go
+++ b/kernel/model/transaction.go
@@ -2044,7 +2044,14 @@ func (tx *Transaction) degradeCrossBoundaryBlockRefs(root *ast.Node, srcBox stri
}
func degradeCrossBoundaryBlockRefs0(root *ast.Node, srcBox string, restoredCreatedDocBoxes map[string]string) int {
- degraded := 0
+ return degradeCrossBoundaryBlockRefsWithAllowed(root, srcBox, restoredCreatedDocBoxes, nil)
+}
+
+// degradeCrossBoundaryBlockRefsWithAllowed 与 degradeCrossBoundaryBlockRefs 语义相同,
+// 额外把 allowedBlockIDs 视为本地块。导入 .sy.zip 时包内文档尚未入库,块树查不到,
+// 只有把本次导入的全部块 ID 一并放行,才能既拦住包外引用又不误伤包内跨文档引用。
+func degradeCrossBoundaryBlockRefsWithAllowed(root *ast.Node, srcBox string, restoredCreatedDocBoxes map[string]string,
+ allowedBlockIDs map[string]bool) (degraded int) {
localBlockIDs := map[string]struct{}{}
ast.Walk(root, func(n *ast.Node, entering bool) ast.WalkStatus {
if entering && n.IsBlock() && n.ID != "" {
@@ -2061,6 +2068,9 @@ func degradeCrossBoundaryBlockRefs0(root *ast.Node, srcBox string, restoredCreat
if _, local := localBlockIDs[n.TextMarkBlockRefID]; local {
return ast.WalkContinue
}
+ if allowedBlockIDs[n.TextMarkBlockRefID] {
+ return ast.WalkContinue
+ }
if targetBox, restored := restoredCreatedDocBoxes[n.TextMarkBlockRefID]; restored && "" != targetBox && targetBox == srcBox {
return ast.WalkContinue
}