siyuan-note/siyuan · critical

unmarshal notebook crypt backup failed

Error message

unmarshal notebook crypt backup failed: %w

What it means

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.

Solutions

  1. Inspect the backup JSON file for the notebook and confirm it parses (e.g. jq . file)
  2. Restore the backup file from a recent snapshot/sync copy or the notebook's file history
  3. 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
  4. As a last resort use the recovery material to re-run the enable-encryption flow, keeping the original key material

Example fix

// before
cat data/<box>/.siyuan/crypt.json   # empty / truncated
// after
cp snapshot/data/<box>/.siyuan/crypt.json data/<box>/.siyuan/crypt.json
jq . data/<box>/.siyuan/crypt.json  # validates as JSON
Defensive patterns

Strategy: fallback

Validate before calling

let parsed;
try { parsed = JSON.parse(rawJson); } catch { parsed = null; }
if (!parsed || typeof parsed !== "object") {
    restoreFromSnapshot();
}

Type guard

function isBoxEncryption(v: unknown): v is BoxEncryption {
    return !!v && typeof v === "object" && "version" in v && "wrappedDEK" in v;
}

Try / catch

try {
    await enableEncryption(boxID);
} catch (e) {
    if (String(e.message).includes("unmarshal notebook crypt backup failed")) {
        restoreBackupFileFromSnapshot(boxID);
    }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "failed to unmarshal" / json.Unmarshal errors: why parsing a response into a Go struct fails and how to fix it — this error's family across 23 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@8641553a1f (2026-09-11). Data as JSON: /api/errors/4b2d0e4fd17d6a9e. Report an issue: GitHub.

Appendix: source

Thrown at kernel/model/crypto.go:2561

func readNotebookCryptBackup(boxID string) (*conf.BoxEncryption, error) {
	if !ast.IsNodeIDPattern(boxID) {
		return nil, errors.New("invalid notebook ID")
	}
	backupPath := notebookCryptoBackupPath(boxID)
	if !filelock.IsExist(backupPath) {
		return nil, nil
	}
	return readBoxEncryptionFile(backupPath)
}

func readBoxEncryptionFile(backupPath string) (*conf.BoxEncryption, error) {
	data, err := filelock.ReadFile(backupPath)
	if err != nil {
		return nil, fmt.Errorf("read notebook crypt backup failed: %w", err)
	}
	var crypt conf.BoxEncryption
	if err = gulu.JSON.UnmarshalJSON(data, &crypt); err != nil {
		return nil, fmt.Errorf("unmarshal notebook crypt backup failed: %w", err)
	}
	if err = validateBoxEncryption(&crypt); err != nil {
		return nil, err
	}
	return &crypt, nil
}

// copyAssetDecryptIfEncrypted 把 srcPath 的 asset 复制到 destPath。
// 若 srcPath 在已解锁的加密笔记本下,读密文→解密→写明文到 destPath(导出目录);
// 否则走 filelock.Copy 原路径(字节级复制,密文/明文均可)。
func copyAssetDecryptIfEncrypted(srcPath, destPath string) error {
	if err := os.MkdirAll(filepath.Dir(destPath), 0755); err != nil {
		return err
	}

	boxID := ExtractBoxIDFromAssetsPath(srcPath)
	if boxID != "" && IsEncryptedBox(boxID) {
		HoldBoxReadLock(boxID)

View on GitHub (pinned to 8641553a1f)