claude_cli: run claude inside the sandbox, outside only after the user allows it
This commit is contained in:
1 parent
f519374312
commit
3756c12357
3 files changed
+173
-2
No files matched your search
@@ -224,6 +224,102 @@ impl Config {
|
||||
}
|
||||
}
|
||||
|
||||
/// Tool context for the claude sandbox question (set once the agent exists).
|
||||
static CLAUDE_CX: std::sync::OnceLock<ToolCtx> = std::sync::OnceLock::new();
|
||||
/// Answer to "run claude outside the sandbox?" for this process.
|
||||
static CLAUDE_OUTSIDE: std::sync::Mutex<Option<bool>> = std::sync::Mutex::new(None);
|
||||
const OUTSIDE_RULE: &str = "claude:unsandboxed";
|
||||
|
||||
/// Allowed to leave the sandbox? A stored rule says yes; else the TUI asks (once per run, "always"
|
||||
/// is stored in `.hx/allow.json`); without a prompt hook (`-p`) the answer is no.
|
||||
async fn claude_outside() -> bool {
|
||||
if let Some(v) = *CLAUDE_OUTSIDE.lock().unwrap_or_else(|e| e.into_inner()) {
|
||||
return v;
|
||||
}
|
||||
let ok = match CLAUDE_CX.get() {
|
||||
Some(cx) if cx.state.permit.get().is_some() => {
|
||||
hx_core::tool::gate(
|
||||
cx,
|
||||
hx_core::permit::Request {
|
||||
tool: "claude".into(),
|
||||
detail: "run claude outside the sandbox (its tools then run unconfined)".into(),
|
||||
rule: OUTSIDE_RULE.into(),
|
||||
},
|
||||
)
|
||||
.await
|
||||
}
|
||||
Some(cx) => cx
|
||||
.state
|
||||
.allow
|
||||
.lock()
|
||||
.unwrap_or_else(|e| e.into_inner())
|
||||
.allows(&hx_core::permit::Request {
|
||||
tool: "claude".into(),
|
||||
detail: String::new(),
|
||||
rule: OUTSIDE_RULE.into(),
|
||||
}),
|
||||
None => false,
|
||||
};
|
||||
*CLAUDE_OUTSIDE.lock().unwrap_or_else(|e| e.into_inner()) = Some(ok);
|
||||
ok
|
||||
}
|
||||
|
||||
/// What `claude` (and its `hx mcp-serve` child) need inside the sandbox: its binary and config,
|
||||
/// the network, hx itself and the permission socket.
|
||||
fn claude_extra(bin: &str) -> hx_core::sandbox::Extra {
|
||||
let home = std::path::PathBuf::from(std::env::var_os("HOME").unwrap_or_default());
|
||||
let mut ro = vec![];
|
||||
let found = if bin.contains('/') {
|
||||
Some(bin.into())
|
||||
} else {
|
||||
std::env::var_os("PATH").and_then(|p| {
|
||||
std::env::split_paths(&p)
|
||||
.map(|d| d.join(bin))
|
||||
.find(|f| f.is_file())
|
||||
})
|
||||
};
|
||||
for f in found
|
||||
.into_iter()
|
||||
.chain(std::env::current_exe().ok())
|
||||
.flat_map(|f: std::path::PathBuf| [f.canonicalize().ok(), Some(f)])
|
||||
.flatten()
|
||||
{
|
||||
ro.extend(f.parent().map(Into::into));
|
||||
}
|
||||
// claude's install dir (versions/..) sits next to the resolved symlink target
|
||||
hx_core::sandbox::Extra {
|
||||
ro,
|
||||
rw: vec![
|
||||
home.join(".claude"),
|
||||
home.join(".claude.json"),
|
||||
home.join(".config/claude"),
|
||||
hx_core::approve::sock_path(),
|
||||
],
|
||||
network: true,
|
||||
env: [
|
||||
"ANTHROPIC_*",
|
||||
"CLAUDE_*",
|
||||
"NODE_*",
|
||||
"XDG_*",
|
||||
"HTTP_PROXY",
|
||||
"HTTPS_PROXY",
|
||||
"NO_PROXY",
|
||||
"SSL_CERT_*",
|
||||
"HX_PERMIT_SOCK",
|
||||
]
|
||||
.map(String::from)
|
||||
.into(),
|
||||
}
|
||||
}
|
||||
|
||||
/// The `claude` command: sandboxed when the sandbox is on, unless the user let it out.
|
||||
async fn claude_command(bin: String) -> tokio::process::Command {
|
||||
if hx_core::sandbox::active().is_none() || claude_outside().await {
|
||||
return tokio::process::Command::new(bin);
|
||||
}
|
||||
hx_core::sandbox::command_with(&bin, &claude_extra(&bin))
|
||||
}
|
||||
|
||||
/// Builtins, MCP prompts (`/server:prompt`), then skills (builtins win name clashes).
|
||||
/// `models` feeds `/model` completion (any other id still works); `kinds` = logged-in providers.
|
||||
fn command_specs(
|
||||
@@ -366,6 +462,7 @@ fn provider(k: Kind) -> hx_ai::Result<Arc<dyn Provider>> {
|
||||
Kind::Claude if CLAUDE_CLI.get().is_some() => {
|
||||
let c =
|
||||
hx_ai::claude_cli::ClaudeCli::new(CLAUDE_CLI.get().cloned().unwrap_or_default());
|
||||
let c = c.with_spawner(Arc::new(|bin| Box::pin(claude_command(bin))));
|
||||
Arc::new(match std::env::current_exe() {
|
||||
Ok(e) => c.with_mcp(
|
||||
e.to_string_lossy().into(),
|
||||
@@ -901,6 +998,7 @@ async fn main() -> Result<()> {
|
||||
if CLAUDE_CLI.get().is_some() {
|
||||
// claude asks here (via `hx mcp-serve`) before running its tools
|
||||
let (p, cx) = (hx_core::approve::sock_path(), agent.cx.clone());
|
||||
let _ = CLAUDE_CX.set(cx.clone());
|
||||
tokio::spawn(async move {
|
||||
let _ = hx_core::approve::serve(&p, cx).await;
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user