An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:sparkles: Add atomic unlock-and-mount for encrypted notebooks and unify API naming #18034
Daniel58d5fb815b24
diff --git a/app/src/util/mount.ts b/app/src/util/mount.tsindex 5d2948d104..c5bb1b867b 100644--- a/app/src/util/mount.ts+++ b/app/src/util/mount.ts@@ -181,10 +181,7 @@ export const newEncryptedNotebook = () => {password});if (response.code === 0) {- // 内核创建时已解锁(DEK 已缓存 + 加密 db 已打开),直接挂载- fetchPost("/api/notebook/openNotebook", {- notebook: response.data.notebook.id- });+ // createEncryptedNotebook 内核已原子完成创建+挂载,无需再单独 openNotebookdialog.destroy();} else {btnsElement[1].disabled = false;@@ -221,15 +218,12 @@ export const openEncryptedNotebook = (app: App, notebookId: string, name: stringreturn false;}btnsElement[1].disabled = true;- // 先解锁(派生 KEK + 解 DEK + 打开加密 db,Argon2id 约耗时 1 秒),成功后再挂载- const response = await fetchSyncPost("/api/notebook/unlockBox", {+ // 原子化解锁并挂载:UnlockBox 成功后立即 Mount,Mount 失败则后端自动 LockBox 回滚,避免 DEK 残留+ const response = await fetchSyncPost("/api/notebook/unlockAndOpenNotebook", {notebook: notebookId,password});if (response.code === 0) {- fetchPost("/api/notebook/openNotebook", {- notebook: notebookId- });dialog.destroy();} else {btnsElement[1].disabled = false;diff --git a/kernel/api/notebook.go b/kernel/api/notebook.goindex 49b772fdb7..944fa30268 100644--- a/kernel/api/notebook.go+++ b/kernel/api/notebook.go@@ -502,8 +502,7 @@ func disableEncryptedNotebooks(c *gin.Context) {}// createEncryptedNotebook 创建一个新的加密笔记本。前置:加密功能已启用。-// 创建时需提供主密码(用于派生 KEK 包络 DEK)。创建后笔记本处于锁定状态,-// 调用方需再调 unlockBox + openNotebook 才能打开。+// 创建时需提供主密码(用于派生 KEK 包络 DEK)。创建成功后内核已原子完成挂载。func createEncryptedNotebook(c *gin.Context) {ret := gulu.Ret.NewResult()defer c.JSON(http.StatusOK, ret)@@ -528,14 +527,30 @@ func createEncryptedNotebook(c *gin.Context) {return}+ // 创建时 DEK 已缓存 + 加密 db 已打开,此处直接挂载;失败则锁定回滚,避免 DEK 残留+ if _, mountErr := model.Mount(id); mountErr != nil {+ model.LockBox(id)+ ret.Code = -1+ ret.Msg = mountErr.Error()+ return+ }++ box := model.Conf.GetBox(id)+ evt := util.NewCmdResult("mount", 0, util.PushModeBroadcast)+ evt.Data = map[string]any{+ "box": box,+ "existed": true,+ }+ util.PushEvent(evt)+ret.Data = map[string]any{- "notebook": model.Conf.GetBox(id),+ "notebook": box,}}-// unlockBox 用主密码派生 KEK 并解出指定加密笔记本的 DEK,缓存到内存。+// unlockNotebook 用主密码派生 KEK 并解出指定加密笔记本的 DEK,缓存到内存。// 解锁后该笔记本即可被 Mount。每次调用跑一次 Argon2id(约 1 秒)。-func unlockBox(c *gin.Context) {+func unlockNotebook(c *gin.Context) {ret := gulu.Ret.NewResult()defer c.JSON(http.StatusOK, ret)@@ -575,8 +590,75 @@ func unlockBox(c *gin.Context) {}}-// lockBox 锁定指定加密笔记本:清除其 DEK 缓存并 Unmount。-func lockBox(c *gin.Context) {+// unlockAndOpenNotebook 原子化解锁并挂载加密笔记本:UnlockBox 成功后立即 Mount,+// Mount 失败则 LockBox 回滚(清除 DEK),避免 DEK 残留在内存但笔记本未挂载的不一致状态。+func unlockAndOpenNotebook(c *gin.Context) {+ ret := gulu.Ret.NewResult()+ defer c.JSON(http.StatusOK, ret)++ arg, ok := util.JsonArg(c, ret)+ if !ok {+ return+ }++ var notebook, password string+ if !util.ParseJsonArgs(arg, ret,+ util.BindJsonArg("notebook", ¬ebook, true, true),+ util.BindJsonArg("password", &password, true, true),+ ) {+ return+ }++ if util.InvalidIDPattern(notebook, ret) {+ return+ }++ boxCrypt, err := model.GetBoxEncryption(notebook)+ if err != nil {+ ret.Code = -1+ ret.Msg = model.Conf.Language(318)+ return+ }+ if boxCrypt == nil || len(boxCrypt.WrappedDEK) == 0 {+ ret.Code = -1+ ret.Msg = model.Conf.Language(319)+ return+ }++ if err := model.UnlockBox(notebook, password, boxCrypt); err != nil {+ ret.Code = -1+ ret.Msg = err.Error()+ return+ }++ // 解锁成功后立即挂载;失败则回滚锁定,清除 DEK 避免残留+ msgId := util.PushMsg(model.Conf.Language(45), 1000*60*15)+ defer util.PushClearMsg(msgId)+ if _, mountErr := model.Mount(notebook); mountErr != nil {+ model.LockBox(notebook)+ ret.Code = -1+ ret.Msg = mountErr.Error()+ return+ }++ box := model.Conf.Box(notebook)+ if nil == box {+ model.LockBox(notebook)+ ret.Code = -1+ ret.Msg = "opened notebook [" + notebook + "] not found"+ return+ }++ evt := util.NewCmdResult("mount", 0, util.PushModeBroadcast)+ evt.Data = map[string]any{+ "box": box,+ "existed": true,+ }+ util.PushEvent(evt)+}++// lockNotebook 锁定指定加密笔记本:清除其 DEK 缓存并 Unmount。+func lockNotebook(c *gin.Context) {ret := gulu.Ret.NewResult()defer c.JSON(http.StatusOK, ret)diff --git a/kernel/api/router.go b/kernel/api/router.goindex 28a19a8c0e..221da2fb1d 100644--- a/kernel/api/router.go+++ b/kernel/api/router.go@@ -121,8 +121,9 @@ func ServeAPI(ginServer *gin.Engine) {ginServer.Handle("POST", "/api/notebook/enableEncryptedNotebooks", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, enableEncryptedNotebooks)ginServer.Handle("POST", "/api/notebook/disableEncryptedNotebooks", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, disableEncryptedNotebooks)ginServer.Handle("POST", "/api/notebook/createEncryptedNotebook", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, createEncryptedNotebook)- ginServer.Handle("POST", "/api/notebook/unlockBox", model.CheckAuth, model.CheckAdminRole, unlockBox)- ginServer.Handle("POST", "/api/notebook/lockBox", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, lockBox)+ ginServer.Handle("POST", "/api/notebook/unlockNotebook", model.CheckAuth, model.CheckAdminRole, unlockNotebook)+ ginServer.Handle("POST", "/api/notebook/lockNotebook", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, lockNotebook)+ ginServer.Handle("POST", "/api/notebook/unlockAndOpenNotebook", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, unlockAndOpenNotebook)ginServer.Handle("POST", "/api/notebook/changeMasterPassword", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, changeMasterPassword)ginServer.Handle("POST", "/api/notebook/getEncryptedNotebookStatus", model.CheckAuth, getEncryptedNotebookStatus)ginServer.Handle("POST", "/api/notebook/exportNotebookCryptoBackup", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, exportNotebookCryptoBackup)