3015 lines
153 KiB
Rust
3015 lines
153 KiB
Rust
// What a bot may do on this computer: list, search, read and edit files, run commands
|
||
// (long ones move to the background), and the rules that decide when to ask first.
|
||
//
|
||
// Limits (measured against the reference assistant's local tool runner):
|
||
// search: MAIN_TIMEOUT_MS=25e3, HARD_MAX_OUTPUT_LINES=1e4 (), client limit Jut=2e3 ()
|
||
// read: at most 8388608 bytes (gut,)
|
||
// shell: on timeout a background-able command keeps running and is handed off (Mye,/)
|
||
// Permission order follows the neighbouring investigation's design notes (fnd_8):
|
||
// deny > ignore > allow > ask, with chained shell commands split into parts first.
|
||
|
||
use serde_json::{json, Value};
|
||
use std::collections::HashMap;
|
||
use std::io::Read;
|
||
use std::path::{Path, PathBuf};
|
||
use std::process::{Command, Stdio};
|
||
use std::sync::atomic::{AtomicBool, Ordering};
|
||
use std::sync::{Arc, Mutex, OnceLock};
|
||
use std::time::{Duration, Instant};
|
||
|
||
pub const SEARCH_TIMEOUT_MS: u64 = 25_000;
|
||
pub const SEARCH_HARD_MAX_LINES: usize = 10_000;
|
||
pub const SEARCH_CLIENT_LIMIT_LINES: usize = 2_000;
|
||
pub const READ_MAX_BYTES: u64 = 8_388_608;
|
||
/// Lines returned by one read_file call when the model gives no limit (estimated).
|
||
pub const READ_DEFAULT_LINES: usize = 2_000;
|
||
/// Foreground time before a command is moved to the background (estimated; the original's comes per call).
|
||
pub const SHELL_DEFAULT_TIMEOUT_MS: u64 = 30_000;
|
||
pub const SHELL_MAX_TIMEOUT_MS: u64 = 600_000;
|
||
/// A command on a relayed computer must answer before the relay's forward timeout (35 s):
|
||
/// on the device it moves to the background by this deadline, and read_terminal picks it up.
|
||
pub const DEVICE_SHELL_TIMEOUT_MS: u64 = 25_000;
|
||
/// Output kept from a command: its start and its end, with the middle left out (sizes estimated;
|
||
/// the original reports output_head / output_tail / elided_chars).
|
||
pub const OUTPUT_HEAD_CHARS: usize = 6_000;
|
||
pub const OUTPUT_TAIL_CHARS: usize = 6_000;
|
||
|
||
/// Names the model sees, with JSON-schema parameters.
|
||
pub fn specs() -> Vec<Value> {
|
||
vec![
|
||
json!({"name": "list_dir", "description": "List the files and folders in a folder (relative to the work folder unless absolute).",
|
||
"parameters": {"type": "object", "properties": {"path": {"type": "string", "description": "Folder to list; empty means the work folder."}}, "required": []}}),
|
||
json!({"name": "search_documents", "description": "In the person's own indexed document folders (their 設定 › 一般 › 文件索引), find the passages that answer a question, by meaning as well as words. Use it for 「我上個月那份報表在哪」, 「我寫過的語法筆記」 — questions about THEIR files, not about the computer. Returns the file path and the text of each match.",
|
||
"parameters": {"type": "object", "properties": {
|
||
"query": {"type": "string", "description": "What to look for, in a sentence."},
|
||
"limit": {"type": "integer", "description": "How many passages (default 6)."}}, "required": ["query"]}}),
|
||
json!({"name": "search", "description": "Find text in files under a folder. Returns matching lines as path:line:text.",
|
||
"parameters": {"type": "object", "properties": {
|
||
"pattern": {"type": "string", "description": "Text to look for (regular expression when ripgrep is installed)."},
|
||
"path": {"type": "string", "description": "Folder or file to search; empty means the work folder."},
|
||
"files_only": {"type": "boolean", "description": "Only list the files that match."}}, "required": ["pattern"]}}),
|
||
json!({"name": "read_file", "description": "Read a text file. Lines come back numbered.",
|
||
"parameters": {"type": "object", "properties": {
|
||
"path": {"type": "string"},
|
||
"offset": {"type": "integer", "description": "First line to read (1-based)."},
|
||
"limit": {"type": "integer", "description": "How many lines to read."}}, "required": ["path"]}}),
|
||
json!({"name": "edit_file", "description": "Change a file. Replaces old_text (which must appear exactly once) with new_text. With empty old_text the file is created or overwritten with new_text.",
|
||
"parameters": {"type": "object", "properties": {
|
||
"path": {"type": "string"}, "old_text": {"type": "string"}, "new_text": {"type": "string"}}, "required": ["path", "new_text"]}}),
|
||
json!({"name": "shell", "description": "Run a shell command in the work folder (bash or sh; on a Windows computer Git Bash when it is there, else PowerShell). Its input is closed, so nothing waits for typing: use a program's non-interactive form (flags like -y/--yes; coding agents' print mode, e.g. `claude -p \"…\"`, `pi-bolt -p \"…\"`, `codex exec \"…\"`, `gemini -p \"…\"`). If it runs longer than timeout_ms it keeps running in the background and you get a terminal id to check with read_terminal (pass the same computer to read_terminal/write_terminal). interactive=true runs it in a real terminal instead (互動終端機): for a program that needs one — its own full-screen interface, questions to answer, an editor; you see its screen and type with write_terminal.",
|
||
"parameters": {"type": "object", "properties": {
|
||
"command": {"type": "string"},
|
||
"timeout_ms": {"type": "integer", "description": "How long to wait before moving it to the background (default 30000). Interactive: how long to wait for its first screen (at most 15000)."},
|
||
"interactive": {"type": "boolean", "description": "Run in a real terminal you can see and type into (default false)."}}, "required": ["command"]}}),
|
||
json!({"name": "read_terminal", "description": "Look at a background command: its latest output, or a 互動終端機's screen. wait_ms waits (up to 120000) until it shows something new and goes quiet, or until the text in `until` appears, or it ends — instead of checking again and again.",
|
||
"parameters": {"type": "object", "properties": {"id": {"type": "integer"}, "wait_ms": {"type": "integer"}, "until": {"type": "string", "description": "Return as soon as this text is on the screen / in the output."}}, "required": ["id"]}}),
|
||
json!({"name": "write_terminal", "description": "Type into a 互動終端機 (a command started with interactive=true): text (each new line presses Enter) and/or keys, then see its screen once it settles (wait_ms, default 4000, up to 120000). close=true sends Ctrl-D. A plain command's input is closed: it can't be typed into.",
|
||
"parameters": {"type": "object", "properties": {"id": {"type": "integer"}, "input": {"type": "string", "description": "Text to type; a new line presses Enter."}, "keys": {"type": "array", "items": {"type": "string"}, "description": "Keys to press after the text: enter, tab, shift-tab, esc, backspace, delete, space, up, down, left, right, home, end, pageup, pagedown, ctrl-c, ctrl-d (any ctrl-letter)."}, "wait_ms": {"type": "integer"}, "close": {"type": "boolean"}, "stop": {"type": "boolean", "description": "true: end this background command (plain or interactive) and everything it started. Do this for the ones you no longer need."}}, "required": ["id"]}}),
|
||
]
|
||
}
|
||
|
||
/// 請你接手: the step that asks the person to do something by hand on a computer.
|
||
pub const HANDOFF_TOOL: &str = "hand_over_computer";
|
||
|
||
pub fn label(name: &str) -> &'static str {
|
||
match name {
|
||
HANDOFF_TOOL => "請你接手電腦",
|
||
crate::ask::TOOL => "問你",
|
||
crate::outputs::TOOL => "看完整的結果",
|
||
crate::plan::SUBMIT_TOOL => "交計畫給你核准",
|
||
crate::plan::UPDATE_TOOL => "更新計畫進度",
|
||
"list_dir" => "查看資料夾",
|
||
"search_documents" => "翻你的文件",
|
||
"search" => "搜尋檔案",
|
||
"read_file" => "讀取檔案",
|
||
"edit_file" => "修改檔案",
|
||
"shell" => "執行指令",
|
||
"read_terminal" => "查看背景指令",
|
||
"write_terminal" => "輸入到背景指令",
|
||
"open_for_person" => "打開給你看",
|
||
"send_file" => "傳檔案給你",
|
||
"send_to_device" => "傳到你的另一台裝置",
|
||
"react" => "按表情",
|
||
"view_demo_step" => "看示範",
|
||
"publish_site" => "發布網站",
|
||
"place_call" => "打電話",
|
||
"send_sms" => "傳簡訊",
|
||
"post_to_x" => "在 X 發文",
|
||
"show_card" => "顯示卡片",
|
||
"vault_list" => "查看保險庫",
|
||
"browser_fill_login" => "用保險庫登入",
|
||
"browser_fill_card" => "填入卡片",
|
||
"browser_fill_contact" => "填入聯絡資訊",
|
||
crate::mcp::FIND_TOOL => "找連接器的工具",
|
||
n if n.starts_with(crate::mcp::PREFIX) || n == crate::mcp::USE_TOOL => "用連接器",
|
||
_ => "工具",
|
||
}
|
||
}
|
||
|
||
/// A tool call's arguments (none = an empty object).
|
||
pub fn args_of(args: &str) -> Value {
|
||
serde_json::from_str(if args.trim().is_empty() { "{}" } else { args }).unwrap_or(Value::Null)
|
||
}
|
||
|
||
/// Fields a model has been seen to wrap a call's arguments in.
|
||
const ARG_WRAPPERS: [&str; 7] = ["data", "arguments", "args", "input", "params", "parameters", "kwargs"];
|
||
|
||
/// A call's arguments wrapped once more — {"data": "{\"action\": \"press\", …}"} (a small
|
||
/// model through a gateway did that 65 times in one afternoon, and every one read as "no
|
||
/// action"), {"arguments": {…}} — unwrapped. Only a lone wrapper that the tool's own schema
|
||
/// (`props`, its parameters' properties) doesn't have, holding an object or an object's JSON.
|
||
pub fn unwrapped_args(args: &str, props: Option<&Value>) -> Option<String> {
|
||
let v: Value = serde_json::from_str(args).ok()?;
|
||
let o = v.as_object().filter(|o| o.len() == 1)?;
|
||
let (k, inner) = o.iter().next()?;
|
||
if !ARG_WRAPPERS.contains(&k.as_str()) || props.is_some_and(|p| p.get(k).is_some()) {
|
||
return None;
|
||
}
|
||
let inner = match inner {
|
||
Value::String(s) => serde_json::from_str::<Value>(s).ok()?,
|
||
other => other.clone(),
|
||
};
|
||
inner.is_object().then(|| inner.to_string())
|
||
}
|
||
|
||
fn arg_str(v: &Value, k: &str) -> String {
|
||
v.get(k).and_then(Value::as_str).unwrap_or("").to_string()
|
||
}
|
||
|
||
/// One short line describing a call, for the chat and the approval card.
|
||
pub fn describe(name: &str, args: &str) -> String {
|
||
let v = args_of(args);
|
||
match name {
|
||
crate::ask::TOOL => crate::ask::describe(args),
|
||
"shell" if interactive_of(&v) => format!("{}(互動終端機)", arg_str(&v, "command")),
|
||
"shell" => arg_str(&v, "command"),
|
||
"search" => format!("「{}」 {}", arg_str(&v, "pattern"), arg_str(&v, "path")).trim().to_string(),
|
||
"read_terminal" => format!("背景指令 #{}", v.get("id").and_then(Value::as_u64).unwrap_or(0)),
|
||
"write_terminal" => {
|
||
let input: String = arg_str(&v, "input").chars().take(40).collect();
|
||
let keys = keys_of(&v);
|
||
let keys = if keys.is_empty() { String::new() } else { format!(" [{}]", keys.join(" ")) };
|
||
format!("背景指令 #{}:{}{keys}{}", v.get("id").and_then(Value::as_u64).unwrap_or(0), input.escape_debug(), if v.get("close").and_then(Value::as_bool).unwrap_or(false) { "(結束輸入)" } else { "" })
|
||
}
|
||
"vault_list" => match arg_str(&v, "kind").as_str() {
|
||
"login" => "登入資訊".into(),
|
||
"card" => "卡片".into(),
|
||
"contact" => "聯絡資訊".into(),
|
||
_ => "全部項目".into(),
|
||
},
|
||
"browser_fill_login" => {
|
||
let site = arg_str(&v, "site");
|
||
if site.is_empty() { "目前的網站".into() } else { site }
|
||
}
|
||
"browser_fill_card" | "browser_fill_contact" => "目前的網頁".into(),
|
||
_ => {
|
||
let p = arg_str(&v, "path");
|
||
if p.is_empty() { "工作資料夾".into() } else { p }
|
||
}
|
||
}
|
||
}
|
||
|
||
// ---------- paths ----------
|
||
|
||
pub fn expand_home(p: &str) -> PathBuf {
|
||
if let Some(rest) = p.strip_prefix("~/") {
|
||
if let Some(h) = dirs::home_dir() {
|
||
return h.join(rest);
|
||
}
|
||
} else if p == "~" {
|
||
if let Some(h) = dirs::home_dir() {
|
||
return h;
|
||
}
|
||
}
|
||
PathBuf::from(p)
|
||
}
|
||
|
||
/// A folder under somebody's home — `/Users/name/…` (macOS), `/home/name/…` (Linux),
|
||
/// `C:\Users\name\…` (Windows) — as what comes after the home, with `/` between the parts
|
||
/// ("" for the home itself). None when it isn't under a home.
|
||
fn under_a_home(p: &str) -> Option<String> {
|
||
let parts: Vec<&str> = p.split(['/', '\\']).filter(|s| !s.is_empty()).collect();
|
||
let skip = match parts.as_slice() {
|
||
["Users" | "home", _, ..] if p.starts_with('/') => 2,
|
||
[drive, users, _, ..] if drive.len() == 2 && drive.ends_with(':') && users.eq_ignore_ascii_case("Users") => 3,
|
||
_ => return None,
|
||
};
|
||
Some(parts[skip..].join("/"))
|
||
}
|
||
|
||
/// A bot's work folder as it is sent to another computer: a folder under a home goes as
|
||
/// `~/…`, so `/Users/daniel/Documents` chosen on a Mac is `~/Documents` on a Linux computer,
|
||
/// whose home is elsewhere. Anything else goes as it is.
|
||
pub fn portable_dir(dir: &str) -> String {
|
||
let dir = dir.trim();
|
||
match under_a_home(dir) {
|
||
Some(rest) if rest.is_empty() => "~".into(),
|
||
Some(rest) => format!("~/{rest}"),
|
||
None => dir.to_string(),
|
||
}
|
||
}
|
||
|
||
/// The folder a tool runs in on this computer, from the work folder another computer sent.
|
||
/// The folder if it is here; else the same place under this computer's home (the folder was
|
||
/// chosen on a computer with another system or another user name); else the home itself — a
|
||
/// folder that isn't here never stops the tools, the way an unset folder doesn't.
|
||
pub fn workdir_here(dir: &str) -> PathBuf {
|
||
let home = dirs::home_dir().unwrap_or_else(std::env::temp_dir);
|
||
workdir_under(dir, &home)
|
||
}
|
||
|
||
/// The same, from a tool's arguments: `workdir_as_chosen` when that very folder is here,
|
||
/// else `workdir` (see workdir_here).
|
||
pub fn workdir_of_args(v: &Value) -> PathBuf {
|
||
let s = |k: &str| v.get(k).and_then(Value::as_str).unwrap_or("").trim().to_string();
|
||
let chosen = PathBuf::from(s("workdir_as_chosen"));
|
||
if chosen.is_absolute() && chosen.is_dir() {
|
||
return chosen;
|
||
}
|
||
workdir_here(&s("workdir"))
|
||
}
|
||
|
||
fn workdir_under(dir: &str, home: &Path) -> PathBuf {
|
||
let dir = dir.trim();
|
||
if dir.is_empty() || dir == "~" {
|
||
return home.to_path_buf();
|
||
}
|
||
let rest = dir.strip_prefix("~/").map(|r| r.to_string()).or_else(|| under_a_home(dir));
|
||
let as_given = PathBuf::from(dir);
|
||
if as_given.is_absolute() && as_given.is_dir() {
|
||
return as_given;
|
||
}
|
||
match rest {
|
||
Some(rest) if home.join(&rest).is_dir() => home.join(rest),
|
||
_ => home.to_path_buf(),
|
||
}
|
||
}
|
||
|
||
/// Make a path absolute and tidy (no `.` or `..`) without touching the disk.
|
||
pub fn resolve(workdir: &Path, p: &str) -> PathBuf {
|
||
let raw = if p.trim().is_empty() { workdir.to_path_buf() } else {
|
||
let e = expand_home(p.trim());
|
||
if e.is_absolute() { e } else { workdir.join(e) }
|
||
};
|
||
let mut out = PathBuf::new();
|
||
for c in raw.components() {
|
||
match c {
|
||
std::path::Component::ParentDir => {
|
||
out.pop();
|
||
}
|
||
std::path::Component::CurDir => {}
|
||
other => out.push(other),
|
||
}
|
||
}
|
||
out
|
||
}
|
||
|
||
fn inside(workdir: &Path, p: &Path) -> bool {
|
||
p.starts_with(workdir)
|
||
}
|
||
|
||
/// Where a path really leads, links followed — also for a file that isn't there yet (its
|
||
/// folder's real place, plus its name) and for a link to something that isn't there yet (what
|
||
/// would be made by writing to it).
|
||
fn real_of(p: &Path) -> PathBuf {
|
||
fn steps(p: &Path, links: usize) -> PathBuf {
|
||
if let Ok(real) = std::fs::canonicalize(p) {
|
||
return real;
|
||
}
|
||
if links < 16 {
|
||
if let Ok(to) = std::fs::read_link(p) {
|
||
let to = if to.is_absolute() { to } else { p.parent().unwrap_or(Path::new("/")).join(to) };
|
||
return steps(&resolve(Path::new("/"), &to.to_string_lossy()), links + 1);
|
||
}
|
||
}
|
||
match (p.parent(), p.file_name()) {
|
||
(Some(parent), Some(name)) if parent != p => steps(parent, links).join(name),
|
||
_ => p.to_path_buf(),
|
||
}
|
||
}
|
||
steps(p, 0)
|
||
}
|
||
|
||
/// The file is in the folder — the real file, in the real folder. How the path is spelled is
|
||
/// not enough: a link inside the folder that points out of it is a way out. (A command the
|
||
/// sandbox keeps inside the folder can still make such a link; reading or editing "through"
|
||
/// it then reached any file of the person's, with no question — from the bot's sealed folder
|
||
/// too.)
|
||
fn within(folder: &Path, p: &Path) -> bool {
|
||
inside(&real_of(folder), &real_of(p))
|
||
}
|
||
|
||
/// Files a bot never reads or writes: secrets and version-control internals
|
||
/// (the original blocks reads/writes by ignore files and repo-block globs),
|
||
/// plus this app's own data folder, where API keys are saved.
|
||
pub fn is_ignored(p: &Path) -> bool {
|
||
if let Some(d) = crate::model::State::path().parent() {
|
||
if p.starts_with(d) {
|
||
return true;
|
||
}
|
||
}
|
||
if let Ok(vd) = std::env::var("ANYBOT_VAULT_DIR").map(PathBuf::from) {
|
||
if !vd.as_os_str().is_empty() && p.starts_with(&vd) {
|
||
return true;
|
||
}
|
||
}
|
||
if let Ok(vk) = std::env::var("ANYBOT_VAULT_KEY_FILE").map(PathBuf::from) {
|
||
if !vk.as_os_str().is_empty() && p == vk {
|
||
return true;
|
||
}
|
||
}
|
||
let s = p.to_string_lossy().replace('\\', "/");
|
||
let name = p.file_name().map(|n| n.to_string_lossy().to_lowercase()).unwrap_or_default();
|
||
s.contains("/.git/")
|
||
|| s.ends_with("/.git")
|
||
|| s.contains("/.ssh/")
|
||
|| s.ends_with("/.ssh")
|
||
|| s.contains("/.gnupg/")
|
||
|| s.ends_with("/.gnupg")
|
||
|| s.contains("/Library/Keychains")
|
||
|| s.contains("/.aws/")
|
||
|| s.ends_with("/.aws")
|
||
|| s.contains("/.config/gcloud/")
|
||
|| s.ends_with("/.config/gcloud")
|
||
|| s.contains("/.kube/")
|
||
|| s.ends_with("/.kube")
|
||
|| s.contains("/.docker/")
|
||
|| s.contains("/.anybot/")
|
||
|| s.ends_with("/.anybot")
|
||
|| s.contains("/.local/share/keyrings/")
|
||
|| s.contains("/Application Support/1Password")
|
||
|| name == ".env"
|
||
|| name.starts_with(".env.")
|
||
|| name.ends_with(".pem")
|
||
|| name.ends_with(".key")
|
||
|| name.starts_with("id_rsa")
|
||
|| name.starts_with("id_ed25519")
|
||
|| name.starts_with("id_ecdsa")
|
||
|| name.starts_with("id_dsa")
|
||
|| matches!(
|
||
name.as_str(),
|
||
".netrc" | "credentials" | "credentials.json" | "credentials.toml" | "application_default_credentials.json" | ".npmrc" | ".pypirc" | ".zsh_history" | ".bash_history" | "vault.bin" | "vault.key"
|
||
)
|
||
}
|
||
|
||
// ---------- permissions ----------
|
||
|
||
#[derive(Debug, PartialEq, Clone)]
|
||
pub enum Decision {
|
||
Allow,
|
||
Ask { reason: String, grant: String },
|
||
Deny(String),
|
||
}
|
||
|
||
/// Commands that only look at things; chains made only of these run without asking.
|
||
const READ_ONLY_COMMANDS: [&str; 24] = [
|
||
"ls", "pwd", "cat", "head", "tail", "wc", "echo", "rg", "grep", "find", "file", "stat", "du", "df", "date", "which", "tree", "sort", "uniq", "diff",
|
||
"git status", "git diff", "git log", "git show",
|
||
];
|
||
|
||
/// Programs that are themselves a command line: opened in a 互動終端機, what the bot types into
|
||
/// them later runs without the rules seeing it, so opening one is asked every time.
|
||
const SHELLS: [&str; 13] = ["sh", "bash", "zsh", "fish", "dash", "ksh", "csh", "tcsh", "pwsh", "powershell", "cmd", "nu", "xonsh"];
|
||
|
||
/// Whether this command (any part of it) starts an interactive shell.
|
||
fn opens_a_shell(cmd: &str) -> bool {
|
||
split_commands(cmd).unwrap_or_else(|| vec![cmd.to_string()]).iter().any(|part| {
|
||
let w = words(part);
|
||
let first = w.first().copied().unwrap_or("");
|
||
let name = first.rsplit(['/', '\\']).next().unwrap_or(first).trim_end_matches(".exe").to_lowercase();
|
||
// `bash -c '…'` / `sh script.sh` run something and end; a bare shell (or -i / -l) stays.
|
||
SHELLS.contains(&name.as_str()) && !w.iter().skip(1).any(|a| *a == "-c" || a.ends_with(".sh") || a.ends_with(".ps1") || *a == "-Command" || *a == "/C" || *a == "/c")
|
||
})
|
||
}
|
||
|
||
/// Always refused.
|
||
const DENIED_COMMANDS: [&str; 10] = ["sudo", "su", "rm -rf /", "rm -rf ~", "mkfs", "dd", "shutdown", "reboot", "diskutil erase", "chmod -R 777 /"];
|
||
|
||
/// One command of a chain. `hidden`: it carries commands the rules can't read ($(…), backticks,
|
||
/// <(…), eval…), so being a "looking" command says nothing about what it runs.
|
||
struct Piece {
|
||
text: String,
|
||
hidden: bool,
|
||
}
|
||
|
||
/// Split `a && b | c; d` where the shell itself would: at the separators outside quotes and
|
||
/// outside $(…) and backticks. None when a quote or a substitution never closes (nothing in
|
||
/// such a command can be read for sure).
|
||
///
|
||
/// A backslash makes the next character plain text, as it does for the shell: `\"` opens no
|
||
/// quote and `\;` ends no command. Without that, `echo \"; rm -rf x; echo \"` read as one
|
||
/// harmless `echo`.
|
||
fn split_shell(cmd: &str) -> Option<Vec<Piece>> {
|
||
#[derive(PartialEq)]
|
||
enum In {
|
||
Double,
|
||
Paren,
|
||
Tick,
|
||
}
|
||
fn close(cur: &mut String, hidden: &mut bool, pieces: &mut Vec<Piece>) {
|
||
let text = cur.trim().to_string();
|
||
if !text.is_empty() {
|
||
let first = text.split_whitespace().next().unwrap_or("");
|
||
let runs_words = matches!(first, "eval" | "exec" | "source" | ".");
|
||
pieces.push(Piece { text, hidden: *hidden || runs_words });
|
||
}
|
||
cur.clear();
|
||
*hidden = false;
|
||
}
|
||
let mut pieces = vec![];
|
||
let mut cur = String::new();
|
||
let mut hidden = false;
|
||
let mut single = false;
|
||
let mut stack: Vec<In> = vec![];
|
||
let chars: Vec<char> = cmd.chars().collect();
|
||
let mut i = 0;
|
||
while i < chars.len() {
|
||
let c = chars[i];
|
||
let next = chars.get(i + 1).copied();
|
||
if single {
|
||
single = c != '\'';
|
||
cur.push(c);
|
||
i += 1;
|
||
continue;
|
||
}
|
||
if c == '\\' {
|
||
cur.push(c);
|
||
if let Some(n) = next {
|
||
cur.push(n);
|
||
i += 1;
|
||
}
|
||
i += 1;
|
||
continue;
|
||
}
|
||
let in_double = stack.last() == Some(&In::Double);
|
||
match c {
|
||
'"' if in_double => {
|
||
stack.pop();
|
||
}
|
||
'"' => stack.push(In::Double),
|
||
'\'' if !in_double => single = true,
|
||
'`' => {
|
||
hidden = true;
|
||
if stack.last() == Some(&In::Tick) {
|
||
stack.pop();
|
||
} else {
|
||
stack.push(In::Tick);
|
||
}
|
||
}
|
||
'$' | '<' | '>' if next == Some('(') && (c == '$' || !in_double) => {
|
||
hidden = true;
|
||
stack.push(In::Paren);
|
||
cur.push(c);
|
||
cur.push('(');
|
||
i += 2;
|
||
continue;
|
||
}
|
||
'(' if stack.last() == Some(&In::Paren) => stack.push(In::Paren),
|
||
')' if stack.last() == Some(&In::Paren) => {
|
||
stack.pop();
|
||
}
|
||
';' | '\n' | '|' | '&' if stack.is_empty() => {
|
||
close(&mut cur, &mut hidden, &mut pieces);
|
||
if (c == '|' || c == '&') && next == Some(c) {
|
||
i += 1;
|
||
}
|
||
i += 1;
|
||
continue;
|
||
}
|
||
_ => {}
|
||
}
|
||
cur.push(c);
|
||
i += 1;
|
||
}
|
||
if single || !stack.is_empty() {
|
||
return None;
|
||
}
|
||
close(&mut cur, &mut hidden, &mut pieces);
|
||
Some(pieces)
|
||
}
|
||
|
||
/// Split `a && b | c; d` into its commands, keeping quoted text together.
|
||
/// Returns None when the command hides other commands ($(), backticks, eval).
|
||
pub fn split_commands(cmd: &str) -> Option<Vec<String>> {
|
||
let pieces = split_shell(cmd)?;
|
||
if pieces.iter().any(|p| p.hidden) {
|
||
return None;
|
||
}
|
||
Some(pieces.into_iter().map(|p| p.text).collect())
|
||
}
|
||
|
||
/// The shell would rewrite this command's words before running it: a variable or a brace list
|
||
/// (`{a,b}`, `{1..3}`) outside single quotes can stand for any word, an option included.
|
||
/// (`HEAD@{1}` is not a list and stays as written.) So can a file name: `*`, `?` or `[` at the
|
||
/// head of a word, or in a word that begins with `-`, becomes whatever is in the folder, and a
|
||
/// file may be called `-delete` or `--pre=./x`. (`src/*.rs` can only become paths under `src/`.)
|
||
fn shell_rewrites(part: &str) -> bool {
|
||
let chars: Vec<char> = part.chars().collect();
|
||
let (mut single, mut double) = (false, false);
|
||
// The first character of the word being read (None between words).
|
||
let mut head: Option<char> = None;
|
||
let mut i = 0;
|
||
while i < chars.len() {
|
||
let c = chars[i];
|
||
if single {
|
||
single = c != '\'';
|
||
} else if c == '\\' {
|
||
head = head.or(Some('\\'));
|
||
i += 1;
|
||
} else if c == '\'' {
|
||
head = head.or(Some(c));
|
||
single = true;
|
||
} else if c == '"' {
|
||
head = head.or(Some(c));
|
||
double = !double;
|
||
} else if c == '$' && chars.get(i + 1).is_some_and(|n| n.is_alphanumeric() || *n == '_' || *n == '{') {
|
||
return true;
|
||
} else if double {
|
||
// (Inside double quotes only `$` is rewritten.)
|
||
} else if c.is_whitespace() {
|
||
head = None;
|
||
} else if c == '{' {
|
||
let inner: String = chars[i + 1..].iter().take_while(|x| **x != '}').collect();
|
||
if inner.contains(',') || inner.contains("..") {
|
||
return true;
|
||
}
|
||
head = head.or(Some(c));
|
||
} else if matches!(c, '*' | '?' | '[') {
|
||
if head.is_none() || head == Some('-') {
|
||
return true;
|
||
}
|
||
} else {
|
||
head = head.or(Some(c));
|
||
}
|
||
i += 1;
|
||
}
|
||
false
|
||
}
|
||
|
||
/// A looking command (READ_ONLY_COMMANDS) stays one only without the options that make it
|
||
/// write, delete or run something else: `find -delete`, `find -exec`, `sort -o`, `rg --pre`…
|
||
/// These went through without a question because only the first word was read.
|
||
///
|
||
/// Quotes and backslashes are dropped before matching, as the shell drops them (`-del"ete"`),
|
||
/// and a command whose words the shell would rewrite asks instead of being guessed at.
|
||
fn read_only(part: &str) -> bool {
|
||
if !READ_ONLY_COMMANDS.iter().any(|r| starts_with_words(part, r)) {
|
||
return false;
|
||
}
|
||
let w: Vec<String> = words(part).iter().map(|a| a.replace(['\'', '"', '\\'], "")).collect();
|
||
let long = |names: &[&str]| w.iter().any(|a| names.iter().any(|n| a == n || a.strip_prefix(n).is_some_and(|r| r.starts_with('='))));
|
||
let short = |c: char| w.iter().skip(1).any(|a| a.starts_with('-') && !a.starts_with("--") && a.contains(c));
|
||
let changes = match w.first().map(String::as_str).unwrap_or("") {
|
||
"find" => w.iter().any(|a| matches!(a.as_str(), "-exec" | "-execdir" | "-ok" | "-okdir" | "-delete" | "-fprint" | "-fprint0" | "-fprintf" | "-fls")),
|
||
"sort" => short('o') || long(&["--output", "--compress-program"]),
|
||
"tree" => short('o'),
|
||
"rg" => long(&["--pre", "--hostname-bin"]),
|
||
"git" => long(&["--output"]),
|
||
// `uniq in out` writes its second file.
|
||
// (`-` alone is a file too: standard input.)
|
||
"uniq" => w.iter().skip(1).filter(|a| *a == "-" || !a.starts_with('-')).count() > 1,
|
||
"file" => short('C') || long(&["--compile"]),
|
||
// The rest have no such option: nothing to hide behind a variable.
|
||
_ => return true,
|
||
};
|
||
!changes && !shell_rewrites(part)
|
||
}
|
||
|
||
/// A looking command whose options can make it write, delete or run something (the ones
|
||
/// `read_only` reads the options of).
|
||
fn has_changing_options(part: &str) -> bool {
|
||
READ_ONLY_COMMANDS.iter().any(|r| starts_with_words(part, r)) && matches!(words(part).first().copied().unwrap_or(""), "find" | "sort" | "tree" | "rg" | "git" | "uniq" | "file")
|
||
}
|
||
|
||
fn words(cmd: &str) -> Vec<&str> {
|
||
cmd.split_whitespace().collect()
|
||
}
|
||
|
||
fn starts_with_words(cmd: &str, prefix: &str) -> bool {
|
||
let (c, p) = (words(cmd), words(prefix));
|
||
!p.is_empty() && c.len() >= p.len() && c[..p.len()] == p[..]
|
||
}
|
||
|
||
/// The denied command hiding inside a wrapper: `(sudo …)`, `FOO=1 sudo …`, `env sudo …`,
|
||
/// `nohup sudo …` all run the same thing, so peel the wrappings off before matching.
|
||
fn denied_hit(part: &str) -> Option<&'static str> {
|
||
let mut toks: Vec<String> = Vec::new();
|
||
for w in words(part) {
|
||
let mut w = w;
|
||
while matches!(w.as_bytes().first(), Some(b'(') | Some(b'{')) {
|
||
w = &w[1..];
|
||
}
|
||
if w.is_empty() {
|
||
continue;
|
||
}
|
||
// VAR=val prefixes set the environment; they are not the command.
|
||
let is_assign = w.split_once('=').map(|(k, v)| !k.is_empty() && v.len() + 1 < w.len() && k.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')).unwrap_or(false);
|
||
if toks.is_empty() && is_assign {
|
||
continue;
|
||
}
|
||
toks.push(w.to_string());
|
||
}
|
||
const WRAP: [&str; 7] = ["env", "command", "nohup", "time", "nice", "stdbuf", "timeout"];
|
||
while toks.first().map(|t| WRAP.contains(&t.as_str())).unwrap_or(false) {
|
||
toks.remove(0);
|
||
while toks.first().map(|t| t.starts_with('-')).unwrap_or(false) {
|
||
toks.remove(0);
|
||
}
|
||
// timeout takes a duration before the command.
|
||
if toks.first().map(|t| !t.is_empty() && t.chars().next().map(|c| c.is_ascii_digit()).unwrap_or(false)).unwrap_or(false) {
|
||
toks.remove(0);
|
||
}
|
||
}
|
||
let joined = toks.join(" ");
|
||
DENIED_COMMANDS.iter().find(|d| starts_with_words(&joined, d)).copied()
|
||
}
|
||
|
||
/// The "always allow" grant for a command: its first word, or first two for git/npm/cargo-style tools.
|
||
pub fn shell_grant(part: &str) -> String {
|
||
let w = words(part);
|
||
let two = ["git", "npm", "pnpm", "yarn", "cargo", "brew", "pip", "pip3", "python3", "docker", "go"];
|
||
let key = if w.len() >= 2 && two.contains(&w[0]) { format!("{} {}", w[0], w[1]) } else { w.first().map(|s| s.to_string()).unwrap_or_default() };
|
||
format!("shell:{key}")
|
||
}
|
||
|
||
fn writes_files(part: &str) -> bool {
|
||
part.contains('>')
|
||
}
|
||
|
||
pub fn decide(name: &str, args: &str, workdir: &Path, grants: &[String]) -> Decision {
|
||
let v = args_of(args);
|
||
let granted = |g: &str| grants.iter().any(|x| x == g);
|
||
match name {
|
||
"list_dir" | "search" | "read_file" => {
|
||
let p = resolve(workdir, &arg_str(&v, "path"));
|
||
if is_ignored(&p) || is_ignored(&real_of(&p)) {
|
||
return Decision::Deny(format!("「{}」是私密或系統檔案,Bot 不能讀取。", p.display()));
|
||
}
|
||
if within(workdir, &p) || granted("read:anywhere") {
|
||
Decision::Allow
|
||
} else {
|
||
Decision::Ask { reason: format!("讀取工作資料夾以外的地方:{}", p.display()), grant: "read:anywhere".into() }
|
||
}
|
||
}
|
||
"read_terminal" | "write_terminal" => Decision::Allow,
|
||
"edit_file" => {
|
||
let p = resolve(workdir, &arg_str(&v, "path"));
|
||
if is_ignored(&p) || is_ignored(&real_of(&p)) {
|
||
return Decision::Deny(format!("「{}」是私密或系統檔案,Bot 不能修改。", p.display()));
|
||
}
|
||
if !within(workdir, &p) {
|
||
return Decision::Ask { reason: format!("修改工作資料夾以外的檔案:{}", p.display()), grant: String::new() };
|
||
}
|
||
if granted("edit_file") {
|
||
Decision::Allow
|
||
} else {
|
||
Decision::Ask { reason: format!("修改檔案:{}", p.display()), grant: "edit_file".into() }
|
||
}
|
||
}
|
||
"shell" => {
|
||
let cmd = arg_str(&v, "command");
|
||
if cmd.trim().is_empty() {
|
||
return Decision::Deny("沒有指令".into());
|
||
}
|
||
if interactive_of(&v) && opens_a_shell(&cmd) {
|
||
return Decision::Ask { reason: format!("開一個互動的 {}:之後 Bot 在裡面打的指令不會再一條一條問你。指令:{cmd}", words(&cmd).first().copied().unwrap_or("shell")), grant: String::new() };
|
||
}
|
||
let Some(pieces) = split_shell(&cmd) else {
|
||
// A quote or a $(…) that never closes: where one command ends and the next
|
||
// begins can't be told, so no grant covers it and none is offered.
|
||
if let Some(d) = denied_hit(&cmd) {
|
||
return Decision::Deny(format!("「{d}」這類指令一律不允許。"));
|
||
}
|
||
return Decision::Ask { reason: format!("執行指令(引號或括號沒有成對,無法自動判斷):{cmd}"), grant: String::new() };
|
||
};
|
||
// deny
|
||
for piece in &pieces {
|
||
if let Some(d) = denied_hit(&piece.text) {
|
||
return Decision::Deny(format!("「{d}」這類指令一律不允許。"));
|
||
}
|
||
}
|
||
// (No word-by-word path scan here: secret folders are the sandbox's job, and
|
||
// scanning words as paths denied perfectly ordinary commands.)
|
||
// allow: every part is read-only or already granted. A part that hides other
|
||
// commands ($(), backticks, eval…) is never read-only: it asks once and is
|
||
// remembered by its first word, so 「一律允許」 on `git commit` covers
|
||
// `git commit -m "$(…)"` afterwards (models use $(…) constantly) — but only that
|
||
// part: the grant used to let everything chained after it through as well.
|
||
//
|
||
// A looking command that may do more than look (`find … -delete`, `sort -o`, or
|
||
// one whose words can't be read) is asked each time: 「一律允許」 on its first word
|
||
// would be every `find -exec` from then on, on a word nobody thinks of as dangerous.
|
||
let each_time = |p: &Piece| has_changing_options(&p.text) && (p.hidden || !read_only(&p.text));
|
||
let ok = |p: &Piece| !writes_files(&p.text) && ((!each_time(p) && granted(&shell_grant(&p.text))) || (!p.hidden && read_only(&p.text)));
|
||
let unapproved: Vec<&Piece> = pieces.iter().filter(|p| !ok(p)).collect();
|
||
if unapproved.is_empty() {
|
||
return Decision::Allow;
|
||
}
|
||
let grant = if unapproved.len() == 1 && !writes_files(&unapproved[0].text) && !each_time(unapproved[0]) { shell_grant(&unapproved[0].text) } else { String::new() };
|
||
let note = if pieces.iter().any(|p| p.hidden) { "(含有隱藏的子指令,無法自動判斷)" } else { "" };
|
||
Decision::Ask { reason: format!("執行指令{note}:{cmd}"), grant }
|
||
}
|
||
other => Decision::Deny(format!("沒有「{other}」這個工具")),
|
||
}
|
||
}
|
||
|
||
// ---------- process trees ----------
|
||
// From (the original's native process scanner):
|
||
// getDescendantPids: breadth-first over children by parent pid (proc_listpids PROC_PPID_ONLY),
|
||
// de-duplicated, stops once 0xfff (4095) pids are collected → at most 4096 processes.
|
||
//: scans the roots plus all descendants.
|
||
// getProcessInformation: memoryMB = resident bytes >> 20 (lsr #0x14);
|
||
// cpuTimeMs = (user + system time, in Mach ticks × timebase) / 1 000 000;
|
||
// each process is tied to its agent by CONVERSATION_ID= / REQUEST_ID= in its environment.
|
||
|
||
pub const MAX_TRACKED_PROCESSES: usize = 4096;
|
||
/// Our own names for the tags the original reads (it uses its vendor prefix).
|
||
pub const ENV_CONVERSATION_ID: &str = "ANYBOT_CONVERSATION_ID";
|
||
pub const ENV_REQUEST_ID: &str = "ANYBOT_REQUEST_ID";
|
||
|
||
#[derive(Clone, Copy, Debug)]
|
||
pub struct ProcRow {
|
||
pub pid: u32,
|
||
pub ppid: u32,
|
||
pub rss_kb: u64,
|
||
pub cpu_ms: u64,
|
||
}
|
||
|
||
/// `[[dd-]hh:]mm:ss[.cc]` as printed by ps, in milliseconds.
|
||
pub fn parse_cpu_time(s: &str) -> u64 {
|
||
let (days, rest) = match s.split_once('-') {
|
||
Some((d, r)) => (d.parse::<u64>().unwrap_or(0), r),
|
||
None => (0, s),
|
||
};
|
||
let mut secs = 0f64;
|
||
for part in rest.split(':') {
|
||
secs = secs * 60.0 + part.parse::<f64>().unwrap_or(0.0);
|
||
}
|
||
days * 86_400_000 + (secs * 1000.0).round() as u64
|
||
}
|
||
|
||
pub fn proc_table() -> Vec<ProcRow> {
|
||
let Ok(out) = Command::new("ps").args(["-A", "-o", "pid=,ppid=,rss=,time="]).output() else { return vec![] };
|
||
String::from_utf8_lossy(&out.stdout)
|
||
.lines()
|
||
.filter_map(|l| {
|
||
let f: Vec<&str> = l.split_whitespace().collect();
|
||
(f.len() >= 4).then(|| ProcRow { pid: f[0].parse().unwrap_or(0), ppid: f[1].parse().unwrap_or(0), rss_kb: f[2].parse().unwrap_or(0), cpu_ms: parse_cpu_time(f[3]) })
|
||
})
|
||
.filter(|r| r.pid > 0)
|
||
.collect()
|
||
}
|
||
|
||
/// The roots and everything they started, breadth-first, without repeats, at most 4096.
|
||
pub fn descendants(table: &[ProcRow], roots: &[u32]) -> Vec<u32> {
|
||
let mut seen = std::collections::HashSet::new();
|
||
let mut out = vec![];
|
||
let mut queue: std::collections::VecDeque<u32> = roots.iter().copied().filter(|p| *p > 0).collect();
|
||
while let Some(p) = queue.pop_front() {
|
||
if !seen.insert(p) {
|
||
continue;
|
||
}
|
||
out.push(p);
|
||
if out.len() >= MAX_TRACKED_PROCESSES {
|
||
break;
|
||
}
|
||
queue.extend(table.iter().filter(|r| r.ppid == p).map(|r| r.pid));
|
||
}
|
||
out
|
||
}
|
||
|
||
/// (processes, cpu ms, memory MB) for a command and everything it started.
|
||
pub fn tree_stats(root: u32) -> (usize, u64, u64) {
|
||
let table = proc_table();
|
||
let pids = descendants(&table, &[root]);
|
||
let rows: Vec<&ProcRow> = table.iter().filter(|r| pids.contains(&r.pid)).collect();
|
||
(rows.len(), rows.iter().map(|r| r.cpu_ms).sum(), rows.iter().map(|r| (r.rss_kb * 1024) >> 20).sum())
|
||
}
|
||
|
||
/// Stop a command and everything it started (newest first), not just the shell.
|
||
pub fn kill_tree(root: u32) {
|
||
let table = proc_table();
|
||
let mut pids = descendants(&table, &[root]);
|
||
pids.reverse();
|
||
if pids.is_empty() {
|
||
return;
|
||
}
|
||
let _ = Command::new("kill").arg("-TERM").args(pids.iter().map(|p| p.to_string())).stderr(Stdio::null()).status();
|
||
// What ignores a polite stop is ended two seconds later (it used to stay behind for ever).
|
||
std::thread::spawn(move || {
|
||
std::thread::sleep(Duration::from_secs(2));
|
||
let left: Vec<String> = proc_table().iter().filter(|r| pids.contains(&r.pid)).map(|r| r.pid.to_string()).collect();
|
||
if !left.is_empty() {
|
||
let _ = Command::new("kill").arg("-KILL").args(left).stderr(Stdio::null()).status();
|
||
}
|
||
});
|
||
}
|
||
|
||
// ---------- sandbox ----------
|
||
// From sand-op-launcher `_main`: before exec it applies a sandbox profile
|
||
// "(version 1)(allow default)(deny file-read* (with no-report) (subpath (param …))))" that hides
|
||
// one private folder, builds a fresh environment, and refuses invocations outside an exact list.
|
||
// Here: commands can't read or write the app's own data (where API keys are saved) or the
|
||
// usual secret folders, and API keys are removed from their environment.
|
||
|
||
pub(crate) const KEY_ENV_VARS: [&str; 7] = ["XAI_API_KEY", "OPENAI_API_KEY", "ANTHROPIC_API_KEY", "GEMINI_API_KEY", "OPENROUTER_API_KEY", "ANTHROPIC_AUTH_TOKEN", "ANYBOT_ACCOUNT_PASSWORD"];
|
||
pub(crate) const DANGEROUS_ENV_VARS: [&str; 8] = ["DYLD_INSERT_LIBRARIES", "DYLD_LIBRARY_PATH", "DYLD_FRAMEWORK_PATH", "LD_PRELOAD", "LD_LIBRARY_PATH", "BASH_ENV", "ENV", "PROMPT_COMMAND"];
|
||
|
||
pub fn protected_dirs() -> Vec<PathBuf> {
|
||
let mut v = vec![];
|
||
if let Some(d) = crate::model::State::path().parent() {
|
||
v.push(d.to_path_buf());
|
||
}
|
||
if let Ok(vd) = std::env::var("ANYBOT_VAULT_DIR").map(PathBuf::from) {
|
||
if !vd.as_os_str().is_empty() {
|
||
v.push(vd);
|
||
}
|
||
}
|
||
if let Ok(vk) = std::env::var("ANYBOT_VAULT_KEY_FILE").map(PathBuf::from) {
|
||
if !vk.as_os_str().is_empty() {
|
||
v.push(vk);
|
||
}
|
||
}
|
||
if let Some(h) = dirs::home_dir() {
|
||
for s in [".ssh", ".gnupg", "Library/Keychains", ".aws", ".config/gcloud", ".kube", ".docker", ".anybot", "Library/Application Support/1Password"] {
|
||
v.push(h.join(s));
|
||
}
|
||
}
|
||
v
|
||
}
|
||
|
||
/// Files and folders no sandboxed command may write to (shell startup files, LaunchAgents, git hooks),
|
||
/// preventing silent persistence outside the sandbox.
|
||
fn write_denied_paths(workdir: &Path) -> Vec<PathBuf> {
|
||
let mut v = vec![workdir.join(".git/hooks"), workdir.join(".git/config")];
|
||
if let Some(h) = dirs::home_dir() {
|
||
for s in [".zshrc", ".zshenv", ".zprofile", ".zlogin", ".bashrc", ".bash_profile", ".bash_login", ".bash_logout", ".profile", "Library/LaunchAgents", ".config/autostart", ".config/systemd", ".xprofile", ".xsessionrc", ".pam_environment"] {
|
||
v.push(h.join(s));
|
||
}
|
||
}
|
||
v
|
||
}
|
||
|
||
/// Inside the protected folders, what commands may still read: the chat's attachments (a picture
|
||
/// the person sent is the bot's to look at; the keys next to it are not), installed skill packages
|
||
/// (`<data>/skills/<slug>/scripts/...`, read-only), and the ssh keys, so git and ssh can reach
|
||
/// remote repos (writing them stays denied).
|
||
pub fn readable_dirs() -> Vec<PathBuf> {
|
||
let mut v: Vec<PathBuf> = crate::model::State::path()
|
||
.parent()
|
||
.map(|d| vec![d.join("attachments"), d.join("skills")])
|
||
.unwrap_or_default();
|
||
if let Some(h) = dirs::home_dir() {
|
||
v.push(h.join(".ssh"));
|
||
}
|
||
v
|
||
}
|
||
|
||
fn sandbox_exec() -> Option<&'static str> {
|
||
let p = "/usr/bin/sandbox-exec";
|
||
(cfg!(target_os = "macos") && Path::new(p).exists()).then_some(p)
|
||
}
|
||
|
||
/// Can the bot's own computer run commands without Docker? Only inside a sandbox (macOS):
|
||
/// elsewhere a command there would run on this computer itself, as AnyBot, keys and all.
|
||
pub fn sealed_runs_commands() -> bool {
|
||
sandbox_exec().is_some()
|
||
}
|
||
|
||
/// Why the bot's own computer can't run a command right now.
|
||
pub const NO_BOX_FOR_COMMANDS: &str = "你自己的電腦現在不能執行指令:AnyBot 所在的這台沒有 Docker(或連不到 Docker),沒有隔離的地方可以跑指令。請告訴使用者把 Docker 打開,或請他指名別台電腦來做。";
|
||
|
||
fn real(p: &Path) -> String {
|
||
// The sandbox matches real paths, so resolve links like /var → /private/var.
|
||
std::fs::canonicalize(p).unwrap_or_else(|_| p.to_path_buf()).display().to_string()
|
||
}
|
||
|
||
/// The shell command, wrapped in the sandbox when the system has one.
|
||
#[cfg(test)]
|
||
pub fn build_command(cmd: &str, workdir: &Path, protected: &[PathBuf], conversation: &str, request: &str) -> Command {
|
||
build_command_sealed(cmd, workdir, protected, &[], None, conversation, request)
|
||
}
|
||
|
||
/// Characters that join, redirect or substitute commands: a command with any of them is not one
|
||
/// plain program call.
|
||
const SHELL_JOINERS: [&str; 9] = [";", "&", "|", "<", ">", "`", "$(", "\n", "\\"];
|
||
|
||
/// The program a plain one-program command runs, quotes stripped ("" when it isn't one).
|
||
fn plain_program(cmd: &str) -> Option<String> {
|
||
let c = cmd.trim();
|
||
if c.is_empty() || SHELL_JOINERS.iter().any(|j| c.contains(j)) {
|
||
return None;
|
||
}
|
||
let first = if let Some(rest) = c.strip_prefix('"') {
|
||
rest.split('"').next()?.to_string()
|
||
} else if let Some(rest) = c.strip_prefix('\'') {
|
||
rest.split('\'').next()?.to_string()
|
||
} else {
|
||
c.split_whitespace().next()?.to_string()
|
||
};
|
||
Some(first)
|
||
}
|
||
|
||
/// An App Store app's own command line (`/Applications/X.app/Contents/MacOS/Y`): it carries
|
||
/// Apple's app sandbox, so outside AnyBot's sandbox it still can't read the secret folders —
|
||
/// and inside it, the two sandboxes clash and the program dies at once (Tailscale's CLI:
|
||
/// "Trace/BPT trap: 5"). Checked once per program with codesign.
|
||
fn app_sandboxed_cli(program: &str) -> bool {
|
||
let p = Path::new(program);
|
||
let in_app = p.is_absolute()
|
||
&& !program.contains("..")
|
||
&& p.components().count() >= 6
|
||
&& program.starts_with("/Applications/")
|
||
&& program.split('/').nth(2).map(|a| a.ends_with(".app")).unwrap_or(false)
|
||
&& program.contains(".app/Contents/MacOS/");
|
||
if !in_app || !p.is_file() {
|
||
return false;
|
||
}
|
||
static SEEN: OnceLock<Mutex<HashMap<String, bool>>> = OnceLock::new();
|
||
let seen = SEEN.get_or_init(|| Mutex::new(HashMap::new()));
|
||
if let Some(v) = seen.lock().unwrap_or_else(|e| e.into_inner()).get(program) {
|
||
return *v;
|
||
}
|
||
let ok = Command::new("/usr/bin/codesign")
|
||
.args(["-d", "--entitlements", "-", "--xml", program])
|
||
.stderr(Stdio::null())
|
||
.output()
|
||
.map(|o| {
|
||
let x = String::from_utf8_lossy(&o.stdout).replace(['\n', '\t', ' '], "");
|
||
x.contains("<key>com.apple.security.app-sandbox</key><true/>")
|
||
&& !x.contains("temporary-exception.files.home-relative-path")
|
||
&& !x.contains("temporary-exception.files.absolute-path.read-write")
|
||
&& !x.contains("temporary-exception.apple-events")
|
||
})
|
||
.unwrap_or(false);
|
||
seen.lock().unwrap_or_else(|e| e.into_inner()).insert(program.to_string(), ok);
|
||
ok
|
||
}
|
||
|
||
/// `ps` / `top`, alone or piped into plain text filters that take no file: they need system
|
||
/// rights (setuid), which no sandboxed process gets ("/bin/ps: Operation not permitted"), and
|
||
/// they read no files.
|
||
fn process_listing(cmd: &str) -> bool {
|
||
let c = cmd.trim();
|
||
if [";", "&", "<", ">", "`", "$(", "\n", "\\", "~"].iter().any(|j| c.contains(j)) {
|
||
return false;
|
||
}
|
||
let mut parts = c.split('|').map(str::trim);
|
||
let Some(first) = parts.next() else { return false };
|
||
let mut w = first.split_whitespace();
|
||
let prog = w.next().unwrap_or("");
|
||
if !matches!(prog, "ps" | "/bin/ps" | "top" | "/usr/bin/top") || w.any(|a| a.contains('/')) {
|
||
return false;
|
||
}
|
||
parts.all(|p| {
|
||
if p.contains('/') {
|
||
return false;
|
||
}
|
||
match p.split_whitespace().next().unwrap_or("") {
|
||
"egrep" | "cut" => true,
|
||
"awk" => awk_only_prints(p),
|
||
// (`sort -o`, `sort --compress-program`, `uniq in out`: not filters any more.)
|
||
"grep" | "head" | "tail" | "wc" | "sort" | "uniq" => read_only(p),
|
||
_ => false,
|
||
}
|
||
})
|
||
}
|
||
|
||
/// `awk [-F sep] [-v name=value] 'program'` whose program starts nothing: awk can run any
|
||
/// command itself (`system("…")`, `"cmd" | getline`, `print | "cmd"`), read a program from a
|
||
/// file (`-f`) and load code (`@include`, `@load`) — here that would be outside the sandbox.
|
||
fn awk_only_prints(part: &str) -> bool {
|
||
let Some(w) = plain_words(part) else { return false };
|
||
let mut rest = w.iter().skip(1).map(String::as_str).peekable();
|
||
while let Some(a) = rest.peek().copied() {
|
||
match a {
|
||
"-F" | "-v" => {
|
||
rest.next();
|
||
if rest.next().is_none() {
|
||
return false;
|
||
}
|
||
}
|
||
_ if a.starts_with("-F") || a.starts_with("-v") => {
|
||
rest.next();
|
||
}
|
||
_ => break,
|
||
}
|
||
}
|
||
let (Some(program), None) = (rest.next(), rest.next()) else { return false };
|
||
!program.starts_with('-') && !["system", "getline", "|", "@", "fflush", "close"].iter().any(|k| program.contains(k))
|
||
}
|
||
|
||
/// A command's words the way the shell reads them, or None when it is more than one plain
|
||
/// program call: anything outside quotes that joins, redirects, pipes, backgrounds or
|
||
/// substitutes (`;` `&` `|` `<` `>` `(` `)` a backtick, `$(`, a line break), and substitution
|
||
/// inside double quotes. Quotes, backslashes and `&` inside an AppleScript string are fine.
|
||
pub fn plain_words(cmd: &str) -> Option<Vec<String>> {
|
||
let mut words = vec![];
|
||
let mut cur = String::new();
|
||
let mut started = false;
|
||
let mut it = cmd.trim().chars().peekable();
|
||
while let Some(c) = it.next() {
|
||
match c {
|
||
'\'' => {
|
||
started = true;
|
||
loop {
|
||
match it.next()? {
|
||
'\'' => break,
|
||
ch => cur.push(ch),
|
||
}
|
||
}
|
||
}
|
||
'"' => {
|
||
started = true;
|
||
loop {
|
||
match it.next()? {
|
||
'"' => break,
|
||
'\\' => match it.next()? {
|
||
e @ ('"' | '\\' | '$' | '`') => cur.push(e),
|
||
'\n' => {}
|
||
e => {
|
||
cur.push('\\');
|
||
cur.push(e);
|
||
}
|
||
},
|
||
'`' => return None,
|
||
'$' if it.peek() == Some(&'(') => return None,
|
||
ch => cur.push(ch),
|
||
}
|
||
}
|
||
}
|
||
'\\' => {
|
||
started = true;
|
||
match it.next()? {
|
||
'\n' => {}
|
||
e => cur.push(e),
|
||
}
|
||
}
|
||
';' | '&' | '|' | '<' | '>' | '(' | ')' | '`' | '\n' => return None,
|
||
'$' if it.peek() == Some(&'(') => return None,
|
||
c if c.is_whitespace() => {
|
||
if started {
|
||
words.push(std::mem::take(&mut cur));
|
||
started = false;
|
||
}
|
||
}
|
||
c => {
|
||
started = true;
|
||
cur.push(c);
|
||
}
|
||
}
|
||
}
|
||
if started {
|
||
words.push(cur);
|
||
}
|
||
(!words.is_empty()).then_some(words)
|
||
}
|
||
|
||
/// What an AppleScript run outside the sandbox may not do: run shell commands or other
|
||
/// scripts, drive a terminal or type keystrokes (into one), read files or reach the Objective-C
|
||
/// bridge, or name the secret folders. Outside the sandbox nothing else stops it, so this list
|
||
/// errs on the side of refusing; refused scripts still run, inside the sandbox.
|
||
const OSASCRIPT_OUTSIDE_REFUSED: [&str; 42] = [
|
||
"do shell script", "run script", "load script", "store script", "do script", "terminal", "iterm", "warp", "ghostty", "keystroke", "key code",
|
||
"objc", "$.ns", "nsstring", "nsdata", "nsfilemanager", "read file", "read posix file", "open for access", ".ssh", "keychain", ".gnupg", ".aws", "gcloud",
|
||
"«", "»", "shortcuts", "automator", "script editor", "osascript", "finder", "anybot", "posix", "open location", "clipboard", "application id", "bundle identifier",
|
||
"write ", "delete ", "remove ", "make new", "click",
|
||
];
|
||
|
||
/// A plain `osascript -e …` call that may run outside the sandbox (Apple events to other apps
|
||
/// only work there: inside, every app answers -10004), and why not when it may not.
|
||
pub fn osascript_outside(cmd: &str) -> Result<(), String> {
|
||
let words = plain_words(cmd).ok_or("不是單獨一條 osascript 指令")?;
|
||
if !matches!(words[0].as_str(), "osascript" | "/usr/bin/osascript") {
|
||
return Err("不是 osascript".into());
|
||
}
|
||
// Only inline AppleScript (-e …): no script files (unseen code), no JavaScript (its ObjC
|
||
// bridge reads anything), no interactive mode.
|
||
let mut script = String::new();
|
||
let mut i = 1;
|
||
while i < words.len() {
|
||
match words[i].as_str() {
|
||
"-e" if i + 1 < words.len() => {
|
||
script.push_str(&words[i + 1]);
|
||
script.push('\n');
|
||
i += 2;
|
||
}
|
||
"-s" if i + 1 < words.len() => i += 2,
|
||
w if w.starts_with("-s") => i += 1,
|
||
_ => return Err("只能用 -e 寫 AppleScript(不能跑腳本檔、JavaScript 或互動模式)".into()),
|
||
}
|
||
}
|
||
if script.trim().is_empty() {
|
||
return Err("沒有 -e 的 AppleScript".into());
|
||
}
|
||
let low = script.to_lowercase();
|
||
// Collapse AppleScript string concatenation (`"ca" & "t ~/.s" & "sh"`) so split keywords are caught too.
|
||
let joined = low.replace("\" & \"", "").replace("\"&\"", "").replace("' & '", "").replace("'&'", "");
|
||
let home = dirs::home_dir().map(|h| h.display().to_string().to_lowercase()).unwrap_or_default();
|
||
let data = crate::model::State::path().parent().map(|d| d.display().to_string().to_lowercase()).unwrap_or_default();
|
||
if let Some(bad) = OSASCRIPT_OUTSIDE_REFUSED.iter().find(|b| low.contains(*b) || joined.contains(*b)) {
|
||
return Err(format!("用到了「{bad}」"));
|
||
}
|
||
for check in [&low, &joined] {
|
||
if (!data.is_empty() && check.contains(&data)) || check.contains("application support/anybot") || (!home.is_empty() && check.contains(&format!("{home}/library/keychains"))) {
|
||
return Err("碰到 AnyBot 的資料夾或鑰匙圈".into());
|
||
}
|
||
}
|
||
Ok(())
|
||
}
|
||
|
||
/// Commands that run outside AnyBot's sandbox on the person's Mac, because the sandbox breaks
|
||
/// them and they can't reach the secret folders anyway (see app_sandboxed_cli, process_listing),
|
||
/// or because the person allowed AppleScript to drive their apps (osascript_outside; asked first).
|
||
pub fn runs_outside_sandbox(cmd: &str) -> bool {
|
||
if !cfg!(target_os = "macos") {
|
||
return false;
|
||
}
|
||
process_listing(cmd) || osascript_outside(cmd).is_ok() || plain_program(cmd).map(|p| app_sandboxed_cli(&p)).unwrap_or(false)
|
||
}
|
||
|
||
/// The grant that lets AppleScript run outside the sandbox without asking again.
|
||
pub const OSASCRIPT_GRANT: &str = "osascript:outside";
|
||
|
||
/// A word for the bot when a command failed because of the sandbox, not because the program or
|
||
/// the computer is broken — so it changes course instead of trying the same thing again.
|
||
pub fn sandbox_note(cmd: &str, out: &str) -> Option<&'static str> {
|
||
if crate::landlock::active() {
|
||
return (out.contains("Permission denied") || out.contains("Operation not permitted")).then_some(crate::landlock::NOTE);
|
||
}
|
||
sandbox_exec()?;
|
||
if runs_outside_sandbox(cmd) {
|
||
return None;
|
||
}
|
||
let setuid = ["/bin/ps: Operation not permitted", "/usr/bin/top: Operation not permitted", "sudo: Operation not permitted", "/usr/bin/sudo: Operation not permitted"];
|
||
if setuid.iter().any(|s| out.contains(s)) {
|
||
return Some("(這是 AnyBot 的指令沙盒擋下的,不是電腦壞了:ps、top、sudo 這類要系統權限的程式在沙盒裡不能跑。查程序改用 pgrep -fl 名字,或把 ps/top 單獨執行(後面只接 grep、head 這類過濾),那樣會在沙盒外跑。)");
|
||
}
|
||
if out.contains("(-10004)") || out.contains("越權取用") || out.to_lowercase().contains("privilege violation") {
|
||
return Some(match osascript_outside(cmd) {
|
||
// Plain osascript that still got -10004 can't be the sandbox: macOS itself said no.
|
||
Ok(()) => return None,
|
||
Err(_) if cmd.contains("osascript") => "(這是 AnyBot 的指令沙盒擋下的,不是那個 App 壞了:AppleScript 只有寫成單獨一條 osascript -e '…' 指令(不接其他指令、不用 heredoc,多行就多個 -e),而且不跑 shell 指令、不碰終端機、不讀檔案時,才會在沙盒外跑、能指揮別的 App。改寫成那樣,或改用電腦操作(computer 工具的 action,如 click、typing)。)",
|
||
Err(_) => "(這是 AnyBot 的指令沙盒擋下的,不是那個 App 壞了:在沙盒裡不能指揮別的 App(-10004)。改用電腦操作(computer 工具的 action),或寫成單獨一條 osascript -e '…' 指令。)",
|
||
});
|
||
}
|
||
if (out.contains("Trace/BPT trap") || out.contains("指令因為收到訊號而終止")) && cmd.contains(".app/Contents/MacOS/") {
|
||
return Some("(這個 App 的指令列在 AnyBot 的沙盒裡會當掉,不是 App 壞了,也不是參數的問題。用它的完整路徑單獨執行:不要接 ;、|、&&,也不要用 >、< 把輸出存到檔案(要處理輸出就先單獨執行,再用下一條指令處理),App Store 版的 App 就會在沙盒外跑;不行就改用畫面操作。)");
|
||
}
|
||
None
|
||
}
|
||
|
||
|
||
/// The shell a command runs in: sh on Mac and Linux. On Windows (no /bin/sh there — every
|
||
/// command failed to start) Git Bash when it is installed, since bots write sh; else PowerShell.
|
||
fn shell_of(cmd: &str) -> Command {
|
||
#[cfg(windows)]
|
||
{
|
||
use std::os::windows::process::CommandExt;
|
||
// (No console window flashing up for each command.)
|
||
const CREATE_NO_WINDOW: u32 = 0x0800_0000;
|
||
let mut c = match git_bash() {
|
||
Some(bash) => {
|
||
let mut c = Command::new(bash);
|
||
c.arg("-c").arg(cmd);
|
||
c
|
||
}
|
||
None => {
|
||
let mut c = Command::new("powershell.exe");
|
||
c.args(["-NoLogo", "-NoProfile", "-ExecutionPolicy", "Bypass", "-Command", cmd]);
|
||
c
|
||
}
|
||
};
|
||
c.creation_flags(CREATE_NO_WINDOW);
|
||
c
|
||
}
|
||
#[cfg(not(windows))]
|
||
{
|
||
let mut c = Command::new("/bin/sh");
|
||
c.arg("-c").arg(cmd);
|
||
c
|
||
}
|
||
}
|
||
|
||
/// Git for Windows' bash (not System32's, which is WSL's).
|
||
#[cfg(windows)]
|
||
fn git_bash() -> Option<PathBuf> {
|
||
let env = |k: &str| std::env::var_os(k).map(PathBuf::from);
|
||
[env("ProgramFiles"), env("ProgramW6432"), env("ProgramFiles(x86)"), env("LOCALAPPDATA").map(|l| l.join("Programs"))]
|
||
.into_iter()
|
||
.flatten()
|
||
.map(|p| p.join("Git").join("bin").join("bash.exe"))
|
||
.find(|p| p.is_file())
|
||
}
|
||
|
||
/// `seal = Some(home)`: the bot's own computer. Nothing under the person's home folder is
|
||
/// visible or writable except the bot's folder (`workdir`), like a separate machine.
|
||
/// `readable`: folders inside `protected` that may still be read (never written).
|
||
pub fn build_command_sealed(cmd: &str, workdir: &Path, protected: &[PathBuf], readable: &[PathBuf], seal: Option<&Path>, conversation: &str, request: &str) -> Command {
|
||
let mut c = match sandbox_exec() {
|
||
Some(se) if (!protected.is_empty() || seal.is_some()) && !(seal.is_none() && runs_outside_sandbox(cmd)) => {
|
||
let rules: String = (0..protected.len()).map(|i| format!(" (subpath (param \"P{i}\"))")).collect();
|
||
let mut profile = format!("(version 1)(allow default)(deny file-read* file-write* (with no-report){rules})");
|
||
let write_deny = write_denied_paths(workdir);
|
||
if !write_deny.is_empty() {
|
||
let wrules: String = (0..write_deny.len()).map(|i| format!(" (literal (param \"W{i}\")) (subpath (param \"W{i}\"))")).collect();
|
||
profile.push_str(&format!("(deny file-write* (with no-report){wrules})"));
|
||
}
|
||
if seal.is_some() {
|
||
profile.push_str("(deny file-read* file-write* (with no-report) (require-all (subpath (param \"HOME\")) (require-not (subpath (param \"ROOT\")))))");
|
||
// Sealed commands never reach host loopback services (e.g. Chrome CDP 9333) or Docker sockets.
|
||
profile.push_str("(deny network-outbound (with no-report) (remote ip \"localhost:*\"))(deny file-read* file-write* (with no-report) (subpath \"/var/run/docker.sock\") (subpath \"/private/var/run/docker.sock\"))");
|
||
}
|
||
// Later rules win: reading these is allowed again (writing stays denied).
|
||
// In `Sealed` mode never re-open `~/.ssh` or anything outside AnyBot's own attachments/skills folder.
|
||
let data_parent = crate::model::State::path().parent().map(|d| d.to_path_buf());
|
||
let open: Vec<&PathBuf> = readable
|
||
.iter()
|
||
.filter(|r| seal.is_none() || data_parent.as_ref().is_some_and(|d| r.starts_with(d)))
|
||
.filter(|r| protected.iter().any(|p| r.starts_with(p)))
|
||
.collect();
|
||
for i in 0..open.len() {
|
||
profile.push_str(&format!("(allow file-read* (subpath (param \"R{i}\")))"));
|
||
}
|
||
let mut c = Command::new(se);
|
||
for (i, p) in protected.iter().enumerate() {
|
||
c.arg("-D").arg(format!("P{i}={}", real(p)));
|
||
}
|
||
for (i, w) in write_deny.iter().enumerate() {
|
||
c.arg("-D").arg(format!("W{i}={}", real(w)));
|
||
}
|
||
for (i, r) in open.iter().enumerate() {
|
||
c.arg("-D").arg(format!("R{i}={}", real(r)));
|
||
}
|
||
if let Some(home) = seal {
|
||
c.arg("-D").arg(format!("HOME={}", real(home))).arg("-D").arg(format!("ROOT={}", real(workdir)));
|
||
}
|
||
c.arg("-p").arg(profile).arg("/bin/sh").arg("-c").arg(cmd);
|
||
c
|
||
}
|
||
// Linux: Landlock (landlock.rs), the same folders kept out; the bot's own sealed computer
|
||
// there never runs commands (sealed_runs_commands).
|
||
_ if seal.is_none() => {
|
||
let spec = crate::landlock::Spec { hidden: protected.to_vec(), readable: readable.to_vec(), no_write: write_denied_paths(workdir), work: workdir.to_path_buf() };
|
||
crate::landlock::wrap(cmd, &spec).unwrap_or_else(|| shell_of(cmd))
|
||
}
|
||
_ => shell_of(cmd),
|
||
};
|
||
for k in KEY_ENV_VARS.into_iter().chain(DANGEROUS_ENV_VARS) {
|
||
c.env_remove(k);
|
||
}
|
||
if seal.is_some() {
|
||
c.env_remove("SSH_AUTH_SOCK");
|
||
c.env_remove("DOCKER_HOST");
|
||
}
|
||
// The app started from Finder or launchd has no shell PATH (no /opt/homebrew/bin): a
|
||
// command run on an always-on Mac (a Mac mini under LaunchAgent) must find its tools too.
|
||
c.env("PATH", crate::cloudbox::tool_path());
|
||
c.env(ENV_CONVERSATION_ID, conversation).env(ENV_REQUEST_ID, request);
|
||
#[cfg(unix)]
|
||
{
|
||
use std::os::unix::process::CommandExt;
|
||
c.process_group(0);
|
||
}
|
||
c.current_dir(workdir).stdin(Stdio::piped()).stdout(Stdio::piped()).stderr(Stdio::piped());
|
||
c
|
||
}
|
||
|
||
/// A command AnyBot itself wraps around the bot's (ssh / docker exec): no sandbox, because the
|
||
/// wrapper needs the person's SSH keys and Docker settings; the bot's part runs on the other machine.
|
||
pub fn build_wrapper_command(cmd: &str, conversation: &str, request: &str) -> Command {
|
||
let mut c = shell_of(cmd);
|
||
for k in KEY_ENV_VARS.into_iter().chain(DANGEROUS_ENV_VARS) {
|
||
c.env_remove(k);
|
||
}
|
||
c.env(ENV_CONVERSATION_ID, conversation).env(ENV_REQUEST_ID, request).env("PATH", crate::cloudbox::tool_path());
|
||
#[cfg(unix)]
|
||
{
|
||
use std::os::unix::process::CommandExt;
|
||
c.process_group(0);
|
||
}
|
||
c.current_dir(std::env::temp_dir()).stdin(Stdio::piped()).stdout(Stdio::piped()).stderr(Stdio::piped());
|
||
c
|
||
}
|
||
|
||
// ---------- running tools ----------
|
||
|
||
/// Which chat and which tool call a command belongs to.
|
||
#[derive(Clone, Default)]
|
||
pub struct RunIds {
|
||
pub conversation: String,
|
||
pub request: String,
|
||
}
|
||
|
||
struct Terminal {
|
||
command: String,
|
||
io: Io,
|
||
child: Arc<Mutex<Proc>>,
|
||
exit: Arc<Mutex<Option<i32>>>,
|
||
/// When it ended (set with `exit`): a finished one is kept a while to be read, then goes.
|
||
ended: Arc<Mutex<Option<Instant>>>,
|
||
/// What also ends it where it really runs (a program inside the bots' computer: stopping
|
||
/// the local `docker exec` alone left it running there).
|
||
kill: Option<Vec<String>>,
|
||
root: u32,
|
||
/// The bot it belongs to (the ANYBOT_CONVERSATION_ID tag).
|
||
conversation: String,
|
||
}
|
||
|
||
/// Background commands running at once, all bots together: past this a new one isn't kept
|
||
/// running (each is a process tree, reader threads and its output in memory).
|
||
pub const MAX_RUNNING_TERMINALS: usize = 24;
|
||
/// A finished background command can be read for this long.
|
||
const TERMINAL_KEEP: Duration = Duration::from_secs(10 * 60);
|
||
|
||
/// What a background command shows: a plain command's output (its input closed from the start),
|
||
/// or a 互動終端機's screen (pty.rs), which takes typing.
|
||
#[derive(Clone)]
|
||
enum Io {
|
||
Pipe(Arc<Mutex<String>>),
|
||
Screen(Arc<crate::pty::Screen>),
|
||
}
|
||
|
||
/// The running program, piped or in a terminal.
|
||
enum Proc {
|
||
Std(std::process::Child),
|
||
Pty(Box<dyn portable_pty::Child + Send + Sync>),
|
||
}
|
||
|
||
impl Proc {
|
||
fn try_wait(&mut self) -> Option<i32> {
|
||
match self {
|
||
Proc::Std(c) => c.try_wait().ok().flatten().map(|s| s.code().unwrap_or(-1)),
|
||
Proc::Pty(c) => c.try_wait().ok().flatten().map(|s| s.exit_code() as i32),
|
||
}
|
||
}
|
||
|
||
/// Stop it and reap it (a killed child nobody waits for stays a zombie).
|
||
fn kill(&mut self) {
|
||
match self {
|
||
Proc::Std(c) => {
|
||
let _ = c.kill();
|
||
let _ = c.wait();
|
||
}
|
||
Proc::Pty(c) => {
|
||
let _ = c.kill();
|
||
let _ = c.wait();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Watch for the program's end (its exit code lands in `exit`).
|
||
fn watch_exit(child: &Arc<Mutex<Proc>>, exit: &Arc<Mutex<Option<i32>>>, ended: &Arc<Mutex<Option<Instant>>>) {
|
||
let (c2, e2, t2) = (child.clone(), exit.clone(), ended.clone());
|
||
std::thread::spawn(move || loop {
|
||
if let Some(code) = c2.lock().unwrap().try_wait() {
|
||
*e2.lock().unwrap() = Some(code);
|
||
*t2.lock().unwrap() = Some(Instant::now());
|
||
break;
|
||
}
|
||
std::thread::sleep(Duration::from_millis(200));
|
||
});
|
||
}
|
||
|
||
/// Keep a background command (the oldest finished ones go when there are too many).
|
||
fn keep_terminal(t: Terminal) -> u64 {
|
||
let id = next_terminal_id();
|
||
let mut terms = terminals().lock().unwrap();
|
||
tidy_terminals(&mut terms);
|
||
terms.insert(id, t);
|
||
id
|
||
}
|
||
|
||
/// Finished commands let go of what they held: a terminal's screen becomes its last text (its
|
||
/// pty, reader and scrollback are freed), and after TERMINAL_KEEP the entry itself goes.
|
||
fn tidy_terminals(terms: &mut HashMap<u64, Terminal>) {
|
||
terms.retain(|_, t| t.ended.lock().unwrap().is_none_or(|at| at.elapsed() < TERMINAL_KEEP));
|
||
for t in terms.values_mut() {
|
||
if t.ended.lock().unwrap().is_some() {
|
||
if let Io::Screen(s) = &t.io {
|
||
t.io = Io::Pipe(Arc::new(Mutex::new(s.text())));
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Whether one more background command may be kept running.
|
||
fn room_for_terminal() -> bool {
|
||
let mut terms = terminals().lock().unwrap();
|
||
tidy_terminals(&mut terms);
|
||
terms.values().filter(|t| t.exit.lock().unwrap().is_none()).count() < MAX_RUNNING_TERMINALS
|
||
}
|
||
|
||
const NO_ROOM: &str = "背景已經有太多指令在跑了,這個沒有留在背景(已停掉)。先看看哪些不用了:對它們用 write_terminal 的 stop: true 停掉,再試一次。";
|
||
|
||
/// A bot's background commands still running: (id, command).
|
||
pub fn running_for(conversation: &str) -> Vec<(u64, String)> {
|
||
let t = terminals().lock().unwrap_or_else(|e| e.into_inner());
|
||
let mut v: Vec<(u64, String)> = t.iter().filter(|(_, x)| x.conversation == conversation && x.exit.lock().map(|e| e.is_none()).unwrap_or(false)).map(|(id, x)| (*id, x.command.clone())).collect();
|
||
v.sort();
|
||
v
|
||
}
|
||
|
||
fn terminals() -> &'static Mutex<HashMap<u64, Terminal>> {
|
||
static T: OnceLock<Mutex<HashMap<u64, Terminal>>> = OnceLock::new();
|
||
T.get_or_init(|| Mutex::new(HashMap::new()))
|
||
}
|
||
|
||
fn next_terminal_id() -> u64 {
|
||
static N: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
|
||
N.fetch_add(1, Ordering::Relaxed)
|
||
}
|
||
|
||
/// Keep the start and the end of long output (the original's output_head / output_tail).
|
||
pub fn head_tail(s: &str) -> String {
|
||
let n = s.chars().count();
|
||
if n <= OUTPUT_HEAD_CHARS + OUTPUT_TAIL_CHARS {
|
||
return s.to_string();
|
||
}
|
||
let head: String = s.chars().take(OUTPUT_HEAD_CHARS).collect();
|
||
let tail: String = s.chars().skip(n - OUTPUT_TAIL_CHARS).collect();
|
||
// 完整結果: the whole is kept, and the note says how to read the middle (outputs.rs).
|
||
let id = crate::outputs::keep(s);
|
||
format!("{head}\n{}\n{tail}", crate::outputs::note(n - OUTPUT_HEAD_CHARS - OUTPUT_TAIL_CHARS, &id))
|
||
}
|
||
|
||
fn timeout_of(v: &Value) -> u64 {
|
||
v.get("timeout_ms").and_then(Value::as_u64).unwrap_or(SHELL_DEFAULT_TIMEOUT_MS).min(SHELL_MAX_TIMEOUT_MS)
|
||
}
|
||
|
||
// ---------- computers ----------
|
||
// The reference assistant runs a bot's work on its own cloud box and only runs on the person's
|
||
// Mac when they turn on 在這台電腦上執行 (setting `local-execution`). Permission questions name
|
||
// the machine ("允許助理在 {targetMachineDisplay} 上使用 {app} 嗎?").
|
||
// Here the bot's own computer is a sealed folder, this Mac is the person's computer, and other
|
||
// computers are reached over SSH.
|
||
|
||
#[derive(Clone, Debug)]
|
||
pub enum Place {
|
||
/// The bot's own computer when Docker isn't there: everything outside `root` in the home
|
||
/// folder is invisible.
|
||
Sealed { root: PathBuf },
|
||
/// The bot's own computer: AnyBot's Linux box (Docker), starting in `dir` inside it.
|
||
Box { dir: String },
|
||
/// The person's computer, starting in `workdir`.
|
||
Local { workdir: PathBuf },
|
||
/// Another computer: `ssh target`, starting in `dir` (empty = its home).
|
||
Ssh { target: String, dir: String },
|
||
/// 裝置連結: another AnyBot of the same account (through the relay), by its host id,
|
||
/// starting in `dir` (empty = its home).
|
||
Device { host: String, dir: String },
|
||
}
|
||
|
||
#[derive(Clone, Debug)]
|
||
pub struct Target {
|
||
pub name: String,
|
||
pub place: Place,
|
||
}
|
||
|
||
/// Which of a bot's computers is which, by name (every computer goes by one plain name).
|
||
#[derive(Clone, Debug, Default)]
|
||
pub struct Roles {
|
||
/// The bot's own computer: 「Master 的電腦」.
|
||
pub own: String,
|
||
/// The computer the person is using now (where they typed to the bot), when known.
|
||
pub person: Option<String>,
|
||
/// This AnyBot runs on a server: the computer it is on is nobody's desk.
|
||
pub server: bool,
|
||
}
|
||
|
||
/// The tools with a `computer` choice added to each, listing the computers this bot can use.
|
||
pub fn specs_for(computers: &[String], default: &str) -> Vec<Value> {
|
||
let mut v = specs();
|
||
for t in v.iter_mut() {
|
||
// Every tool that works on a computer names one, background terminals included: a command
|
||
// moved to the background on the Mac mini is read on the Mac mini.
|
||
// Each computer goes by its own name; the instructions say which is which. Working on
|
||
// one and calling it another (the server as "your computer") is what confused people.
|
||
let desc = format!("Which computer to work on, by its name. Default: {default} (the one the person chose). Use another only when the person names it; when unsure which one they mean, ask.");
|
||
t["parameters"]["properties"]["computer"] = json!({
|
||
"type": "string",
|
||
"enum": computers,
|
||
"description": desc
|
||
});
|
||
}
|
||
v
|
||
}
|
||
|
||
pub fn sh_quote(s: &str) -> String {
|
||
format!("'{}'", s.replace('\'', "'\\''"))
|
||
}
|
||
|
||
fn ssh_command(target: &str, dir: &str, cmd: &str) -> String {
|
||
let target = target.trim().trim_start_matches('-');
|
||
let inner = if dir.trim().is_empty() { cmd.to_string() } else { format!("cd {} && {}", sh_quote(dir), cmd) };
|
||
format!("ssh -o BatchMode=yes -o ConnectTimeout=15 {} {}", sh_quote(target), sh_quote(&inner))
|
||
}
|
||
|
||
/// The local wrapper that runs `cmd` on a remote computer (an SSH host or the bot's box).
|
||
/// `tty`: the command gets a terminal over there too (a 互動終端機: ssh -tt, docker exec -t).
|
||
fn wrapper_for(place: &Place, cmd: &str, interactive: bool, tty: bool, job: &str) -> Option<String> {
|
||
match place {
|
||
Place::Ssh { target, dir } => {
|
||
let w = ssh_command(target, dir, cmd);
|
||
Some(if tty { w.replacen("ssh ", "ssh -tt ", 1) } else { w })
|
||
}
|
||
Place::Box { dir } => Some(crate::cloudbox::exec_wrapper(dir, cmd, interactive, tty, job)),
|
||
_ => None,
|
||
}
|
||
}
|
||
|
||
/// On the bot's own box sudo is the bot's (the box gives it sudo without a password, and the
|
||
/// instructions say it may install packages): only what follows sudo is checked against the
|
||
/// always-denied list (`sudo rm -rf /` is still refused). It was refused outright, so the bot,
|
||
/// told it could sudo, worked around a block it couldn't see the reason for.
|
||
fn denied_on_box(cmd: &str) -> Option<Decision> {
|
||
for part in split_commands(cmd)? {
|
||
let mut rest = part.trim();
|
||
while let Some(after) = rest.strip_prefix("sudo").filter(|a| a.is_empty() || a.starts_with(char::is_whitespace)) {
|
||
rest = after.trim_start();
|
||
// sudo's own options (-n, -E, -H, -u root…) before the command.
|
||
while rest.starts_with('-') {
|
||
let (opt, more) = rest.split_once(char::is_whitespace).unwrap_or((rest, ""));
|
||
rest = more.trim_start();
|
||
if matches!(opt, "-u" | "-g" | "-C" | "-D" | "-h" | "-p" | "-r" | "-t" | "-U") {
|
||
rest = rest.split_once(char::is_whitespace).map(|(_, m)| m.trim_start()).unwrap_or("");
|
||
}
|
||
}
|
||
}
|
||
if let Some(d) = DENIED_COMMANDS.iter().filter(|d| **d != "sudo").find(|d| starts_with_words(rest, d)) {
|
||
return Some(Decision::Deny(format!("「{d}」這類指令一律不允許。")));
|
||
}
|
||
}
|
||
None
|
||
}
|
||
|
||
pub fn decide_on(name: &str, args: &str, target: &Target, grants: &[String]) -> Decision {
|
||
let v = args_of(args);
|
||
let denied = |cmd: &str| -> Option<Decision> {
|
||
for part in split_commands(cmd)? {
|
||
if let Some(d) = DENIED_COMMANDS.iter().find(|d| starts_with_words(&part, d)) {
|
||
return Some(Decision::Deny(format!("「{d}」這類指令一律不允許。")));
|
||
}
|
||
}
|
||
None
|
||
};
|
||
match &target.place {
|
||
Place::Local { workdir } => decide(name, args, workdir, grants),
|
||
Place::Sealed { root } => {
|
||
if name == "search_documents" {
|
||
return Decision::Deny(format!("{} 看不到這台電腦上的文件索引。要查使用者的文件,請在使用者的電腦上做。", target.name));
|
||
}
|
||
if name == "shell" {
|
||
// Its own computer: the sandbox keeps it inside its folder. No sandbox, no command.
|
||
if !sealed_runs_commands() {
|
||
return Decision::Deny(NO_BOX_FOR_COMMANDS.into());
|
||
}
|
||
return denied(&arg_str(&v, "command")).unwrap_or(Decision::Allow);
|
||
}
|
||
if name == "read_terminal" || name == "write_terminal" {
|
||
return Decision::Allow;
|
||
}
|
||
let p = resolve(root, &arg_str(&v, "path"));
|
||
if !within(root, &p) {
|
||
return Decision::Deny(format!("{} 看不到這台電腦上的其他檔案({})。要處理使用者的檔案,請在使用者的電腦上做(不確定是哪台就先問)。", target.name, p.display()));
|
||
}
|
||
Decision::Allow
|
||
}
|
||
// Its own computer: everything is allowed (it can be rebuilt), like the original's box.
|
||
Place::Box { .. } => {
|
||
if name == "search_documents" {
|
||
return Decision::Deny(format!("{} 看不到這台電腦上的文件索引。", target.name));
|
||
}
|
||
if name == "shell" {
|
||
if let Some(d) = denied_on_box(&arg_str(&v, "command")) {
|
||
return d;
|
||
}
|
||
}
|
||
Decision::Allow
|
||
}
|
||
Place::Ssh { .. } | Place::Device { .. } => {
|
||
let p_arg = arg_str(&v, "path");
|
||
if matches!(name, "list_dir" | "read_file" | "search" | "edit_file") && is_ignored(Path::new(&p_arg)) {
|
||
return Decision::Deny(format!("「{p_arg}」是私密或系統檔案,Bot 不能存取。"));
|
||
}
|
||
match name {
|
||
"list_dir" | "read_file" | "search" | "read_terminal" | "write_terminal" => Decision::Allow,
|
||
"edit_file" => {
|
||
if grants.iter().any(|g| g == "edit_file") {
|
||
Decision::Allow
|
||
} else {
|
||
Decision::Ask { reason: format!("修改 {} 上的檔案:{p_arg}", target.name), grant: "edit_file".into() }
|
||
}
|
||
}
|
||
_ => {
|
||
let cmd = arg_str(&v, "command");
|
||
match decide("shell", args, Path::new("/nonexistent-remote"), grants) {
|
||
Decision::Ask { grant, .. } => Decision::Ask { reason: format!("在 {} 上執行指令:{cmd}", target.name), grant },
|
||
other => other,
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// A file on its way out of a computer — into the chat (send_file) or onto the person's screen
|
||
/// (open_for_person) — is read on the way, so it meets read_file's walls. It used to meet none:
|
||
/// any path on this computer went out without a question, and from the bot's sealed folder a
|
||
/// `..` reached the rest of the home folder.
|
||
///
|
||
/// The real file is what counts, not how the path is spelled: a link inside the folder that
|
||
/// points outside it is outside.
|
||
pub fn decide_file_out(target: &Target, path: &str, grants: &[String]) -> Decision {
|
||
let (base, sealed) = match &target.place {
|
||
Place::Local { workdir } => (workdir.as_path(), false),
|
||
Place::Sealed { root } => (root.as_path(), true),
|
||
Place::Box { dir } => return match box_path(Path::new(dir), path) {
|
||
Ok(_) => Decision::Allow,
|
||
Err(e) => Decision::Deny(e),
|
||
},
|
||
// Another computer answers for its own files.
|
||
_ => return Decision::Allow,
|
||
};
|
||
let named = resolve(base, path);
|
||
let real = std::fs::canonicalize(&named).unwrap_or_else(|_| named.clone());
|
||
if is_ignored(&named) || is_ignored(&real) {
|
||
return Decision::Deny(format!("「{}」是私密或系統檔案,Bot 不能讀取。", named.display()));
|
||
}
|
||
let home = std::fs::canonicalize(base).unwrap_or_else(|_| base.to_path_buf());
|
||
// (A path that names nothing yet has no real form: it is judged as spelled.)
|
||
let within = inside(&home, &real) || (real == named && inside(base, &named));
|
||
if within {
|
||
Decision::Allow
|
||
} else if sealed {
|
||
Decision::Deny(format!("{} 看不到這台電腦上的其他檔案({})。要處理使用者的檔案,請在使用者的電腦上做(不確定是哪台就先問)。", target.name, named.display()))
|
||
} else if grants.iter().any(|g| g == "read:anywhere") {
|
||
Decision::Allow
|
||
} else {
|
||
Decision::Ask { reason: format!("讀取工作資料夾以外的地方:{}", named.display()), grant: "read:anywhere".into() }
|
||
}
|
||
}
|
||
|
||
/// Run a remote command and give back its output (no background hand-off).
|
||
fn remote_capture(place: &Place, cmd: &str, stdin: Option<&[u8]>) -> Result<String, String> {
|
||
use std::io::Write;
|
||
let w = wrapper_for(place, cmd, stdin.is_some(), false, "").ok_or("不是遠端電腦")?;
|
||
let mut c = build_wrapper_command(&w, "", "");
|
||
let mut child = c.spawn().map_err(|e| format!("無法執行:{e}"))?;
|
||
if let Some(data) = stdin {
|
||
if let Some(mut si) = child.stdin.take() {
|
||
si.write_all(data).map_err(|e| format!("寫入失敗:{e}"))?;
|
||
}
|
||
} else {
|
||
drop(child.stdin.take());
|
||
}
|
||
let out = child.wait_with_output().map_err(|e| e.to_string())?;
|
||
let text = String::from_utf8_lossy(&out.stdout).to_string();
|
||
if out.status.success() {
|
||
Ok(text)
|
||
} else {
|
||
Err(format!("{}{}", String::from_utf8_lossy(&out.stderr).trim(), if text.trim().is_empty() { String::new() } else { format!("\n{}", text.trim()) }))
|
||
}
|
||
}
|
||
|
||
/// Run an allowed tool on a computer.
|
||
pub fn run_on(name: &str, args: &str, target: &Target, cancel: &AtomicBool, ids: &RunIds) -> (String, bool) {
|
||
let v = args_of(args);
|
||
let done = |r: Result<String, String>| match r {
|
||
Ok(s) => (s, true),
|
||
Err(e) => (e, false),
|
||
};
|
||
match &target.place {
|
||
Place::Local { workdir } => run(name, args, workdir, cancel, ids),
|
||
Place::Device { host, dir } => {
|
||
// The device runs the tool in the bot's work folder (its own home when unset), and
|
||
// must answer before the relay gives up at 35 s: long commands move to the
|
||
// background there, and read_terminal now names this computer to find them.
|
||
let mut v = v.clone();
|
||
let cap = timeout_of(&v).min(DEVICE_SHELL_TIMEOUT_MS);
|
||
if let Some(o) = v.as_object_mut() {
|
||
// A read or a typing that waits answers before the relay gives up, too.
|
||
if let Some(w) = o.get("wait_ms").and_then(Value::as_u64) {
|
||
o.insert("wait_ms".into(), serde_json::Value::from(w.min(DEVICE_SHELL_TIMEOUT_MS)));
|
||
}
|
||
if !dir.is_empty() && o.get("workdir").and_then(Value::as_str).unwrap_or("").is_empty() {
|
||
// (As `~/…` when it is under a home: that computer's home is its own. The
|
||
// folder as it was chosen goes along, for a computer where it is real.)
|
||
o.insert("workdir".into(), serde_json::Value::String(portable_dir(dir)));
|
||
o.insert("workdir_as_chosen".into(), serde_json::Value::String(dir.trim().to_string()));
|
||
}
|
||
o.insert("timeout_ms".into(), serde_json::Value::from(cap));
|
||
}
|
||
let args = v.to_string();
|
||
match crate::remote::run_on_device(host, name, &args, "") {
|
||
Ok(v) => {
|
||
let mut text = v["text"].as_str().unwrap_or("").to_string();
|
||
if text.contains("read_terminal") {
|
||
text.push_str(&format!("(這台是「{}」:read_terminal 的 computer 填「{}」。)", target.name, target.name));
|
||
}
|
||
(text, v["ok"].as_bool().unwrap_or(false))
|
||
}
|
||
Err(e) => (e, false),
|
||
}
|
||
}
|
||
Place::Sealed { root } => {
|
||
let _ = std::fs::create_dir_all(root);
|
||
if name == "shell" && !sealed_runs_commands() {
|
||
return (NO_BOX_FOR_COMMANDS.into(), false);
|
||
}
|
||
if name == "shell" {
|
||
let home = dirs::home_dir().unwrap_or_else(|| PathBuf::from("/Users"));
|
||
return done(shell(&arg_str(&v, "command"), root, timeout_of(&v), interactive_of(&v), cancel, ids, Some(&home)));
|
||
}
|
||
run(name, args, root, cancel, ids)
|
||
}
|
||
place @ (Place::Ssh { .. } | Place::Box { .. }) => {
|
||
if let Place::Box { .. } = place {
|
||
if let Err(e) = crate::cloudbox::ensure_running() {
|
||
return (e, false);
|
||
}
|
||
}
|
||
let _busy = crate::cloudbox::Busy::new(matches!(place, Place::Box { .. }));
|
||
let path = arg_str(&v, "path");
|
||
let p = if path.is_empty() { ".".to_string() } else { path.clone() };
|
||
if v.is_null() && !args.trim().is_empty() {
|
||
return ("參數不是有效的 JSON".into(), false);
|
||
}
|
||
// Box file tools stay inside this bot's own folder: every bot shares one container,
|
||
// one user and one disk, so `../` or an absolute path reached (and rewrote) the
|
||
// other bots' files — a planted .bashrc runs code on their next command.
|
||
let (p, path, box_guard) = match place {
|
||
Place::Box { dir } if name != "shell" => match box_path(std::path::Path::new(dir), &p) {
|
||
Ok(s) => {
|
||
let g = format!(
|
||
"base=$(realpath -m -- {base}) && real=$(realpath -m -- {target}) && case \"$real\" in \"$base\"|\"$base\"/*) ;; *) echo '連結指到了你的專屬資料夾以外的地方' >&2; exit 1;; esac; ",
|
||
base = sh_quote(dir),
|
||
target = sh_quote(&s),
|
||
);
|
||
(s.clone(), s, g)
|
||
}
|
||
Err(e) => return (e, false),
|
||
},
|
||
_ => (p, path, String::new()),
|
||
};
|
||
let remote = match name {
|
||
"shell" if arg_str(&v, "command").trim().is_empty() => return ("請給要執行的指令".into(), false),
|
||
"shell" => arg_str(&v, "command"),
|
||
"list_dir" => format!("{box_guard}ls -la {}", sh_quote(&p)),
|
||
"read_file" => {
|
||
let from = v.get("offset").and_then(Value::as_u64).unwrap_or(1).max(1);
|
||
let n = v.get("limit").and_then(Value::as_u64).unwrap_or(READ_DEFAULT_LINES as u64);
|
||
format!("{box_guard}test -f {q} || {{ echo '找不到檔案:'{q} >&2; exit 1; }}; sed -n '{from},{}p' {q} | cat -n", from + n - 1, q = sh_quote(&p))
|
||
}
|
||
"search" => {
|
||
let flag = if v.get("files_only").and_then(Value::as_bool).unwrap_or(false) { "l" } else { "n" };
|
||
format!("{box_guard}(command -v rg >/dev/null && rg -{flag} --no-heading -- {pat} {p} || grep -rI{flag} -- {pat} {p}) | head -n {SEARCH_CLIENT_LIMIT_LINES}", pat = sh_quote(&arg_str(&v, "pattern")), p = sh_quote(&p))
|
||
}
|
||
"edit_file" => {
|
||
if path.is_empty() {
|
||
return ("請給檔案路徑".into(), false);
|
||
}
|
||
let old = arg_str(&v, "old_text");
|
||
let new = arg_str(&v, "new_text");
|
||
let q = sh_quote(&path);
|
||
let content = if old.is_empty() {
|
||
new
|
||
} else {
|
||
let cur = match remote_capture(place, &format!("{box_guard}cat {q}"), None) {
|
||
Ok(c) => c,
|
||
Err(e) => return (format!("讀不到 {path}:{e}"), false),
|
||
};
|
||
match cur.matches(old.as_str()).count() {
|
||
0 => return ("找不到 old_text,檔案沒有改。請先讀檔確認原文。".into(), false),
|
||
1 => cur.replacen(old.as_str(), &new, 1),
|
||
n => return (format!("old_text 出現了 {n} 次,請多給一點前後文讓它只出現一次。"), false),
|
||
}
|
||
};
|
||
let write = format!("{box_guard}mkdir -p \"$(dirname {q})\" && cat > {q}");
|
||
return match remote_capture(place, &write, Some(content.as_bytes())) {
|
||
Ok(_) => (format!("已寫入 {path}({} 行)。", content.lines().count()), true),
|
||
Err(e) => (format!("寫不進 {path}:{e}"), false),
|
||
};
|
||
}
|
||
"read_terminal" | "write_terminal" => return run(name, args, Path::new("/"), cancel, ids),
|
||
other => return (format!("沒有「{other}」這個工具"), false),
|
||
};
|
||
// Tag the in-container command so stopping it here also stops it inside the box
|
||
// (killing the local docker client alone left the program running in the box).
|
||
let job = crate::httpd::random_hex(8);
|
||
let tty = name == "shell" && interactive_of(&v);
|
||
let wrapper = wrapper_for(place, &remote, name == "shell", tty, &job).unwrap_or_default();
|
||
let kill = matches!(place, Place::Box { .. }).then(|| crate::cloudbox::exec_kill_argv(&job));
|
||
done(shell_wrapped(&wrapper, &remote, timeout_of(&v), tty, cancel, ids, kill))
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Keep a Box file path inside the bot's own folder: relative paths join the bot's dir, and
|
||
/// a normalised result that lands outside it (`../`, absolute paths to other bots' folders)
|
||
/// is refused. All bots share one container and one user, so the folder is the only wall.
|
||
pub fn box_path(dir: &Path, p: &str) -> Result<String, String> {
|
||
let joined = if p.starts_with('/') { PathBuf::from(p) } else { dir.join(p) };
|
||
let mut parts: Vec<std::ffi::OsString> = vec![];
|
||
for c in joined.components() {
|
||
use std::path::Component::*;
|
||
match c {
|
||
ParentDir => {
|
||
parts.pop();
|
||
}
|
||
CurDir => {}
|
||
RootDir => parts.push(std::ffi::OsString::from("/")),
|
||
Normal(x) => parts.push(x.to_os_string()),
|
||
Prefix(x) => parts.push(x.as_os_str().to_os_string()),
|
||
}
|
||
}
|
||
let mut norm = PathBuf::new();
|
||
for x in parts {
|
||
norm.push(x);
|
||
}
|
||
if norm.starts_with(dir) {
|
||
Ok(norm.display().to_string())
|
||
} else {
|
||
Err(format!("這個檔案不在你的資料夾裡(Bot 的電腦上每個 Bot 只用自己的資料夾):{p}"))
|
||
}
|
||
}
|
||
|
||
/// Run a tool that the rules already allowed. Returns (output, ok).
|
||
pub fn run(name: &str, args: &str, workdir: &Path, cancel: &AtomicBool, ids: &RunIds) -> (String, bool) {
|
||
let v = args_of(args);
|
||
if v.is_null() && !args.trim().is_empty() {
|
||
return ("參數不是有效的 JSON".into(), false);
|
||
}
|
||
let _ = std::fs::create_dir_all(workdir);
|
||
let r = match name {
|
||
"list_dir" => list_dir(&resolve(workdir, &arg_str(&v, "path"))),
|
||
"search_documents" => docs_search_tool(&v),
|
||
"search" => search(&arg_str(&v, "pattern"), &resolve(workdir, &arg_str(&v, "path")), v.get("files_only").and_then(Value::as_bool).unwrap_or(false)),
|
||
"read_file" => read_file(
|
||
&resolve(workdir, &arg_str(&v, "path")),
|
||
v.get("offset").and_then(Value::as_u64).unwrap_or(1).max(1) as usize,
|
||
v.get("limit").and_then(Value::as_u64).map(|l| l as usize).unwrap_or(READ_DEFAULT_LINES),
|
||
),
|
||
"edit_file" => edit_file(&resolve(workdir, &arg_str(&v, "path")), &arg_str(&v, "old_text"), &arg_str(&v, "new_text")),
|
||
"shell" => shell(&arg_str(&v, "command"), workdir, timeout_of(&v), interactive_of(&v), cancel, ids, None),
|
||
"read_terminal" => read_terminal(v.get("id").and_then(Value::as_u64).unwrap_or(0), v.get("wait_ms").and_then(Value::as_u64).unwrap_or(0), &arg_str(&v, "until")),
|
||
"write_terminal" if v.get("stop").and_then(Value::as_bool).unwrap_or(false) => stop_terminal(v.get("id").and_then(Value::as_u64).unwrap_or(0)),
|
||
"write_terminal" => write_terminal(v.get("id").and_then(Value::as_u64).unwrap_or(0), &arg_str(&v, "input"), &keys_of(&v), v.get("close").and_then(Value::as_bool).unwrap_or(false), v.get("wait_ms").and_then(Value::as_u64).unwrap_or(0)),
|
||
other => Err(format!("沒有「{other}」這個工具")),
|
||
};
|
||
match r {
|
||
Ok(s) => (s, true),
|
||
Err(e) => (e, false),
|
||
}
|
||
}
|
||
|
||
/// 翻你的文件: the indexed folders, by meaning and words (store::docs_search).
|
||
fn docs_search_tool(v: &Value) -> Result<String, String> {
|
||
if !crate::policy::read().local.allows() {
|
||
return Err("這台電腦已被系統管理員政策鎖定,不允許讀取本機文件。".to_string());
|
||
}
|
||
let q = v.get("query").and_then(Value::as_str).unwrap_or("").trim();
|
||
if q.is_empty() {
|
||
return Err("請給一句要找的話。".to_string());
|
||
}
|
||
let limit = v.get("limit").and_then(Value::as_u64).unwrap_or(6).clamp(1, 20) as usize;
|
||
let hits: Vec<(String, String)> = crate::store::docs_search(q, limit).into_iter().filter(|(path, _)| !is_ignored(Path::new(path))).collect();
|
||
if hits.is_empty() {
|
||
return Err("在你的文件資料夾裡找不到相關的內容(可以在 設定 › 一般 › 文件索引 加入資料夾)。".to_string());
|
||
}
|
||
let mut out = String::new();
|
||
for (path, chunk) in hits {
|
||
out.push_str(&format!("【{path}】\n{}\n\n", chunk.chars().take(600).collect::<String>()));
|
||
}
|
||
Ok(out.trim().to_string())
|
||
}
|
||
|
||
fn list_dir(p: &Path) -> Result<String, String> {
|
||
let rd = std::fs::read_dir(p).map_err(|e| format!("無法開啟資料夾 {}:{e}", p.display()))?;
|
||
let mut rows: Vec<(bool, String, u64)> = rd
|
||
.filter_map(|e| e.ok())
|
||
.filter(|e| !is_ignored(&e.path()))
|
||
.map(|e| {
|
||
let md = e.metadata().ok();
|
||
(md.as_ref().map(|m| m.is_dir()).unwrap_or(false), e.file_name().to_string_lossy().to_string(), md.map(|m| m.len()).unwrap_or(0))
|
||
})
|
||
.collect();
|
||
rows.sort_by(|a, b| b.0.cmp(&a.0).then(a.1.to_lowercase().cmp(&b.1.to_lowercase())));
|
||
let total = rows.len();
|
||
let mut out: Vec<String> = rows.into_iter().take(SEARCH_CLIENT_LIMIT_LINES).map(|(d, n, s)| if d { format!("{n}/") } else { format!("{n} {}", human_size(s)) }).collect();
|
||
if total > SEARCH_CLIENT_LIMIT_LINES {
|
||
out.push(format!("…還有 {} 項", total - SEARCH_CLIENT_LIMIT_LINES));
|
||
}
|
||
if out.is_empty() {
|
||
out.push("(空資料夾)".into());
|
||
}
|
||
Ok(format!("{}\n{}", p.display(), out.join("\n")))
|
||
}
|
||
|
||
fn human_size(b: u64) -> String {
|
||
match b {
|
||
0..=1023 => format!("{b} B"),
|
||
1024..=1_048_575 => format!("{:.1} KB", b as f64 / 1024.0),
|
||
_ => format!("{:.1} MB", b as f64 / 1_048_576.0),
|
||
}
|
||
}
|
||
|
||
fn search(pattern: &str, root: &Path, files_only: bool) -> Result<String, String> {
|
||
if pattern.is_empty() {
|
||
return Err("請給要搜尋的文字".into());
|
||
}
|
||
let deadline = Instant::now() + Duration::from_millis(SEARCH_TIMEOUT_MS);
|
||
let mut lines: Vec<String> = vec![];
|
||
let mut timed_out = false;
|
||
// Prefer ripgrep when it's installed; fall back to a plain walk with substring matching.
|
||
let rg = Command::new("rg")
|
||
.args(["--no-heading", "--line-number", "--color", "never", "--max-columns", "300", "-g", "!.git", "-g", "!.env*", "-g", "!*.pem", "-g", "!*.key"])
|
||
.args(if files_only { vec!["-l"] } else { vec![] })
|
||
.arg("--")
|
||
.arg(pattern)
|
||
.arg(root)
|
||
.stdout(Stdio::piped())
|
||
.stderr(Stdio::null())
|
||
.spawn();
|
||
if let Ok(mut child) = rg {
|
||
let mut out = child.stdout.take().unwrap();
|
||
let buf = Arc::new(Mutex::new(Vec::new()));
|
||
let b2 = buf.clone();
|
||
let reader = std::thread::spawn(move || {
|
||
let mut chunk = [0u8; 8192];
|
||
while let Ok(n) = out.read(&mut chunk) {
|
||
if n == 0 {
|
||
break;
|
||
}
|
||
let mut b = b2.lock().unwrap();
|
||
b.extend_from_slice(&chunk[..n]);
|
||
if b.iter().filter(|c| **c == b'\n').count() > SEARCH_HARD_MAX_LINES {
|
||
break;
|
||
}
|
||
}
|
||
});
|
||
loop {
|
||
if child.try_wait().ok().flatten().is_some() || reader.is_finished() {
|
||
break;
|
||
}
|
||
if Instant::now() > deadline {
|
||
timed_out = true;
|
||
let _ = child.kill();
|
||
break;
|
||
}
|
||
std::thread::sleep(Duration::from_millis(20));
|
||
}
|
||
let _ = child.kill();
|
||
let _ = child.wait();
|
||
let _ = reader.join();
|
||
let text = String::from_utf8_lossy(&buf.lock().unwrap()).to_string();
|
||
lines = text.lines().map(|l| l.to_string()).collect();
|
||
} else {
|
||
let needle = pattern.to_lowercase();
|
||
let mut stack = vec![root.to_path_buf()];
|
||
'walk: while let Some(p) = stack.pop() {
|
||
if Instant::now() > deadline {
|
||
timed_out = true;
|
||
break;
|
||
}
|
||
if is_ignored(&p) {
|
||
continue;
|
||
}
|
||
// Links are not followed (ripgrep doesn't either): a linked folder is a way out
|
||
// of the folder being searched, and a link back up is a walk without end.
|
||
if p != root && p.symlink_metadata().map(|m| m.file_type().is_symlink()).unwrap_or(false) {
|
||
continue;
|
||
}
|
||
if p.is_dir() {
|
||
if let Ok(rd) = std::fs::read_dir(&p) {
|
||
stack.extend(rd.filter_map(|e| e.ok()).map(|e| e.path()).filter(|q| q.file_name().map(|n| n != "node_modules" && n != "target").unwrap_or(true)));
|
||
}
|
||
continue;
|
||
}
|
||
let Ok(text) = std::fs::read_to_string(&p) else { continue };
|
||
for (i, l) in text.lines().enumerate() {
|
||
if l.to_lowercase().contains(&needle) {
|
||
lines.push(if files_only { p.display().to_string() } else { format!("{}:{}:{}", p.display(), i + 1, l.chars().take(300).collect::<String>()) });
|
||
if files_only {
|
||
break;
|
||
}
|
||
if lines.len() >= SEARCH_HARD_MAX_LINES {
|
||
break 'walk;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
let total = lines.len();
|
||
let mut out: Vec<String> = lines.into_iter().take(SEARCH_CLIENT_LIMIT_LINES).collect();
|
||
if total > SEARCH_CLIENT_LIMIT_LINES {
|
||
out.push(format!("…結果太多,只列出前 {SEARCH_CLIENT_LIMIT_LINES} 行(共 {total} 行以上)"));
|
||
}
|
||
if timed_out {
|
||
out.push(format!("…搜尋超過 {} 秒,結果可能不完整", SEARCH_TIMEOUT_MS / 1000));
|
||
}
|
||
if out.is_empty() {
|
||
out.push("沒有找到".into());
|
||
}
|
||
Ok(out.join("\n"))
|
||
}
|
||
|
||
fn read_file(p: &Path, offset: usize, limit: usize) -> Result<String, String> {
|
||
let md = std::fs::metadata(p).map_err(|e| format!("找不到 {}:{e}", p.display()))?;
|
||
if md.is_dir() {
|
||
return Err(format!("{} 是資料夾,請用 list_dir", p.display()));
|
||
}
|
||
if md.len() > READ_MAX_BYTES {
|
||
return Err(format!("{} 太大了({}),最多讀 8 MB", p.display(), human_size(md.len())));
|
||
}
|
||
let bytes = std::fs::read(p).map_err(|e| format!("無法讀取 {}:{e}", p.display()))?;
|
||
if bytes.iter().take(8000).any(|b| *b == 0) {
|
||
return Err(format!("{} 不是文字檔({})", p.display(), human_size(md.len())));
|
||
}
|
||
let text = String::from_utf8_lossy(&bytes);
|
||
let all: Vec<&str> = text.lines().collect();
|
||
let end = (offset - 1 + limit).min(all.len());
|
||
let mut out: Vec<String> = all.iter().enumerate().skip(offset - 1).take(end.saturating_sub(offset - 1)).map(|(i, l)| format!("{:>5} {l}", i + 1)).collect();
|
||
if end < all.len() {
|
||
out.push(format!("…還有 {} 行(用 offset={} 繼續讀)", all.len() - end, end + 1));
|
||
}
|
||
if out.is_empty() {
|
||
out.push("(空檔案)".into());
|
||
}
|
||
Ok(out.join("\n"))
|
||
}
|
||
|
||
fn edit_file(p: &Path, old: &str, new: &str) -> Result<String, String> {
|
||
if old.is_empty() {
|
||
if let Some(dir) = p.parent() {
|
||
std::fs::create_dir_all(dir).map_err(|e| format!("無法建立資料夾:{e}"))?;
|
||
}
|
||
let existed = p.exists();
|
||
std::fs::write(p, new).map_err(|e| format!("無法寫入 {}:{e}", p.display()))?;
|
||
return Ok(format!("{} {}({} 行)", if existed { "已覆寫" } else { "已建立" }, p.display(), new.lines().count()));
|
||
}
|
||
let text = std::fs::read_to_string(p).map_err(|e| format!("無法讀取 {}:{e}", p.display()))?;
|
||
match text.matches(old).count() {
|
||
0 => Err(format!("在 {} 裡找不到要替換的文字", p.display())),
|
||
1 => {
|
||
std::fs::write(p, text.replacen(old, new, 1)).map_err(|e| format!("無法寫入 {}:{e}", p.display()))?;
|
||
Ok(format!("已修改 {}", p.display()))
|
||
}
|
||
n => Err(format!("要替換的文字在 {} 出現了 {n} 次,請多給一些前後文讓它只出現一次", p.display())),
|
||
}
|
||
}
|
||
|
||
fn shell(cmd: &str, workdir: &Path, timeout_ms: u64, interactive: bool, cancel: &AtomicBool, ids: &RunIds, seal: Option<&Path>) -> Result<String, String> {
|
||
let command = build_command_sealed(cmd, workdir, &protected_dirs(), &readable_dirs(), seal, &ids.conversation, &ids.request);
|
||
let r = if interactive { run_terminal(command, cmd, first_look_ms(timeout_ms), cancel, ids, None) } else { run_command(command, cmd, timeout_ms, cancel, ids, None) };
|
||
// (The bot's own sealed computer keeps every command in the sandbox: no hints about outside.)
|
||
let note = |out: &str| sandbox_note(cmd, out).filter(|_| seal.is_none()).map(|n| format!("{out}\n{n}")).unwrap_or_else(|| out.to_string());
|
||
match r {
|
||
Ok(out) => Ok(note(&out)),
|
||
Err(out) => Err(note(&out)),
|
||
}
|
||
}
|
||
|
||
/// Run a wrapper command (ssh / docker exec) the same way: background hand-off, stop, output.
|
||
pub fn shell_wrapped(wrapper: &str, shown: &str, timeout_ms: u64, interactive: bool, cancel: &AtomicBool, ids: &RunIds, kill: Option<Vec<String>>) -> Result<String, String> {
|
||
let command = build_wrapper_command(wrapper, &ids.conversation, &ids.request);
|
||
if interactive {
|
||
run_terminal(command, shown, first_look_ms(timeout_ms), cancel, ids, kill)
|
||
} else {
|
||
run_command(command, shown, timeout_ms, cancel, ids, kill)
|
||
}
|
||
}
|
||
|
||
/// A 互動終端機's first look waits for its first screen — not the 30 s a plain command gets.
|
||
fn first_look_ms(timeout_ms: u64) -> u64 {
|
||
timeout_ms.min(15_000)
|
||
}
|
||
|
||
/// Whether a shell call asks for a 互動終端機.
|
||
fn interactive_of(v: &Value) -> bool {
|
||
v.get("interactive").and_then(Value::as_bool).unwrap_or(false)
|
||
}
|
||
|
||
fn keys_of(v: &Value) -> Vec<String> {
|
||
match v.get("keys") {
|
||
Some(Value::Array(a)) => a.iter().filter_map(|k| k.as_str().map(String::from)).collect(),
|
||
Some(Value::String(k)) if !k.trim().is_empty() => k.split([',', ' ']).filter(|x| !x.is_empty()).map(String::from).collect(),
|
||
_ => vec![],
|
||
}
|
||
}
|
||
|
||
fn run_command(mut command: Command, cmd: &str, timeout_ms: u64, cancel: &AtomicBool, ids: &RunIds, kill: Option<Vec<String>>) -> Result<String, String> {
|
||
let mut child = command.spawn().map_err(|e| format!("無法執行:{e}"))?;
|
||
// Its input is closed at once (as if from /dev/null): a program that reads its input to the
|
||
// end — `pi-bolt -p`, `cat`, a script piped to python — goes on instead of waiting forever
|
||
// for words that never come. One that needs typing runs as a 互動終端機 (interactive).
|
||
drop(child.stdin.take());
|
||
let root = child.id();
|
||
let output = Arc::new(Mutex::new(String::new()));
|
||
for stream in [child.stdout.take().map(|s| Box::new(s) as Box<dyn Read + Send>), child.stderr.take().map(|s| Box::new(s) as Box<dyn Read + Send>)].into_iter().flatten() {
|
||
let out = output.clone();
|
||
std::thread::spawn(move || {
|
||
let mut stream = stream;
|
||
let mut chunk = [0u8; 4096];
|
||
while let Ok(n) = stream.read(&mut chunk) {
|
||
if n == 0 {
|
||
break;
|
||
}
|
||
// Cap the kept output (keep the newest): `yes` must not eat all the RAM.
|
||
let mut out = out.lock().unwrap();
|
||
if out.len() < 4_000_000 {
|
||
out.push_str(&String::from_utf8_lossy(&chunk[..n]));
|
||
}
|
||
}
|
||
});
|
||
}
|
||
let start = Instant::now();
|
||
loop {
|
||
if let Ok(Some(status)) = child.try_wait() {
|
||
std::thread::sleep(Duration::from_millis(30));
|
||
let out = head_tail(&output.lock().unwrap());
|
||
let code = status.code().unwrap_or(-1);
|
||
return if code == 0 { Ok(if out.trim().is_empty() { "(完成,沒有輸出)".into() } else { out }) } else { Err(format!("結束代碼 {code}\n{out}")) };
|
||
}
|
||
if cancel.load(Ordering::Relaxed) {
|
||
kill_tree(root);
|
||
let _ = child.kill();
|
||
// Reap it: a killed child nobody waits for stays a zombie for the app's lifetime.
|
||
let _ = child.wait();
|
||
// The remote end (a program running inside the box) is not in this process tree.
|
||
if let Some(k) = &kill {
|
||
if let Some((head, rest)) = k.split_first() {
|
||
let _ = Command::new(head).args(rest).status();
|
||
}
|
||
}
|
||
return Err("已停止(連同它啟動的程式)".into());
|
||
}
|
||
if start.elapsed() >= Duration::from_millis(timeout_ms) {
|
||
// Hand off to the background instead of killing it (while there is room).
|
||
if !room_for_terminal() {
|
||
kill_tree(root);
|
||
let _ = child.kill();
|
||
let _ = child.wait();
|
||
return Err(NO_ROOM.into());
|
||
}
|
||
let child = Arc::new(Mutex::new(Proc::Std(child)));
|
||
let (exit, ended) = (Arc::new(Mutex::new(None)), Arc::new(Mutex::new(None)));
|
||
watch_exit(&child, &exit, &ended);
|
||
let so_far = head_tail(&output.lock().unwrap());
|
||
let id = keep_terminal(Terminal { command: cmd.to_string(), io: Io::Pipe(output), child, exit, ended, kill, root, conversation: ids.conversation.clone() });
|
||
return Ok(format!("指令跑了超過 {} 秒,已移到背景繼續執行(背景指令 #{id})。用 read_terminal 查看進度(wait_ms 可以等它有新進度再回來)。\n目前的輸出:\n{so_far}", timeout_ms / 1000));
|
||
}
|
||
std::thread::sleep(Duration::from_millis(20));
|
||
}
|
||
}
|
||
|
||
/// The longest read_terminal / write_terminal waits for something new.
|
||
pub const TERMINAL_WAIT_MAX_MS: u64 = 120_000;
|
||
/// Quiet this long after writing, a program is waiting (its prompt, its answer done).
|
||
const SCREEN_QUIET_MS: u64 = 1_200;
|
||
|
||
/// 互動終端機: run `command` in a terminal (pty.rs). The first look waits until its screen
|
||
/// settles (its first screen, a prompt) or `wait_ms`; a program that ended gives its last screen.
|
||
fn run_terminal(command: Command, cmd: &str, wait_ms: u64, cancel: &AtomicBool, ids: &RunIds, kill: Option<Vec<String>>) -> Result<String, String> {
|
||
let (screen, child) = crate::pty::spawn(&command)?;
|
||
let root = child.process_id().unwrap_or(0);
|
||
let child = Arc::new(Mutex::new(Proc::Pty(child)));
|
||
let (exit, ended) = (Arc::new(Mutex::new(None)), Arc::new(Mutex::new(None)));
|
||
watch_exit(&child, &exit, &ended);
|
||
crate::pty::settle(&screen, 0, Duration::from_millis(wait_ms), Duration::from_millis(SCREEN_QUIET_MS), "", || exit.lock().unwrap().is_some(), || cancel.load(Ordering::Relaxed));
|
||
if cancel.load(Ordering::Relaxed) {
|
||
kill_tree(root);
|
||
child.lock().unwrap().kill();
|
||
if let Some((head, rest)) = kill.as_ref().and_then(|k| k.split_first()) {
|
||
let _ = Command::new(head).args(rest).status();
|
||
}
|
||
return Err("已停止(連同它啟動的程式)".into());
|
||
}
|
||
if let Some(code) = *exit.lock().unwrap() {
|
||
let out = screen.text();
|
||
let out = if out.trim().is_empty() { "(完成,沒有輸出)".to_string() } else { out };
|
||
return if code == 0 { Ok(out) } else { Err(format!("結束代碼 {code}\n{out}")) };
|
||
}
|
||
if !room_for_terminal() {
|
||
kill_tree(root);
|
||
child.lock().unwrap().kill();
|
||
return Err(NO_ROOM.into());
|
||
}
|
||
let shown = screen.text();
|
||
let id = keep_terminal(Terminal { command: cmd.to_string(), io: Io::Screen(screen), child, exit, ended, kill, root, conversation: ids.conversation.clone() });
|
||
Ok(format!(
|
||
"已在互動終端機 #{id} 執行({} 欄 × {} 列)。現在的畫面:\n{}\n\n用 write_terminal 打字(input 裡的換行就是按 Enter)或按鍵(keys:enter、up、down、tab、esc、ctrl-c…),用 read_terminal 看畫面(wait_ms/until 可以等它有新畫面或出現某段字再回來)。用完按 ctrl-c 或讓它結束。",
|
||
crate::pty::COLS,
|
||
crate::pty::ROWS,
|
||
boxed(&shown)
|
||
))
|
||
}
|
||
|
||
/// A screen between rules, so where it starts and ends is plain.
|
||
fn boxed(screen: &str) -> String {
|
||
format!("────────\n{}\n────────", if screen.trim().is_empty() { "(畫面還是空的)" } else { screen })
|
||
}
|
||
|
||
/// (command, what it shows, its exit code once ended, its process).
|
||
type TermHandles = (String, Io, Arc<Mutex<Option<i32>>>, u32);
|
||
|
||
/// A background command's handles, taken out of the registry (its lock isn't held while
|
||
/// waiting or writing: a full pipe or a slow program must not stop every other terminal).
|
||
fn terminal_of(id: u64) -> Result<TermHandles, String> {
|
||
let t = terminals().lock().unwrap();
|
||
let term = t.get(&id).ok_or_else(|| format!("沒有背景指令 #{id}"))?;
|
||
Ok((term.command.clone(), term.io.clone(), term.exit.clone(), term.root))
|
||
}
|
||
|
||
/// read_terminal: what it shows now — or, with `wait_ms`, once it has something new (or
|
||
/// `until` shows up, or it ends).
|
||
fn read_terminal(id: u64, wait_ms: u64, until: &str) -> Result<String, String> {
|
||
let (command, io, exit, root) = terminal_of(id)?;
|
||
let wait = Duration::from_millis(wait_ms.min(TERMINAL_WAIT_MAX_MS));
|
||
let ended = || exit.lock().unwrap().is_some();
|
||
let body = match &io {
|
||
Io::Screen(screen) => {
|
||
if !wait.is_zero() || !until.is_empty() {
|
||
let since = screen.bytes();
|
||
crate::pty::settle(screen, since, wait.max(Duration::from_millis(1)), Duration::from_millis(SCREEN_QUIET_MS), until, ended, || false);
|
||
}
|
||
format!("畫面:\n{}", boxed(&screen.text()))
|
||
}
|
||
Io::Pipe(output) => {
|
||
if !wait.is_zero() || !until.is_empty() {
|
||
let since = output.lock().unwrap().len();
|
||
let start = Instant::now();
|
||
while start.elapsed() < wait.max(Duration::from_millis(1)) && !ended() {
|
||
let o = output.lock().unwrap();
|
||
if (!until.is_empty() && o.contains(until)) || (until.is_empty() && o.len() > since) {
|
||
break;
|
||
}
|
||
drop(o);
|
||
std::thread::sleep(Duration::from_millis(100));
|
||
}
|
||
}
|
||
head_tail(&output.lock().unwrap())
|
||
}
|
||
};
|
||
let state = match *exit.lock().unwrap() {
|
||
Some(c) => format!("已結束(代碼 {c})"),
|
||
None => {
|
||
let (n, cpu, mem) = tree_stats(root);
|
||
format!("還在執行:{n} 個程式,CPU 時間 {:.1} 秒,記憶體 {mem} MB", cpu as f64 / 1000.0)
|
||
}
|
||
};
|
||
let found = if until.is_empty() { String::new() } else if body.contains(until) { format!("\n(已出現「{until}」)") } else { format!("\n(還沒出現「{until}」)") };
|
||
Ok(format!("背景指令 #{id}:{command}\n狀態:{state}{found}\n{body}"))
|
||
}
|
||
|
||
/// write_terminal: type into a 互動終端機 — words (a new line is Enter) and named keys — then
|
||
/// look at its screen once it settles (or after `wait_ms`).
|
||
fn write_terminal(id: u64, input: &str, keys: &[String], close: bool, wait_ms: u64) -> Result<String, String> {
|
||
let (_, io, exit, _) = terminal_of(id)?;
|
||
if exit.lock().unwrap().is_some() {
|
||
return Err(format!("背景指令 #{id} 已經結束了"));
|
||
}
|
||
let Io::Screen(screen) = &io else {
|
||
return Err(format!("背景指令 #{id} 不是互動終端機,沒有開著輸入(一般指令一開始就關掉輸入,程式才不會一直等)。要回答它的問題或操作它的畫面,用 shell 加 interactive: true 重新執行;能用參數的(-y、--yes、-p)就用參數,不用互動。"));
|
||
};
|
||
let mut seq: Vec<Vec<u8>> = vec![];
|
||
// Words, then Enter for each new line — sent apart, as typed: a program that sees the words
|
||
// and the Enter arrive together may take them as a paste and not send.
|
||
let lines: Vec<&str> = input.split('\n').collect();
|
||
for (i, line) in lines.iter().enumerate() {
|
||
let line = line.strip_suffix('\r').unwrap_or(line);
|
||
if !line.is_empty() {
|
||
seq.push(line.as_bytes().to_vec());
|
||
}
|
||
if i + 1 < lines.len() {
|
||
seq.push(b"\r".to_vec());
|
||
}
|
||
}
|
||
for k in keys {
|
||
seq.push(crate::pty::key(k)?);
|
||
}
|
||
if close {
|
||
seq.push(vec![4]);
|
||
}
|
||
if seq.is_empty() {
|
||
return Err("要送什麼?給 input(文字)或 keys(按鍵)".into());
|
||
}
|
||
let since = screen.bytes();
|
||
for (i, bytes) in seq.iter().enumerate() {
|
||
if i > 0 {
|
||
std::thread::sleep(Duration::from_millis(120));
|
||
}
|
||
screen.send(bytes)?;
|
||
}
|
||
let wait = if wait_ms == 0 { 4_000 } else { wait_ms.min(TERMINAL_WAIT_MAX_MS) };
|
||
crate::pty::settle(screen, since, Duration::from_millis(wait), Duration::from_millis(SCREEN_QUIET_MS), "", || exit.lock().unwrap().is_some(), || false);
|
||
let what: Vec<String> = [(!input.is_empty()).then(|| format!("{} 個字", input.chars().count())), (!keys.is_empty()).then(|| format!("按鍵 {}", keys.join("、"))), close.then(|| "ctrl-d".to_string())].into_iter().flatten().collect();
|
||
Ok(format!("已送出 {}。\n{}", what.join("、"), read_terminal(id, 0, "")?))
|
||
}
|
||
|
||
/// End a command where it really runs (see Terminal::kill).
|
||
fn kill_far(kill: &Option<Vec<String>>) {
|
||
if let Some((head, rest)) = kill.as_ref().and_then(|k| k.split_first()) {
|
||
let _ = Command::new(head).args(rest).stdin(Stdio::null()).stdout(Stdio::null()).stderr(Stdio::null()).status();
|
||
}
|
||
}
|
||
|
||
/// Stop one background command and what it started (write_terminal with stop: true).
|
||
fn stop_terminal(id: u64) -> Result<String, String> {
|
||
let (child, exit, root, far) = {
|
||
let t = terminals().lock().unwrap();
|
||
let term = t.get(&id).ok_or_else(|| format!("沒有背景指令 #{id}"))?;
|
||
(term.child.clone(), term.exit.clone(), term.root, term.kill.clone())
|
||
};
|
||
if exit.lock().unwrap().is_some() {
|
||
return Ok(format!("背景指令 #{id} 本來就結束了。"));
|
||
}
|
||
kill_tree(root);
|
||
child.lock().unwrap().kill();
|
||
kill_far(&far);
|
||
Ok(format!("已停止背景指令 #{id}(連同它啟動的程式)。"))
|
||
}
|
||
|
||
/// Stop every background command and what it started (when the app quits).
|
||
pub fn stop_all() {
|
||
for t in terminals().lock().unwrap().values() {
|
||
// child before exit — the watcher thread takes them in this order; the reverse order
|
||
// here could wedge both threads forever (AB-BA).
|
||
let mut child = t.child.lock().unwrap();
|
||
if t.exit.lock().unwrap().is_none() {
|
||
kill_tree(t.root);
|
||
child.kill();
|
||
kill_far(&t.kill);
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Stop one bot's background commands (and what they started). Returns how many were running.
|
||
/// 按停止: a bot's 互動終端機 (and what each started) end with its turn — the plain background
|
||
/// commands keep running (a server it started on purpose), as they do until the bot is restarted
|
||
/// or AnyBot closes. Returns how many it stopped.
|
||
pub fn stop_screens_for(conversation: &str) -> usize {
|
||
let mut n = 0;
|
||
for t in terminals().lock().unwrap().values().filter(|t| t.conversation == conversation && matches!(t.io, Io::Screen(_))) {
|
||
// Same lock order as the watcher thread (child, then exit).
|
||
let mut child = t.child.lock().unwrap();
|
||
if t.exit.lock().unwrap().is_none() {
|
||
kill_tree(t.root);
|
||
child.kill();
|
||
kill_far(&t.kill);
|
||
n += 1;
|
||
}
|
||
}
|
||
n
|
||
}
|
||
|
||
pub fn stop_for(conversation: &str) -> usize {
|
||
let mut n = 0;
|
||
for t in terminals().lock().unwrap().values().filter(|t| t.conversation == conversation) {
|
||
// Same lock order as the watcher thread (child, then exit).
|
||
let mut child = t.child.lock().unwrap();
|
||
if t.exit.lock().unwrap().is_none() {
|
||
kill_tree(t.root);
|
||
child.kill();
|
||
kill_far(&t.kill);
|
||
n += 1;
|
||
}
|
||
}
|
||
n
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
|
||
fn wd() -> PathBuf {
|
||
let d = std::env::temp_dir().join(format!("anybot-test-{}", std::process::id()));
|
||
std::fs::create_dir_all(&d).unwrap();
|
||
d
|
||
}
|
||
|
||
#[test]
|
||
fn splits_chains() {
|
||
assert_eq!(split_commands("ls -la && git status | head; echo 'a;b'").unwrap(), vec!["ls -la", "git status", "head", "echo 'a;b'"]);
|
||
assert!(split_commands("echo $(whoami)").is_none());
|
||
assert!(split_commands("eval ls").is_none());
|
||
// A backslash-quote opens no quote: these are three commands, as the shell runs them.
|
||
assert_eq!(split_commands(r#"echo \"; rm -rf x; echo \""#).unwrap(), vec![r#"echo \""#, "rm -rf x", r#"echo \""#]);
|
||
assert_eq!(split_commands(r"echo a\;b; ls").unwrap(), vec![r"echo a\;b", "ls"]);
|
||
assert_eq!(split_commands(r#"echo "a\"; b" && ls"#).unwrap(), vec![r#"echo "a\"; b""#, "ls"]);
|
||
assert_eq!(split_commands(r"echo 'a\' ; ls").unwrap(), vec![r"echo 'a\'", "ls"]);
|
||
assert!(split_commands("echo 'never closed").is_none());
|
||
// What hides inside $(…) stays inside its own part.
|
||
let pieces = split_shell(r#"npm install; rm -rf x; git commit -m "$(cat a; cat b)" | head"#).unwrap();
|
||
assert_eq!(pieces.iter().map(|p| (p.text.as_str(), p.hidden)).collect::<Vec<_>>(), vec![("npm install", false), ("rm -rf x", false), (r#"git commit -m "$(cat a; cat b)""#, true), ("head", false)]);
|
||
assert!(split_shell("echo $(ls").is_none());
|
||
}
|
||
|
||
#[test]
|
||
fn looking_commands_that_do_more_than_look_ask() {
|
||
let w = wd();
|
||
let ask = |cmd: &str, grants: &[String]| decide("shell", &serde_json::json!({"command": cmd}).to_string(), &w, grants);
|
||
// The first word says "look"; the options delete, write, or run something else.
|
||
for cmd in [
|
||
"find . -name '*.docx' -delete",
|
||
"find . -maxdepth 0 -exec sh -c 'id' \\;",
|
||
"find . -execdir rm {} +",
|
||
"find . -ok rm {} \\;",
|
||
"find . -fprint out.txt",
|
||
"find . -del\"ete\"",
|
||
"find . -de\\lete",
|
||
"find . -${nothing}delete",
|
||
"find . -{delete,print}",
|
||
"sort -o notes.txt notes.txt",
|
||
"sort -ro notes.txt notes.txt",
|
||
"sort --output=notes.txt notes.txt",
|
||
"sort --compress-program=sh notes.txt",
|
||
"rg --pre ./run.sh pattern",
|
||
"rg --pre=./run.sh pattern",
|
||
"git diff --output=notes.txt",
|
||
"git log --output notes.txt",
|
||
"tree -o notes.txt",
|
||
"uniq a.txt b.txt",
|
||
"ls && find . -delete",
|
||
] {
|
||
assert!(matches!(ask(cmd, &[]), Decision::Ask { .. }), "{cmd} went through without asking");
|
||
}
|
||
// Plain looking still doesn't ask.
|
||
for cmd in [
|
||
"find . -name '*.rs' -type f",
|
||
"find . -newer Cargo.toml -print | sort | uniq -c | head",
|
||
"rg -n 'fn main$' src",
|
||
"git show HEAD@{1} --stat",
|
||
"git log --oneline -5",
|
||
"sort -rn counts.txt",
|
||
"echo $HOME && ls $HOME",
|
||
"tree -L 2",
|
||
] {
|
||
assert_eq!(ask(cmd, &[]), Decision::Allow, "{cmd}");
|
||
}
|
||
// A file can be named like an option: a pattern that may become one asks; one that
|
||
// can only become paths in a folder doesn't.
|
||
for cmd in ["find . *", "rg pattern --pr*", "sort [a-z]*", "find . -name ?delete", "sort -?"] {
|
||
assert!(matches!(ask(cmd, &[]), Decision::Ask { .. }), "{cmd} went through without asking");
|
||
}
|
||
for cmd in ["rg -n main src/*.rs", "find . -name \"*.rs\"", "ls *", "cat *.md | head", "sort notes/*.txt"] {
|
||
assert_eq!(ask(cmd, &[]), Decision::Allow, "{cmd}");
|
||
}
|
||
// 「一律允許」 isn't offered for these, and one on the first word doesn't cover them.
|
||
let find = vec!["shell:find".to_string(), "shell:sort".to_string()];
|
||
for cmd in ["find . -delete", "sort -o notes.txt notes.txt", "find . $(echo -delete)", "find . -name \"$(cat name.txt)\""] {
|
||
assert!(matches!(ask(cmd, &find), Decision::Ask { ref grant, .. } if grant.is_empty()), "{cmd}");
|
||
}
|
||
assert_eq!(ask("find . -name '*.rs'", &find), Decision::Allow);
|
||
// The reported one: an escaped quote hid two more commands inside "one echo".
|
||
assert!(matches!(ask(r#"echo \"; rm -rf Documents; echo \""#, &[]), Decision::Ask { .. }));
|
||
// 「一律允許」 on one command covers that command, not what is chained after it.
|
||
let npm = vec!["shell:npm install".to_string()];
|
||
assert_eq!(ask("npm install && ls", &npm), Decision::Allow);
|
||
assert!(matches!(ask("npm install; rm -rf x; echo $(true)", &npm), Decision::Ask { ref grant, .. } if grant.is_empty()));
|
||
assert!(matches!(ask("npm install; echo $(true)", &npm), Decision::Ask { ref grant, .. } if grant == "shell:echo"));
|
||
let git = vec!["shell:git commit".to_string()];
|
||
assert_eq!(ask(r#"git status && git commit -m "$(cat msg.txt)""#, &git), Decision::Allow);
|
||
// The always-denied list is read in every part, hidden commands or not.
|
||
assert!(matches!(ask("echo $(true); sudo rm x", &["shell:echo".to_string()]), Decision::Deny(_)));
|
||
// Quotes that never close: asked every time, no grant offered or honoured.
|
||
assert!(matches!(ask("npm install\nrm -rf x\necho '", &npm), Decision::Ask { ref grant, .. } if grant.is_empty()));
|
||
}
|
||
|
||
/// A link in the folder that points out of it is outside it: for reading, for editing, and
|
||
/// from the bot's sealed folder.
|
||
#[cfg(unix)]
|
||
#[test]
|
||
fn a_link_out_of_the_folder_is_outside_it() {
|
||
let w = wd().join("link-out");
|
||
let _ = std::fs::remove_dir_all(&w);
|
||
let (work, elsewhere) = (w.join("work"), w.join("elsewhere"));
|
||
std::fs::create_dir_all(work.join("sub")).unwrap();
|
||
std::fs::create_dir_all(&elsewhere).unwrap();
|
||
std::fs::write(elsewhere.join("taxes.txt"), "x").unwrap();
|
||
std::fs::write(work.join("sub/notes.txt"), "x").unwrap();
|
||
std::os::unix::fs::symlink(&elsewhere, work.join("out")).unwrap();
|
||
std::os::unix::fs::symlink(elsewhere.join("taxes.txt"), work.join("taxes")).unwrap();
|
||
std::os::unix::fs::symlink(elsewhere.join("not-yet.txt"), work.join("new")).unwrap();
|
||
std::os::unix::fs::symlink(work.join("sub"), work.join("same")).unwrap();
|
||
let args = |p: &str| json!({"path": p}).to_string();
|
||
let edits = ["edit_file".to_string()];
|
||
for p in ["out/taxes.txt", "taxes", "out", "new", "out/made-here.txt"] {
|
||
assert!(matches!(decide("read_file", &args(p), &work, &[]), Decision::Ask { ref grant, .. } if grant == "read:anywhere"), "read {p}");
|
||
assert!(matches!(decide("edit_file", &args(p), &work, &edits), Decision::Ask { ref grant, .. } if grant.is_empty()), "edit {p}: asked every time");
|
||
let sealed = Target { name: crate::model::BOT_COMPUTER_NAME.into(), place: Place::Sealed { root: work.clone() } };
|
||
assert!(matches!(decide_on("read_file", &args(p), &sealed, &[]), Decision::Deny(_)), "sealed read {p}");
|
||
assert!(matches!(decide_on("edit_file", &args(p), &sealed, &edits), Decision::Deny(_)), "sealed edit {p}");
|
||
}
|
||
assert!(matches!(decide("search", &args("out"), &work, &[]), Decision::Ask { .. }));
|
||
// What stays in the folder is as before: its files, a link to its own inside, a new file.
|
||
for p in ["sub/notes.txt", "same/notes.txt", "sub/new.txt", "brand/new/file.txt", ""] {
|
||
assert_eq!(decide("read_file", &args(p), &work, &[]), Decision::Allow, "read {p}");
|
||
assert_eq!(decide("edit_file", &args(p), &work, &edits), Decision::Allow, "edit {p}");
|
||
}
|
||
// A private file is private by where it really is, whatever the link is called.
|
||
std::fs::create_dir_all(elsewhere.join(".ssh")).unwrap();
|
||
std::fs::write(elsewhere.join(".ssh/config"), "x").unwrap();
|
||
std::os::unix::fs::symlink(elsewhere.join(".ssh/config"), work.join("readme.txt")).unwrap();
|
||
assert!(matches!(decide("read_file", &args("readme.txt"), &work, &["read:anywhere".to_string()]), Decision::Deny(_)));
|
||
let _ = std::fs::remove_dir_all(&w);
|
||
}
|
||
|
||
#[test]
|
||
fn a_file_on_its_way_out_meets_the_walls_of_reading_it() {
|
||
let w = wd().join("out-gate");
|
||
let _ = std::fs::remove_dir_all(&w);
|
||
let (inside_dir, elsewhere) = (w.join("work"), w.join("elsewhere"));
|
||
std::fs::create_dir_all(&inside_dir).unwrap();
|
||
std::fs::create_dir_all(&elsewhere).unwrap();
|
||
std::fs::write(inside_dir.join("report.pdf"), "x").unwrap();
|
||
std::fs::write(elsewhere.join("taxes.pdf"), "x").unwrap();
|
||
let local = Target { name: "這台電腦".into(), place: Place::Local { workdir: inside_dir.clone() } };
|
||
let sealed = Target { name: crate::model::BOT_COMPUTER_NAME.into(), place: Place::Sealed { root: inside_dir.clone() } };
|
||
let outside = elsewhere.join("taxes.pdf").display().to_string();
|
||
// Its own folder: nothing to ask, on either kind of computer; a file not there yet too.
|
||
for t in [&local, &sealed] {
|
||
assert_eq!(decide_file_out(t, "report.pdf", &[]), Decision::Allow);
|
||
assert_eq!(decide_file_out(t, "not-yet.pdf", &[]), Decision::Allow);
|
||
}
|
||
// The person's computer: outside the work folder asks, like read_file, and remembers.
|
||
assert!(matches!(decide_file_out(&local, &outside, &[]), Decision::Ask { ref grant, .. } if grant == "read:anywhere"));
|
||
assert!(matches!(decide_file_out(&local, "../elsewhere/taxes.pdf", &[]), Decision::Ask { .. }));
|
||
assert_eq!(decide_file_out(&local, &outside, &["read:anywhere".to_string()]), Decision::Allow);
|
||
// The bot's sealed folder: the rest of the computer isn't there, however it is spelled.
|
||
assert!(matches!(decide_file_out(&sealed, &outside, &["read:anywhere".to_string()]), Decision::Deny(_)));
|
||
assert!(matches!(decide_file_out(&sealed, "../elsewhere/taxes.pdf", &[]), Decision::Deny(_)));
|
||
// Secrets stay, granted or not.
|
||
assert!(matches!(decide_file_out(&local, ".env", &["read:anywhere".to_string()]), Decision::Deny(_)));
|
||
// A link inside the folder that points out of it is outside.
|
||
#[cfg(unix)]
|
||
{
|
||
std::os::unix::fs::symlink(&elsewhere, inside_dir.join("way-out")).unwrap();
|
||
assert!(matches!(decide_file_out(&sealed, "way-out/taxes.pdf", &[]), Decision::Deny(_)));
|
||
assert!(matches!(decide_file_out(&local, "way-out/taxes.pdf", &[]), Decision::Ask { .. }));
|
||
std::fs::create_dir_all(elsewhere.join(".ssh")).unwrap();
|
||
std::fs::write(elsewhere.join(".ssh/config"), "x").unwrap();
|
||
std::os::unix::fs::symlink(elsewhere.join(".ssh/config"), inside_dir.join("notes.txt")).unwrap();
|
||
assert!(matches!(decide_file_out(&local, "notes.txt", &["read:anywhere".to_string()]), Decision::Deny(_)));
|
||
}
|
||
// The box keeps each bot inside its own folder; other computers answer for their own files.
|
||
let on_box = Target { name: "Bot 的電腦".into(), place: Place::Box { dir: "/home/bot/bot-1".into() } };
|
||
assert!(matches!(decide_file_out(&on_box, "/etc/hosts", &[]), Decision::Deny(_)));
|
||
assert_eq!(decide_file_out(&on_box, "report.pdf", &[]), Decision::Allow);
|
||
let _ = std::fs::remove_dir_all(&w);
|
||
}
|
||
|
||
#[test]
|
||
fn layered_rules() {
|
||
let w = wd();
|
||
assert_eq!(decide("shell", r#"{"command":"ls && git status"}"#, &w, &[]), Decision::Allow);
|
||
assert!(matches!(decide("shell", r#"{"command":"ls && sudo rm x"}"#, &w, &[]), Decision::Deny(_)));
|
||
// Secrets are the sandbox's job; the permission layer no longer denies words that
|
||
// merely look like secret paths. Read-only commands run (cat .env is cat), and the
|
||
// sandbox hides the protected folders from them.
|
||
assert_eq!(decide("shell", r#"{"command":"cat .env"}"#, &w, &[]), Decision::Allow);
|
||
assert!(matches!(decide("shell", r#"{"command":"echo $(whoami)"}"#, &w, &[]), Decision::Ask { ref grant, .. } if grant == "shell:echo"));
|
||
assert_eq!(decide("shell", r#"{"command":"echo $(whoami)"}"#, &w, &["shell:echo".into()]), Decision::Allow);
|
||
assert!(matches!(decide("shell", r#"{"command":"npm install"}"#, &w, &[]), Decision::Ask { ref grant, .. } if grant == "shell:npm install"));
|
||
assert_eq!(decide("shell", r#"{"command":"npm install && ls"}"#, &w, &["shell:npm install".into()]), Decision::Allow);
|
||
assert!(matches!(decide("shell", r#"{"command":"echo hi > out.txt"}"#, &w, &[]), Decision::Ask { .. }));
|
||
assert_eq!(decide("read_file", r#"{"path":"a.txt"}"#, &w, &[]), Decision::Allow);
|
||
assert!(matches!(decide("read_file", r#"{"path":"/etc/hosts"}"#, &w, &[]), Decision::Ask { .. }));
|
||
assert!(matches!(decide("read_file", r#"{"path":"../../.ssh/id_rsa"}"#, &w, &[]), Decision::Deny(_) | Decision::Ask { .. }));
|
||
assert!(matches!(decide("edit_file", r#"{"path":"a.txt","new_text":"x"}"#, &w, &[]), Decision::Ask { .. }));
|
||
assert_eq!(decide("edit_file", r#"{"path":"a.txt","new_text":"x"}"#, &w, &["edit_file".into()]), Decision::Allow);
|
||
}
|
||
|
||
/// 換電腦: a work folder chosen on one computer still works on another with a different
|
||
/// system or user name.
|
||
#[test]
|
||
fn a_work_folder_from_another_computer_lands_under_this_home() {
|
||
assert_eq!(portable_dir("/Users/daniel"), "~");
|
||
assert_eq!(portable_dir("/Users/daniel/Documents/專案/"), "~/Documents/專案");
|
||
assert_eq!(portable_dir("/home/user/work"), "~/work");
|
||
assert_eq!(portable_dir(r"C:\Users\Daniel\Desktop"), "~/Desktop");
|
||
// Not under a home: as it is.
|
||
assert_eq!(portable_dir("/srv/data"), "/srv/data");
|
||
assert_eq!(portable_dir("~/x"), "~/x");
|
||
assert_eq!(portable_dir("Users/daniel"), "Users/daniel");
|
||
assert_eq!(portable_dir(""), "");
|
||
|
||
let home = std::env::temp_dir().join(format!("anybot-home-{}", std::process::id()));
|
||
let _ = std::fs::remove_dir_all(&home);
|
||
std::fs::create_dir_all(home.join("Documents")).unwrap();
|
||
// The Mac's home, on a Linux computer: this home.
|
||
assert_eq!(workdir_under("/Users/someone-else", &home), home);
|
||
// A folder under it that is here too, and one that isn't.
|
||
assert_eq!(workdir_under("/Users/someone-else/Documents", &home), home.join("Documents"));
|
||
assert_eq!(workdir_under("/Users/someone-else/Nope", &home), home);
|
||
assert_eq!(workdir_under("~/Documents", &home), home.join("Documents"));
|
||
assert_eq!(workdir_under("~/Nope", &home), home);
|
||
assert_eq!(workdir_under("", &home), home);
|
||
assert_eq!(workdir_under("relative/path", &home), home);
|
||
// A real folder outside any home is used as given; a missing one falls back.
|
||
let real = home.join("Documents");
|
||
assert_eq!(workdir_under(real.to_str().unwrap(), &home), real);
|
||
assert_eq!(workdir_under("/no/such/folder/anywhere", &home), home);
|
||
let _ = std::fs::remove_dir_all(&home);
|
||
}
|
||
|
||
#[test]
|
||
fn edit_read_search() {
|
||
let w = wd();
|
||
let c = AtomicBool::new(false);
|
||
assert!(run("edit_file", r#"{"path":"n/a.txt","new_text":"hello\nworld\n"}"#, &w, &c, &RunIds::default()).1);
|
||
assert!(run("edit_file", r#"{"path":"n/a.txt","old_text":"world","new_text":"there"}"#, &w, &c, &RunIds::default()).1);
|
||
let (r, ok) = run("read_file", r#"{"path":"n/a.txt"}"#, &w, &c, &RunIds::default());
|
||
assert!(ok && r.contains("there"), "{r}");
|
||
let (s, ok) = run("search", r#"{"pattern":"there"}"#, &w, &c, &RunIds::default());
|
||
assert!(ok && s.contains("a.txt"), "{s}");
|
||
assert!(!run("edit_file", r#"{"path":"n/a.txt","old_text":"missing","new_text":"x"}"#, &w, &c, &RunIds::default()).1);
|
||
}
|
||
|
||
#[test]
|
||
fn shell_moves_to_background() {
|
||
let w = wd();
|
||
let c = AtomicBool::new(false);
|
||
let (r, ok) = run("shell", r#"{"command":"echo start; sleep 1; echo end","timeout_ms":200}"#, &w, &c, &RunIds::default());
|
||
assert!(ok && r.contains("背景指令 #"), "{r}");
|
||
let id: u64 = r.split("背景指令 #").nth(1).unwrap().split(')').next().unwrap().parse().unwrap();
|
||
std::thread::sleep(Duration::from_millis(1500));
|
||
let (t, _) = run("read_terminal", &format!(r#"{{"id":{id}}}"#), &w, &c, &RunIds::default());
|
||
assert!(t.contains("end") && t.contains("已結束"), "{t}");
|
||
let (q, ok) = run("shell", r#"{"command":"echo quick"}"#, &w, &c, &RunIds::default());
|
||
assert!(ok && q.contains("quick"));
|
||
}
|
||
|
||
#[test]
|
||
fn sandbox_breakers_are_recognized_and_nothing_else() {
|
||
assert!(process_listing("ps aux"));
|
||
assert!(process_listing("ps -ax | grep -i tailscale | grep -v grep"));
|
||
assert!(process_listing("/bin/ps -o pid,comm | awk '{print $1}' | head -5"));
|
||
assert!(process_listing("ps aux | awk -F: -v n=2 'NR == n {print $2, $11}' | sort -rn | uniq -c"));
|
||
// awk can start any program itself, and these run outside the sandbox.
|
||
assert!(!process_listing(r#"ps aux | awk 'BEGIN { system("rm -rf Documents") }'"#));
|
||
assert!(!process_listing(r#"ps aux | awk '{ "id" | getline x; print x }'"#));
|
||
assert!(!process_listing("ps aux | awk -f steal.awk"));
|
||
assert!(!process_listing("ps aux | awk '{print}' notes.txt"));
|
||
assert!(!process_listing("ps aux | awk '@load \"x\"; {print}'"));
|
||
assert!(!process_listing("ps aux | sort -o listing.txt"));
|
||
assert!(!process_listing("ps aux | sort --compress-program=sh"));
|
||
assert!(!process_listing("ps aux | uniq - listing.txt"));
|
||
assert!(process_listing("top -l 1 -n 0"));
|
||
assert!(!process_listing("ps aux > /tmp/x"));
|
||
assert!(!process_listing("ps aux | grep x ~/.ssh/id_rsa"));
|
||
assert!(!process_listing("ps aux | xargs cat"));
|
||
assert!(!process_listing("ps aux; cat ~/.ssh/id_rsa"));
|
||
assert!(!process_listing("ps $(cat /etc/passwd)"));
|
||
assert!(!process_listing("pss aux"));
|
||
assert_eq!(plain_program("/Applications/Tailscale.app/Contents/MacOS/Tailscale status").as_deref(), Some("/Applications/Tailscale.app/Contents/MacOS/Tailscale"));
|
||
assert_eq!(plain_program("\"/Applications/My App.app/Contents/MacOS/My App\" --help").as_deref(), Some("/Applications/My App.app/Contents/MacOS/My App"));
|
||
assert_eq!(plain_program("/Applications/T.app/Contents/MacOS/T status; cat ~/.ssh/id_rsa"), None);
|
||
assert_eq!(plain_program("/Applications/T.app/Contents/MacOS/T status | tee x"), None);
|
||
assert!(!app_sandboxed_cli("/usr/bin/osascript"));
|
||
assert!(!app_sandboxed_cli("/Applications/../usr/bin/osascript"));
|
||
assert!(!app_sandboxed_cli("/Applications/Nope.app/Contents/MacOS/Nope"));
|
||
assert!(!runs_outside_sandbox("osascript -e 'do shell script \"cat ~/.ssh/id_rsa\"'"));
|
||
assert!(!runs_outside_sandbox("cat ~/.ssh/id_rsa"));
|
||
}
|
||
|
||
#[test]
|
||
fn sudo_is_the_bots_on_its_own_box_but_what_follows_is_still_checked() {
|
||
let b = Target { name: "Bot 的電腦".into(), place: Place::Box { dir: "/home/bot/bot-1".into() } };
|
||
let d = |c: &str| decide_on("shell", &json!({"command": c}).to_string(), &b, &[]);
|
||
for ok in ["sudo apt-get install -y tree", "sudo ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime && echo Asia/Tokyo | sudo tee /etc/timezone", "sudo -n -u root true", "ls; sudo -E make install"] {
|
||
assert!(matches!(d(ok), Decision::Allow), "{ok}");
|
||
}
|
||
for bad in ["sudo rm -rf /", "sudo -u root mkfs /dev/sda", "rm -rf ~", "sudo sudo dd if=/dev/zero of=/dev/sda", "su -c id", "sudo su"] {
|
||
assert!(matches!(d(bad), Decision::Deny(_)), "{bad}");
|
||
}
|
||
// The person's own computer is unchanged: sudo stays refused there.
|
||
let w = std::env::temp_dir();
|
||
assert!(matches!(decide("shell", r#"{"command":"sudo ls"}"#, &w, &[]), Decision::Deny(_)));
|
||
}
|
||
|
||
#[test]
|
||
fn applescript_runs_outside_only_when_plain_and_harmless() {
|
||
assert_eq!(plain_words(r#"osascript -e 'tell application "Tailscale" to quit' -e "set x to \"a\" & \"b\"""#).unwrap(), vec!["osascript", "-e", r#"tell application "Tailscale" to quit"#, "-e", r#"set x to "a" & "b""#]);
|
||
assert!(plain_words("osascript -e 'x'; cat ~/.ssh/id_rsa").is_none());
|
||
assert!(plain_words("osascript -e \"$(cat ~/.ssh/id_rsa)\"").is_none());
|
||
assert!(plain_words("osascript <<EOF\nx\nEOF").is_none());
|
||
assert!(plain_words("osascript -e 'x' > /tmp/o").is_none());
|
||
assert!(osascript_outside(r#"osascript -e 'tell application "System Events" to get name of first process whose frontmost is true'"#).is_ok());
|
||
assert!(osascript_outside(r#"osascript -e 'tell application "Tailscale" to activate' -e 'delay 1'"#).is_ok());
|
||
for bad in [
|
||
r#"osascript -e 'do shell script "cat ~/.ssh/id_rsa"'"#,
|
||
r#"osascript -e '«event sysoexec» ("ca" & "t ~/.s" & "sh/id_rsa")'"#,
|
||
r#"osascript -e 'tell application "Shortcuts Events" to run shortcut "x"'"#,
|
||
r#"osascript -e 'tell application "Terminal" to do script "id"'"#,
|
||
r#"osascript -e 'tell application "System Events" to keystroke "rm -rf ~"'"#,
|
||
r#"osascript -e 'read POSIX file "/Users/x/.aws/credentials"'"#,
|
||
r#"osascript -l JavaScript -e 'ObjC.import("Foundation")'"#,
|
||
"osascript ~/evil.scpt",
|
||
"osascript -i",
|
||
"osascript",
|
||
] {
|
||
assert!(osascript_outside(bad).is_err(), "{bad}");
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn a_command_the_sandbox_broke_says_so() {
|
||
if sandbox_exec().is_none() {
|
||
return;
|
||
}
|
||
assert!(sandbox_note("ps aux; echo", "/bin/sh: /bin/ps: Operation not permitted").unwrap().contains("pgrep"));
|
||
let err = "40:85: execution error: System Events發生錯誤:發生越權取用的錯誤。 (-10004)";
|
||
// Joined to another command, it stayed in the sandbox: say how to write it.
|
||
assert!(sandbox_note("osascript -e 'tell application \"System Events\" to …'; echo done", err).unwrap().contains("單獨一條 osascript"));
|
||
// Plain and harmless, it ran outside: a -10004 then is macOS's own answer, not the sandbox.
|
||
assert_eq!(sandbox_note("osascript -e 'tell application \"System Events\" to …'", err), None);
|
||
assert!(sandbox_note("/Applications/T.app/Contents/MacOS/T status && echo", "/bin/sh: line 1: 9823 Trace/BPT trap: 5").unwrap().contains("單獨執行"));
|
||
assert_eq!(sandbox_note("ls", "No such file or directory"), None);
|
||
}
|
||
|
||
/// Plain AppleScript drives another app (outside the sandbox); joined to another command it
|
||
/// stays in the sandbox and gets -10004 with a note. On a Mac with 自動化 allowed for the
|
||
/// test runner: `cargo test applescript_drives_apps -- --ignored --nocapture`.
|
||
#[test]
|
||
#[ignore]
|
||
fn applescript_drives_apps_outside_the_sandbox() {
|
||
let w = std::env::temp_dir();
|
||
let ask = r#"osascript -e 'tell application "System Events" to get name of first process whose frontmost is true'"#;
|
||
let out = build_command_sealed(ask, &w, &protected_dirs(), &readable_dirs(), None, "1", "r").output().unwrap();
|
||
println!("plain: {:?} {}", out.status, String::from_utf8_lossy(&out.stdout).trim());
|
||
assert!(out.status.success() && !String::from_utf8_lossy(&out.stdout).trim().is_empty(), "{out:?}");
|
||
let joined = format!("{ask}; true");
|
||
let out = build_command_sealed(&joined, &w, &protected_dirs(), &readable_dirs(), None, "1", "r").output().unwrap();
|
||
let text = String::from_utf8_lossy(&out.stderr).to_string();
|
||
println!("joined: {text}");
|
||
assert!(text.contains("-10004"));
|
||
assert!(sandbox_note(&joined, &text).unwrap().contains("單獨一條 osascript"));
|
||
}
|
||
|
||
/// An App Store app's command line runs outside AnyBot's sandbox and works; inside it dies.
|
||
/// Needs Tailscale from the App Store: `cargo test app_store_cli_runs -- --ignored`.
|
||
#[test]
|
||
#[ignore]
|
||
fn app_store_cli_runs_outside_the_sandbox() {
|
||
let t = "/Applications/Tailscale.app/Contents/MacOS/Tailscale";
|
||
assert!(runs_outside_sandbox(&format!("{t} version")));
|
||
let w = std::env::temp_dir();
|
||
let out = build_command_sealed(&format!("{t} version"), &w, &protected_dirs(), &readable_dirs(), None, "1", "r").output().unwrap();
|
||
assert!(String::from_utf8_lossy(&out.stdout).trim().chars().next().map(|c| c.is_ascii_digit()).unwrap_or(false), "{out:?}");
|
||
let ps = build_command_sealed("ps -ax | grep -c .", &w, &protected_dirs(), &readable_dirs(), None, "1", "r").output().unwrap();
|
||
assert!(ps.status.success(), "{ps:?}");
|
||
}
|
||
|
||
#[test]
|
||
fn head_tail_trims_middle() {
|
||
let s = "a".repeat(OUTPUT_HEAD_CHARS) + &"b".repeat(5000) + &"c".repeat(OUTPUT_TAIL_CHARS);
|
||
let t = head_tail(&s);
|
||
assert!(t.contains("中間省略 5000 個字;完整內容用 read_result 讀"));
|
||
}
|
||
|
||
#[test]
|
||
fn cpu_time_and_tree_walk() {
|
||
assert_eq!(parse_cpu_time("0:01.50"), 1500);
|
||
assert_eq!(parse_cpu_time("01:02:03"), 3_723_000);
|
||
assert_eq!(parse_cpu_time("1-00:00:00"), 86_400_000);
|
||
let t = [
|
||
ProcRow { pid: 10, ppid: 1, rss_kb: 2048, cpu_ms: 5 },
|
||
ProcRow { pid: 11, ppid: 10, rss_kb: 1024, cpu_ms: 7 },
|
||
ProcRow { pid: 12, ppid: 11, rss_kb: 0, cpu_ms: 0 },
|
||
ProcRow { pid: 13, ppid: 1, rss_kb: 0, cpu_ms: 0 },
|
||
];
|
||
assert_eq!(descendants(&t, &[10]), vec![10, 11, 12]);
|
||
assert_eq!(descendants(&t, &[10, 11]), vec![10, 11, 12]);
|
||
}
|
||
|
||
#[test]
|
||
fn commands_are_tagged_and_keys_hidden() {
|
||
let w = wd();
|
||
let ids = RunIds { conversation: "7".into(), request: "call_x".into() };
|
||
let (r, ok) = run("shell", r#"{"command":"echo $ANYBOT_CONVERSATION_ID/$ANYBOT_REQUEST_ID/${OPENAI_API_KEY:-none}"}"#, &w, &AtomicBool::new(false), &ids);
|
||
assert!(ok && r.contains("7/call_x/none"), "{r}");
|
||
}
|
||
|
||
#[test]
|
||
fn sandbox_blocks_protected_folders() {
|
||
if sandbox_exec().is_none() {
|
||
return;
|
||
}
|
||
let w = wd();
|
||
let secret = std::env::temp_dir().join(format!("anybot-secret-{}", std::process::id()));
|
||
std::fs::create_dir_all(&secret).unwrap();
|
||
std::fs::write(secret.join("k.txt"), "top-secret").unwrap();
|
||
let out = build_command(&format!("cat '{}/k.txt'", secret.display()), &w, std::slice::from_ref(&secret), "1", "r").output().unwrap();
|
||
assert!(!String::from_utf8_lossy(&out.stdout).contains("top-secret"));
|
||
let out = build_command(&format!("cat '{}/k.txt'", secret.display()), &w, &[], "1", "r").output().unwrap();
|
||
assert!(String::from_utf8_lossy(&out.stdout).contains("top-secret"));
|
||
// The chat's attachments inside it: readable, not writable; the keys next to them: hidden.
|
||
let att = secret.join("attachments");
|
||
std::fs::create_dir_all(&att).unwrap();
|
||
std::fs::write(att.join("photo.txt"), "the-photo").unwrap();
|
||
let run = |cmd: String| build_command_sealed(&cmd, &w, std::slice::from_ref(&secret), std::slice::from_ref(&att), None, "1", "r").output().unwrap();
|
||
assert!(String::from_utf8_lossy(&run(format!("cat '{}/photo.txt'", att.display())).stdout).contains("the-photo"));
|
||
assert!(!String::from_utf8_lossy(&run(format!("cat '{}/k.txt'", secret.display())).stdout).contains("top-secret"));
|
||
run(format!("echo changed > '{}/photo.txt'", att.display()));
|
||
assert_eq!(std::fs::read_to_string(att.join("photo.txt")).unwrap(), "the-photo");
|
||
let _ = std::fs::remove_dir_all(&secret);
|
||
}
|
||
|
||
#[test]
|
||
fn bot_computer_is_sealed() {
|
||
if sandbox_exec().is_none() {
|
||
// No sandbox (Linux, Windows): no command runs there at all, asked or not.
|
||
let t = Target { name: crate::model::BOT_COMPUTER_NAME.into(), place: Place::Sealed { root: std::env::temp_dir().join("anybot-no-seal") } };
|
||
assert!(matches!(decide_on("shell", r#"{"command":"cat ~/.ssh/id_rsa"}"#, &t, &[]), Decision::Deny(_)));
|
||
let (r, ok) = run_on("shell", r#"{"command":"echo ran"}"#, &t, &AtomicBool::new(false), &RunIds::default());
|
||
assert!(!ok && !r.contains("ran"), "{r}");
|
||
return;
|
||
}
|
||
let Some(home) = dirs::home_dir() else { return };
|
||
let root = home.join("AnyBot 工作區").join(format!("test-seal-{}", std::process::id()));
|
||
std::fs::create_dir_all(&root).unwrap();
|
||
let t = Target { name: crate::model::BOT_COMPUTER_NAME.into(), place: Place::Sealed { root: root.clone() } };
|
||
let c = AtomicBool::new(false);
|
||
let (r, ok) = run_on("shell", r#"{"command":"echo hi > mine.txt && cat mine.txt"}"#, &t, &c, &RunIds::default());
|
||
assert!(ok && r.contains("hi"), "{r}");
|
||
let (r, _) = run_on("shell", r#"{"command":"ls ~/Documents >/dev/null 2>&1 && echo SEEN || echo HIDDEN"}"#, &t, &c, &RunIds::default());
|
||
assert!(r.contains("HIDDEN"), "{r}");
|
||
let (r, _) = run_on("shell", r#"{"command":"ls ~/.ssh >/dev/null 2>&1 && echo SEEN || echo HIDDEN"}"#, &t, &c, &RunIds::default());
|
||
assert!(r.contains("HIDDEN"), "Sealed sandbox must never expose ~/.ssh: {r}");
|
||
assert!(matches!(decide_on("search_documents", r#"{"query":"密碼"}"#, &t, &[]), Decision::Deny(_)));
|
||
assert!(matches!(decide_on("read_file", r#"{"path":"~/Documents/x.txt"}"#, &t, &[]), Decision::Deny(_)));
|
||
assert_eq!(decide_on("edit_file", r#"{"path":"a.txt","new_text":"x"}"#, &t, &[]), Decision::Allow);
|
||
let _ = std::fs::remove_dir_all(&root);
|
||
}
|
||
|
||
#[test]
|
||
fn ssh_commands_are_quoted() {
|
||
assert_eq!(ssh_command("me@box", "/srv/app", "echo 'hi'"), "ssh -o BatchMode=yes -o ConnectTimeout=15 'me@box' 'cd '\\''/srv/app'\\'' && echo '\\''hi'\\'''");
|
||
let t = Target { name: "工作站".into(), place: Place::Ssh { target: "me@box".into(), dir: String::new() } };
|
||
assert!(matches!(decide_on("shell", r#"{"command":"npm test"}"#, &t, &[]), Decision::Ask { ref reason, .. } if reason.contains("工作站")));
|
||
assert_eq!(decide_on("shell", r#"{"command":"ls"}"#, &t, &[]), Decision::Allow);
|
||
let s = specs_for(&["A".into(), "B".into()], "A");
|
||
assert_eq!(s[0]["parameters"]["properties"]["computer"]["enum"][1], "B");
|
||
}
|
||
|
||
/// The terminal number after `mark` in a tool's answer.
|
||
fn term_id(text: &str, mark: &str) -> u64 {
|
||
text.split(mark).nth(1).map(|r| r.chars().take_while(char::is_ascii_digit).collect::<String>()).and_then(|n| n.parse().ok()).expect("a terminal id")
|
||
}
|
||
|
||
/// A plain command's input is closed: one that reads its input to the end finishes at once
|
||
/// (it used to wait for ever — `pi-bolt -p` never answered), and can't be typed into.
|
||
#[cfg(unix)]
|
||
#[test]
|
||
fn a_plain_command_never_waits_for_typing() {
|
||
let w = wd();
|
||
let cancel = AtomicBool::new(false);
|
||
let t = Instant::now();
|
||
let (out, ok) = run("shell", r#"{"command":"cat; echo 讀完了","timeout_ms":5000}"#, &w, &cancel, &RunIds::default());
|
||
assert!(ok && out.contains("讀完了") && t.elapsed() < Duration::from_secs(3), "{out}");
|
||
// In the background, it still takes no typing — and says how to get a terminal.
|
||
let (bg, _) = run("shell", r#"{"command":"sleep 3","timeout_ms":100}"#, &w, &cancel, &RunIds::default());
|
||
let id = term_id(&bg, "背景指令 #");
|
||
let (e, ok) = run("write_terminal", &json!({"id": id, "input": "y\n"}).to_string(), &w, &cancel, &RunIds::default());
|
||
assert!(!ok && e.contains("interactive"), "{e}");
|
||
}
|
||
|
||
/// 互動終端機: the screen is read as shown, typing and keys reach the program, and a wait
|
||
/// returns as soon as the words looked for show up.
|
||
#[cfg(unix)]
|
||
#[test]
|
||
fn an_interactive_terminal_shows_its_screen_and_takes_typing() {
|
||
let w = wd();
|
||
let cancel = AtomicBool::new(false);
|
||
let ids = RunIds { conversation: "t-pty".into(), request: "r".into() };
|
||
let cmd = json!({"command": "printf '\\033[2J\\033[H請輸入代號:'; read a; printf '\\n收到 %s\\n按任意鍵結束' \"$a\"; read b; echo 再見", "interactive": true, "timeout_ms": 5000}).to_string();
|
||
let (first, ok) = run("shell", &cmd, &w, &cancel, &ids);
|
||
assert!(ok && first.contains("互動終端機 #") && first.contains("請輸入代號:"), "{first}");
|
||
assert!(!first.contains("\u{1b}"), "the screen, not escape codes");
|
||
let id = term_id(&first, "互動終端機 #");
|
||
let (typed, ok) = run("write_terminal", &json!({"id": id, "input": "A7\n"}).to_string(), &w, &cancel, &ids);
|
||
assert!(ok && typed.contains("收到 A7"), "{typed}");
|
||
let (end, ok) = run("write_terminal", &json!({"id": id, "keys": ["enter"]}).to_string(), &w, &cancel, &ids);
|
||
assert!(ok && end.contains("再見") && end.contains("已結束"), "{end}");
|
||
// Stopping the bot stops its terminals.
|
||
let (_, ok) = run("shell", r#"{"command":"sleep 30","interactive":true,"timeout_ms":300}"#, &w, &cancel, &ids);
|
||
assert!(ok);
|
||
assert_eq!(stop_for("t-pty"), 1);
|
||
// 按停止: the turn's interactive terminal ends; a plain background command keeps going.
|
||
let ids = RunIds { conversation: "t-stop".into(), request: "r".into() };
|
||
let (_, ok) = run("shell", r#"{"command":"sleep 30","interactive":true,"timeout_ms":300}"#, &w, &cancel, &ids);
|
||
assert!(ok);
|
||
let (bg, ok) = run("shell", r#"{"command":"sleep 30","timeout_ms":300}"#, &w, &cancel, &ids);
|
||
assert!(ok && bg.contains("#"), "{bg}");
|
||
assert_eq!(stop_screens_for("t-stop"), 1);
|
||
// (Its end is noted by the watcher thread a moment later.)
|
||
let screens_left = || terminals().lock().unwrap().values().filter(|t| t.conversation == "t-stop" && matches!(t.io, Io::Screen(_)) && t.exit.lock().unwrap().is_none()).count();
|
||
for _ in 0..50 {
|
||
if screens_left() == 0 {
|
||
break;
|
||
}
|
||
std::thread::sleep(Duration::from_millis(100));
|
||
}
|
||
assert_eq!(screens_left(), 0);
|
||
assert_eq!(stop_for("t-stop"), 1, "the background one was still running");
|
||
}
|
||
|
||
/// A background command can be stopped by its number, and a finished one lets go of its
|
||
/// terminal (its screen becomes plain text) and is forgotten after a while.
|
||
#[cfg(unix)]
|
||
#[test]
|
||
fn background_commands_are_stopped_and_let_go() {
|
||
let w = wd();
|
||
let cancel = AtomicBool::new(false);
|
||
let ids = RunIds { conversation: "t-tidy".into(), request: "r".into() };
|
||
let (bg, _) = run("shell", r#"{"command":"sleep 30","timeout_ms":100}"#, &w, &cancel, &ids);
|
||
let id = term_id(&bg, "背景指令 #");
|
||
let (out, ok) = run("write_terminal", &json!({"id": id, "stop": true}).to_string(), &w, &cancel, &ids);
|
||
assert!(ok && out.contains("已停止"), "{out}");
|
||
let (pty, _) = run("shell", r#"{"command":"printf hi; sleep 30","interactive":true,"timeout_ms":500}"#, &w, &cancel, &ids);
|
||
let pid = term_id(&pty, "互動終端機 #");
|
||
run("write_terminal", &json!({"id": pid, "stop": true}).to_string(), &w, &cancel, &ids);
|
||
for _ in 0..50 {
|
||
if terminals().lock().unwrap().get(&pid).is_some_and(|t| t.ended.lock().unwrap().is_some()) {
|
||
break;
|
||
}
|
||
std::thread::sleep(Duration::from_millis(100));
|
||
}
|
||
let mut terms = terminals().lock().unwrap();
|
||
tidy_terminals(&mut terms);
|
||
assert!(matches!(terms.get(&pid).map(|t| &t.io), Some(Io::Pipe(_))), "its terminal is let go");
|
||
// Old enough: forgotten.
|
||
*terms.get(&pid).unwrap().ended.lock().unwrap() = Instant::now().checked_sub(TERMINAL_KEEP + Duration::from_secs(1));
|
||
tidy_terminals(&mut terms);
|
||
assert!(!terms.contains_key(&pid));
|
||
}
|
||
|
||
/// Opening a shell in a terminal is asked every time (what is typed into it later isn't
|
||
/// checked one by one); a program in a terminal follows the usual rules.
|
||
#[test]
|
||
fn an_interactive_shell_is_asked_each_time() {
|
||
let w = wd();
|
||
let ask = |c: &str| decide("shell", &json!({"command": c, "interactive": true}).to_string(), &w, &["shell:bash".into(), "shell:pi-bolt".into()]);
|
||
assert!(matches!(ask("bash"), Decision::Ask { ref grant, .. } if grant.is_empty()));
|
||
assert!(matches!(ask("cd x && zsh -l"), Decision::Ask { .. }));
|
||
assert_eq!(ask("pi-bolt"), Decision::Allow, "granted, and not a shell");
|
||
assert_eq!(decide("shell", r#"{"command":"bash"}"#, &w, &["shell:bash".into()]), Decision::Allow, "not in a terminal: as before");
|
||
assert_eq!(ask("bash -c 'ls'"), Decision::Allow);
|
||
}
|
||
|
||
/// The real thing, by hand (`cargo test pi_bolt -- --ignored` with pi-bolt installed and
|
||
/// signed in): through the tools as a bot uses them, with the PATH an AnyBot started at
|
||
/// login has. Its print mode answers; its own screen takes a question and shows the answer.
|
||
#[cfg(unix)]
|
||
#[test]
|
||
#[ignore]
|
||
fn pi_bolt_answers_in_print_mode_and_in_its_own_screen() {
|
||
std::env::set_var("PATH", "/usr/bin:/bin:/usr/sbin:/sbin");
|
||
let w = wd();
|
||
let cancel = AtomicBool::new(false);
|
||
let ids = RunIds { conversation: "t-pi".into(), request: "r".into() };
|
||
let (out, ok) = run("shell", r#"{"command":"pi-bolt -p --no-session '37 加 5 等於多少?只回答數字,不要用任何工具'","timeout_ms":120000}"#, &w, &cancel, &ids);
|
||
eprintln!("print mode:\n{out}");
|
||
assert!(ok && out.contains("42"), "{out}");
|
||
let (first, ok) = run("shell", r#"{"command":"pi-bolt --no-session","interactive":true,"timeout_ms":15000}"#, &w, &cancel, &ids);
|
||
eprintln!("first screen:\n{first}");
|
||
assert!(ok, "{first}");
|
||
let id = term_id(&first, "互動終端機 #");
|
||
let (typed, _) = run("write_terminal", &json!({"id": id, "input": "19 加 4 等於多少?只回答數字,不要用任何工具\n", "wait_ms": 3000}).to_string(), &w, &cancel, &ids);
|
||
eprintln!("after typing:\n{typed}");
|
||
let (answer, _) = run("read_terminal", &json!({"id": id, "wait_ms": 90000, "until": "23"}).to_string(), &w, &cancel, &ids);
|
||
eprintln!("answer:\n{answer}");
|
||
assert!(answer.contains("已出現「23」"), "{answer}");
|
||
stop_for("t-pi");
|
||
}
|
||
|
||
#[test]
|
||
fn stop_ends_the_whole_tree() {
|
||
let w = wd();
|
||
let cancel = Arc::new(AtomicBool::new(false));
|
||
let c2 = cancel.clone();
|
||
let marker = format!("anybot-sleep-{}", std::process::id());
|
||
let cmd = format!(r#"{{"command":"sh -c 'exec -a {marker} sleep 30' & sleep 30","timeout_ms":20000}}"#);
|
||
let h = std::thread::spawn(move || run("shell", &cmd, &wd(), &c2, &RunIds::default()));
|
||
std::thread::sleep(Duration::from_millis(800));
|
||
cancel.store(true, Ordering::Relaxed);
|
||
let (r, ok) = h.join().unwrap();
|
||
assert!(!ok && r.contains("已停止"), "{r}");
|
||
std::thread::sleep(Duration::from_millis(300));
|
||
let ps = Command::new("ps").args(["-A", "-o", "command="]).output().unwrap();
|
||
assert!(!String::from_utf8_lossy(&ps.stdout).contains(&marker), "child still running");
|
||
let _ = w;
|
||
}
|
||
|
||
#[test]
|
||
fn box_file_out_stays_in_bot_folder() {
|
||
let t = Target { name: "Bot 的電腦".into(), place: Place::Box { dir: "/home/bot/bot-1".into() } };
|
||
assert_eq!(decide_file_out(&t, "report.html", &[]), Decision::Allow);
|
||
assert!(matches!(decide_file_out(&t, "../bot-2/secret.txt", &[]), Decision::Deny(_)));
|
||
assert!(matches!(decide_file_out(&t, "/home/bot/bot-2/site", &[]), Decision::Deny(_)));
|
||
assert!(matches!(decide_file_out(&t, "/etc/passwd", &[]), Decision::Deny(_)));
|
||
}
|
||
}
|