246 lines
13 KiB
JavaScript
246 lines
13 KiB
JavaScript
/**
|
||
* guard-core — path-guard 的判定核心(纯函数,无 pi 依赖,node 可直接单测:node agent/extensions/guard-core.test.js)。
|
||
*
|
||
* 读白名单,不是黑名单正则:agent 能碰的路径只有
|
||
* workspace/(可写)· 本 rollout 的 agent/ stages/ handles/ rollout.json(只读)
|
||
* ../../job.json · ../../data(→ bench/data)· bench/cache · bench/data · bench/bin · .horizon/ · AGENTS.md
|
||
* repair 模式额外:../../oracle/
|
||
* 项目树(PROJECT_ROOT 下)里其余一切拒:别的 rollout(GRPO 组内独立性)、oracle(e2e 抄答案)、harness/(评判代码)、
|
||
* state/(全部 job 的 sqlite)、export/ datasets/ reports/ .env*。
|
||
* $HOME 下默认全拒(~/.pi 凭证、~/toolchains 源码、bench-state / bench-work 都在那儿),只放行 ALLOW_EXTRA
|
||
* 与白名单目录的真实路径(data/ cache/ 是指向 /home 的软链)。$HOME 与项目树之外的系统路径(/usr /tmp /dev …)放行。
|
||
*
|
||
* bash:命令里含 cd/pushd/popd 直接拒(cwd 就是 workspace,看别处用绝对路径;cd 是所有绕过的根)——唯一例外 `cd <workspace 本身>`;
|
||
* 命令替换 `$(…)`/反引号、`$IFS`、路径里的变量展开一律拒(都是把路径藏起来躲过 token 扫描的手段);
|
||
* 剩下的 token 全抠出来逐个判(相对路径按 cwd=workspace 解析,glob 在第一个 *?[ 处截断取前缀,软链按 realpath 复判)。
|
||
*
|
||
* ⚠ 这一层是"教学层":给 agent 一句人话的拒绝理由,并挡住绝大多数顺手的越界。
|
||
* 真正的隔离在 scripts/agent_jail.sh(mount namespace 把 oracle/ 兄弟 rollout/ ~/.pi 直接从文件系统里摘掉)。
|
||
* 解析 shell 字符串永远追不平所有变体——别把它当唯一防线。
|
||
*/
|
||
"use strict";
|
||
const path = require("node:path");
|
||
const fs = require("node:fs");
|
||
|
||
function norm(p) {
|
||
return path.resolve(p).replace(/\/+$/, "");
|
||
}
|
||
function under(p, root) {
|
||
return p === root || p.startsWith(root + path.sep);
|
||
}
|
||
/** realpath;路径不存在就用"最近的存在的祖先"的 realpath 拼回去(写新文件、glob 前缀都会碰到)。
|
||
* 悬空软链(目标暂时挂不上,比如 data/ 指向的盘没挂)用 readlink 手动跟一跳——
|
||
* 否则 bench/data 的真实路径进不了白名单,$HOME 默认拒会把合法的读校准集误杀。 */
|
||
function rpath(p) {
|
||
let cur = p, tail = [];
|
||
for (let i = 0; i < 40; i++) {
|
||
try { return tail.length ? path.join(fs.realpathSync(cur), ...tail) : fs.realpathSync(cur); } catch {}
|
||
try {
|
||
if (fs.lstatSync(cur).isSymbolicLink()) {
|
||
const t = path.resolve(path.dirname(cur), fs.readlinkSync(cur));
|
||
return tail.length ? path.join(t, ...tail) : t;
|
||
}
|
||
} catch {}
|
||
const parent = path.dirname(cur);
|
||
if (parent === cur) return p;
|
||
tail.unshift(path.basename(cur));
|
||
cur = parent;
|
||
}
|
||
return p;
|
||
}
|
||
|
||
/**
|
||
* @param {object} env { WORKSPACE, ROLLOUT, JOB_DIR, BENCH_ROOT, PROJECT_ROOT, HOME, MODE, DENY_EXTRA?, ALLOW_EXTRA? }
|
||
* ALLOW_EXTRA:优先于一切拒绝的放行前缀——本 rollout 在 work_root 下的编译工作目录(work_root/<run>/<job>/r<kk>)。
|
||
* DENY_EXTRA:$HOME 之外也要拒的绝对路径(state/ work_root 若被挪出 /home 时用)。$HOME 本身已默认全拒。
|
||
*/
|
||
function makeGuard(env) {
|
||
const WS = norm(env.WORKSPACE);
|
||
const RO = norm(env.ROLLOUT);
|
||
const JOB = norm(env.JOB_DIR);
|
||
const BENCH = norm(env.BENCH_ROOT);
|
||
const PROJ = norm(env.PROJECT_ROOT);
|
||
const HOME = norm(env.HOME || "/nonexistent-home");
|
||
const MODE = env.MODE || "e2e";
|
||
const WS_R = rpath(WS);
|
||
|
||
// 从 workspace/ 看:.. = 本 rollout(agent/ stages/ handles/ rollout.json)· ../.. = rollouts/(兄弟,禁)· ../../.. = job 目录(job.json data/ oracle/)
|
||
const allowRead = [
|
||
WS, RO,
|
||
path.join(JOB, "job.json"), path.join(JOB, "data"),
|
||
path.join(BENCH, "harness", "adapters"),
|
||
path.join(BENCH, "data"), path.join(BENCH, "cache"), path.join(BENCH, "bin"), path.join(BENCH, "agent", "oe-doc.sh"),
|
||
path.join(PROJ, ".horizon"), path.join(PROJ, "AGENTS.md"),
|
||
];
|
||
if (MODE === "repair") allowRead.push(path.join(JOB, "oracle"));
|
||
// 白名单目录的真实路径(data/ cache/ 是指向 /home 的软链):agent 用 realpath 访问也放行。
|
||
// 与 allowRead 合成一张表,所有分支都查这张表——旧版只在"项目树外"分支查,$HOME 默认拒之后会误杀 data/。
|
||
for (const a of allowRead.slice()) { const r = rpath(a); if (r !== a && !allowRead.includes(r)) allowRead.push(r); }
|
||
const allowExtra = (env.ALLOW_EXTRA || []).filter(Boolean).map(norm);
|
||
// 明确拒(即使不在项目树 / $HOME 内)
|
||
const denyAlways = [path.join(BENCH, ".env.local"), ...(env.DENY_EXTRA || []).filter(Boolean).map(norm)];
|
||
// 报错信息用
|
||
const why = (p) => {
|
||
if (under(p, path.join(JOB, "oracle"))) return "e2e 不能看 oracle/(标准答案)";
|
||
if (under(p, path.join(JOB, "rollouts")) && !under(p, RO)) return "不能看别的 rollout(轨迹必须独立)";
|
||
if (under(p, path.join(BENCH, "harness")) || under(p, path.join(BENCH, "export"))) return "harness/ 是评判代码,读它不会让你编译得更好";
|
||
if (under(p, path.join(BENCH, "state"))) return "state/ 是全部 job 的状态库";
|
||
for (const d of denyAlways) if (under(p, d)) return "harness 的工作目录/状态库/凭证,不是你的";
|
||
if (under(p, path.join(HOME, ".pi"))) return "~/.pi 含凭证";
|
||
if (under(p, path.join(HOME, "toolchains"))) return "工具链源码不用读,可改的旋钮都在系统提示里";
|
||
if (under(p, HOME)) return "主目录不在你的范围内(编译产物看 handle 给的路径)";
|
||
return "不在可读白名单里";
|
||
};
|
||
|
||
/** 解析一个路径 token → 绝对路径(去引号、去 glob 尾巴、展开 ~),空则 null */
|
||
function resolveToken(tok) {
|
||
let t = tok.replace(/^['"`]+|['"`]+$/g, "");
|
||
if (!t) return null;
|
||
if (t.startsWith("~")) t = HOME + t.slice(1);
|
||
const g = t.search(/[*?[]/);
|
||
if (g >= 0) t = t.slice(0, g) || ".";
|
||
if (t.endsWith("/") && t.length > 1) t = t.slice(0, -1);
|
||
return norm(path.isAbsolute(t) ? t : path.join(WS, t));
|
||
}
|
||
|
||
/** 判一个已解析的绝对路径。judgePath 会对 p 和 realpath(p) 各判一次——两次都过才放行。 */
|
||
function judgeOne(p, write, tok) {
|
||
if (write) {
|
||
return (under(p, WS) || under(p, WS_R)) ? null : { block: true, reason: `只能写 workspace/ 内(${tok})`, path: p };
|
||
}
|
||
for (const a of allowExtra) if (under(p, a)) return null;
|
||
for (const a of allowRead) if (under(p, a)) return null;
|
||
for (const d of denyAlways) if (under(p, d)) return { block: true, reason: `${why(p)}(${tok})`, path: p };
|
||
// 不放行白名单目录的祖先本身:ls ../.. 会列出 oracle/ rollouts/ —— 用不到
|
||
if (under(p, PROJ) || under(p, HOME)) return { block: true, reason: `${why(p)}(${tok})`, path: p };
|
||
return null; // 项目树 / $HOME 之外的系统路径(/usr /tmp /dev …)
|
||
}
|
||
|
||
/** @returns {null | {block:true, reason:string, path:string}} */
|
||
function judgePath(tok, { write = false } = {}) {
|
||
const p = resolveToken(tok);
|
||
if (!p) return null;
|
||
const v = judgeOne(p, write, tok);
|
||
if (v) return v;
|
||
// 软链逃逸:workspace 里 `ln -s ../../../oracle o` 之后 `cat o/result.json` 表面在 workspace 内。
|
||
// 真实路径再判一次,两次都过才算过。
|
||
const pr = rpath(p);
|
||
if (pr !== p) {
|
||
const v2 = judgeOne(pr, write, `${tok} → ${pr}`);
|
||
if (v2) return v2;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
// cd/pushd/popd:命令位置 = 字符串开头、换行后、或 ; & | ( then do else 之后。
|
||
// m 标志是必须的——`echo hi\ncd ..\ncd ..\ncd ..\ncd oracle\ncat result.json` 靠换行躲过了 v3 的 ^。
|
||
const CD_RX = /(^|[;&|(\n]\s*|\bthen\s+|\bdo\s+|\belse\s+)\s*(cd|pushd|popd)(\s|$|;|&|\|)/m;
|
||
// 把路径藏起来躲过 token 扫描的几种手法,一律拒(agent 在这个环境里都不需要)
|
||
const OBFUSCATE = [
|
||
[/\$\{?IFS\}?/, "命令里不要用 $IFS"],
|
||
[/\$\(|`/, "命令里不要用命令替换 $(…) 或反引号:路径直接写出来"],
|
||
[/\$\{?\w+\}?\/|\/\$\{?\w+/, "路径里不要用变量展开($VAR/…),直接写绝对路径"],
|
||
];
|
||
// 像路径的 token:含 / ,或以 . ~ 开头(含 .. .env),排除 URL、选项开头、数字
|
||
const TOKEN_RX = /(?:^|[\s=:(){}'"`,;|&<>])((?:~|\.{1,2}|\/)[^\s'"`;|&<>(){}]*|[A-Za-z0-9_.\-@+]+\/[^\s'"`;|&<>(){}]*)/g;
|
||
|
||
// cd 的唯一放行:目标就是 workspace 本身(实测 agent 爱写 `cd <绝对 workspace> && bench stage run …`,cwd 没变,相对路径判定仍成立)
|
||
const CD_ALL_RX = new RegExp(CD_RX.source, "gm");
|
||
function judgeCommand(cmd) {
|
||
for (const [rx, msg] of OBFUSCATE) {
|
||
if (rx.test(cmd)) return { block: true, reason: msg, path: "" };
|
||
}
|
||
let cm;
|
||
CD_ALL_RX.lastIndex = 0;
|
||
while ((cm = CD_ALL_RX.exec(cmd)) !== null) {
|
||
const verb = cm[2];
|
||
const rest = cmd.slice(cm.index + cm[0].length).trim();
|
||
const arg = verb === "cd" ? (rest.match(/^([^\s;|&)]+)/) || [])[1] : null;
|
||
const target = arg ? resolveToken(arg) : null;
|
||
if (verb === "cd" && (target === WS || target === WS_R)) continue;
|
||
return { block: true, reason: "命令里不要用 cd/pushd/popd(除了 cd 到你自己的 workspace):cwd 就是 workspace,看别处请用绝对路径", path: verb };
|
||
}
|
||
let m;
|
||
TOKEN_RX.lastIndex = 0;
|
||
while ((m = TOKEN_RX.exec(cmd)) !== null) {
|
||
const tok = m[1];
|
||
if (/^(https?|ssh|git|file):\/\//.test(tok)) continue;
|
||
if (/^\d+(\.\d+)*\/?$/.test(tok)) continue; // 版本号之类
|
||
if (tok === "/" || tok === "." ) continue;
|
||
const v = judgePath(tok);
|
||
if (v) return v;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
return { judgePath, judgeCommand, resolveToken, allowRead };
|
||
}
|
||
|
||
module.exports = { makeGuard };
|
||
|
||
// Small lexical aid for diagnostics, not a shell security boundary. Quotes and
|
||
// escapes must be respected: a search pattern containing '| hb_compile' is data.
|
||
// Uncertain syntax stays on the legacy path; the mount jail enforces isolation.
|
||
function shellSegments(source) {
|
||
const segments = [], words = [];
|
||
let word = '', started = false, quote = null;
|
||
const flush = () => { if (started) words.push(word); word = ''; started = false; };
|
||
const segment = () => { flush(); if (words.length) segments.push(words.splice(0)); };
|
||
for (let i = 0; i < source.length; i++) {
|
||
const char = source[i];
|
||
if (char === '\\' && quote !== "'") {
|
||
if (++i >= source.length) return null;
|
||
word += source[i]; started = true; continue;
|
||
}
|
||
if (quote) {
|
||
if (char === quote) quote = null;
|
||
else {
|
||
if (quote === '"' && (char === '`' || (char === '$' && source[i + 1] === '('))) return null;
|
||
word += char;
|
||
}
|
||
started = true; continue;
|
||
}
|
||
if (char === "'" || char === '"') { quote = char; started = true; continue; }
|
||
if (char === '`' || (char === '$' && source[i + 1] === '(')) return null;
|
||
if (';&|\n()'.includes(char)) { segment(); continue; }
|
||
if (/\s/.test(char)) { flush(); continue; }
|
||
word += char; started = true;
|
||
}
|
||
if (quote) return null;
|
||
segment();
|
||
return segments;
|
||
}
|
||
function forbiddenCommand(source, forbidden) {
|
||
const segments = shellSegments(source);
|
||
if (!segments) return {uncertain: true};
|
||
for (const words of segments) {
|
||
let index = 0;
|
||
while (index < words.length && (/^[A-Za-z_][A-Za-z0-9_]*=/.test(words[index]) ||
|
||
['sudo', 'env', 'nohup', 'time', 'command', 'exec', 'then', 'do', 'else'].includes(words[index]))) index++;
|
||
const command = path.basename(words[index] || '');
|
||
if (forbidden.includes(command)) return {command};
|
||
}
|
||
return null;
|
||
}
|
||
module.exports.shellSegments = shellSegments;
|
||
module.exports.forbiddenCommand = forbiddenCommand;
|
||
|
||
// Detect only an actual find command whose search roots include /. Quoted
|
||
// search patterns and predicates after the roots remain ordinary data.
|
||
function rootFind(source) {
|
||
const segments = shellSegments(source);
|
||
if (!segments) return false;
|
||
for (const words of segments) {
|
||
let index = 0;
|
||
while (index < words.length && (/^[A-Za-z_][A-Za-z0-9_]*=/.test(words[index]) ||
|
||
['sudo', 'env', 'nohup', 'time', 'command', 'exec', 'then', 'do', 'else'].includes(words[index]))) index++;
|
||
if (path.basename(words[index++] || '') !== 'find') continue;
|
||
while (['-H', '-L', '-P', '--'].includes(words[index])) index++;
|
||
for (; index < words.length && !words[index].startsWith('-'); index++) {
|
||
if (/^\/+$/.test(words[index])) return true;
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
module.exports.rootFind = rootFind;
|