siyuan-note/siyuan · error
cannot generate notebook crypto backup without KEK
Error message
cannot generate notebook crypto backup without KEK
What it means
saveNotebookCryptoBackup refuses to write a key backup when the kek argument is nil. A backup without a KEK would have an empty KEKMAC, which deriveKEK and every recovery path reject — generating it would create a backup that can never unlock the workspace (a self-inflicted lockout, see design doc §19). This is an internal invariant guard, normally only reachable through programming errors or the dedicated test.
Solutions
- Fix the caller to derive the KEK first and return its error instead of proceeding with nil
- Add/keep an explicit check: if kek derivation fails, abort before calling saveNotebookCryptoBackup
- In tests, only pass nil intentionally (TestSaveNotebookCryptoBackupRejectsNilKEK) to assert the guard
- If this fires in production code paths, treat it as a bug — users should never reach it
Example fix
// before
kek, err := deriveKEK(password)
_ = err
saveNotebookCryptoBackup(kek) // panics into "cannot generate notebook crypto backup without KEK"
// after
kek, err := deriveKEK(password)
if err != nil {
return err
}
return saveNotebookCryptoBackup(kek) Defensive patterns
Strategy: type-guard
Validate before calling
if kek == nil || len(kek) == 0 {
return errors.New("KEK derivation failed; refusing to write backup")
} Type guard
func hasKEK(kek []byte) bool { return len(kek) > 0 } Try / catch
if err := saveNotebookCryptoBackup(kek); err != nil {
if err.Error() == "cannot generate notebook crypto backup without KEK" {
// caller bug: fix the derivation path, never retry with the same nil KEK
}
} Prevention
- Always check the error from KEK derivation before using the key
- Never reorder derive-after-save in refactors; keep the invariant "no backup without KEK"
- Keep the nil-KEK test (TestSaveNotebookCryptoBackupRejectsNilKEK) in CI
- Treat any production occurrence of this error as a code bug to file immediately
When it happens
Trigger: Calling saveNotebookCryptoBackup(nil) directly, or an internal caller (EnableEncryptedNotebook / deriveKEK / ChangeMasterPassword code path) passing a nil KEK because the key-derivation step silently failed or was skipped.
Common situations: Custom/test code invoking the unexported helper with nil; a regression where deriveKEK returns nil on failure instead of erroring out first; refactoring that reordered KEK derivation after the backup call.
Understand the failure class
Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.
Related errors
- cannot save incomplete notebook crypto configuration
- Conf.Language(315)
- marshal notebook crypt backup failed
- marshal notebook crypto backup failed
- master password migration is pending: Master password…
AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19).
Data as JSON: /api/errors/614d25ad1cd5c6d1.
Report an issue: GitHub.
Appendix: source
Thrown at kernel/model/crypto.go:382
if err := writeNotebookCryptoBackupData(nc, kek); err != nil {
return fmt.Errorf("failed to persist key backup: %w", err)
}
Conf.m.Lock()
*Conf.NotebookCrypto = *nc
Conf.m.Unlock()
Conf.Save()
IncSync()
return nil
}
// saveNotebookCryptoBackup 把当前 NotebookCrypto(含 MasterSalt/KEKVerifier/KDFParams)备份到 DataDir。
// kek 必须非 nil:在 Checksum 定型后计算 KEKMAC 并落盘,保证恢复路径可通过 MAC 校验。
// 无 KEK 生成的备份 KEKMAC 必为空,会被 deriveKEK/恢复路径拒绝,等于制造无法解锁的状态(详见设计 §19)。
func saveNotebookCryptoBackup(kek []byte) error {
if kek == nil {
// 无 KEK 时不得生成当前格式备份:KEKMAC 缺失会被 deriveKEK/恢复路径拒绝,
// 生成即等于制造无法解锁的状态。
return errors.New("cannot generate notebook crypto backup without KEK")
}
Conf.m.Lock()
nc := *Conf.NotebookCrypto // 值拷贝
prepareBackupForWrite(&nc)
nc.KEKMAC = computeKEKMAC(&nc, kek)
if !notebookCryptoConfigurationComplete(&nc) {
Conf.m.Unlock()
return errors.New("cannot save incomplete notebook crypto configuration")
}
Conf.NotebookCrypto.Spec = nc.Spec
Conf.NotebookCrypto.BackupID = nc.BackupID
Conf.NotebookCrypto.CreatedAt = nc.CreatedAt
Conf.NotebookCrypto.Checksum = nc.Checksum
Conf.NotebookCrypto.KEKMAC = nc.KEKMAC // 保持 Conf 与备份文件的 KEKMAC 一致
Conf.m.Unlock()
backupPath := dataCryptoBackupPath()
if err := os.MkdirAll(filepath.Dir(backupPath), 0755); err != nil {
return fmt.Errorf("mkdir notebook crypto backup dir failed: %w", err)View on GitHub (pinned to 9f775e8a12)