Skip to content

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

:bug: fix encrypted-notebook enable/change-password deadlock + add loading feedback #18034

Danieldb2ac73cf511
diff --git a/app/src/config/tabs/accessTab.ts b/app/src/config/tabs/accessTab.ts
index 1859687424..6e0c47a970 100644
--- a/app/src/config/tabs/accessTab.ts
+++ b/app/src/config/tabs/accessTab.ts
@@ -420,10 +420,17 @@ const openEnableEncryptedDialog = (onSuccess: () => void, onCancel: () => void)
showMessage(window.siyuan.languages.passwordNoMatch);
return;
}
+ // Argon2id 密钥派生约耗时 1 秒,期间禁用按钮并显示 loading,避免用户误以为卡住而重复点击
+ const originalText = confirmBtn.textContent;
+ confirmBtn.setAttribute("disabled", "disabled");
+ confirmBtn.textContent = window.siyuan.languages.loading;
fetchPost("/api/notebook/enableEncryptedNotebooks", {password: pwd1}, () => {
showMessage(window.siyuan.languages.encryptedNotebookEnabled);
dialog.destroy();
onSuccess();
+ }, undefined, () => {
+ confirmBtn.removeAttribute("disabled");
+ confirmBtn.textContent = originalText;
});
});
};
@@ -460,12 +467,20 @@ const openChangeMasterPasswordDialog = () => {
showMessage(window.siyuan.languages.passwordNoMatch);
return;
}
+ // 改密需校验旧密码 + 重新派生 KEK + 重包络所有加密 box 的 DEK,耗时较长
+ const confirmBtn = btnsElement[1] as HTMLButtonElement;
+ const originalText = confirmBtn.textContent;
+ confirmBtn.setAttribute("disabled", "disabled");
+ confirmBtn.textContent = window.siyuan.languages.loading;
fetchPost("/api/notebook/changeMasterPassword", {
oldPassword: oldPwd,
newPassword: newPwd
}, () => {
showMessage(window.siyuan.languages.changeMasterPassword);
dialog.destroy();
+ }, undefined, () => {
+ confirmBtn.removeAttribute("disabled");
+ confirmBtn.textContent = originalText;
});
});
};
diff --git a/app/src/util/mount.ts b/app/src/util/mount.ts
index 8f27b8a63d..cc72d9fd4d 100644
--- a/app/src/util/mount.ts
+++ b/app/src/util/mount.ts
@@ -210,8 +210,11 @@ export const openEncryptedNotebook = (app: App, notebookId: string, name: string
if (!password) {
return false;
}
- btnsElement[1].setAttribute("disabled", "disabled");
- // 先解锁(派生 KEK + 解 DEK + 打开加密 db),成功后再挂载
+ const unlockBtn = btnsElement[1] as HTMLButtonElement;
+ const originalText = unlockBtn.textContent;
+ unlockBtn.setAttribute("disabled", "disabled");
+ unlockBtn.textContent = window.siyuan.languages.loading;
+ // 先解锁(派生 KEK + 解 DEK + 打开加密 db,Argon2id 约耗时 1 秒),成功后再挂载
fetchPost("/api/notebook/unlockBox", {
notebook: notebookId,
password
@@ -220,8 +223,9 @@ export const openEncryptedNotebook = (app: App, notebookId: string, name: string
notebook: notebookId
});
dialog.destroy();
- }, (err) => {
- btnsElement[1].removeAttribute("disabled");
+ }, undefined, (err) => {
+ unlockBtn.removeAttribute("disabled");
+ unlockBtn.textContent = originalText;
showMessage(err.msg || window.siyuan.languages.incorrectMasterPassword);
});
});
diff --git a/kernel/model/crypto.go b/kernel/model/crypto.go
index 9a7be2b5da..7a16419930 100644
--- a/kernel/model/crypto.go
+++ b/kernel/model/crypto.go
@@ -50,12 +50,6 @@ func EnableEncryptedNotebook(password string) error {
if len(password) == 0 {
return errors.New("password must not be empty")
}
- Conf.m.Lock()
- defer Conf.m.Unlock()
-
- if Conf.NotebookCrypto.Enabled {
- return errors.New("encrypted notebook already enabled")
- }
salt, err := util.GenerateSalt()
if err != nil {
@@ -73,12 +67,19 @@ func EnableEncryptedNotebook(password string) error {
return err
}
+ Conf.m.Lock()
+ if Conf.NotebookCrypto.Enabled {
+ Conf.m.Unlock()
+ return errors.New("encrypted notebook already enabled")
+ }
Conf.NotebookCrypto.Enabled = true
Conf.NotebookCrypto.MasterSalt = salt
Conf.NotebookCrypto.KDFParams = params
Conf.NotebookCrypto.KEKVerifier = verifierCT
Conf.NotebookCrypto.VerifierNonce = verifierCT[:12]
+ Conf.m.Unlock()
+ // Conf.Save 内部会加 Conf.m,不能在持锁状态下调用(RWMutex 不可重入)
Conf.Save()
return nil
}
@@ -245,9 +246,9 @@ func ChangeMasterPassword(oldPassword, newPassword string) error {
}
Conf.m.Lock()
- defer Conf.m.Unlock()
-
nc := Conf.NotebookCrypto
+ Conf.m.Unlock()
+
newKEK := util.DeriveKey(newPassword, nc.MasterSalt, nc.KDFParams)
newVerifier, err := util.Encrypt(newKEK, kekVerifierMagic)
if err != nil {
@@ -277,9 +278,12 @@ func ChangeMasterPassword(oldPassword, newPassword string) error {
b.SaveConf(boxConf)
}
+ Conf.m.Lock()
Conf.NotebookCrypto.KEKVerifier = newVerifier
Conf.NotebookCrypto.VerifierNonce = newVerifier[:12]
+ Conf.m.Unlock()
+ // Conf.Save 内部会加 Conf.m,不能在持锁状态下调用(RWMutex 不可重入)
Conf.Save()
return nil
}