{"record":{"id":"2e353499b43bb13f","repo":"siyuan-note/siyuan","slug":"notebook-crypto-backup-is-incomplete-or-corrupted","errorCode":null,"errorMessage":"notebook crypto backup is incomplete or corrupted","messagePattern":"notebook crypto backup is incomplete or corrupted","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"kernel/model/crypto.go","lineNumber":497,"sourceCode":"\n// loadNotebookCryptoBackup 从 DataDir 读取 NotebookCrypto 备份。文件不存在返回 (nil, nil)。\nfunc loadNotebookCryptoBackup() (*conf.NotebookCrypto, error) {\n\tdata, err := filelock.ReadFile(dataCryptoBackupPath())\n\tif err != nil {\n\t\tif os.IsNotExist(err) {\n\t\t\treturn nil, nil\n\t\t}\n\t\treturn nil, err\n\t}\n\tnc := &conf.NotebookCrypto{}\n\tif err := json.Unmarshal(data, nc); err != nil {\n\t\treturn nil, err\n\t}\n\tif nc.Spec != conf.CurrentNotebookCryptoSpec {\n\t\treturn nil, fmt.Errorf(\"unsupported notebook crypto backup spec [%d]\", nc.Spec)\n\t}\n\tif !notebookCryptoConfigurationComplete(nc) {\n\t\treturn nil, errors.New(\"notebook crypto backup is incomplete or corrupted\")\n\t}\n\treturn nc, nil\n}\n\n// removeNotebookCryptoBackup 删除备份文件（禁用加密功能时调用）。文件不存在视为成功。\nfunc removeNotebookCryptoBackup() {\n\tif err := os.Remove(dataCryptoBackupPath()); err != nil && !os.IsNotExist(err) {\n\t\tlogging.LogErrorf(\"remove notebook crypto backup failed: %s\", err)\n\t}\n}\n\n// masterPasswordMigration 记录改密迁移的完整状态，用于崩溃后恢复。\ntype masterPasswordMigration struct {\n\tOldVerifier      []byte              `json:\"oldVerifier\"`\n\tNewVerifier      []byte              `json:\"newVerifier\"`\n\tNewVerifierNonce []byte              `json:\"newVerifierNonce\"`\n\tNewKDFParams     json.RawMessage     `json:\"newKDFParams\"`\n\tNewHistoryKEKs   [][]byte            `json:\"newHistoryKEKs,omitempty\"`","sourceCodeStart":479,"sourceCodeEnd":515,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/crypto.go#L479-L515","documentation":"After the spec check, loadNotebookCryptoBackup runs notebookCryptoConfigurationComplete on the parsed backup. If mandatory fields are missing, empty, or mutually inconsistent, the backup is deemed incomplete or corrupted and is rejected. This prevents restoring a backup that could never be authenticated, protecting recoverability of encrypted notebooks.","triggerScenarios":"Loading a backup whose JSON lacks required fields (MasterSalt, WrappedDEK, Checksum, KEKMAC, etc.) or has empty/malformed values: truncated file, partial write interrupted mid-save, manual editing, JSON that unmarshals with null fields, or corruption from a failing disk.","commonSituations":"Restore attempts after an interrupted master-password migration; a backup file truncated by disk-full during an old non-atomic write; user accidentally deleted fields while inspecting the JSON; data-directory corruption after a crash.","solutions":["Restore the backup from a known-good copy (another snapshot, sync version history, OS backup)","Regenerate the crypto configuration by re-running the enable-encryption setup with the master password if no valid backup exists","Check the full JSON is present and valid (no truncation) with all required fields populated","If corruption is environmental, check disk health and stop sync tools that might have truncated the file"],"exampleFix":"// before (truncated backup)\n{\"spec\": 1, \"masterSalt\": \"...\"}\n\n// after (complete backup restored from snapshot)\n{\"spec\": 1, \"masterSalt\": \"...\", \"wrappedDek\": \"...\", \"checksum\": \"...\", \"kekmac\": \"...\"}","handlingStrategy":"fallback","validationCode":"var nc conf.NotebookCrypto\nif err := json.Unmarshal(raw, &nc); err != nil || !notebookCryptoConfigurationComplete(&nc) {\n    return errors.New(\"backup is incomplete or corrupted; restore from a known-good copy\")\n}","typeGuard":null,"tryCatchPattern":"nc, err := loadNotebookCryptoBackup()\nif err != nil {\n    if strings.Contains(err.Error(), \"incomplete or corrupted\") {\n        return tryAlternateBackupSnapshot() // sync history, OS backup\n    }\n    return err\n}","preventionTips":["Keep at least one verified backup copy outside the data directory","Never manually edit backup files; inspect copies, not originals","Verify backup integrity after enabling encryption and after each master-password change","Ensure writes complete cleanly: avoid killing the kernel during migration operations"],"tags":["go","crypto","backup","corruption","validation"],"backgroundTag":"schema-validation-failed","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"}