{"record":{"id":"00465dfc7c20c322","repo":"siyuan-note/siyuan","slug":"cannot-save-incomplete-notebook-crypto-configurati","errorCode":null,"errorMessage":"cannot save incomplete notebook crypto configuration","messagePattern":"cannot save incomplete notebook crypto configuration","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/crypto.go","lineNumber":390,"sourceCode":"\treturn nil\n}\n\n// saveNotebookCryptoBackup 把当前 NotebookCrypto（含 MasterSalt/KEKVerifier/KDFParams）备份到 DataDir。\n// kek 必须非 nil：在 Checksum 定型后计算 KEKMAC 并落盘，保证恢复路径可通过 MAC 校验。\n// 无 KEK 生成的备份 KEKMAC 必为空，会被 deriveKEK/恢复路径拒绝，等于制造无法解锁的状态（详见设计 §19）。\nfunc saveNotebookCryptoBackup(kek []byte) error {\n\tif kek == nil {\n\t\t// 无 KEK 时不得生成当前格式备份：KEKMAC 缺失会被 deriveKEK/恢复路径拒绝，\n\t\t// 生成即等于制造无法解锁的状态。\n\t\treturn errors.New(\"cannot generate notebook crypto backup without KEK\")\n\t}\n\tConf.m.Lock()\n\tnc := *Conf.NotebookCrypto // 值拷贝\n\tprepareBackupForWrite(&nc)\n\tnc.KEKMAC = computeKEKMAC(&nc, kek)\n\tif !notebookCryptoConfigurationComplete(&nc) {\n\t\tConf.m.Unlock()\n\t\treturn errors.New(\"cannot save incomplete notebook crypto configuration\")\n\t}\n\tConf.NotebookCrypto.Spec = nc.Spec\n\tConf.NotebookCrypto.BackupID = nc.BackupID\n\tConf.NotebookCrypto.CreatedAt = nc.CreatedAt\n\tConf.NotebookCrypto.Checksum = nc.Checksum\n\tConf.NotebookCrypto.KEKMAC = nc.KEKMAC // 保持 Conf 与备份文件的 KEKMAC 一致\n\tConf.m.Unlock()\n\tbackupPath := dataCryptoBackupPath()\n\tif err := os.MkdirAll(filepath.Dir(backupPath), 0755); err != nil {\n\t\treturn fmt.Errorf(\"mkdir notebook crypto backup dir failed: %w\", err)\n\t}\n\tdata, err := json.Marshal(nc)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"marshal notebook crypto backup failed: %w\", err)\n\t}\n\tif err := atomicWriteFile(backupPath, data); err != nil {\n\t\treturn fmt.Errorf(\"write notebook crypto backup failed: %w\", err)\n\t}","sourceCodeStart":372,"sourceCodeEnd":408,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/crypto.go#L372-L408","documentation":"saveNotebookCryptoBackup rejects the write when notebookCryptoConfigurationComplete(&nc) is false after preparing the copy and computing KEKMAC — i.e. the current NotebookCrypto configuration is missing required fields (salt, verifier, KDF params, etc.) and writing a backup of it would produce an unusable artifact. The mutex is released and nothing is persisted or copied back to Conf.","triggerScenarios":"Calling saveNotebookCryptoBackup while Conf.NotebookCrypto is only partially populated — e.g. encryption enablement failed midway, a migration left fields empty, or ChangeMasterPassword/deriveKEK ran against a config that lacks MasterSalt/KEKVerifier/KDFParams.","commonSituations":"Corrupted or partially written conf.json after a crash; manually edited configuration missing fields; a failed first-time enable of encrypted notebooks leaving a half-initialized NotebookCrypto; version migration bugs.","solutions":["Inspect Conf.NotebookCrypto (conf.json) for missing required fields and re-run the enable/derive flow to populate them completely","Restore conf.json from a known-good backup or re-enable encrypted notebooks from scratch (re-encrypting data)","Ensure KEK derivation succeeded before the save call — a failed derivation can leave the config incomplete","Check for recent version migrations that may have introduced partial configuration and apply the documented fix"],"exampleFix":"// before: saving with a half-initialized config\nif err := saveNotebookCryptoBackup(kek); err != nil { /* incomplete config */ }\n// after: complete the configuration first\nnc.MasterSalt = newSalt\nnc.KEKVerifier = makeVerifier(kek)\nnc.KDFParams = params\napplyNotebookCrypto(nc)\nreturn saveNotebookCryptoBackup(kek)","handlingStrategy":"validation","validationCode":"Conf.m.RLock()\ncomplete := notebookCryptoConfigurationComplete(Conf.NotebookCrypto)\nConf.m.RUnlock()\nif !complete { return errors.New(\"refusing to save backup of incomplete config\") }","typeGuard":"null","tryCatchPattern":"if err := saveNotebookCryptoBackup(kek); err != nil {\n    if strings.Contains(err.Error(), \"incomplete notebook crypto configuration\") {\n        // inspect and repair Conf.NotebookCrypto before retrying\n    }\n}","preventionTips":["Complete the full enable flow (salt, verifier, KDFParams, checksum) before any save call","Validate conf.json integrity at startup and surface partial configurations early","Avoid hand-editing conf.json; use the API/flows that populate all required fields","Add a completeness assertion in tests around any code path that touches NotebookCrypto"],"tags":["encryption","backup","configuration","validation"],"backgroundTag":"missing-required-config-field","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"}