agent-executor per-task 挂载隔离:任务根下的命令 spawn 前包一层 unshare --mount wrapper(isolatedCommand),先 bind 会话根到 /tmp anchor,再把任务根覆盖为 tmpfs、仅把当前会话根 bind 回原路径——AI 命令只能读写自己会话,看不到别人的 task 目录;深层子目录随 bind 自然保留(terminal 的 cd 不断),mount ns 随进程退出销毁。cwd 恰在任务根或其外(skill 入口等)不隔离直接 spawn;core 侧 terminal 的 cwd 恒为 taskDir 且 cd 被 resolveInTask 钳住,AI 无法把 cwd 挪出任务根绕开隔离。compose 移除 cap_drop 改 privileged: true(默认 seccomp/apparmor 拦 unshare/mount),cgroup 资源护栏不变,容器仍仅内网 expose 9000。smoke 新增隔离三用例(无 mount 权限自动 SKIP),并修两处断言误判:cat 成功只回显内容不含文件名;cd 失败信息含兄弟目录名污染 ls 段断言,改分隔符切段 + leaked/blocked 标记,16/16 全过。compose 注释错别字兑住→兜住;ARCHITECTURE.md §6 同步隔离与 privileged 说明
This commit is contained in:
+43
-1
@@ -17,6 +17,11 @@
|
||||
// - timeout_ms 夹在 [1s, EXECUTOR_MAX_TIMEOUT_MS](默认 30 分钟);
|
||||
// - stdout/stderr 各自按 max_output_chars 截断(默认 EXECUTOR_MAX_OUTPUT_CHARS=200000),超限后丢弃剩余输出,exit 事件带 truncated 标记。
|
||||
//
|
||||
// per-task 隔离:每条命令默认在新的 mount 命名空间里跑。启动 wrapper 把 EXECUTOR_CWD(任务根,/workspace/tasks)
|
||||
// 覆盖为 tmpfs,只把当前会话的子目录 bind 回原路径上——结果:AI 命令能读写自己的项目,看不到别人的 task 目录。
|
||||
// 任务根以外的路径(/workspace/skills、/tmp、/usr、依赖包等)不受影响。mount 命名空间随进程退出自动销毁。
|
||||
// 需 CAP_SYS_ADMIN + 默认 seccomp/apparmor unconfined:compose 里 agent-executor 配了 privileged: true。
|
||||
//
|
||||
// 日志:EXECUTOR_LOG_LEVEL=debug|info|warn|error|none(默认 info,none/off 全静默),分级语义与 core 的 log.js 一致。
|
||||
// info = 关键动作(启动/进程 start/exit/取消/关闭);debug = 额外打印与 core 的完整交互:
|
||||
// 每个请求行、/exec 请求体、每条 SSE 下发帧、每个 JSON 响应(超长内容截断显示)。改环境配置后重启容器生效。
|
||||
@@ -80,6 +85,36 @@ function brief(s, n) {
|
||||
return s.length > n ? s.slice(0, n) + '…(+' + (s.length - n) + ')' : s;
|
||||
}
|
||||
|
||||
// 单引号安全包裹:内部单引号按 '\'' 规则转义,给 sh -c 拼命令串用。
|
||||
function shq(s) { return "'" + String(s).replace(/'/g, "'\\''") + "'"; }
|
||||
|
||||
// per-task 挂载隔离 wrapper:返回一个 sh -c 脚本,shell 启动时跑:
|
||||
// 1) 把 cwd 预先 bind 到 /tmp 下的 anchor(避开后面被覆盖的任务根)
|
||||
// 2) 在新 mount ns 里把任务根覆盖为 tmpfs(其他会话从此不可见)
|
||||
// 3) 在 tmpfs 下重建当前会话目录名,把 anchor bind 回原路径上
|
||||
// 4) 拆掉 anchor,切到 cwd,exec 用户命令
|
||||
// cwd 不在任务根下时返回 null(不隔离;主要适用于 skill 入口直接跑在 /workspace/skills/<name> 的场景)。
|
||||
function isolatedCommand(command, cwd) {
|
||||
if (cwd === DEFAULT_CWD || !cwd.startsWith(DEFAULT_CWD + path.sep)) return null;
|
||||
// 会话根 = 任务根下的第一层子目录(task-YYYYMMDD-NNN);cwd 可能是其下任意深子目录(terminal 的 cd)。
|
||||
// 隔离时整个会话根 bind 回去,相对子路径自然保留,cd 回原 cwd 不会断。
|
||||
const rel = path.relative(DEFAULT_CWD, cwd);
|
||||
const sessionName = rel.split(path.sep)[0];
|
||||
const sessionRoot = path.join(DEFAULT_CWD, sessionName);
|
||||
return [
|
||||
'set -e',
|
||||
'anchor=$(mktemp -d /tmp/.exanchor.XXXXXXXXXX)',
|
||||
'mount --bind ' + shq(sessionRoot) + ' "$anchor"',
|
||||
'mount -t tmpfs tmpfs ' + shq(DEFAULT_CWD),
|
||||
'mkdir ' + shq(sessionRoot),
|
||||
'mount --bind "$anchor" ' + shq(sessionRoot),
|
||||
'umount "$anchor"',
|
||||
'rmdir "$anchor"',
|
||||
'cd ' + shq(cwd),
|
||||
'exec /bin/sh -c ' + shq(command),
|
||||
].join('\n');
|
||||
}
|
||||
|
||||
// 常量时间比较,避免通过响应耗时猜 token
|
||||
function tokenOk(req) {
|
||||
if (!TOKEN) return true;
|
||||
@@ -164,7 +199,14 @@ async function handleExec(req, res) {
|
||||
const id = crypto.randomUUID();
|
||||
let child;
|
||||
try {
|
||||
child = spawn('/bin/sh', ['-c', command], { cwd, env, detached: true, stdio: ['ignore', 'pipe', 'pipe'] });
|
||||
// 任务根下的命令走 unshare(2) + mount(2) 临时覆盖 /workspace/tasks 为 tmpfs,只保留当前会话目录;
|
||||
// cwd 在任务根外(如 skill 入口跑在技能目录)时直接 spawn。详见 isolatedCommand。
|
||||
const isolation = isolatedCommand(command, cwd);
|
||||
if (isolation) {
|
||||
child = spawn('unshare', ['--mount', '/bin/sh', '-c', isolation], { cwd, env, detached: true, stdio: ['ignore', 'pipe', 'pipe'] });
|
||||
} else {
|
||||
child = spawn('/bin/sh', ['-c', command], { cwd, env, detached: true, stdio: ['ignore', 'pipe', 'pipe'] });
|
||||
}
|
||||
} catch (e) {
|
||||
return send(res, 500, { error: 'spawn 失败:' + e.message });
|
||||
}
|
||||
|
||||
+36
-3
@@ -1,11 +1,14 @@
|
||||
// agent-executor 冒烟测试:node smoke-test.js(在本目录执行)。
|
||||
// 覆盖:/health、/exec 流式输出与退出事件、增量下发时序、cwd 白名单拒绝、DELETE 取消、EXECUTOR_TOKEN 鉴权。
|
||||
// 覆盖:/health、/exec 流式输出与退出事件、增量下发时序、cwd 白名单拒绝、DELETE 取消、EXECUTOR_TOKEN 鉴权、per-task 挂载隔离。
|
||||
// 注意:隔离用例需 root/CAP_SYS_ADMIN(unshare+mount),无权限时自动 SKIP 不计失败。
|
||||
process.env.PORT = process.env.PORT || '19000';
|
||||
process.env.EXECUTOR_ROOTS = process.env.__SELF_DIR__ || __dirname; // 宿主机测试:白名单取本目录
|
||||
process.env.EXECUTOR_CWD = process.env.__SELF_DIR__ || __dirname;
|
||||
delete process.env.EXECUTOR_TOKEN; // 主实例不带 token,鉴权单独测
|
||||
|
||||
const { spawn } = require('child_process');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
require('./server.js');
|
||||
|
||||
const BASE = 'http://127.0.0.1:' + process.env.PORT;
|
||||
@@ -76,8 +79,38 @@ async function main() {
|
||||
const exit6 = evs6[evs6.length - 1];
|
||||
check('输出按 max_output_chars 截断', outChars <= 100 && exit6.truncated === true, 'chars=' + outChars + ' exit=' + JSON.stringify(exit6));
|
||||
|
||||
// 7. 未知路由
|
||||
check('未知路由 404', (await fetch(BASE + '/nope')).status === 404);
|
||||
// 7.5 per-task 挂载隔离:另起一个实例(端口 19002),EXECUTOR_CWD=.iso-tasks(任务根),
|
||||
// 下放 iso-a / iso-b 两个「会话」目录;cwd 取 iso-a 的深层子目录(模拟 terminal 的 cd),验证:
|
||||
// 任务根下只看到 iso-a、cd 兄弟会话被拒、自己文件可读;无 mount 权限时整组 SKIP。
|
||||
const isoRoot = path.join(__dirname, '.iso-tasks');
|
||||
fs.rmSync(isoRoot, { recursive: true, force: true });
|
||||
fs.mkdirSync(path.join(isoRoot, 'iso-a', 'deep'), { recursive: true });
|
||||
fs.mkdirSync(path.join(isoRoot, 'iso-b'), { recursive: true });
|
||||
fs.writeFileSync(path.join(isoRoot, 'iso-a', 'a.txt'), 'a');
|
||||
fs.writeFileSync(path.join(isoRoot, 'iso-b', 'b.txt'), 'b');
|
||||
const isoChild = spawn(process.execPath, [__dirname + '/server.js'], { env: Object.assign({}, process.env, { PORT: '19002', EXECUTOR_ROOTS: isoRoot, EXECUTOR_CWD: isoRoot }), stdio: 'ignore' });
|
||||
await new Promise(r => setTimeout(r, 500));
|
||||
const isoCwd = path.join(isoRoot, 'iso-a', 'deep');
|
||||
// 分隔符切段便于精确断言;cd 失败信息不走 stderr 上送(含 iso-b 字样会污染 ls 段断言),改用 leaked/blocked 标记区分。
|
||||
const evsI = await collect(await fetch('http://127.0.0.1:19002/exec', {
|
||||
method: 'POST', headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ command: 'pwd; echo @LS@; ls ../../; echo @CD@; cd ../../iso-b 2>/dev/null && echo leaked || echo blocked; echo @CAT@; cat ../../iso-a/a.txt', cwd: isoCwd, timeout_ms: 5000 }),
|
||||
}));
|
||||
const outI = evsI.filter(e => e.type === 'out' || e.type === 'err').map(e => e.chunk).join('');
|
||||
const exitI = evsI[evsI.length - 1];
|
||||
if (!exitI || exitI.code !== 0) {
|
||||
console.log('SKIP per-task 挂载隔离(需 root/CAP_SYS_ADMIN;exit=' + (exitI && exitI.code) + ')');
|
||||
} else {
|
||||
const lsRegion = outI.split('@LS@')[1].split('@CD@')[0];
|
||||
check('隔离: 任务根只见 iso-a', /iso-a/.test(lsRegion) && !/iso-b/.test(lsRegion), 'ls=' + JSON.stringify(lsRegion));
|
||||
check('隔离: cd 兄弟会话被拒', /blocked/.test(outI) && !/leaked/.test(outI), 'out=' + JSON.stringify(outI));
|
||||
// cat 成功只回显文件内容(a),不含文件名;取 @CAT@ 段精确比对。
|
||||
check('隔离: 深子目录 pwd 不丢 + 自己文件可读', outI.includes(isoCwd) && outI.split('@CAT@')[1].trim() === 'a', 'out=' + JSON.stringify(outI));
|
||||
}
|
||||
isoChild.kill('SIGKILL');
|
||||
fs.rmSync(isoRoot, { recursive: true, force: true });
|
||||
|
||||
// 8. token 鉴权:另起一个带 EXECUTOR_TOKEN 的实例(子进程,端口 19001)
|
||||
|
||||
// 8. token 鉴权:另起一个带 EXECUTOR_TOKEN 的实例(子进程,端口 19001)
|
||||
const child = spawn(process.execPath, [__dirname + '/server.js'], { env: Object.assign({}, process.env, { PORT: '19001', EXECUTOR_TOKEN: 't9' }), stdio: 'ignore' });
|
||||
|
||||
Reference in New Issue
Block a user