An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Fix AAD path normalization (strip leading slash) to prevent cipher authentication failure, preserve original boxID for history rollback decryption when getRollbackBox creates new box https://github.com/siyuan-note/siyuan/issues/18034
Daniel5681cc0f7335
diff --git a/kernel/filesys/crypto_hook.go b/kernel/filesys/crypto_hook.goindex a3fcf78521..a4483eee16 100644--- a/kernel/filesys/crypto_hook.go+++ b/kernel/filesys/crypto_hook.go@@ -30,6 +30,13 @@ import (// filesys 不能直接 import model(会形成 model → filesys → model 循环依赖),故采用回调注入。var DEKProvider func(boxID string) ([]byte, error)+// normalizeRelPath 标准化 box 内相对路径用于 AAD:去掉前导 /、统一为正斜杠。+func normalizeRelPath(p string) string {+ p = filepath.ToSlash(p)+ p = strings.TrimPrefix(p, "/")+ return p+}+// encryptData 若 boxID 是已解锁的加密 box,用 fileKey(DEK 派生子密钥)加密 data,// AAD 绑定 boxID + 相对路径;非加密笔记本原样返回;加密但未解锁时返回 error,拒绝写盘(防止明文泄漏)。func encryptData(boxID, relativePath string, data []byte) ([]byte, error) {@@ -44,7 +51,7 @@ func encryptData(boxID, relativePath string, data []byte) ([]byte, error) {return data, nil // 非加密 box}fileKey := util.DeriveSubKey(dek, "siyuan/file")- aad := "siyuan:v1:file:" + boxID + ":" + relativePath+ aad := "siyuan:v1:file:" + boxID + ":" + normalizeRelPath(relativePath)return util.EncryptWithAAD(fileKey, data, []byte(aad))}@@ -61,7 +68,7 @@ func decryptData(boxID, relativePath string, data []byte) ([]byte, error) {return data, nil // 非加密 box}fileKey := util.DeriveSubKey(dek, "siyuan/file")- aad := "siyuan:v1:file:" + boxID + ":" + relativePath+ aad := "siyuan:v1:file:" + boxID + ":" + normalizeRelPath(relativePath)return util.DecryptWithAAD(fileKey, data, []byte(aad))}diff --git a/kernel/model/crypto.go b/kernel/model/crypto.goindex 0b5d884988..ade4608693 100644--- a/kernel/model/crypto.go+++ b/kernel/model/crypto.go@@ -1165,18 +1165,25 @@ func ExtractBoxIDFromHistoryPath(absPath string) string {// copyAssetDecryptIfEncrypted 把 srcPath 的 asset 复制到 destPath。// 若 srcPath 在已解锁的加密笔记本下,读密文→解密→写明文到 destPath(导出目录);// 否则走 filelock.Copy 原路径(字节级复制,密文/明文均可)。+// normalizeRelPath 标准化 box 内相对路径用于 AAD:去掉前导 /、统一为正斜杠。+func normalizeRelPath(p string) string {+ p = filepath.ToSlash(p)+ p = strings.TrimPrefix(p, "/")+ return p+}+// EncryptFile 用 fileKey(DEK 派生子密钥)加密 .sy 文档字节,AAD 绑定 boxID + 相对路径。-// relativePath 形如 "/20240101000000-abc123.sy",与 filesys.encryptData/decryptData 保持一致。+// relativePath 在构造 AAD 前会标准化(去前导 /、统一斜杠),与 filesys.encryptData/decryptData 保持一致。func EncryptFile(boxID, relativePath string, dek, plaintext []byte) ([]byte, error) {fileKey := util.DeriveSubKey(dek, "siyuan/file")- aad := "siyuan:v1:file:" + boxID + ":" + relativePath+ aad := "siyuan:v1:file:" + boxID + ":" + normalizeRelPath(relativePath)return util.EncryptWithAAD(fileKey, plaintext, []byte(aad))}// DecryptFile 对应解密。func DecryptFile(boxID, relativePath string, dek, ciphertext []byte) ([]byte, error) {fileKey := util.DeriveSubKey(dek, "siyuan/file")- aad := "siyuan:v1:file:" + boxID + ":" + relativePath+ aad := "siyuan:v1:file:" + boxID + ":" + normalizeRelPath(relativePath)return util.DecryptWithAAD(fileKey, ciphertext, []byte(aad))}diff --git a/kernel/model/history.go b/kernel/model/history.goindex 64f676550b..b82af1f87d 100644--- a/kernel/model/history.go+++ b/kernel/model/history.go@@ -284,6 +284,7 @@ func RollbackDocHistory(historyPath string) (err error) {return}boxID := parts[1]+ origBoxID := boxID // 保留原始 boxID 用于解密(getRollbackBox 可能返回不同的 box)box, needResetTree, err := getRollbackBox(boxID)if err != nil {@@ -312,16 +313,17 @@ func RollbackDocHistory(historyPath string) (err error) {logging.LogErrorf("read history [%s] failed: %s", srcPath, srcReadErr)return}- if IsEncryptedBox(boxID) {- dek, dekErr := GetDEKIfUnlocked(boxID)+ if IsEncryptedBox(origBoxID) {+ dek, dekErr := GetDEKIfUnlocked(origBoxID)if dekErr != nil {err = errors.New(Conf.Language(314))return}var decErr error// 历史路径格式:<historyDir>/<datePrefix>/<boxID>/<relativePath>+ // 用原始 boxID 解密(getRollbackBox 可能创建了新 box,密文仍属于原加密 box)filePath := parts[2]- srcData, decErr = DecryptFile(boxID, filePath, dek, srcData)+ srcData, decErr = DecryptFile(origBoxID, filePath, dek, srcData)if decErr != nil {logging.LogErrorf("decrypt history [%s] failed: %s", srcPath, decErr)err = decErr