siyuan-note/siyuan · error

cannot write incomplete notebook crypto backup

Error message

cannot write incomplete notebook crypto backup

What it means

After computing the KEKMAC, writeNotebookCryptoBackupData validates the NotebookCrypto struct with notebookCryptoConfigurationComplete. If required fields (spec, salts, wrapped DEKs, checksum, KEKMAC, etc.) are missing or inconsistent, it refuses to persist an incomplete backup, because a partial backup could never be authenticated back during restore and would endanger recoverability.

Solutions

  1. Validate the NotebookCrypto struct and fill all fields required by notebookCryptoConfigurationComplete before writing the backup
  2. Re-run the full enable-encryption setup (salt generation, DEK wrap, checksum finalization) instead of writing a partially built config
  3. When importing, verify the imported JSON is a complete, unmodified backup produced by a compatible version
  4. Check for version skew: if a newer format added required fields, migrate the old config through the supported migration path rather than writing it directly

Example fix

// before
nc := &conf.NotebookCrypto{Spec: conf.CurrentNotebookCryptoSpec}
writeNotebookCryptoBackupData(nc, kek) // fails: incomplete

// after
nc := newCompleteNotebookCrypto(password) // generates salt, wraps DEK, finalizes checksum
nc.KEKMAC = computeKEKMAC(nc, kek)
writeNotebookCryptoBackupData(nc, kek)
Defensive patterns

Strategy: validation

Validate before calling

if !notebookCryptoConfigurationComplete(nc) {
    return errors.New("notebook crypto config incomplete; run full setup before backup")
}
// proceed to writeNotebookCryptoBackupData(nc, kek)

Type guard

func backupWritable(nc *conf.NotebookCrypto) bool {
    return nc != nil && nc.Spec == conf.CurrentNotebookCryptoStatus && notebookCryptoConfigurationComplete(nc)
}

Try / catch

if err := writeNotebookCryptoBackupData(nc, kek); err != nil {
    return fmt.Errorf("refusing to persist incomplete backup: %w", err)
}

Prevention

When it happens

Trigger: Calling writeNotebookCryptoBackupData with a NotebookCrypto struct that fails notebookCryptoConfigurationComplete — e.g. missing MasterSalt, missing WrappedDEK, empty Checksum, unset Spec, or a KEKMAC that was never computed because kek was garbage. Raised from ImportNotebookCryptoBackup with a truncated/malformed import payload, or restore/repair paths that built a partial config.

Common situations: Importing a backup JSON that was hand-edited or truncated; master-password migration interrupted midway leaving fields empty; constructing a NotebookCrypto in tests or tooling without filling every mandatory field; a version change introduced a new required field the old config lacks.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/c7eed877501b0f11. Report an issue: GitHub.

Appendix: source

Thrown at kernel/model/crypto.go:421

	if err != nil {
		return fmt.Errorf("marshal notebook crypto backup failed: %w", err)
	}
	if err := atomicWriteFile(backupPath, data); err != nil {
		return fmt.Errorf("write notebook crypto backup failed: %w", err)
	}
	return nil
}

// writeNotebookCryptoBackupData 将指定的 NotebookCrypto 写入备份文件(不依赖 Conf.NotebookCrypto)。
// kek 必须非 nil:在 Checksum 定型后计算 KEKMAC,保证落盘 MAC 与落盘内容一致。
func writeNotebookCryptoBackupData(nc *conf.NotebookCrypto, kek []byte) error {
	if kek == nil {
		return errors.New("cannot generate notebook crypto backup without KEK")
	}
	prepareBackupForWrite(nc)
	nc.KEKMAC = computeKEKMAC(nc, kek)
	if !notebookCryptoConfigurationComplete(nc) {
		return errors.New("cannot write incomplete notebook crypto backup")
	}
	backupPath := dataCryptoBackupPath()
	if err := os.MkdirAll(filepath.Dir(backupPath), 0755); err != nil {
		return fmt.Errorf("mkdir notebook crypto backup dir failed: %w", err)
	}
	data, err := json.Marshal(nc)
	if err != nil {
		return fmt.Errorf("marshal notebook crypto backup failed: %w", err)
	}
	if err := atomicWriteFile(backupPath, data); err != nil {
		return fmt.Errorf("write notebook crypto backup failed: %w", err)
	}
	return nil
}

// verifyKEKAgainstExistingBoxes 用 KEK 对所有现有加密笔记本的 WrappedDEK 做无副作用解密校验。
// 优先尝试 conf 的 WrappedDEK,解密失败时 fallback 到 backup(与解锁路径一致);
// GetBoxEncryption 报错时 fail-closed(元数据损坏的加密笔记本不能静默跳过)。

View on GitHub (pinned to 9f775e8a12)