{"record":{"id":"4b2d0e4fd17d6a9e","repo":"siyuan-note/siyuan","slug":"unmarshal-notebook-crypt-backup-failed-w","errorCode":null,"errorMessage":"unmarshal notebook crypt backup failed: %w","messagePattern":"unmarshal notebook crypt backup failed: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"kernel/model/crypto.go","lineNumber":2561,"sourceCode":"func readNotebookCryptBackup(boxID string) (*conf.BoxEncryption, error) {\n\tif !ast.IsNodeIDPattern(boxID) {\n\t\treturn nil, errors.New(\"invalid notebook ID\")\n\t}\n\tbackupPath := notebookCryptoBackupPath(boxID)\n\tif !filelock.IsExist(backupPath) {\n\t\treturn nil, nil\n\t}\n\treturn readBoxEncryptionFile(backupPath)\n}\n\nfunc readBoxEncryptionFile(backupPath string) (*conf.BoxEncryption, error) {\n\tdata, err := filelock.ReadFile(backupPath)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"read notebook crypt backup failed: %w\", err)\n\t}\n\tvar crypt conf.BoxEncryption\n\tif err = gulu.JSON.UnmarshalJSON(data, &crypt); err != nil {\n\t\treturn nil, fmt.Errorf(\"unmarshal notebook crypt backup failed: %w\", err)\n\t}\n\tif err = validateBoxEncryption(&crypt); err != nil {\n\t\treturn nil, err\n\t}\n\treturn &crypt, nil\n}\n\n// copyAssetDecryptIfEncrypted 把 srcPath 的 asset 复制到 destPath。\n// 若 srcPath 在已解锁的加密笔记本下，读密文→解密→写明文到 destPath（导出目录）；\n// 否则走 filelock.Copy 原路径（字节级复制，密文/明文均可）。\nfunc copyAssetDecryptIfEncrypted(srcPath, destPath string) error {\n\tif err := os.MkdirAll(filepath.Dir(destPath), 0755); err != nil {\n\t\treturn err\n\t}\n\n\tboxID := ExtractBoxIDFromAssetsPath(srcPath)\n\tif boxID != \"\" && IsEncryptedBox(boxID) {\n\t\tHoldBoxReadLock(boxID)","sourceCodeStart":2543,"sourceCodeEnd":2579,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/8641553a1f07374001902d3ce773285db1292b2d/kernel/model/crypto.go#L2543-L2579","documentation":"The BoxCrypt backup file was read successfully but its contents are not valid JSON matching conf.BoxEncryption, so gulu.JSON.UnmarshalJSON fails and this wrapped error is returned. It indicates the backup envelope is corrupt, empty, or was written by an incompatible format version.","triggerScenarios":"readBoxEncryptionFile calls gulu.JSON.UnmarshalJSON(data, &crypt) and parsing fails: the backup file is 0 bytes, was truncated by a crash mid-write, contains garbage after corruption, or was hand-edited.","commonSituations":"Power loss or crash while the kernel was writing the backup; manual edits to files under data/<notebook>/.siyuan/; partial restore from backup/sync that clipped the file; mixing data from different SiYuan versions with envelope format changes.","solutions":["Inspect the backup JSON file for the notebook and confirm it parses (e.g. jq . file)","Restore the backup file from a recent snapshot/sync copy or the notebook's file history","If a valid older-format envelope is present, follow the documented migration path in docs/ENCRYPTED-NOTEBOOK.md instead of hand-editing — never regenerate the MasterSalt","As a last resort use the recovery material to re-run the enable-encryption flow, keeping the original key material"],"exampleFix":"// before\ncat data/<box>/.siyuan/crypt.json   # empty / truncated\n// after\ncp snapshot/data/<box>/.siyuan/crypt.json data/<box>/.siyuan/crypt.json\njq . data/<box>/.siyuan/crypt.json  # validates as JSON","handlingStrategy":"fallback","validationCode":"let parsed;\ntry { parsed = JSON.parse(rawJson); } catch { parsed = null; }\nif (!parsed || typeof parsed !== \"object\") {\n    restoreFromSnapshot();\n}","typeGuard":"function isBoxEncryption(v: unknown): v is BoxEncryption {\n    return !!v && typeof v === \"object\" && \"version\" in v && \"wrappedDEK\" in v;\n}","tryCatchPattern":"try {\n    await enableEncryption(boxID);\n} catch (e) {\n    if (String(e.message).includes(\"unmarshal notebook crypt backup failed\")) {\n        restoreBackupFileFromSnapshot(boxID);\n    }\n}","preventionTips":["Never hand-edit files under data/<box>/.siyuan/","Keep sync/file-history snapshots enabled so corrupt backups can be restored","Shut the kernel down cleanly; avoid hard power-offs during encryption operations"],"tags":["json","corruption","encrypted-notebook"],"backgroundTag":"json-unmarshal-failed","analyzedSha":"8641553a1f07374001902d3ce773285db1292b2d","analyzedAt":"2026-09-11T16:08:28.414Z","contentChangedAt":"2026-09-11T16:08:28.414Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}