An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:memo: Document master key backup and recovery https://github.com/siyuan-note/siyuan/issues/18034
Danielf37e333f12c6
diff --git a/docs/ENCRYPTED-NOTEBOOK.md b/docs/ENCRYPTED-NOTEBOOK.mdindex 5a16965bae..86c99d537e 100644--- a/docs/ENCRYPTED-NOTEBOOK.md+++ b/docs/ENCRYPTED-NOTEBOOK.md@@ -87,6 +87,27 @@ User master password- **KEK envelope**: Changing the password only re-wraps the DEK; data is not re-encrypted- **KEK not cached**: Derived on every unlock; strict per-box isolation+### 4.1 MasterSalt Backup and Cross-Device Recovery++MasterSalt is the global root of KEK derivation — the master password + MasterSalt derive the KEK via Argon2id, and the KEK then unwraps each box's WrappedDEK. **Losing MasterSalt permanently locks the data**: even with the same master password, a changed salt derives a different KEK, so old WrappedDEKs cannot be unwrapped. A backup-and-recovery mechanism is therefore introduced.++**Backup**: `<DataDir>/.siyuan/notebook-crypto-backup.json`, holding the full NotebookCrypto (MasterSalt/KEKVerifier/KDFParams). Located inside DataDir, it **enters the dejavu sync scope**. It is refreshed when enabling encrypted notebooks and when changing the master password, and deleted on disable. Stored as plaintext JSON (salt is not secret, verifier is ciphertext — identical to how they are stored in `conf/conf.json`).++**Recovery triggers** (covering scenarios such as conf.json loss, sync to a new device, importing Data.zip):++| Trigger scenario | Function | Password required? | Notes |+|---|---|---|---|+| After sync completes (backup file pulled down) | `restoreNotebookCryptoConfigFromBackup` | No | Loads the config back into the local conf.json and sets `Enabled=true`; UI shows "enabled, locked" |+| After importing Data.zip | `restoreNotebookCryptoConfigFromBackup` | No | Same; covers "backup arrived with Data.zip but the local machine is not enabled" |+| User enters master password to unlock (fallback) | `tryRestoreNotebookCryptoFromBackup` | Yes | `deriveKEK` attempts recovery when the local `Enabled=false`; returns the KEK after verifying the password |+| User manually enables (fool-proof guard) | `tryRestoreNotebookCryptoFromBackup` | Yes | When encrypted notebooks already exist, refuses to regenerate salt and instead recovers from backup, verifying the password |++**Config recovery vs key recovery, separated**: Config recovery only needs to read the backup file and load back the salt/verifier (no password required, since salt is not secret and data remains undecryptable without the password); key recovery requires the password to derive the KEK and verify against the verifier. This allows the "enabled" state to be reached automatically after sync/import, so the user can unlock by entering the master password.++**Fool-proof guard**: When encrypted notebooks already exist on disk (boxes with `Encrypted=true`), `EnableEncryptedNotebook` **refuses to regenerate MasterSalt** (which would orphan old WrappedDEKs) and instead recovers from backup first; only if the backup is missing does it error out and guide the user to restore `conf.json` or the backup file. This guard mirrors `DisableEncryptedNotebook`'s "cannot disable while encrypted notebooks exist".++**Prerequisite**: `restoreNotebookCryptoConfigFromBackup` only takes effect when the local `Enabled=false`; it never overwrites an in-use local config.+## 5. Encrypted File Layout```@@ -94,6 +115,8 @@ User master password├── conf/conf.json ← global encryption config (MasterSalt/KEKVerifier)├── storage/av/ ← normal-box AV definitions (plaintext)├── data/+│ ├── .siyuan/+│ │ └── notebook-crypto-backup.json ← NotebookCrypto backup (MasterSalt/KEKVerifier, synced; see §4.1)│ ├── <boxID>/ ← encrypted notebook directory│ │ ├── .siyuan/conf.json ← BoxConf (Encrypted=true + WrappedDEK)│ │ ├── *.sy ← AES-256-GCM ciphertextdiff --git a/docs/ENCRYPTED-NOTEBOOK.zh-CN.md b/docs/ENCRYPTED-NOTEBOOK.zh-CN.mdindex 72f93ac2be..9ae94b1661 100644--- a/docs/ENCRYPTED-NOTEBOOK.zh-CN.md+++ b/docs/ENCRYPTED-NOTEBOOK.zh-CN.md@@ -87,6 +87,27 @@- **KEK 包络**:改密码只需重新包络 DEK,不重新加密数据- **不缓存 KEK**:每次解锁都派生,严格 per-box 隔离+### 4.1 MasterSalt 备份与跨设备恢复++MasterSalt 是 KEK 派生的全局根基——主密码 + MasterSalt 经 Argon2id 派生 KEK,KEK 再解开各 box 的 WrappedDEK。**MasterSalt 丢失等于数据永久锁死**:即使输入相同主密码,因 salt 变化派生出的 KEK 不同,旧 WrappedDEK 无法解开。为此引入备份与恢复机制。++**备份**:`<DataDir>/.siyuan/notebook-crypto-backup.json`,保存整套 NotebookCrypto(MasterSalt/KEKVerifier/KDFParams)。位于 DataDir 内,**进入 dejavu 同步范围**。启用加密笔记本、修改主密码时刷新;禁用时删除。备份文件按明文 JSON 存储(salt 不保密、verifier 是密文,与 `conf/conf.json` 的存储方式一致)。++**恢复触发点**(覆盖 conf.json 丢失 / 同步到新设备 / 导入 Data.zip 等场景):++| 触发场景 | 函数 | 需要主密码? | 说明 |+|---|---|---|---|+| 同步完成后(拉取到备份文件) | `restoreNotebookCryptoConfigFromBackup` | 否 | 把配置装回本机 conf.json 并置 `Enabled=true`,UI 显示"已启用、锁定" |+| 导入 Data.zip 后 | `restoreNotebookCryptoConfigFromBackup` | 否 | 同上,覆盖"备份随 Data.zip 到达本机但本机未启用"的情况 |+| 用户输主密码解锁(兜底) | `tryRestoreNotebookCryptoFromBackup` | 是 | `deriveKEK` 在本机 `Enabled=false` 时尝试恢复,校验主密码后返回 KEK |+| 用户手动启用(防呆) | `tryRestoreNotebookCryptoFromBackup` | 是 | 已存在加密笔记本时拒绝生成新 salt,改为从备份恢复并校验主密码 |++**配置恢复 vs 密钥恢复分离**:配置恢复只需读备份文件、装回 salt/verifier(不需主密码,因 salt 不保密、没主密码仍解不开数据);密钥恢复才需主密码派生 KEK 并校验 verifier。这让同步/导入后能自动进入"已启用"状态,用户输主密码即可解锁。++**防呆守卫**:`EnableEncryptedNotebook` 在磁盘上已存在加密笔记本(`Encrypted=true` 的 box)时,**禁止重新生成 MasterSalt**(否则会孤立旧 WrappedDEK),改为优先从备份恢复;备份缺失才报错引导用户恢复 `conf.json` 或备份文件。此守卫与 `DisableEncryptedNotebook` 的"有加密笔记本时禁止禁用"对称。++**前提条件**:`restoreNotebookCryptoConfigFromBackup` 仅在本机 `Enabled=false` 时生效,不覆盖正在使用的本机配置。+## 5. 加密文件布局```@@ -94,6 +115,8 @@├── conf/conf.json ← 全局加密配置(MasterSalt/KEKVerifier)├── storage/av/ ← 普通 box 的 AV 定义(明文)├── data/+│ ├── .siyuan/+│ │ └── notebook-crypto-backup.json ← NotebookCrypto 备份(MasterSalt/KEKVerifier,进同步范围,详见 §4.1)│ ├── <boxID>/ ← 加密笔记本目录│ │ ├── .siyuan/conf.json ← BoxConf(Encrypted=true + WrappedDEK)│ │ ├── *.sy ← AES-256-GCM 密文