diff --git a/plugins/pi-mail-bridge/lib/workspace.js b/plugins/pi-mail-bridge/lib/workspace.js index a1b8745..e6c814b 100644 --- a/plugins/pi-mail-bridge/lib/workspace.js +++ b/plugins/pi-mail-bridge/lib/workspace.js @@ -35,6 +35,42 @@ import { isAbsolute, join, resolve } from 'node:path'; * @param {string} fallback 不可用时的兜底目录(可为空串 = 交给平台自己决定) * @returns {{cwd: string, grouped: boolean}} grouped 为真表示落在了寻址指定的目录里 */ +/** + * 这个路径**像不像**一个工作目录。 + * + * 与网关侧 `repo.IsPlausibleWorkspace` 同一套规则(两层各有一份实现, + * 但因为一层在 Go、一层在 Node,无法共享代码;两边都有判据钉住同样的形状): + * + * 1. 绝对路径; + * 2. **深度 ≥ 2** —— 工作目录是树里的具体位置,不是顶层挂载点。 + * `/home`、`/tmp`、`/root`、`/opt` 本身都不是"干活的地方"; + * 3. 不含隐藏段(`.pi`/`.cache` 这类是缓存与会话存储,不是工作目录)。 + * + * # 为什么必须有这一条(2026-09-15 实测的严重错投) + * + * 旧实现只判"存在且是目录",而 `/home` **存在**、也是目录 ⇒ 一路放行。 + * 于是:地址里的 path 被填成 `/home` → 会话 workspace=/home → 桥把 worker + * 起在 `/home`(日志实测 `新建 pi 会话 …(cwd=/home)`)→ 那个 worker 发的信 + * 继续带 `/home` → 自我强化。而且那个 worker 的 rw 清单里**没有** `/home`, + * 它在自己"工作目录"里连文件都写不了。 + * + * 网关侧现在已在地址受理处拒收这类 path(400),这里是**同一规则的第二道**: + * 万一还有别的路径把毒值送进来(老数据、别的客户端、以后新增的入口), + * worker 也不会被起在一个它写不了的目录里。 + */ +export function isPlausibleWorkspace(p) { + const raw = typeof p === 'string' ? p.trim() : ''; + if (!raw || !isAbsolute(raw)) return false; + const abs = resolve(raw); + // 深度 ≥ 2:去掉两端斜杠后至少还要有一个 '/'。/home → 0 段 → 不通过。 + if (abs.replace(/^\/+|\/+$/g, '').split('/').length < 2) return false; + // 隐藏段:/root/.pi/mail-sessions/x 这类不是工作目录。 + for (const seg of abs.split('/')) { + if (seg.length > 1 && seg.startsWith('.')) return false; + } + return true; +} + export function resolveWorkspaceCwd(workspace, fallback) { const raw = typeof workspace === 'string' ? workspace.trim() : ''; const fb = typeof fallback === 'string' ? fallback : ''; @@ -42,6 +78,13 @@ export function resolveWorkspaceCwd(workspace, fallback) { if (!raw || !isAbsolute(raw)) return { cwd: fb, grouped: false }; const abs = resolve(raw); + // 地址里的 path 必须"像工作目录":/home、/tmp、/root/.pi/... 一律退回兜底目录。 + // 只对**地址来的**值判,不判兜底目录本身(兜底目录是平台自己的选择, + // 例如 /root/.pi/mail-sessions/<会话>,那正是"没有工作目录"的表达)。 + if (!isPlausibleWorkspace(abs)) { + console.warn(`[pi-mail-bridge] 地址里的 path 不像工作目录,已退回兜底目录: ${abs} → ${fb}`); + return { cwd: fb, grouped: false }; + } try { if (existsSync(abs) && statSync(abs).isDirectory()) { return { cwd: abs, grouped: true }; diff --git a/plugins/pi-mail-bridge/test/workspace-plausible.test.mjs b/plugins/pi-mail-bridge/test/workspace-plausible.test.mjs new file mode 100644 index 0000000..307152e --- /dev/null +++ b/plugins/pi-mail-bridge/test/workspace-plausible.test.mjs @@ -0,0 +1,60 @@ +/** + * 地址里的 path 必须像工作目录 —— 桥侧的第二道闸(2026-09-15)。 + * + * 背景(用户报的严重错投): + * 客户端把收件人的 path 填成 `/home` → 会话 workspace=/home → + * 桥按来信的 to_workspace 起 worker(实测日志 `新建 pi 会话 …(cwd=/home)`)→ + * 那个 worker 发的信继续带 /home → 建议接口把它学成第一条候选 → 自我强化。 + * + * 旧实现的漏洞就一行:只判"存在且是目录",而 `/home` **存在**、也是目录 ⇒ 放行 ✗。 + * 而且那个 worker 的沙箱 rw 里没有 `/home`,它连自己"工作目录"里都写不了文件。 + * + * 网关侧已在地址受理处拒收这类 path(400);这里钉住**同一规则的第二道**, + * 因为旧数据、别的客户端、以后新增的入口都可能再送进来。 + * 判据两侧都写:实测污染过的形状必须被拒,真实工作目录必须放行 + * (只验一侧的话,"永远退回兜底"也能骗过坏样本那一侧)。 + */ +import assert from 'node:assert/strict'; +import { test } from 'node:test'; +import { mkdtempSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { isPlausibleWorkspace, resolveWorkspaceCwd } from '../lib/workspace.js'; + +test('isPlausibleWorkspace:实测污染过的形状必须被拒', () => { + for (const bad of ['/home', '/root', '/tmp', '/root/.pi/mail-sessions/8f056b73', 'rel/path', '', ' ']) { + assert.equal(isPlausibleWorkspace(bad), false, `${JSON.stringify(bad)} 不该被当成工作目录`); + } +}); + +test('isPlausibleWorkspace:真实工作目录必须放行(含尚未创建的)', () => { + for (const good of [ + '/home/program/agentmail', + '/home/newqqagent', + '/tmp/remotebot-ws', // 注册时自报、还没建的目录 + '/home/program/llmsproxy', + ]) { + assert.equal(isPlausibleWorkspace(good), true, `${good} 应被当成工作目录`); + } +}); + +test('resolveWorkspaceCwd:/home 一律退回兜底目录且标记 grouped=false', () => { + const fb = '/root/.agentmail-pi/workspaces/sess-1'; + const got = resolveWorkspaceCwd('/home', fb); + assert.equal(got.cwd, fb, '/home 必须被换成兜底目录(它存在,旧实现会照用)'); + assert.equal(got.grouped, false, 'grouped=false 才能让上层知道"这不是它自己的目录"'); +}); + +test('resolveWorkspaceCwd:真实且存在的目录照用', () => { + const dir = mkdtempSync(join(tmpdir(), 'pi-ws-')); + const got = resolveWorkspaceCwd(dir, '/root/.agentmail-pi/workspaces/sess-2'); + assert.equal(got.cwd, dir); + assert.equal(got.grouped, true); +}); + +test('resolveWorkspaceCwd:不存在但像工作目录的 → 退回兜底(原行为不变)', () => { + const fb = '/root/.agentmail-pi/workspaces/sess-3'; + const got = resolveWorkspaceCwd('/home/program/不存在的目录-xyz', fb); + assert.equal(got.cwd, fb); + assert.equal(got.grouped, false); +});