fix: orchestrated runs honour yolo/auto and work inside the sandbox
- serve --acp applies --yolo / --auto (or `permissions` in config.toml) like the TUI; before, every edit and command went to the orchestrator. - claude no longer asks to run outside the sandbox. With the sandbox on it stays confined; only a stored `claude:unsandboxed` rule or --no-claude-sandbox lets it out, and yolo never does. - The sandbox reads the files the global git config includes and may stat $HOME and the directories above allowed paths and includeIf gitdir paths (metadata only). git died on both before. - hx mcp-serve logs a failed start to $TMPDIR/hx-mcp-serve.log; claude hides its stderr and only reports "mcp__hx__approve not found". Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
37ebf96108
commit
b1ca582d6b
3 files changed
+223
-78
No files matched your search
@@ -397,6 +397,16 @@ impl Allow {
|
||||
self.yolo
|
||||
}
|
||||
|
||||
/// Allow everything (not persisted).
|
||||
pub fn set_yolo(&mut self, on: bool) {
|
||||
self.yolo = on;
|
||||
}
|
||||
|
||||
/// A stored rule (`.hx/allow.json` or "always" this session), ignoring yolo and edit grants.
|
||||
pub fn has_rule(&self, rule: &str) -> bool {
|
||||
self.rules.contains(rule)
|
||||
}
|
||||
|
||||
/// Toggle: allow everything (not persisted); returns the new state.
|
||||
pub fn toggle_yolo(&mut self) -> bool {
|
||||
self.yolo = !self.yolo;
|
||||
|
||||
@@ -91,6 +91,60 @@ impl Default for Cfg {
|
||||
}
|
||||
}
|
||||
|
||||
/// Paths named by `path =` in `[include]` / `[includeIf ...]` sections of the global git config, followed
|
||||
/// into the included files (a few levels), and the `gitdir:` directories of `includeIf` (git resolves
|
||||
/// them on every start). `~/` expands; relative paths are relative to the including file.
|
||||
fn git_includes(home: &Path) -> (Vec<PathBuf>, Vec<PathBuf>) {
|
||||
let mut todo = vec![home.join(".gitconfig"), home.join(".config/git/config")];
|
||||
let mut found: Vec<PathBuf> = vec![];
|
||||
let mut dirs: Vec<PathBuf> = vec![];
|
||||
let mut depth = 0;
|
||||
while !todo.is_empty() && depth < 5 {
|
||||
depth += 1;
|
||||
let mut next = vec![];
|
||||
for f in todo {
|
||||
let Ok(text) = std::fs::read_to_string(&f) else {
|
||||
continue;
|
||||
};
|
||||
let mut include = false;
|
||||
for line in text.lines().map(str::trim) {
|
||||
if let Some(sec) = line.strip_prefix('[') {
|
||||
let sec = sec.trim_start();
|
||||
include = sec.to_ascii_lowercase().starts_with("include");
|
||||
// [includeIf "gitdir:~/w/"] or "gitdir/i:..."
|
||||
if let Some(d) = sec
|
||||
.split_once('"')
|
||||
.and_then(|(_, r)| r.split_once(':'))
|
||||
.filter(|(k, _)| k.starts_with("gitdir"))
|
||||
.and_then(|(_, v)| v.split('"').next())
|
||||
.and_then(|v| v.strip_prefix("~/"))
|
||||
{
|
||||
dirs.push(home.join(d.trim_end_matches("**").trim_end_matches('/')));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let Some((k, v)) = line.split_once('=') else {
|
||||
continue;
|
||||
};
|
||||
if !include || !k.trim().eq_ignore_ascii_case("path") {
|
||||
continue;
|
||||
}
|
||||
let v = v.trim().trim_matches('"');
|
||||
let p = match v.strip_prefix("~/") {
|
||||
Some(r) => home.join(r),
|
||||
None => f.parent().unwrap_or(Path::new("/")).join(v),
|
||||
};
|
||||
if !found.contains(&p) {
|
||||
found.push(p.clone());
|
||||
next.push(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
todo = next;
|
||||
}
|
||||
(found, dirs)
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Sandbox {
|
||||
backend: Backend,
|
||||
@@ -99,6 +153,8 @@ pub struct Sandbox {
|
||||
cwd: PathBuf,
|
||||
rw: Vec<PathBuf>,
|
||||
ro: Vec<PathBuf>,
|
||||
/// paths under `$HOME` whose metadata (not content) may be read, e.g. `includeIf "gitdir:~/w"`
|
||||
meta: Vec<PathBuf>,
|
||||
env_allow: Vec<String>,
|
||||
}
|
||||
|
||||
@@ -273,6 +329,9 @@ impl Sandbox {
|
||||
.collect()
|
||||
};
|
||||
let mut ro = paths(&cfg.ro);
|
||||
// files the global git config includes (`[include] path = ...`): without them git fails to start
|
||||
let (inc, meta) = git_includes(&home);
|
||||
ro.extend(inc.into_iter().filter(|p| p.exists()).map(|p| canon(&p)));
|
||||
let skills = crate::context::hx_home().join("skills");
|
||||
if skills.exists() {
|
||||
ro.push(canon(&skills));
|
||||
@@ -284,6 +343,7 @@ impl Sandbox {
|
||||
cwd: canon(cwd),
|
||||
rw: paths(&cfg.rw),
|
||||
ro,
|
||||
meta,
|
||||
env_allow: cfg.env_allow.clone(),
|
||||
})
|
||||
}
|
||||
@@ -442,6 +502,28 @@ impl Sandbox {
|
||||
let w = self.writable();
|
||||
let readable: Vec<PathBuf> = w.iter().chain(self.ro.iter()).cloned().collect();
|
||||
p += &format!("(allow file-read* {})\n", list(&readable));
|
||||
// stat of $HOME and the directories above each allowed path (git resolves paths component by
|
||||
// component and dies on EPERM); `literal` = these directories only, not what is inside
|
||||
let mut dirs: Vec<&Path> = readable
|
||||
.iter()
|
||||
.chain(&self.meta)
|
||||
.flat_map(|p| p.ancestors())
|
||||
.filter(|a| a.starts_with(&self.home))
|
||||
.collect();
|
||||
dirs.sort();
|
||||
dirs.dedup();
|
||||
let lit = |p: &Path| {
|
||||
format!(
|
||||
"(literal \"{}\")",
|
||||
p.to_string_lossy()
|
||||
.replace('\\', "\\\\")
|
||||
.replace('"', "\\\"")
|
||||
)
|
||||
};
|
||||
p += &format!(
|
||||
"(allow file-read-metadata {})\n",
|
||||
dirs.iter().map(|d| lit(d)).collect::<Vec<_>>().join(" ")
|
||||
);
|
||||
p += &format!("(allow file-write* {})\n", list(&w));
|
||||
p += "(allow file-write* (literal \"/dev/null\") (literal \"/dev/tty\"))\n";
|
||||
if self.network {
|
||||
@@ -455,6 +537,33 @@ impl Sandbox {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn git_includes_follow_include_sections() {
|
||||
let home = std::env::temp_dir().join(format!("hx-gitinc-{}", std::process::id()));
|
||||
std::fs::create_dir_all(home.join(".config/delta")).unwrap();
|
||||
std::fs::write(
|
||||
home.join(".gitconfig"),
|
||||
"[user]\n path = /not/an/include\n[include]\n path = ~/.config/delta/theme.gitconfig\n[includeIf \"gitdir:~/w/\"]\n path = \"work.gitconfig\"\n",
|
||||
)
|
||||
.unwrap();
|
||||
std::fs::write(
|
||||
home.join(".config/delta/theme.gitconfig"),
|
||||
"[include]\npath = nested\n",
|
||||
)
|
||||
.unwrap();
|
||||
let (got, dirs) = git_includes(&home);
|
||||
let _ = std::fs::remove_dir_all(&home);
|
||||
assert_eq!(dirs, [home.join("w")]);
|
||||
assert_eq!(
|
||||
got,
|
||||
[
|
||||
home.join(".config/delta/theme.gitconfig"),
|
||||
home.join("work.gitconfig"),
|
||||
home.join(".config/delta/nested"),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
fn sb(backend: Backend, network: bool) -> Sandbox {
|
||||
Sandbox {
|
||||
backend,
|
||||
@@ -463,6 +572,7 @@ mod tests {
|
||||
cwd: "/home/u/proj".into(),
|
||||
rw: vec!["/home/u/.cargo/registry".into()],
|
||||
ro: vec!["/home/u/.cargo".into()],
|
||||
meta: vec!["/home/u/w/x".into()],
|
||||
env_allow: vec!["PATH".into(), "LC_*".into()],
|
||||
}
|
||||
}
|
||||
@@ -495,6 +605,9 @@ mod tests {
|
||||
.unwrap()
|
||||
);
|
||||
assert!(p.contains("(allow file-write* (subpath \"/home/u/proj\")"));
|
||||
assert!(p.contains(
|
||||
"(allow file-read-metadata (literal \"/home/u\") (literal \"/home/u/.cargo\") (literal \"/home/u/.cargo/registry\") (literal \"/home/u/proj\") (literal \"/home/u/w\") (literal \"/home/u/w/x\"))"
|
||||
));
|
||||
assert!(
|
||||
sb(Backend::Seatbelt, true)
|
||||
.seatbelt_profile()
|
||||
|
||||
Reference in new issue
Block a user