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: Simplify encrypted notebook AAD design https://github.com/siyuan-note/siyuan/issues/18034
Daniel66b4d40c138a
diff --git a/docs/ENCRYPTED-NOTEBOOK.md b/docs/ENCRYPTED-NOTEBOOK.mdindex 909eeb0a6f..060064b8c7 100644--- a/docs/ENCRYPTED-NOTEBOOK.md+++ b/docs/ENCRYPTED-NOTEBOOK.md@@ -129,7 +129,6 @@ Import guard: when the local machine is already enabled, import is rejected (to│ │ └── notebook-crypto-backup.json ← NotebookCrypto backup (MasterSalt/KEKVerifier, synced; see §4.1)│ ├── <boxID>/ ← encrypted notebook directory│ │ ├── .siyuan/conf.json ← BoxConf (Encrypted=true + WrappedDEK)-│ │ ├── .siyuan/hierarchy.enc ← encrypted and authenticated document-object-ID → parent-object-ID hierarchy manifest│ │ ├── *.sy ← AES-256-GCM ciphertext│ │ ├── assets/│ │ │ ├── <uuid>-<blockID>.ext ← AES-256-GCM ciphertext, filename desensitized@@ -173,7 +172,7 @@ Encrypted-notebook operations (dedicated read path, with boxID):- Decrypts after read, encrypts before write; cache holds plaintext- Reading an encrypted box's title requires a full read + decrypt before parsing- `.sy` AAD binds the boxID, object type, and stable file basename `<rootID>.sy`, excluding every parent-directory component. The basename matches the decrypted root-block ID and is globally unique within the box-- Parent-child topology is authenticated by `.siyuan/hierarchy.enc`, not by content-ciphertext AAD. Notebook open, full reindex, and post-sync application compare the physical directory relation with the manifest; a missing entry, duplicate object ID, or parent mismatch enters `Error` and is never repaired automatically from unauthenticated directory structure+- Parent directories are outside AAD, and the application continues to build document topology from the existing directory structure. Loading verifies that the basename matches the decrypted root-block ID and rejects duplicate object IDs within one box, invalid paths, and hierarchy cycles, but does not provide cryptographic integrity for parent-directory relations**assets encryption**:- On upload, fully read into memory + encrypt + write (cannot stream-encrypt)@@ -211,7 +210,7 @@ database history:roll back → encrypted-box database file/resources rolled back to notebook-level dir```-Parent directories below the history root are not part of AAD. Every history entry retains, in an unambiguous canonical form, the `boxID`, object type, and stable file basename or object identifier at snapshot creation; these values may be encoded by a fixed history layout or stored as metadata committed with the snapshot. Changing only a live document's parent directory leaves its AAD context unchanged; if its basename changes, existing history continues to authenticate with the old basename. Rollback first verifies the ciphertext and that the basename matches the decrypted object ID, then commits the target parent relation to the hierarchy manifest as one transaction. Re-enveloping is required only when the target basename changes. Missing, malformed, or mismatched snapshot context rejects viewing and rollback; the implementation must not guess from the current parent path, parse ciphertext as plaintext, or restore while bypassing the hierarchy manifest.+Parent directories below the history root are not part of AAD. Every history entry retains, in an unambiguous canonical form, the `boxID`, object type, and stable file basename or object identifier at snapshot creation; these values may be encoded by a fixed history layout or stored as metadata committed with the snapshot. Changing only a live document's parent directory leaves its AAD context unchanged; if its basename changes, existing history continues to authenticate with the old basename. Rollback first verifies the ciphertext and that the basename matches the decrypted object ID, then restores under the target parent directory. Re-enveloping is required only when the target basename changes. Missing, malformed, or mismatched snapshot context rejects viewing and rollback; the implementation must not parse ciphertext as plaintext or replace the snapshot basename with the current basename.## 9. Block-Ref Cross-Boundary Protection@@ -227,7 +226,7 @@ Encrypted notebooks forbid moving documents across the encrypted boundary (norma**Data-corruption risk**: Cross-box moves use a filesystem Rename to directly move `.sy` bytes, with no encrypt/decrypt conversion. Ciphertext moved to a normal directory is unreadable; plaintext moved to an encrypted directory is unreadable. Indexes also cannot be migrated across dbs.-**Move within the same encrypted box**: A `.sy` ciphertext object's AAD binds only its stable basename, not its parent directory. A move that preserves the basename may Rename ciphertext verbatim without re-enveloping the root or descendants, but directory topology is not unauthenticated state: the filesystem Rename and update of parent-object relations in `.siyuan/hierarchy.enc` form one journaled recoverable transaction. Before commit, the operation verifies unique object IDs, basename-to-decrypted-root-ID equality, an acyclic target relation, and agreement between the target layout and new manifest. It syncs the Rename and manifest before updating indexes and clearing the journal, and does not open the notebook before crash recovery completes. Only a basename change verifies and decrypts with the old basename and re-envelopes with the new one. Failure must not leave the directory structure, hierarchy manifest, and indexes inconsistent.+**Move within the same encrypted box**: A `.sy` ciphertext object's AAD binds only its stable basename, not its parent directory. A move that preserves the basename follows normal in-box move semantics and may Rename ciphertext verbatim without re-enveloping the root or descendants. Only a basename change verifies and decrypts with the old basename and re-envelopes with the new one. Indexes are updated after the move, and failure handling remains the same as for a normal notebook; no additional encrypted hierarchy metadata is introduced.**Security-leak risk** (more critical): When moving from an encrypted box to a normal box — the document body escapes encryption protection; associated resources must be moved out and decrypted together; the reference network binding blocks to subdocuments is split or leaked along with it; index-metadata cross-db migration breaks isolation.@@ -245,7 +244,7 @@ Encrypted notebooks forbid moving documents across the encrypted boundary (norma| Lock | Equals close: first block new operations, wait for or cancel in-flight work, then remove managed DEK handles, delete encrypted SQLite databases, and best-effort clean kernel-managed plaintext caches, temporary files, and access tokens. Unsaved edits and file history are automatically saved before locking || Auto-lock | Timed independently per box; only a successful content access to that encrypted box refreshes its timer. Normal-notebook activity, global UI input, or access to another box does not refresh it; headless keepalive requests explicitly name a boxID and pass authentication and authorization || Change password | Settings → Access authorization → "Change master password" |-| Move document | A parent-only move within an encrypted box moves ciphertext verbatim and transactionally updates the encrypted hierarchy manifest; a basename change re-envelopes content. Moves between normal boxes remain normal; cross-boundary (normal↔encrypted) moves are rejected with a prompt |+| Move document | A parent-only move within an encrypted box moves ciphertext verbatim and updates indexes; a basename change re-envelopes content. Moves between normal boxes remain normal; cross-boundary (normal↔encrypted) moves are rejected with a prompt || Doc to heading | Cross-boundary rejected with a prompt || Block ref | Normal refs within an encrypted box (searching `((` only searches this box; backlinks panel displays normally); cross-boundary (normal↔encrypted, encrypted A↔encrypted B) blocked || database mirroring | Normal mirroring within an encrypted box; cross-boundary forbidden |@@ -331,6 +330,7 @@ The following are outside this feature's security boundary and must be stated in- Files intentionally exported by the user, clipboard contents, screenshots, printouts, content shared with third parties, and plaintext files saved outside the workspace are protected by the user.- OS swap, hibernation images, crash dumps, filesystem snapshots, and malware are not absolutely protected by clearing application memory; locking performs best-effort cleanup only within the application's control.- A sync service or backup holder may retain, delete, replace, or roll back ciphertext. Replacement and rollback can cause denial of service or data rollback; they cannot decrypt valid ciphertext without the master password.+- Parent directories are excluded from AAD, so an attacker who can write workspace ciphertext may move valid ciphertext with an unchanged basename within the same box, and the application treats the new parent as the current document topology. Content, box, object type, and stable object ID remain authenticated, but parent-directory topology has no cryptographic integrity guarantee."Only ciphertext remains after locking" means that kernel-managed persistent workspace storage should contain no readable plaintext and that managed temporary files, caches, and key handles have undergone best-effort cleanup. It excludes files deliberately exported to an external location and does not promise erasure of transient or historical copies retained by the process, operating system, or underlying storage medium.@@ -356,9 +356,8 @@ Persistent formats are layered; global KDF configuration, key envelopes, and per1. **Data-object envelope**: Every `.sy`, asset, asset-name mapping, and database-definition object contains at least a format version, algorithm identifier, cryptographically random nonce unique for the same purpose key, ciphertext, and authentication tag. The current format constructs AAD deterministically as `format version + boxID + object type + stable object ID`, excluding parent directories and absolute paths. A `.sy` stable object ID is its canonical basename `<rootID>.sy`; an asset uses its desensitized disk basename, a database definition uses its avID, and a fixed singleton file uses a versioned constant ID. After decryption, the internal object ID matches the AAD object ID, and duplicate object IDs within one box are rejected. AES-GCM nonces come from the operating system CSPRNG; randomness failure aborts the write, and the standard per-purpose-key invocation bound is enforced. An authentication tag cannot retrospectively identify nonce reuse, so the design never claims such reuse will automatically cause authentication failure.2. **Global key and envelope metadata**: `NotebookCrypto` stores the KDF algorithm and parameters, MasterSalt, verifier format, KEK-envelope version, and backup-HMAC version; `BoxConf.WrappedDEK` stores each box's DEK envelope. They are not duplicated in every data object.3. **Purpose separation**: Each box's DEK derives independent subkeys with fixed domain separators for file content, asset content, asset-name mappings, database definitions, content SQLCipher, and blocktree SQLCipher. The KEK separately derives DEK-wrapping, verifier, and backup-HMAC contexts; WrappedDEK authentication is not incorrectly placed in a DEK domain.-4. **Hierarchy integrity**: `.siyuan/hierarchy.enc` uses an independent purpose subkey and fixed AAD to map every document stable object ID to its parent object ID. The filesystem directory structure is only a physical projection of the manifest, not trusted input. Creation, move, deletion, and history rollback use a transaction journal to update files and the manifest atomically. An authenticated historical manifest can still be replayed, consistent with this design's explicit lack of rollback prevention.-5. **Database compatibility metadata**: SQLCipher version, cipher parameters, and schema version live in a verifiable database header or the corresponding encrypted configuration boundary, not every data object. Content and blocktree indexes are rebuildable; after source ciphertext authenticates, an incompatible index is closed and rebuilt rather than weakening cipher parameters or falling back to plaintext SQLite.-6. **Migration and rename**: A legacy envelope bound to the full logical relative path is first authenticated and decrypted with that old path, then transactionally migrated to the stable-object-ID format. Migration validates every object and the current directory relation before creating the initial hierarchy manifest, and retains old copies until every new envelope and the manifest are verified and synced. In the new format, a parent-only move does not re-envelope content; it transactionally updates the directory projection and hierarchy manifest. A stable-basename or format-version change creates a new envelope. Access remains closed until crash recovery completes, and failure neither deletes old data nor publishes partial indexes.+4. **Database compatibility metadata**: SQLCipher version, cipher parameters, and schema version live in a verifiable database header or the corresponding encrypted configuration boundary, not every data object. Content and blocktree indexes are rebuildable; after source ciphertext authenticates, an incompatible index is closed and rebuilt rather than weakening cipher parameters or falling back to plaintext SQLite.+5. **Rename and format stability**: The first released format constructs AAD directly from the stable object ID. It is intentionally incompatible with experimental development ciphertext bound to the full logical relative path and provides neither dual-format reads nor migration; development data is deleted and rebuilt after the format switch. A parent-only move does not re-envelope content. A stable-basename change authenticates and decrypts with the old basename and creates a new envelope with the new basename. After the first released version freezes the format, any future envelope or AAD semantic change increments the format version and requires a separately designed migration.The SQLCipher main database, WAL, SHM, rollback journal, temporary files, and backup copies are protected objects. They remain in controlled directories and use their corresponding purpose key. Locking, crash recovery, and history snapshots enumerate these companion files; unencrypted SQLite pages, query results, or diagnostics must never be written to a global temporary directory.@@ -398,12 +397,11 @@ Exports have two classes: a user-selected external destination may contain plain| Access while locked through UI, HTTP, file APIs, CLI, MCP, WebDAV, plugins, and background tasks | Other than the explicitly exposed notebook name, cannot read, write, copy, delete, or enumerate encrypted content; returns no plaintext, ciphertext, or document-title-inferable error || Unlock, lock, application restart, and authentication failure | Only `Unlocked` permits a managed DEK handle and database connections; after failure or restart, entry points deny access and application-controlled plaintext caches, temporary files, and handles undergo best-effort cleanup || Lock concurrent with response generation, cache publication, export, preview, sync, indexing, or history restore | Lock succeeds only after all in-flight lifecycle leases end; no late plaintext response, cache refill, temporary file, usable token, or database reconnection follows success |-| Move a document within one encrypted box, then clear caches and restart | When the basename is unchanged, ciphertext remains unchanged and authenticates with the stable object ID; the hierarchy manifest agrees with the directory relation, the old directory location is absent, and failure leaves no partial file, manifest, or index |-| Manually move a valid `.sy` with an unchanged basename to another parent directory without updating the hierarchy manifest | Notebook open, post-sync validation, or full reindex detects disagreement with the authenticated manifest and enters `Error`; it neither loads the new topology nor rewrites the manifest automatically |+| Move a document within one encrypted box, then clear caches and restart | When the basename is unchanged, ciphertext remains unchanged and authenticates with the stable object ID; the new parent takes effect under normal-notebook semantics, the old directory location is absent, and failure handling matches a normal notebook || Refs, moves, mirrors, assets, and database operations between an encrypted box and a normal box or another encrypted box | Island boundaries hold; rejection creates no partial files, global indexes, or relation data || Asset-ciphertext write or name-mapping update fails | The whole logical transaction fails and rolls back; the caller receives an error, with no accessible orphan, missing mapping, or plaintext temporary file || Delete an encrypted notebook while retaining history, then disable or restore | A key dependency prevents deletion of global config and backup; the matching backup and master password restore the notebook, and only explicit permanent purge removes the dependency |-| Move a document's parent directory or change its basename after creating history, then view or restore the old entry | Authenticate and decrypt with the snapshot boxID, object type, and stable basename recorded by the history entry; rollback transactionally updates the hierarchy manifest and re-envelopes only when the target basename changes, while missing snapshot context rejects the operation |+| Move a document's parent directory or change its basename after creating history, then view or restore the old entry | Authenticate and decrypt with the snapshot boxID, object type, and stable basename recorded by the history entry; rollback re-envelopes only when the target basename changes, while missing snapshot context rejects the operation || Disk inspection of files, assets, databases, WAL/SHM, history, snapshots, logs, and temporary directories | No readable body, asset contents, original asset names, or plaintext SQLite pages exist in kernel-managed locations; temporary paths whose cleanup failed remain denied to every entry point || Delete or corrupt the global deny-access registry after cleanup failure, then restart and access the residual path | The controlled-plaintext temporary namespace remains unconditionally denied; scanning reconstructs records and resumes cleanup, and missing or malformed records never fail open || Ciphertext tampering, path substitution, and backup corruption | Authentication or format validation fails safely; never fall back to a normal path, generate replacement key material, or silently overwrite configuration |diff --git a/docs/ENCRYPTED-NOTEBOOK.zh-CN.md b/docs/ENCRYPTED-NOTEBOOK.zh-CN.mdindex ac772dca72..efddf8bcde 100644--- a/docs/ENCRYPTED-NOTEBOOK.zh-CN.md+++ b/docs/ENCRYPTED-NOTEBOOK.zh-CN.md@@ -129,7 +129,6 @@ MasterSalt 是 KEK 派生的全局根基——主密码 + MasterSalt 经 Argon2i│ │ └── notebook-crypto-backup.json ← NotebookCrypto 备份(MasterSalt/KEKVerifier,进同步范围,详见 §4.1)│ ├── <boxID>/ ← 加密笔记本目录│ │ ├── .siyuan/conf.json ← BoxConf(Encrypted=true + WrappedDEK)-│ │ ├── .siyuan/hierarchy.enc ← 加密并认证的文档对象 ID → 父对象 ID 层级清单│ │ ├── *.sy ← AES-256-GCM 密文│ │ ├── assets/│ │ │ ├── <uuid>-<blockID>.ext ← AES-256-GCM 密文,文件名已脱敏@@ -173,7 +172,7 @@ MasterSalt 是 KEK 派生的全局根基——主密码 + MasterSalt 经 Argon2i- 读盘后解密,写盘前加密,缓存存明文- 加密 box 的标题读取需整体读 + 解密再解析- `.sy` AAD 绑定 boxID、对象类型和稳定文件基名 `<rootID>.sy`,不包含父目录;文件基名必须与解密后根块 ID 一致,并在 box 内全局唯一-- 父子层级不依赖内容密文 AAD,而由 `.siyuan/hierarchy.enc` 认证;打开、重建索引和同步落地后都校验磁盘目录关系与清单一致,缺项、重复对象 ID 或父关系不匹配时进入 `Error`,不得从未认证目录结构自动修复+- 父目录不属于 AAD,应用仍按现有目录结构构建文档层级;加载时校验文件基名与解密后根块 ID 一致,并拒绝同一 box 内的重复对象 ID、非法路径和循环层级,但不提供父目录关系的密码学完整性**assets 加密**:- 上传时全量读入内存 + 加密 + 写盘(无法流式加密)@@ -211,7 +210,7 @@ MasterSalt 是 KEK 派生的全局根基——主密码 + MasterSalt 经 Argon2i回滚 → 加密 box 的数据库文件/资源回滚到笔记本级目录```-历史文件在历史目录中的父路径不是 AAD 的组成部分。每个历史条目必须以不可歧义的规范格式保留快照写入时的 `boxID`、对象类型和稳定文件基名或对象标识;可由固定历史目录布局编码,也可由随快照提交的元数据记录。文档只改变父目录时 AAD 上下文保持不变;文件基名改变时,既有历史仍使用旧基名认证解密。回滚时先验证密文、文件基名与解密后对象 ID 一致,再把目标父关系作为一个事务写入层级清单;仅当目标文件基名变化时才按新基名重新封装。快照上下文缺失、格式不合法或与密文不匹配时必须拒绝查看和回滚,不得从当前父路径猜测、把密文当明文解析或绕过层级清单直接恢复。+历史文件在历史目录中的父路径不是 AAD 的组成部分。每个历史条目必须以不可歧义的规范格式保留快照写入时的 `boxID`、对象类型和稳定文件基名或对象标识;可由固定历史目录布局编码,也可由随快照提交的元数据记录。文档只改变父目录时 AAD 上下文保持不变;文件基名改变时,既有历史仍使用旧基名认证解密。回滚时先验证密文、文件基名与解密后对象 ID 一致,再按目标父目录恢复;仅当目标文件基名变化时才按新基名重新封装。快照上下文缺失、格式不合法或与密文不匹配时必须拒绝查看和回滚,不得把密文当明文解析或使用当前文件基名替代快照基名。## 9. 块引跨加密边界防护@@ -227,7 +226,7 @@ MasterSalt 是 KEK 派生的全局根基——主密码 + MasterSalt 经 Argon2i**数据损坏风险**:跨 box 移动用文件系统 Rename 直搬 `.sy` 字节流,不经加解密转换。密文搬到普通目录不可读,明文搬到加密目录不可读。索引也无法跨库迁移。-**同一加密 box 内移动**:`.sy` 密文 AAD 只绑定稳定文件基名,不绑定父目录。文件基名不变的移动允许原样 Rename 密文,不需要重新封装根文档或子文档;但目录层级不是无认证状态,移动必须把文件系统 Rename 与 `.siyuan/hierarchy.enc` 的父对象关系更新组成一个带日志的可恢复事务。提交前验证所有对象 ID 唯一、文件基名与解密后根块 ID 一致、目标父关系无环且目录布局与新清单一致;同步落盘 Rename 和新清单后才更新索引并清除日志,崩溃恢复完成前不开放笔记本。只有文件基名本身变化时才使用旧基名验证解密并按新基名重新封装。失败不得留下目录结构、层级清单和索引互相不一致的状态。+**同一加密 box 内移动**:`.sy` 密文 AAD 只绑定稳定文件基名,不绑定父目录。文件基名不变的移动按普通笔记本的同箱移动语义原样 Rename 密文,不需要重新封装根文档或子文档;只有文件基名本身变化时才使用旧基名验证解密并按新基名重新封装。移动完成后更新索引,失败处理与普通笔记本一致,不额外引入加密层级元数据。**安全泄漏风险**(更关键):从加密 box 移动到普通 box 时——文档正文脱离加密保护;关联 resources 必须一起搬出解密;绑定块与子文档的引用网被割裂或连带泄漏;索引元数据跨库迁移破坏隔离。@@ -245,7 +244,7 @@ MasterSalt 是 KEK 派生的全局根基——主密码 + MasterSalt 经 Argon2i| 锁定 | 等于关闭:先阻止新操作,等待或取消在途任务,再移除受管 DEK 句柄、删除加密 SQLite 数据库,并尽力清理内核管理的明文缓存、临时文件和访问令牌。关闭前会自动保存未写入的编辑和文件历史 || 自动锁定 | 按 box 单独计时;只有成功访问该加密 box 内容的操作才刷新其计时,普通笔记本活动、全局界面输入或其他 box 的访问不得刷新;无界面的保活请求必须显式携带 boxID 并通过认证与授权 || 改密 | 设置 → 访问授权 → "修改主密码" |-| 移动文档 | 加密 box 内部仅改变父目录时原样移动密文并事务更新加密层级清单;文件基名变化时重新封装;普通 box 之间正常移动;跨加密边界(普通↔加密)被拒绝并提示 |+| 移动文档 | 加密 box 内部仅改变父目录时原样移动密文并更新索引;文件基名变化时重新封装;普通 box 之间正常移动;跨加密边界(普通↔加密)被拒绝并提示 || 文档转标题 | 跨加密边界被拒绝并提示 || 块引 | 加密 box 内部正常引用(搜索 `((` 只搜本 box,反链面板正常显示);跨加密边界(普通↔加密、加密 A↔加密 B)被阻止 || 数据库镜像 | 加密 box 内部正常镜像;跨加密边界被禁止 |@@ -331,6 +330,7 @@ MasterSalt 是 KEK 派生的全局根基——主密码 + MasterSalt 经 Argon2i- 用户主动导出的文件、剪贴板、截图、打印件、分享给第三方的内容,以及保存在工作区外的明文文件,由用户负责保护。- 操作系统交换分区、休眠映像、崩溃转储、文件系统快照和恶意软件不因“清除内存”而获得绝对保护;锁定仅做应用可控范围内的尽力清除。- 同步服务或备份持有者可以保留、删除、替换或回滚密文;这些行为可能造成拒绝服务或数据回退,但不能在不知道主密码时解密有效密文。+- 父目录不进入 AAD,因此能够写工作空间密文的攻击者可以在同一 box 内移动一个文件基名不变的有效密文,应用会把新父目录视为当前文档层级;内容、box、对象类型和稳定对象 ID 仍受认证,但父目录层级本身不提供密码学完整性。“锁定后仅剩密文”只表示内核管理的持久化工作空间中不应存在可读明文,并且受管临时文件、缓存和密钥句柄已经完成尽力清理;不包括用户明确导出到外部位置的文件,也不承诺抹除进程、操作系统或底层存储介质已经保留的瞬时或历史副本。@@ -356,9 +356,8 @@ MasterSalt 是 KEK 派生的全局根基——主密码 + MasterSalt 经 Argon2i1. **数据对象封装**:每个 `.sy`、资源、资源名称映射和数据库定义对象至少包含格式版本、算法标识、密码学安全随机生成且对同一用途密钥唯一的 nonce、密文和认证标签。当前格式的 AAD 按 `格式版本 + boxID + 对象类型 + 稳定对象 ID` 确定性构造,不包含父目录或绝对路径;`.sy` 的稳定对象 ID 是规范化文件基名 `<rootID>.sy`,资源使用脱敏磁盘文件基名,数据库定义使用 avID,固定单例文件使用版本化常量 ID。解密后必须验证对象内部 ID 与 AAD 对象 ID 一致,并拒绝同一 box 内的重复对象 ID。AES-GCM nonce 由操作系统 CSPRNG 生成,随机源失败必须终止写入,并按标准规定限制每个用途密钥的调用规模;认证标签不能事后识别已经发生的 nonce 重用,因此不得宣称此类重用会被认证失败自动发现。2. **全局密钥与包络元数据**:`NotebookCrypto` 保存 KDF 算法、参数、MasterSalt、verifier 格式、KEK 包络版本和备份 HMAC 版本;`BoxConf.WrappedDEK` 保存每个 box 的 DEK 包络。它们不随每个数据对象重复保存。3. **用途隔离**:每个 box 的 DEK 通过固定域分隔符派生彼此独立的文件内容、资源内容、资源名称映射、数据库定义、content SQLCipher 和 blocktree SQLCipher 子密钥;KEK 另行派生 DEK 包络、verifier 和备份 HMAC 上下文,不得把 WrappedDEK 认证错误地归入 DEK 域。-4. **层级完整性**:`.siyuan/hierarchy.enc` 使用独立用途子密钥和固定 AAD,记录每个文档稳定对象 ID 对应的父对象 ID。文件系统目录结构只是清单的物理投影,不是可信输入;创建、移动、删除和历史回滚使用事务日志原子更新文件与清单。认证有效但历史版本的清单仍可能被回放,遵循本设计不提供回滚防护的边界。-5. **数据库兼容信息**:SQLCipher 版本、cipher 参数和 schema 版本保存在数据库头部可验证位置或对应的加密配置边界,不写入每个数据对象。content 与 blocktree 索引可重建;源密文认证通过后,版本不兼容的索引应关闭并重建,而不是降低 cipher 参数或回退到明文 SQLite。-6. **迁移与改名**:旧版按完整逻辑相对路径绑定的封装只能先用旧路径认证解密,再事务迁移为按稳定对象 ID 绑定的新格式;迁移验证全部对象及当前目录关系后生成初始层级清单,新封装和清单全部验证并同步落盘前保留旧副本。新格式中只改变父目录不重新封装内容,只事务更新目录投影和层级清单;稳定文件基名或格式版本变化时才生成新封装。崩溃恢复完成前不开放访问,失败不得删除旧数据或发布部分索引。+4. **数据库兼容信息**:SQLCipher 版本、cipher 参数和 schema 版本保存在数据库头部可验证位置或对应的加密配置边界,不写入每个数据对象。content 与 blocktree 索引可重建;源密文认证通过后,版本不兼容的索引应关闭并重建,而不是降低 cipher 参数或回退到明文 SQLite。+5. **改名与格式稳定性**:首个发布格式直接以稳定对象 ID 构造 AAD,不兼容开发阶段按完整逻辑相对路径生成的试验密文,也不提供双格式读取或迁移;开发数据在格式切换后删除并重建。只改变父目录不重新封装内容,稳定文件基名变化时才使用旧基名认证解密并按新基名生成新封装。首个发布版本冻结后,未来再修改信封或 AAD 语义必须提升格式版本并另行设计迁移。SQLCipher 的主数据库、WAL、SHM、回滚日志、临时文件和备份副本都是受保护对象,必须放在受控目录并使用对应的用途密钥。锁定、崩溃恢复和历史快照必须枚举这些伴随文件;未加密 SQLite 页面、查询结果或诊断转储不得写入全局临时目录。@@ -398,12 +397,11 @@ HMAC 只能证明备份内容未被不知道 KEK 的一方篡改,不能证明| 锁定后经 UI、HTTP、文件 API、CLI、MCP、WebDAV、插件、后台任务访问 | 除明确允许暴露的笔记本名称外,不能读取、写入、复制、删除或列举加密内容;错误不返回明文、密文或可推断文档标题的信息 || 解锁、锁定、应用重启和认证失败 | 只有 `Unlocked` 才能取得受管 DEK 句柄和数据库连接;失败和重启后入口拒绝访问,内核可控的明文缓存、临时文件与句柄完成尽力清理 || 锁定与响应生成、缓存发布、导出、预览、同步、索引或历史恢复并发 | 锁定在所有在途生命周期租约结束后才成功;成功后无晚到的明文响应、缓存回填、临时文件、可用令牌或数据库重连 |-| 同一加密 box 内移动文档,随后清空缓存并重启 | 文件基名不变时密文保持不变并可按稳定对象 ID 认证解密;层级清单与目录关系一致,旧目录位置不存在,失败时没有半完成文件、清单或索引 |-| 在文件系统中把认证有效且文件基名不变的 `.sy` 手工移动到另一父目录,但不更新层级清单 | 打开、同步落地检查或重建索引时发现目录关系与认证清单不一致并进入 `Error`;不得按新目录关系加载或自动改写清单 |+| 同一加密 box 内移动文档,随后清空缓存并重启 | 文件基名不变时密文保持不变并可按稳定对象 ID 认证解密;新父目录按普通笔记本语义生效,旧目录位置不存在,失败处理与普通笔记本一致 || 加密 box 与普通 box、两个加密 box 之间的引用、移动、镜像、资源和数据库操作 | 必须保持孤岛边界;拒绝时不得产生半完成文件、全局索引或关联数据 || 资源密文写入与名称映射更新任一步失败 | 整个逻辑事务失败并回滚;调用方收到错误,不留下可访问的孤儿资源、缺失映射或明文临时文件 || 删除加密笔记本后保留历史,再尝试禁用或恢复 | 存在密钥依赖时拒绝删除全局配置和备份;使用匹配备份与主密码可恢复,只有明确永久清除恢复数据后才允许解除依赖 |-| 文档生成历史后移动父目录或改变文件基名,再查看或回滚旧历史 | 使用历史条目记录的快照 boxID、对象类型和稳定文件基名认证解密;回滚事务更新层级清单,仅在目标基名变化时重新封装,缺失快照上下文时拒绝操作 |+| 文档生成历史后移动父目录或改变文件基名,再查看或回滚旧历史 | 使用历史条目记录的快照 boxID、对象类型和稳定文件基名认证解密;仅在目标基名变化时重新封装,缺失快照上下文时拒绝操作 || 文件、资源、数据库、WAL/SHM、历史、快照、日志和临时目录的磁盘检查 | 内核管理范围内不存在可读正文、资源内容、原始资源名称或 SQLite 明文页;清理失败的临时路径仍被所有入口拒绝 || 清理失败后删除或损坏全局拒绝访问记录,再重启和访问残留路径 | 受控明文临时命名空间仍被无条件拒绝;扫描重建记录并继续清理,记录缺失或损坏不会使路径失效放行 || 密文篡改、路径替换和备份损坏 | 认证或格式校验失败并安全拒绝;不回退普通路径、不生成新密钥材料、不静默覆盖配置 |