siyuan-note/siyuan · error

encrypted notebook history has no valid key material

Error message

encrypted notebook history has no valid key material

What it means

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.

Solutions

  1. Restore the box's .siyuan/notebook-crypto-backup.json (and conf.json) from backup or sync so valid key material is present again
  2. Confirm the history box actually belongs to an encrypted notebook; if not, exclude it from recovery scanning
  3. Check the box's format version against the current code and migrate or re-snapshot it from the live notebook if it is stale
  4. Regenerate recovery material from the still-unlocked live notebook before deleting anything (never delete history boxes on this error)

Example fix

// before: treating missing key material as fatal and possibly dropping recovery
if _, err := readEncryptedHistoryBoxEncryptionCandidates(dir); err != nil { return err }
// after: restore key material from the live notebook before failing
if _, err := readEncryptedHistoryBoxEncryptionCandidates(dir); err != nil {
    if rErr := writeNotebookCryptoBackupFromLive(dir); rErr != nil {
        return fmt.Errorf("no key material in history box and live restore failed: %w", err)
    }
}
Defensive patterns

Strategy: fallback

Validate before calling

backupPath := filepath.Join(boxDir, ".siyuan", "notebook-crypto-backup.json")
if _, err := os.Stat(backupPath); os.IsNotExist(err) {
    // no key material in box — restore from live notebook before verification
}

Type guard

func hasKeyMaterial(boxDir string) bool {
    _, err := os.Stat(filepath.Join(boxDir, ".siyuan", "notebook-crypto-backup.json"))
    return err == nil
}

Try / catch

cands, err := readEncryptedHistoryBoxEncryptionCandidates(dir)
if err != nil && err.Error() == "encrypted notebook history has no valid key material" {
    // regenerate recovery material from the live notebook; never delete the box
    return err
}

Prevention

When it happens

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

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

Related errors


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

Appendix: source

Thrown at kernel/model/crypto.go:917

			boxConf := conf.NewBoxConf()
			if readErr = gulu.JSON.UnmarshalJSON(data, boxConf); readErr != nil {
				candidateErrors = append(candidateErrors, readErr)
			} else if boxConf.Encrypted && boxConf.BoxCrypt != nil {
				if readErr = validateBoxEncryption(boxConf.BoxCrypt); readErr != nil {
					candidateErrors = append(candidateErrors, readErr)
				} else {
					ret = append(ret, boxConf.BoxCrypt)
				}
			}
		}
	}
	if len(ret) > 0 {
		return ret, nil
	}
	if len(candidateErrors) > 0 {
		return nil, errors.Join(candidateErrors...)
	}
	return nil, errors.New("encrypted notebook history has no valid key material")
}

// HasEncryptedNotebookHistory 在扫描失败时按存在依赖处理,避免调用方因 I/O 或权限错误删除恢复材料。
func HasEncryptedNotebookHistory() bool {
	hasHistory, err := scanEncryptedNotebookHistory()
	if err != nil {
		logging.LogErrorf("scan encrypted notebook history failed: %s", err)
		return true
	}
	return hasHistory
}

// isEncryptedHistoryBoxDir 判断历史目录中的 boxID 子目录是否属于加密笔记本。
// 优先看 notebook-crypto-backup.json(删除前随 box 目录整体备份,是加密身份的权威标识),
// 再 fallback 到 conf.json 的 Encrypted 标志。
func isEncryptedHistoryBoxDir(boxDir string) (bool, error) {
	siyuanDir := filepath.Join(boxDir, ".siyuan")
	backupPath := filepath.Join(siyuanDir, "notebook-crypto-backup.json")

View on GitHub (pinned to 9f775e8a12)