"""path-guard 规则的 Python 平行实现 + 用例。改 agent/extensions/path-guard.ts 的正则时同步改这里并跑一遍。 python3 scripts/test_path_guard.py 为什么需要:guard 只在真跑 rollout 时才生效,一条轨迹几十分钟,靠它试错太慢。 第一条真实轨迹里 10 次工具调用被误杀 4 次(`2>/dev/null` 被当成写绝对路径、`grep hb_compile` 被当成调用 hb_compile), 这些都是本文件里的用例。 """ from __future__ import annotations import re import sys FORBIDDEN = ["hb_compile", "hb_mapper", "hb_verifier", "hrt_model_exec", "hb_perf", "ssh", "scp", "rsync", "wget", "curl", "pip", "pip3", "conda", "npm"] CMD_POSITION = re.compile(r"(?:^|[;&|]\s*|\$\(|`)\s*(?:[A-Za-z_][A-Za-z0-9_]*=\S*\s+)*" r"(?:sudo\s+|env\s+|nohup\s+|time\s+)*(" + "|".join(FORBIDDEN) + r")\b") REDIRECT = re.compile(r"(?:^|[\s;|&(])(?:\d?>>?|&>)\s*['\"]?([^\s'\"|;&)]+)") REDIRECT_OK = {"/dev/null", "/dev/stderr", "/dev/stdout"} WRITE_PATTERNS = [re.compile(r"\bsleep\s+[0-9]{3,}"), re.compile(r"\bgit\s+push\b")] DEST_LAST = {"cp", "mv", "ln", "install", "rsync"} # 最后一个参数才是目的地 ALL_ARGS = {"rm", "rmdir", "tee", "touch", "mkdir", "chmod", "chown", "truncate", "dd", "shred"} PREFIX = {"sudo", "env", "nohup", "time"} def _outside(t: str) -> bool: """近似 TS 里的 !inside(path.resolve(WORKSPACE, t)):绝对路径、或 ../ 跳出 workspace。""" if t.startswith("/tmp/") or t == "/dev/null": return False if t.startswith("/") or t.startswith("~"): return True depth = 0 for part in t.split("/"): if part == "..": depth -= 1 elif part not in ("", "."): depth += 1 if depth < 0: return True return False def _write_targets(seg: str) -> list[str]: toks = seg.split() i = 0 while i < len(toks) and re.match(r"^[A-Za-z_][A-Za-z0-9_]*=", toks[i]): i += 1 while i < len(toks) and toks[i] in PREFIX: i += 1 cmd = toks[i].rsplit("/", 1)[-1] if i < len(toks) else "" args = [t.strip("'\"") for t in toks[i + 1:] if not t.startswith("-")] if cmd in DEST_LAST: return args[-1:] if args else [] if cmd in ALL_ARGS: return args return [] def blocked(cmd: str) -> str | None: for m in REDIRECT.finditer(cmd): t = m.group(1) if t.startswith("&") or t in REDIRECT_OK or t.startswith("/proc/self/fd/"): continue if _outside(t): return f"redirect:{t}" m = CMD_POSITION.search(cmd) if m: return f"cmd:{m.group(1)}" for seg in re.split(r";|\|\||&&|\||&", cmd): for t in _write_targets(seg): if _outside(t): return f"write:{t}" for rx in WRITE_PATTERNS: if rx.search(cmd): return f"rule:{rx.pattern[:20]}" return None CASES: list[tuple[str, bool]] = [ # (命令, 是否应当被拒) ("ls -la .. && cat ../rollout.json", False), ("ls ../stages/04-calibrate && cat ../stages/04-calibrate/a00/meta.json 2>/dev/null", False), ("ps aux | grep -iE 'hb_compile|hbm|hmct' | grep -v grep | head", False), # 只读排查,不是调用 ("grep -rn hb_compile ../stages/05-compile/a00/stderr.log | tail -5", False), ("bench stage run compile --config workspace/compile.yaml", False), ("bench stage wait 05-compile-050600-5a2e6d --poll 120 2>&1 | tail -30", False), ("python3 -c 'import yaml;print(1)' 2>/dev/null", False), ("cat ../job.json | python3 -m json.tool | head -40", False), ("echo 'model_parameters: {}' > compile.yaml", False), # 写 workspace 内,允许 ("hb_compile -c compile.yaml", True), # 直接调工具链 ("cd /tmp && hb_compile -c x.yaml", True), ("OMP_NUM_THREADS=8 hb_compile -c x.yaml", True), ("nohup hrt_model_exec perf --model_file x.hbm &", True), ("ssh root@board 'ls /userdata'", True), ("pip install onnxsim", True), ("echo hack >> /etc/passwd", True), ("cp ../job.json /tmp/x", False), # /tmp 放行 ("cp ../job.json .", False), # 把只读文件读进 workspace ("cp ../stages/04-calibrate/compile.yaml my.yaml", False), ("cp workspace.yaml ../../oracle/artifacts/x.yaml", True), # 往 job 目录外写 ("rm -rf ../oracle", True), ("mkdir -p artifacts && touch artifacts/x", False), ("sleep 3600", True), # 用 bench stage wait ] def main() -> int: bad = 0 for cmd, want in CASES: got = blocked(cmd) ok = bool(got) == want bad += not ok print(f"{'ok ' if ok else 'FAIL'} block={str(bool(got)):5} want={str(want):5} {got or '':<22} {cmd}") print(f"\n{len(CASES) - bad}/{len(CASES)} passed") return 1 if bad else 0 if __name__ == "__main__": sys.exit(main())