An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:lock: Deep copy BoxCrypt in setNotebookConf, use IsEncryptedBox in GetBoxes, cleanup on CreateEncryptedBox failure https://github.com/siyuan-note/siyuan/issues/18034
Danielc92ed10cd86d
diff --git a/kernel/api/notebook.go b/kernel/api/notebook.goindex 1d70886bbf..4f438d17a5 100644--- a/kernel/api/notebook.go+++ b/kernel/api/notebook.go@@ -365,9 +365,10 @@ func setNotebookConf(c *gin.Context) {}boxConf := box.GetConf()- // 保留加密相关字段,不允许通过此 API 覆盖(加密状态只能通过专用 API 改变)+ // 深拷贝加密相关字段,防止反序列化请求体时被覆盖+ // BoxCrypt 是指针,UnmarshalJSON 会修改同一指针对象,必须用 model 层辅助函数深拷贝+ savedBoxCrypt := model.DeepCopyBoxEncryption(boxConf.BoxCrypt)savedEncrypted := boxConf.Encrypted- savedBoxCrypt := boxConf.BoxCryptif err = gulu.JSON.UnmarshalJSON(param, boxConf); err != nil {ret.Code = -1ret.Msg = err.Error()diff --git a/kernel/model/conf.go b/kernel/model/conf.goindex cd680f2ab3..b7af65e219 100644--- a/kernel/model/conf.go+++ b/kernel/model/conf.go@@ -1096,7 +1096,7 @@ func (conf *AppConf) GetBoxes() (ret []*Box) {id := notebook.IDname := notebook.Nameclosed := notebook.Closed- encrypted := notebook.Encrypted+ encrypted := IsEncryptedBox(id) // 使用 IsEncryptedBox 统一判定(含 backup fallback)box := &Box{ID: id, Name: name, Closed: closed, Encrypted: encrypted}ret = append(ret, box)}diff --git a/kernel/model/crypto.go b/kernel/model/crypto.goindex 5943be8353..ffbc0c6580 100644--- a/kernel/model/crypto.go+++ b/kernel/model/crypto.go@@ -996,6 +996,19 @@ func needWriteNotebookCryptBackup(boxID string, crypt *conf.BoxEncryption) boolexisting.CreatedAt != crypt.CreatedAt}+// DeepCopyBoxEncryption 深拷贝 BoxEncryption(含 []byte 字段),输入 nil 时返回 nil。+// 供 api 层在反序列化请求体前保存加密字段的不可变快照。+func DeepCopyBoxEncryption(src *conf.BoxEncryption) *conf.BoxEncryption {+ if src == nil {+ return nil+ }+ return &conf.BoxEncryption{+ WrappedDEK: append([]byte(nil), src.WrappedDEK...),+ WrapNonce: append([]byte(nil), src.WrapNonce...),+ CreatedAt: src.CreatedAt,+ }+}+// ListAllEncryptedBoxIDs 扫描 data 目录下所有含 notebook-crypt-backup.json 的 box 目录,// 补全 ListNotebooks 可能遗漏的 conf 损坏加密笔记本。供改密/禁用/检测等关键路径使用。// 统一使用 IsEncryptedBox 作为"是否加密"的唯一判定入口。@@ -1283,6 +1296,20 @@ func CreateEncryptedBox(name, password string) (id string, err error) {return "", err}+ // 若后续步骤失败,清理已创建的 box 目录和加密 db 文件,避免半创建状态+ boxCreated := true+ defer func() {+ if err != nil && boxCreated {+ sql.RemoveEncryptedDBFile(id)+ treenode.RemoveEncryptedBlockTreeDBFile(id)+ boxDir := filepath.Join(util.DataDir, id)+ if rmErr := filelock.Remove(boxDir); rmErr != nil {+ logging.LogErrorf("cleanup failed encrypted box [%s]: %s", id, rmErr)+ }+ id = ""+ }+ }()+box := &Box{ID: id}boxConf := box.GetConf()boxConf.Encrypted = true@@ -1296,7 +1323,8 @@ func CreateEncryptedBox(name, password string) (id string, err error) {// 回读校验加密配置已落盘,避免写失败后按普通笔记本处理verifyConf := box.GetConf()if verifyConf == nil || !verifyConf.Encrypted || verifyConf.BoxCrypt == nil {- return "", errors.New("encrypted notebook metadata verification failed after write")+ err = errors.New("encrypted notebook metadata verification failed after write")+ return "", err}// 复用刚派生的 DEK 直接开 db + 缓存,省去再次 Argon2id 解锁