Skip to content

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

:lock: Fix sync conflict encrypted .sy parsed as ciphertext, SetAttrViewFilters/Sorts box-aware via deriveAVBoxID helper, ImportSY AV mirror/relation ordering for encrypted boxes (encrypt + SetAVBoxID before mirror ops), remove redundant AV copy block https://github.com/siyuan-note/siyuan/issues/18034

Danielc9c5f644494d
diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go
index 932a04a6fe..08813c313b 100644
--- a/kernel/model/attribute_view.go
+++ b/kernel/model/attribute_view.go
@@ -3544,10 +3544,51 @@ func (tx *Transaction) doSetAttrViewFilters(operation *Operation) (ret *TxErr) {
return
}
+// avParseView 根据 blockID 推导 box 上下文,使用 box-aware 或全局 AV 解析。
+func avParseView(avID, blockID string) (*av.AttributeView, error) {
+ boxID := deriveAVBoxID(blockID)
+ if boxID != "" {
+ return av.ParseAttributeViewInBox(avID, boxID)
+ }
+ return av.ParseAttributeView(avID)
+}
+
+// avSaveView 根据 blockID 推导 box 上下文,使用 box-aware 或全局 AV 保存。
+func avSaveView(attrView *av.AttributeView, blockID string) error {
+ boxID := deriveAVBoxID(blockID)
+ if boxID != "" {
+ _, parseErr := av.ParseAttributeViewInBox(attrView.ID, boxID)
+ if parseErr == nil {
+ return av.SaveAttributeView(attrView)
+ }
+ }
+ return av.SaveAttributeView(attrView)
+}
+
+// deriveAVBoxID 通过 blockID 反查所在 box。blockID 为空或不是加密 box 时返回空串。
+func deriveAVBoxID(blockID string) string {
+ if blockID == "" {
+ return ""
+ }
+ bt := treenode.GetBlockTree(blockID)
+ if bt == nil {
+ for _, encBoxID := range treenode.GetOpenedEncryptedBoxIDs() {
+ if encBT := treenode.GetBlockTreeInBox(blockID, encBoxID); encBT != nil {
+ bt = encBT
+ break
+ }
+ }
+ }
+ if bt == nil || !IsEncryptedBox(bt.BoxID) {
+ return ""
+ }
+ return bt.BoxID
+}
+
// SetAttrViewFilters 用新的过滤规则数组整体替换指定视图的过滤规则,并持久化。
// data 为 JSON 反序列化前的 []any(通常是前端传来的过滤节点树)。
func SetAttrViewFilters(avID, blockID string, data []any) (err error) {
- attrView, err := av.ParseAttributeView(avID)
+ attrView, err := avParseView(avID, blockID)
if err != nil {
return
}
@@ -3579,7 +3620,7 @@ func SetAttrViewFilters(avID, blockID string, data []any) (err error) {
return
}
- err = av.SaveAttributeView(attrView)
+ err = avSaveView(attrView, blockID)
return
}
@@ -3594,7 +3635,7 @@ func (tx *Transaction) doSetAttrViewSorts(operation *Operation) (ret *TxErr) {
// SetAttrViewSorts 用新的排序规则数组整体替换指定视图的排序规则,并持久化。
// data 为 JSON 反序列化前的 []any。
func SetAttrViewSorts(avID, blockID string, data []any) (err error) {
- attrView, err := av.ParseAttributeView(avID)
+ attrView, err := avParseView(avID, blockID)
if err != nil {
return
}
@@ -3615,7 +3656,7 @@ func SetAttrViewSorts(avID, blockID string, data []any) (err error) {
}
view.Sorts = newSorts
- err = av.SaveAttributeView(attrView)
+ err = avSaveView(attrView, blockID)
return
}
diff --git a/kernel/model/attribute_view_render.go b/kernel/model/attribute_view_render.go
index 7eff6f5c29..2528796196 100644
--- a/kernel/model/attribute_view_render.go
+++ b/kernel/model/attribute_view_render.go
@@ -40,7 +40,8 @@ import (
func RenderAttributeView(blockID, avID, viewID, query string, page, pageSize int, groupPaging map[string]any, createIfNotExist, ignoreRows bool) (viewable av.Viewable, attrView *av.AttributeView, err error) {
waitForSyncingStorages()
- // 加密笔记本的 AV 定义存笔记本级路径,通过 blockID 反查 boxID 并预设归属
+ // 加密笔记本的 AV 定义存笔记本级路径,通过 blockID 反查 boxID
+ avBoxID := ""
if "" != blockID {
bt := treenode.GetBlockTree(blockID)
if nil == bt {
@@ -52,12 +53,18 @@ func RenderAttributeView(blockID, avID, viewID, query string, page, pageSize int
}
}
if nil != bt && IsEncryptedBox(bt.BoxID) {
- av.SetAVBoxID(avID, bt.BoxID)
+ avBoxID = bt.BoxID
}
}
// 通过 fallback 查找 AV 定义路径(普通 box 全局,加密笔记本笔记本级)
- existPath, _ := av.FindAttributeViewPath(avID)
+ // 已知 box 时直接用 InBox 查找,避免全局 pending 映射被并发覆盖
+ var existPath string
+ if avBoxID != "" {
+ existPath, _ = av.FindAttributeViewPathInBox(avID, avBoxID)
+ } else {
+ existPath, _ = av.FindAttributeViewPath(avID)
+ }
if "" == existPath {
// fallback 找不到时按全局路径检查(首次创建场景)
existPath = av.GetAttributeViewDataPath(avID)
@@ -73,6 +80,11 @@ func RenderAttributeView(blockID, avID, viewID, query string, page, pageSize int
return
}
+ // 加密笔记本首次创建:仅设置 pending 用于 SaveAttributeView 路径路由,创建后立即清除
+ if avBoxID != "" {
+ av.SetAVBoxID(avID, avBoxID)
+ defer av.SetAVBoxID(avID, "") // 创建完成立即清除,避免污染后续路由
+ }
attrView = av.NewAttributeView(avID)
if err = av.SaveAttributeView(attrView); err != nil {
logging.LogErrorf("save attribute view [%s] failed: %s", avID, err)
@@ -80,7 +92,12 @@ func RenderAttributeView(blockID, avID, viewID, query string, page, pageSize int
}
}
- attrView, err = av.ParseAttributeView(avID)
+ // 已知 box 时直接用 InBox 解析,不依赖全局 pending 状态
+ if avBoxID != "" {
+ attrView, err = av.ParseAttributeViewInBox(avID, avBoxID)
+ } else {
+ attrView, err = av.ParseAttributeView(avID)
+ }
if err != nil {
logging.LogErrorf("parse attribute view [%s] failed: %s", avID, err)
return
diff --git a/kernel/model/import.go b/kernel/model/import.go
index 795261c366..b94e5ee6ae 100644
--- a/kernel/model/import.go
+++ b/kernel/model/import.go
@@ -302,12 +302,17 @@ func ImportSY(zipPath, boxID, toPath string) (err error) {
}
// 加密笔记本的 AV 定义不能拷到全局目录(明文泄漏 + 路由冲突),
- // 后续由笔记本级 AV 拷贝逻辑处理
+ // 需要先加密写入 <boxID>/storage/av/,后续 mirror/relation 操作才能正确路由
if !IsEncryptedBox(boxID) {
targetStorageAvDir := filepath.Join(util.DataDir, "storage", "av")
if copyErr := filelock.Copy(storageAvDir, targetStorageAvDir); nil != copyErr {
logging.LogErrorf("copy storage av dir from [%s] to [%s] failed: %s", storageAvDir, targetStorageAvDir, copyErr)
}
+ } else {
+ // 加密笔记本:先把 AV 定义加密写入笔记本级目录,建立 box 映射后 mirror/relation 才能正确路由
+ if err = encryptBoxAVFiles(boxID, storageAvDir); err != nil {
+ return
+ }
}
// 重新指向数据库属性值
@@ -400,40 +405,7 @@ func ImportSY(zipPath, boxID, toPath string) (err error) {
}
// storage 文件夹已在上方处理,所以这里删除源 storage 文件夹,避免后面被拷贝到导入目录下 targetDir
- // 加密笔记本需要先把 AV 定义加密拷贝到笔记本级目录,再删源 storage
- if IsEncryptedBox(boxID) && gulu.File.IsExist(storageAvDir) {
- boxAVDir := filepath.Join(util.DataDir, boxID, "storage", "av")
- if err = os.MkdirAll(boxAVDir, 0755); err != nil {
- return
- }
- filelock.Walk(storageAvDir, func(path string, d fs.DirEntry, walkErr error) error {
- if walkErr != nil || d == nil || d.IsDir() {
- return walkErr
- }
- if !strings.HasSuffix(d.Name(), ".json") {
- return nil
- }
- src, readErr := os.ReadFile(path)
- if readErr != nil {
- return readErr
- }
- avID := strings.TrimSuffix(d.Name(), ".json")
- // av.EncryptAVData 内部按 box 是否加密/已解锁路由,加密笔记本用 avKey+AAD
- enc, encErr := av.EncryptAVData(boxID, avID, src)
- if encErr != nil {
- return encErr
- }
- return os.WriteFile(filepath.Join(boxAVDir, d.Name()), enc, 0644)
- })
- }
-
- // 普通笔记本拷贝到全局 storage/av/(加密笔记本已在上面处理)
- if !IsEncryptedBox(boxID) {
- targetStorageAvDir := filepath.Join(util.DataDir, "storage", "av")
- if copyErr := filelock.Copy(storageAvDir, targetStorageAvDir); nil != copyErr {
- logging.LogErrorf("copy storage av dir from [%s] to [%s] failed: %s", storageAvDir, targetStorageAvDir, copyErr)
- }
- }
+ // 加密笔记本已在上面完成 notebook 级 AV 拷贝,安全删除
if removeErr := os.RemoveAll(storage); nil != removeErr {
logging.LogErrorf("remove temp storage av dir [%s] failed: %s", storage, removeErr)
@@ -1601,6 +1573,38 @@ func processBase64Img(n *ast.Node, dest string, assetDirPath, boxID string) {
n.Tokens = []byte(assetURL)
}
+// encryptBoxAVFiles 把临时目录中的 AV 定义文件加密写入加密笔记本级目录。
+// 写入后通过 SetAVBoxID 建立 AV 归属映射,确保后续 mirror/relation 操作正确路由。
+func encryptBoxAVFiles(boxID, storageAvDir string) error {
+ if !IsEncryptedBox(boxID) || !gulu.File.IsExist(storageAvDir) {
+ return nil
+ }
+ boxAVDir := filepath.Join(util.DataDir, boxID, "storage", "av")
+ if err := os.MkdirAll(boxAVDir, 0755); err != nil {
+ return err
+ }
+ return filelock.Walk(storageAvDir, func(path string, d fs.DirEntry, walkErr error) error {
+ if walkErr != nil || d == nil || d.IsDir() {
+ return walkErr
+ }
+ if !strings.HasSuffix(d.Name(), ".json") {
+ return nil
+ }
+ src, readErr := os.ReadFile(path)
+ if readErr != nil {
+ return readErr
+ }
+ avID := strings.TrimSuffix(d.Name(), ".json")
+ // 加密写入前建立 box 映射,mirror/relation 操作即可正确路由
+ av.SetAVBoxID(avID, boxID)
+ enc, encErr := av.EncryptAVData(boxID, avID, src)
+ if encErr != nil {
+ return encErr
+ }
+ return os.WriteFile(filepath.Join(boxAVDir, d.Name()), enc, 0644)
+ })
+}
+
func htmlBlock2Inline(tree *parse.Tree) {
imgHtmlBlocks := map[*ast.Node]*html.Node{}
aHtmlBlocks := map[*ast.Node]*html.Node{}
diff --git a/kernel/model/repository.go b/kernel/model/repository.go
index 840e686ecb..8293d66181 100644
--- a/kernel/model/repository.go
+++ b/kernel/model/repository.go
@@ -1988,6 +1988,17 @@ func processSyncMergeResult(exit, byHand bool, mergeResult *dejavu.MergeResult,
boxID := parts[0]
absPath := filepath.Join(util.TempDir, "repo", "sync", "conflicts", mergeResult.Time.Format("2006-01-02-150405"), file.Path)
+ // 加密笔记本的冲突 .sy 在临时目录里是密文,loadTree 无法从 temp 路径反推 box 解密
+ if IsEncryptedBox(boxID) {
+ raw, readErr := os.ReadFile(absPath)
+ if readErr == nil {
+ data := decryptRepoDataIfNeeded(raw, file.Path)
+ if writeErr := os.WriteFile(absPath, data, 0644); writeErr != nil {
+ logging.LogErrorf("decrypt conflicted file [%s] failed: %s", absPath, writeErr)
+ continue
+ }
+ }
+ }
tree, loadTreeErr := loadTree(absPath, luteEngine)
if nil != loadTreeErr {
logging.LogErrorf("load conflicted file [%s] failed: %s", absPath, loadTreeErr)