MemOS icon indicating copy to clipboard operation
MemOS copied to clipboard

memos-local: patternSearch 缺少 ownerFilter 导致多 Agent 记忆隔离泄漏

Open zhenxu138 opened this issue 8 months ago • 2 comments

MemOS memos-local 插件多 Agent 记忆隔离漏洞报告

📋 漏洞概览

项目 详情
漏洞类型 信息泄露 / 访问控制绕过
严重等级 高
影响组件 memos-local-openclaw-plugin v4.x
受影响功能 memory_search 工具的 patternSearch 分支
影响范围 多 Agent 场景下的中文查询和短英文查询

🐛 问题描述

在多 Agent 协作场景中,当 Agent 执行**包含中文或短英文(2字符)**的 memory_search 查询时,会泄露其他 Agent 的私有记忆,违反了 owner 隔离机制。

复现步骤

环境:

  • OpenClaw 版本:2026.3.13
  • memos-local-openclaw-plugin:本地安装版(从 extensions 目录)
  • 多 Agent 配置:main, coding, chore, auto
  • multiAgentMode: true

复现步骤:

  1. Agent coding 对话中记录:"你查一下啊botmark.cc是啥"
  2. Agent main 执行搜索:memory_search({ query: "BotMark.cc AI Agent基准测试平台" })
  3. 预期结果:只返回 agent:main 的记忆
  4. 实际结果:返回了 agent:coding 的记忆
  5. 验证:数据库查询确认记忆归属
    SELECT id, owner FROM chunks WHERE id = 'e1569bfb-4052-4405-b951-efc44bae3ae9';
    -- e1569bfb-4052-4405-b951-efc44bae3ae9|agent:coding  ← 确认是 coding 的记忆
    

🔍 根因分析

代码位置

文件: extensions/memos-local-openclaw-plugin/src/recall/engine.ts

漏洞代码(第 80 行):

const patternHits = shortTerms.length > 0
  ? this.store.patternSearch(shortTerms, { limit: candidatePool })  // ❌ 缺少 ownerFilter
  : [];

对比正确的实现:

// ✅ FTS 搜索有 ownerFilter
this.store.ftsSearch(query, candidatePool, ownerFilter)

// ✅ 向量搜索有 ownerFilter  
vectorSearch(this.store, queryVec, candidatePool, maxChunks, ownerFilter)

// ❌ Pattern 搜索没有 ownerFilter(Bug)
this.store.patternSearch(shortTerms, { limit: candidatePool })

泄漏路径

用户查询
  ↓
包含中文或 2 字符短词
  ↓
触发 CJK bigram 提取(如 "基准"、"测试")
  ↓
调用 patternSearch()
  ↓
SQL 查询缺少 owner 过滤条件
  ↓
返回所有 Agent 的记忆(泄露!)

数据库层面验证

-- FTS 搜索(有 owner 过滤)
SELECT c.id FROM chunks_fts f
JOIN chunks c ON c.rowid = f.rowid
WHERE c.owner IN ('agent:main', 'public')  -- ✅ 正确
;

-- Pattern 搜索(缺少 owner 过滤)
SELECT c.id FROM chunks c
WHERE c.content LIKE '%基准%'  -- ❌ 错误:无 owner 过滤
;

📊 影响范围

查询类型 受影响 场景
中文查询 ✅ 受影响 包含中文的自然语言查询(90% 以上的中文场景)
短英文查询(2字符) ✅ 受影响 "OK", "AI", "ML" 等短词
长英文查询 ✅ 不受影响 使用 FTS + Vector,有 owner 过滤
向量查询 ✅ 不受影响 vectorSearch 有 ownerFilter

潜在损失

  • 隐私泄露:不同 Agent 的私人对话互相可见
  • 数据污染:Agent 可能根据其他 Agent 的记忆做出错误决策
  • 安全风险:敏感配置、API Key 等信息可能跨 Agent 流转

🛠️ 修复方案

方案 1: 修改 src/storage/sqlite.ts(推荐)

在 patternSearch 方法中添加 ownerFilter 参数:

