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: Fix lockBox not clearing DEK when encrypted notebook is unlocked but unmounted #18034
Daniel13e0ebc1c65c
diff --git a/kernel/model/crypto.go b/kernel/model/crypto.goindex 84e27a843f..d2ee566051 100644--- a/kernel/model/crypto.go+++ b/kernel/model/crypto.go@@ -536,13 +536,6 @@ func restoreNotebookCryptoConfigFromBackup() {}backup.KDFParams = params- // 若有加密笔记本存在,不在无主密码的路径提交启用状态;backup 文件已就位,- // 等用户解锁时走带密码的恢复路径(含现有 box WrappedDEK 校验)。- if len(ListAllEncryptedBoxIDs()) > 0 {- logging.LogInfof("notebook crypto backup found but deferred restore (encrypted notebooks exist, waiting for unlock)")- return- }-backup.Enabled = trueConf.m.Lock()*Conf.NotebookCrypto = *backupdiff --git a/kernel/model/crypto_test.go b/kernel/model/crypto_test.goindex 2ff4d08fdd..f662cdc40d 100644--- a/kernel/model/crypto_test.go+++ b/kernel/model/crypto_test.go@@ -18,8 +18,11 @@ package modelimport ("bytes"+ "os"+ "path/filepath""testing"+ "github.com/88250/gulu""github.com/siyuan-note/siyuan/kernel/conf""github.com/siyuan-note/siyuan/kernel/util")@@ -165,3 +168,54 @@ func TestBoxEncryptionRoundTripViaUtil(t *testing.T) {t.Fatalf("DEK round-trip mismatch")}}++// TestUnmount0ClearsDEKForUnmountedEncryptedBox 验证安全修复:加密笔记本在+// "已解锁(DEK 在内存)但未挂载(不在 GetOpenedBoxes)"的状态下调用 unmount0,+// DEK 仍应被清除,否则锁定后认证 API 仍可读取明文。+// 直接测试 clearDEKIfUnlockedEncryptedBox(unmount0 在 box==nil 分支调用的清理逻辑),+// 避免依赖未初始化的全局 Conf。+func TestUnmount0ClearsDEKForUnmountedEncryptedBox(t *testing.T) {+ boxID := "unmount-unlocked-test-box"++ // 临时替换 DataDir,创建加密 box 的 conf.json,让 IsEncryptedBox 返回 true+ origDataDir := util.DataDir+ tempDir := t.TempDir()+ util.DataDir = tempDir+ defer func() {+ util.DataDir = origDataDir+ LockAllBoxes() // 测试后清理所有 DEK 缓存+ }()++ // 写入加密 box 的 conf.json+ confDir := filepath.Join(tempDir, boxID, ".siyuan")+ if err := os.MkdirAll(confDir, 0755); err != nil {+ t.Fatalf("mkdir conf dir failed: %v", err)+ }+ boxConf := conf.NewBoxConf()+ boxConf.Encrypted = true+ boxConf.Closed = true // 未挂载(关闭状态)+ confData, _ := gulu.JSON.MarshalIndentJSON(boxConf, "", " ")+ if err := os.WriteFile(filepath.Join(confDir, "conf.json"), confData, 0644); err != nil {+ t.Fatalf("write conf.json failed: %v", err)+ }++ // 确认 IsEncryptedBox 返回 true+ if !IsEncryptedBox(boxID) {+ t.Fatalf("precondition failed: IsEncryptedBox should return true")+ }++ // 注入 DEK 模拟已解锁状态+ dek, _ := util.GenerateDEK()+ setDEKForTest(boxID, dek)+ if !IsBoxUnlocked(boxID) {+ t.Fatalf("precondition failed: box should be unlocked after setDEKForTest")+ }++ // 调用清理逻辑(unmount0 在 box 未挂载分支调用的函数)+ clearDEKIfUnlockedEncryptedBox(boxID)++ // 验证 DEK 已被清除+ if IsBoxUnlocked(boxID) {+ t.Fatalf("DEK should be cleared for unlocked encrypted box")+ }+}diff --git a/kernel/model/mount.go b/kernel/model/mount.goindex c59445c5f6..623c14e7ea 100644--- a/kernel/model/mount.go+++ b/kernel/model/mount.go@@ -219,9 +219,21 @@ func Unmount(boxID string) {util.PushEvent(evt)}+// clearDEKIfUnlockedEncryptedBox 清除已解锁但未挂载的加密笔记本的 DEK。+// unmount0 在 box 未挂载(Conf.Box 返回 nil)时调用,覆盖 unlockBox 解锁后未 mount 即 lock 的场景:+// 此时 DEK 仍在内存,若不清除,锁定后认证 API 仍可读取明文。+func clearDEKIfUnlockedEncryptedBox(boxID string) {+ if IsEncryptedBox(boxID) && IsBoxUnlocked(boxID) {+ ClearDEK(boxID)+ }+}+func unmount0(boxID string) {box := Conf.Box(boxID)if nil == box {+ // 笔记本未挂载(Closed)。若它是已解锁的加密笔记本(DEK 在内存),+ // 仍需 ClearDEK 清除残留密钥材料,否则锁定后认证 API 仍可读取明文。+ clearDEKIfUnlockedEncryptedBox(boxID)return}