Skip to content

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

:lock: Add symlink resolution to MCP file tools and box write lock to UnlockBox #18034

Daniel3237bc9cc033
diff --git a/kernel/mcp/tools/file.go b/kernel/mcp/tools/file.go
index e5a6e51e95..17647d1e7a 100644
--- a/kernel/mcp/tools/file.go
+++ b/kernel/mcp/tools/file.go
@@ -98,16 +98,47 @@ func resolvePath(rel string) (string, error) {
return abs, nil
}
-// rejectEncryptedPath 检查路径是否属于加密笔记本(含 symlink 绕过)。
-// 返回 boxID 和是否为加密 box。
+// rejectEncryptedPath 检查路径是否属于加密笔记本(含 symlink 绕过),返回 boxID 和是否为加密 box。
func rejectEncryptedPath(absPath string) (boxID string, encrypted bool) {
boxID = model.ExtractBoxIDFromAssetsPath(absPath)
if boxID != "" && model.IsEncryptedBox(boxID) {
return boxID, true
}
+ // 防止 symlink 绕过:找到最长已存在的父路径,解析 symlink 后拼回剩余路径,再检查
+ if resolved := resolveLongestExistingParent(absPath); resolved != absPath {
+ boxID = model.ExtractBoxIDFromAssetsPath(resolved)
+ if boxID != "" && model.IsEncryptedBox(boxID) {
+ return boxID, true
+ }
+ }
return "", false
}
+// resolveLongestExistingParent 解析 absPath 中最长已存在部分的 symlink,拼回剩余路径。
+func resolveLongestExistingParent(absPath string) string {
+ cleaned := filepath.Clean(absPath)
+ dir := cleaned
+ for {
+ if _, err := os.Lstat(dir); err == nil {
+ break
+ }
+ parent := filepath.Dir(dir)
+ if parent == dir {
+ return cleaned
+ }
+ dir = parent
+ }
+ if dir == cleaned {
+ return cleaned
+ }
+ resolved, err := filepath.EvalSymlinks(dir)
+ if err != nil {
+ return cleaned
+ }
+ remaining, _ := filepath.Rel(dir, cleaned)
+ return filepath.Join(resolved, remaining)
+}
+
func fileList(args map[string]interface{}) (CallToolResult, error) {
p, _ := args["path"].(string)
if p == "" {
diff --git a/kernel/model/crypto.go b/kernel/model/crypto.go
index 3ceef16a2b..b0580b68ba 100644
--- a/kernel/model/crypto.go
+++ b/kernel/model/crypto.go
@@ -660,6 +660,10 @@ func UnlockBox(boxID string, password string, boxEnc *conf.BoxEncryption) error
return errors.New("no encrypted key material for box")
}
+ // 获取 box 写锁,与 LockBox/unmount0 串行化,防止并发锁/解锁导致 db/DEK 状态不一致
+ acquireBoxWriteLock(boxID)
+ defer releaseBoxWriteLock(boxID)
+
// 加锁与改密/创建等控制面操作串行化,确保读 verifier + 自动恢复 + 解密 + conf 修复都在同一临界区
notebookCryptoMu.Lock()
defer notebookCryptoMu.Unlock()