patternSearch(patterns: string[], opts: {
  role?: string;
  limit?: number;
  ownerFilter?: string[]  // ✅ 新增参数
} = {}) {
  if (patterns.length === 0) return [];
  const limit = opts.limit ?? 10;

  const conditions = patterns.map(() => "c.content LIKE ?");
  const whereClause = conditions.join(" OR ");
  const roleClause = opts.role ? " AND c.role = ?" : "";

  // ✅ 添加 owner 过滤
  let ownerClause = "";
  const params: (string | number)[] = patterns.map(p => `%${p}%`);
  if (opts.ownerFilter && opts.ownerFilter.length > 0) {
    const placeholders = opts.ownerFilter.map(() => "?").join(",");
    ownerClause = ` AND c.owner IN (${placeholders})`;
    params.push(...opts.ownerFilter);
  }

  if (opts.role) params.push(opts.role);
  params.push(limit);

  const rows = this.db.prepare(`
    SELECT c.id as chunk_id, c.content, c.role, c.created_at
    FROM chunks c
    WHERE (${whereClause})${roleClause}${ownerClause} AND c.dedup_status = 'active'
    ORDER BY c.created_at DESC
    LIMIT ?
  `).all(...params) as Array<{ chunk_id: string; content: string; role: string; created_at: number }>;

  return rows.map(r => ({
    chunkId: r.chunk_id,
    content: r.content,
    role: r.role,
    createdAt: r.created_at,
  }));
}

方案 2: 修改 src/recall/engine.ts(同步修改调用)

// 第 80 行,传递 ownerFilter
const patternHits = shortTerms.length > 0
  ? this.store.patternSearch(shortTerms, { limit: candidatePool, ownerFilter })  // ✅ 添加 ownerFilter
  : [];

// 第 98 行,同步修改 hubMemoryPatternSearch
const hubPatternHits = this.store.hubMemoryPatternSearch(shortTerms, {
  limit: candidatePool,
  ownerFilter  // ✅ 添加 ownerFilter(如适用)
});

方案 3: 同步修改 hubMemoryPatternSearch

如果 Hub 场景也需要隔离,需同步修改 hubMemoryPatternSearch 方法,传递 ownerFilter 参数。


✅ 验证方法

修复前验证

# 1. 确认当前版本有漏洞
sqlite3 ~/.openclaw/memos-local/memos.db "SELECT owner, COUNT(*) FROM chunks GROUP BY owner;"

# 2. Agent main 执行搜索(预期应该失败)
memory_search({ query: "BotMark.cc" })

# 3. 应该能看到 agent:coding 的记忆(泄露)

修复后验证

# 1. 重启 OpenClaw
openclaw gateway restart

# 2. 在不同 Agent 中记录测试数据
# Agent coding: "这是 coding 的私有信息"
# Agent main: "这是 main 的私有信息"

# 3. Agent main 执行搜索
memory_search({ query: "私有信息" })

# 4. 应该只返回 agent:main 的记忆(隐私保护)

数据库验证

-- 修复后,Pattern 搜索应该有 owner 过滤
SELECT c.id, c.owner, c.content
FROM chunks c
WHERE c.content LIKE '%基准%'
  AND c.owner IN ('agent:main', 'public')  -- ✅ 应该存在
  AND c.dedup_status = 'active';

📦 版本信息

组件 版本
OpenClaw 2026.3.13
Node.js v24.14.0
SQLite better-sqlite3
memos-local 插件 本地扩展(extensions 目录)

📞 联系信息

  • 发现者:国庆 AI 助手(OpenClaw 用户)
  • 发现时间:2026-03-27
  • 影响环境:多 Agent 协作场景
  • 测试环境:Linux, Node.js v24.14.0

📌 补充说明

  1. 为什么中文查询会触发漏洞?

    • 中文无空格,需要用 CJK bigram(2字词滑动窗口)提取
    • 提取后调用 patternSearch(基于 LIKE 的匹配)
    • patternSearch 未实现 owner 过滤
  2. 为什么 FTS 和 Vector 是安全的?

    • FTS 搜索(ftsSearch)有 ownerFilter 参数
    • 向量搜索(vectorSearch)有 ownerFilter 参数
    • 只有 patternSearch 缺失
  3. 修复影响评估

    • 修改仅限 patternSearch 方法,不会影响其他功能
    • 向后兼容:ownerFilter 为可选参数,不影响现有调用
    • 性能影响:极小(多拼接一个 SQL WHERE 条件)

🎯 修复优先级

推荐优先级:🔴 高

  • 隐私泄露直接影响多 Agent 协作安全性
  • 中文场景下影响 90% 以上的查询
  • 修复简单、风险低(仅限 patternSearch 方法)

zhenxu138 avatar Mar 27 '26 01:03 zhenxu138

Version 1.0.8 has fixed the issue of incorrect recall in multi-agent memory.

Hun-ger avatar Apr 09 '26 08:04 Hun-ger

Hey @zhenxu138! Thanks for the contribution — already solved the issue, thanks for helping us to catch the bug! 🐞

Btw if you enjoyed contributing and want to stay connected with the project, we run a Discord server for contributors, a chill space to discuss features, ask questions, or pick up new issues  👉🏻 MemOS Discord Server (Contributor Exclusive)

Thanks again for shipping this, and happy MemOSing 🧠✨

angarchie avatar May 19 '26 13:05 angarchie