{"record":{"id":"00eebf53d20ab848","repo":"siyuan-note/siyuan","slug":"conf-language-312","errorCode":null,"errorMessage":"Conf.Language(312)","messagePattern":"Conf\\.Language\\(312\\)","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/crypto.go","lineNumber":1008,"sourceCode":"\treturn EnableEncryptedNotebook(password)\n}\n\n// EnableEncryptedNotebook 启用加密笔记本功能：生成 MasterSalt、派生 KEK、写入校验值并持久化。\n// 重复调用（已启用）返回错误，避免覆盖现有加密笔记本的密钥参数。\n// KEK 不缓存——启用后用户需对每个加密笔记本单独调 UnlockBox 解锁。\nfunc EnableEncryptedNotebook(password string) error {\n\tif len(password) == 0 {\n\t\treturn errors.New(\"password must not be empty\")\n\t}\n\n\tnotebookCryptoMu.Lock()\n\tdefer notebookCryptoMu.Unlock()\n\n\tConf.m.RLock()\n\tcurrent := *Conf.NotebookCrypto\n\tConf.m.RUnlock()\n\tif current.Enabled && notebookCryptoConfigurationComplete(&current) {\n\t\treturn errors.New(Conf.Language(312))\n\t}\n\n\thasEncrypted, listErr := hasEncryptedNotebook()\n\tif listErr != nil {\n\t\treturn fmt.Errorf(\"list encrypted notebooks failed: %w\", listErr)\n\t}\n\thasHistory, historyErr := scanEncryptedNotebookHistory()\n\tif historyErr != nil {\n\t\treturn fmt.Errorf(\"check encrypted notebook history failed: %w\", historyErr)\n\t}\n\thasBackup := filelock.IsExist(dataCryptoBackupPath())\n\tif hasEncrypted || hasHistory || hasBackup {\n\t\t// 现存笔记本、已删除笔记本历史或全局备份均表示已有密钥域，必须恢复并认证，不能生成新 MasterSalt。\n\t\tkek, restoreErr := tryRestoreNotebookCryptoFromBackupLocked(password)\n\t\tif kek != nil {\n\t\t\tzeroAndClear(kek)\n\t\t}\n\t\tif restoreErr != nil {","sourceCodeStart":990,"sourceCodeEnd":1026,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/crypto.go#L990-L1026","documentation":"EnableEncryptedNotebook refuses to run when the encrypted-notebook feature is already fully configured, returning the localized message Conf.Language(312) (\"encryption is already enabled\"). Re-running enablement would overwrite the existing MasterSalt/KEK parameters and destroy access to already-encrypted notebooks, so the guard returns this user-facing error instead.","triggerScenarios":"Calling EnableEncryptedNotebook (directly or via EnableEncryptedNotebookWithSync) when Conf.NotebookCrypto.Enabled is true and notebookCryptoConfigurationComplete(&current) reports the config has MasterSalt/verification data — e.g. double-invoking the enable API, clicking the enable action twice in the UI, or enabling on a workspace where encryption was set up previously.","commonSituations":"A second device or a re-run script calls the enable endpoint against an already-encrypted workspace; a UI flow retries the call after a timeout not knowing the first call succeeded; tests call EnableEncryptedNotebook twice without resetting the config between cases.","solutions":["Before calling, check Conf.NotebookCrypto.Enabled (and completeness) and skip the call if already enabled","Treat this error as success in idempotent flows: if the state is 'already enabled', proceed without re-enabling","If you actually need to change/recover the key material, use the recovery/restore path (tryRestoreNotebookCryptoFromBackup) rather than re-enabling","In tests, reset or disable NotebookCrypto config between test cases"],"exampleFix":"// before\nerr := model.EnableEncryptedNotebook(password)\n// after\nif Conf.NotebookCrypto.Enabled && model.NotebookCryptoConfigurationComplete(Conf.NotebookCrypto) {\n    return nil // already enabled; nothing to do\n}\nerr := model.EnableEncryptedNotebook(password)","handlingStrategy":"try-catch","validationCode":"if Conf.NotebookCrypto.Enabled && model.NotebookCryptoConfigurationComplete(Conf.NotebookCrypto) {\n    return nil // already enabled, skip\n}","typeGuard":null,"tryCatchPattern":"if err := model.EnableEncryptedNotebook(password); err != nil {\n    if err.Error() == model.Conf.Language(312) {\n        return nil // idempotent: already enabled\n    }\n    return err\n}","preventionTips":["Make enable flows idempotent: treat 'already enabled' as success","Check the Enabled flag before invoking enable APIs","Avoid re-running bootstrap scripts against an already-configured workspace","For key changes or loss, use the recovery/restore path, never re-enable"],"tags":["idempotency","invalid-state","encrypted-notebook"],"backgroundTag":"invalid-state-transition","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}