Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Fix encrypted asset resolution, cache pollution, nil panic, and block_embeddings regression https://github.com/siyuan-note/siyuan/issues/18034

Daniel10fad592f7da
diff --git a/kernel/model/assets.go b/kernel/model/assets.go
index 541ce0cbbd..1f32498a22 100644
--- a/kernel/model/assets.go
+++ b/kernel/model/assets.go
@@ -591,12 +591,18 @@ func SearchAssetsByName(keyword string, exts []string) (ret []*cache.Asset) {
}
func GetAssetAbsPath(relativePath string) (string, error) {
+ return GetAssetAbsPathWithOpt(relativePath, false)
+}
+
+// GetAssetAbsPathWithOpt 与 GetAssetAbsPath 一致,但可通过 includeEncrypted 控制是否遍历加密 box。
+// serveAssets 传 true(下游 serveEncryptedAsset 会按锁定状态 fail-closed),其他调用方传 false(安全跳过)。
+func GetAssetAbsPathWithOpt(relativePath string, includeEncrypted bool) (string, error) {
relativePath = strings.TrimSpace(relativePath)
if idx := strings.Index(relativePath, "?"); idx >= 0 {
relativePath = relativePath[:idx]
}
- absPath, err := getAssetAbsPath(relativePath)
+ absPath, err := getAssetAbsPath(relativePath, includeEncrypted)
if err == nil && absPath != "" {
return absPath, nil
}
@@ -607,7 +613,7 @@ func GetAssetAbsPath(relativePath string) (string, error) {
return "", fmt.Errorf(Conf.Language(12), relativePath)
}
-func getAssetAbsPath(relativePath string) (absPath string, err error) {
+func getAssetAbsPath(relativePath string, includeEncrypted bool) (absPath string, err error) {
relativePath = filepath.ToSlash(relativePath)
// 在 data 文件夹下搜索,主要是 data/assets 文件夹
p := filepath.Join(util.DataDir, relativePath)
@@ -627,7 +633,7 @@ func getAssetAbsPath(relativePath string) (absPath string, err error) {
return "", errors.New(Conf.Language(0))
}
for _, notebook := range notebooks {
- if IsEncryptedBox(notebook.ID) {
+ if !includeEncrypted && IsEncryptedBox(notebook.ID) {
continue // 加密 box 的资源不参与全局路径解析(孤岛,资源不跨边界)
}
notebookAbsPath := filepath.Join(util.DataDir, notebook.ID)
diff --git a/kernel/model/crypto.go b/kernel/model/crypto.go
index 06472ef8d7..da480b5661 100644
--- a/kernel/model/crypto.go
+++ b/kernel/model/crypto.go
@@ -699,8 +699,8 @@ func copyAssetDecryptIfEncrypted(srcPath, destPath string) error {
if boxID != "" && IsEncryptedBox(boxID) {
dek, err := GetDEKIfUnlocked(boxID)
if err != nil {
- // 加密 box 未解锁:fail-closed,不解密
- return filelock.Copy(srcPath, destPath)
+ // 加密 box 未解锁:fail-closed,拒绝复制(不复制密文,避免泄漏无效文件)
+ return errors.New(Conf.Language(314))
}
raw, readErr := filelock.ReadFile(srcPath)
if readErr != nil {
diff --git a/kernel/model/upload.go b/kernel/model/upload.go
index 9eb322d649..1451a3e9d7 100644
--- a/kernel/model/upload.go
+++ b/kernel/model/upload.go
@@ -19,6 +19,7 @@ package model
import (
"encoding/json"
"errors"
+ "fmt"
"io"
"os"
"path"
@@ -129,7 +130,9 @@ func InsertLocalAssets(id string, assetAbsPaths []string, isUpload bool) (succMa
p := "assets/" + fName
succMap[baseName] = p
- cache.SetAssetHash(hash, p)
+ if !IsEncryptedBox(bt.BoxID) {
+ cache.SetAssetHash(hash, p) // 加密 box 不写全局 cache,避免跨边界去重污染
+ }
}
}
IncSync()
@@ -369,7 +372,9 @@ func Upload(c *gin.Context) {
p := strings.TrimPrefix(path.Join(relAssetsDirPath, fName), "/")
succMap[baseName] = p
- cache.SetAssetHash(hash, p)
+ if uploadBoxID == "" || !IsEncryptedBox(uploadBoxID) {
+ cache.SetAssetHash(hash, p) // 加密 box 不写全局 cache
+ }
}
}
@@ -403,8 +408,16 @@ func getAssetsDir(boxLocalPath, docDirLocalPath string) (assets string) {
// 加密 box 必须已解锁(DEK 在内存)才写入;加密但未解锁返回错误(fail-closed,避免明文落盘)。
// 非加密 box 按 reader 直接写(走 filelock.WriteFileByReader 原路径,保留锁语义)。
func writeAssetFile(writePath string, src io.Reader, boxID string) (err error) {
- // 以路径反查的 boxID 为准(防绕过):传入 boxID 与路径 box 不一致时以路径为准
+ // 从 writePath 反查真实 boxID,与传入 boxID 交叉校验
pathBoxID := ExtractBoxIDFromAssetsPath(writePath)
+ // 传入 boxID 与路径 box 都非空但不一致:路径指向另一个 box,拒绝(防跨 box 写入)
+ if boxID != "" && pathBoxID != "" && boxID != pathBoxID {
+ return fmt.Errorf("boxID mismatch: param=%s, path=%s", boxID, pathBoxID)
+ }
+ // 路径不在 box 下但传入的是加密 box:加密内容只能写 box 内,拒绝写全局 assets
+ if pathBoxID == "" && boxID != "" && IsEncryptedBox(boxID) {
+ return fmt.Errorf("encrypted box asset must be written inside the box directory, got global path: %s", writePath)
+ }
actualBoxID := pathBoxID
if actualBoxID == "" {
actualBoxID = boxID // 路径不在 box 下(如全局 assets),回退传入值
diff --git a/kernel/server/serve.go b/kernel/server/serve.go
index c80528e5da..cdb46b6375 100644
--- a/kernel/server/serve.go
+++ b/kernel/server/serve.go
@@ -668,7 +668,7 @@ func serveAssets(ginServer *gin.Engine) {
}
relativePath := path.Join("assets", requestPath)
- p, err := model.GetAssetAbsPath(relativePath)
+ p, err := model.GetAssetAbsPathWithOpt(relativePath, true)
if err != nil {
context.Status(http.StatusNotFound)
return
diff --git a/kernel/sql/database.go b/kernel/sql/database.go
index 239eb21449..0bf25e93a8 100644
--- a/kernel/sql/database.go
+++ b/kernel/sql/database.go
@@ -1115,9 +1115,12 @@ func deleteBlocksByIDs(tx *sql.Tx, ids []string) (err error) {
return
}
+ // block_embeddings 表在加密 db 中不存在(加密笔记本不参与嵌入向量化),对该表不存在的错误容错
stmt = "DELETE FROM block_embeddings WHERE id IN (" + strings.Join(ftsIDs, ",") + ")"
- if err = execStmtTx(tx, stmt); err != nil {
- return
+ if _, embedErr := tx.Exec(stmt); embedErr != nil {
+ if !strings.Contains(embedErr.Error(), "no such table") {
+ err = embedErr // 非"表不存在"的真实错误照常返回
+ }
}
return
}
diff --git a/kernel/treenode/blocktree.go b/kernel/treenode/blocktree.go
index 0e3c38646e..c909ee2a04 100644
--- a/kernel/treenode/blocktree.go
+++ b/kernel/treenode/blocktree.go
@@ -1027,7 +1027,11 @@ func GetBlockTreeInBox(id, boxID string) (ret *BlockTree) {
}
ret = &BlockTree{}
sqlStmt := "SELECT * FROM blocktrees WHERE id = ?"
- err := queryRowForBox(boxID, sqlStmt, id).Scan(&ret.ID, &ret.RootID, &ret.ParentID, &ret.BoxID, &ret.Path, &ret.HPath, &ret.Updated, &ret.Type)
+ row := queryRowForBox(boxID, sqlStmt, id)
+ if row == nil {
+ return nil // 加密 box 未解锁,视作不存在
+ }
+ err := row.Scan(&ret.ID, &ret.RootID, &ret.ParentID, &ret.BoxID, &ret.Path, &ret.HPath, &ret.Updated, &ret.Type)
if err != nil {
ret = nil
if !errors.Is(err, sql.ErrNoRows) {