{"record":{"id":"453c3764e95be52f","repo":"siyuan-note/siyuan","slug":"encrypted-notebook-history-has-no-valid-key-materi","errorCode":null,"errorMessage":"encrypted notebook history has no valid key material","messagePattern":"encrypted notebook history has no valid key material","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/crypto.go","lineNumber":917,"sourceCode":"\t\t\tboxConf := conf.NewBoxConf()\n\t\t\tif readErr = gulu.JSON.UnmarshalJSON(data, boxConf); readErr != nil {\n\t\t\t\tcandidateErrors = append(candidateErrors, readErr)\n\t\t\t} else if boxConf.Encrypted && boxConf.BoxCrypt != nil {\n\t\t\t\tif readErr = validateBoxEncryption(boxConf.BoxCrypt); readErr != nil {\n\t\t\t\t\tcandidateErrors = append(candidateErrors, readErr)\n\t\t\t\t} else {\n\t\t\t\t\tret = append(ret, boxConf.BoxCrypt)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tif len(ret) > 0 {\n\t\treturn ret, nil\n\t}\n\tif len(candidateErrors) > 0 {\n\t\treturn nil, errors.Join(candidateErrors...)\n\t}\n\treturn nil, errors.New(\"encrypted notebook history has no valid key material\")\n}\n\n// HasEncryptedNotebookHistory 在扫描失败时按存在依赖处理，避免调用方因 I/O 或权限错误删除恢复材料。\nfunc HasEncryptedNotebookHistory() bool {\n\thasHistory, err := scanEncryptedNotebookHistory()\n\tif err != nil {\n\t\tlogging.LogErrorf(\"scan encrypted notebook history failed: %s\", err)\n\t\treturn true\n\t}\n\treturn hasHistory\n}\n\n// isEncryptedHistoryBoxDir 判断历史目录中的 boxID 子目录是否属于加密笔记本。\n// 优先看 notebook-crypto-backup.json（删除前随 box 目录整体备份，是加密身份的权威标识），\n// 再 fallback 到 conf.json 的 Encrypted 标志。\nfunc isEncryptedHistoryBoxDir(boxDir string) (bool, error) {\n\tsiyuanDir := filepath.Join(boxDir, \".siyuan\")\n\tbackupPath := filepath.Join(siyuanDir, \"notebook-crypto-backup.json\")","sourceCodeStart":899,"sourceCodeEnd":935,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/crypto.go#L899-L935","documentation":"readEncryptedHistoryBoxEncryptionCandidates tries every candidate key source found in a history box directory (backup key material, conf-derived keys). This sentinel error is returned only when the box was scanned without I/O errors but zero usable key candidates were found and no per-candidate errors accumulated — i.e. the encrypted history box contains no recognizable key material at all, so KEK verification cannot proceed.","triggerScenarios":"verifyKEKAgainstEncryptedHistory scanning a history box directory whose .siyuan/notebook-crypto-backup.json and conf.json either are absent (yet the dir still looked like an encrypted box) or carry no valid key fields, leaving ret empty and candidateErrors empty.","commonSituations":"A history box partially restored from backup with key files omitted; cleanup tooling stripped .siyuan metadata; a box dir matched the ID pattern but belongs to a non-encrypted or foreign format notebook; format version drift where key fields were renamed.","solutions":["Restore the box's .siyuan/notebook-crypto-backup.json (and conf.json) from backup or sync so valid key material is present again","Confirm the history box actually belongs to an encrypted notebook; if not, exclude it from recovery scanning","Check the box's format version against the current code and migrate or re-snapshot it from the live notebook if it is stale","Regenerate recovery material from the still-unlocked live notebook before deleting anything (never delete history boxes on this error)"],"exampleFix":"// before: treating missing key material as fatal and possibly dropping recovery\nif _, err := readEncryptedHistoryBoxEncryptionCandidates(dir); err != nil { return err }\n// after: restore key material from the live notebook before failing\nif _, err := readEncryptedHistoryBoxEncryptionCandidates(dir); err != nil {\n    if rErr := writeNotebookCryptoBackupFromLive(dir); rErr != nil {\n        return fmt.Errorf(\"no key material in history box and live restore failed: %w\", err)\n    }\n}","handlingStrategy":"fallback","validationCode":"backupPath := filepath.Join(boxDir, \".siyuan\", \"notebook-crypto-backup.json\")\nif _, err := os.Stat(backupPath); os.IsNotExist(err) {\n    // no key material in box — restore from live notebook before verification\n}","typeGuard":"func hasKeyMaterial(boxDir string) bool {\n    _, err := os.Stat(filepath.Join(boxDir, \".siyuan\", \"notebook-crypto-backup.json\"))\n    return err == nil\n}","tryCatchPattern":"cands, err := readEncryptedHistoryBoxEncryptionCandidates(dir)\nif err != nil && err.Error() == \"encrypted notebook history has no valid key material\" {\n    // regenerate recovery material from the live notebook; never delete the box\n    return err\n}","preventionTips":["Always write notebook-crypto-backup.json when creating encrypted history","Keep .siyuan metadata when restoring or copying history boxes","Verify key material exists before pruning old history","Never treat missing key material as license to delete recovery boxes"],"tags":["encryption","keys","history","recovery"],"backgroundTag":"missing-credentials","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"}