sandbox: runtime /sandbox toggle, sandbox.claude config, --no-claude-sandbox
This commit is contained in:
1 parent
d1bfa22580
commit
a0cd7946c3
5 files changed
+102
-12
No files matched your search
@@ -45,6 +45,9 @@ struct Cli {
|
||||
/// Force the sandbox off (overrides config).
|
||||
#[arg(long)]
|
||||
no_sandbox: bool,
|
||||
/// Run `claude` unconfined even when the sandbox is on (overrides config).
|
||||
#[arg(long)]
|
||||
no_claude_sandbox: bool,
|
||||
/// Output mode: text (default for -p), json (NDJSON events on stdout; needs -p), rpc (JSON-RPC on stdio).
|
||||
#[arg(long, value_enum, default_value_t = Mode::Text)]
|
||||
mode: Mode,
|
||||
@@ -317,7 +320,10 @@ fn claude_extra(bin: &str) -> hx_core::sandbox::Extra {
|
||||
|
||||
/// 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 {
|
||||
if hx_core::sandbox::active().is_none()
|
||||
|| !hx_core::sandbox::claude_sandboxed()
|
||||
|| claude_outside().await
|
||||
{
|
||||
return tokio::process::Command::new(bin);
|
||||
}
|
||||
hx_core::sandbox::command_with(&bin, &claude_extra(&bin))
|
||||
@@ -771,7 +777,13 @@ async fn main() -> Result<()> {
|
||||
if cli.no_sandbox {
|
||||
sb.mode = hx_core::sandbox::Mode::Off;
|
||||
}
|
||||
if cli.no_claude_sandbox {
|
||||
sb.claude = false;
|
||||
}
|
||||
hx_core::sandbox::init(&sb, &cwd).map_err(anyhow::Error::msg)?;
|
||||
if sb.mode == hx_core::sandbox::Mode::On && !sb.claude {
|
||||
eprintln!("hx: sandbox on, but claude runs unconfined (sandbox.claude = false)");
|
||||
}
|
||||
if let Some(oc) = cfg.openai_compat.clone() {
|
||||
let _ = COMPAT.set(oc);
|
||||
}
|
||||
|
||||
@@ -73,6 +73,11 @@ pub fn builtins(models: &[&str]) -> Vec<CmdSpec> {
|
||||
b("clear", "alias of /new", None),
|
||||
b("sessions", "browse, resume, delete sessions", None),
|
||||
b("yolo", "toggle: run every tool without asking", None),
|
||||
b(
|
||||
"sandbox",
|
||||
"sandbox on/off; `claude` toggles it for claude; no arg: status",
|
||||
Enum(["on", "off", "claude"].map(String::from).into()),
|
||||
),
|
||||
b("lsp", "language server status", None),
|
||||
b("copy", "copy the last answer to the clipboard", None),
|
||||
b("usage", "subscription limits, resets, credits", None),
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
use std::ffi::OsStr;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::OnceLock;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
|
||||
#[derive(Default, Clone, Copy, PartialEq, Eq, Debug, serde::Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
@@ -45,6 +46,8 @@ pub struct Cfg {
|
||||
pub ro: Vec<String>,
|
||||
/// env vars passed to children; a trailing `*` matches a prefix
|
||||
pub env_allow: Vec<String>,
|
||||
/// run `claude` (claude_cli provider) inside the sandbox too (default true)
|
||||
pub claude: bool,
|
||||
}
|
||||
|
||||
impl Default for Cfg {
|
||||
@@ -52,6 +55,7 @@ impl Default for Cfg {
|
||||
let s = |v: &[&str]| v.iter().map(|s| s.to_string()).collect();
|
||||
Cfg {
|
||||
mode: Mode::Off,
|
||||
claude: true,
|
||||
backend: Backend::Auto,
|
||||
network: false,
|
||||
rw: s(&["~/.cargo/registry", "~/.cargo/git"]),
|
||||
@@ -98,22 +102,47 @@ pub struct Sandbox {
|
||||
env_allow: Vec<String>,
|
||||
}
|
||||
|
||||
static SANDBOX: OnceLock<Option<Sandbox>> = OnceLock::new();
|
||||
static CFG: OnceLock<(Cfg, PathBuf)> = OnceLock::new();
|
||||
/// Built lazily on first enable (the backend probe spawns a process).
|
||||
static SANDBOX: OnceLock<Sandbox> = OnceLock::new();
|
||||
static ENABLED: AtomicBool = AtomicBool::new(false);
|
||||
static CLAUDE: AtomicBool = AtomicBool::new(true);
|
||||
|
||||
/// Call once at startup. Errors (never silently falls back) if `mode = on` and no backend works.
|
||||
pub fn init(cfg: &Cfg, cwd: &Path) -> Result<(), String> {
|
||||
let sb = if cfg.mode == Mode::On {
|
||||
Some(Sandbox::new(cfg, cwd)?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
SANDBOX
|
||||
.set(sb)
|
||||
.map_err(|_| "sandbox already initialised".into())
|
||||
CFG.set((cfg.clone(), cwd.to_path_buf()))
|
||||
.map_err(|_| "sandbox already initialised".to_string())?;
|
||||
CLAUDE.store(cfg.claude, Ordering::Relaxed);
|
||||
if cfg.mode == Mode::On {
|
||||
set_enabled(true)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn active() -> Option<&'static Sandbox> {
|
||||
SANDBOX.get().and_then(Option::as_ref)
|
||||
ENABLED
|
||||
.load(Ordering::Relaxed)
|
||||
.then(|| SANDBOX.get())
|
||||
.flatten()
|
||||
}
|
||||
|
||||
/// Turn the sandbox on/off at runtime. Affects only children spawned afterwards.
|
||||
pub fn set_enabled(on: bool) -> Result<(), String> {
|
||||
if on && SANDBOX.get().is_none() {
|
||||
let (cfg, cwd) = CFG.get().ok_or("sandbox not initialised")?;
|
||||
let _ = SANDBOX.set(Sandbox::new(cfg, cwd)?);
|
||||
}
|
||||
ENABLED.store(on, Ordering::Relaxed);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Does `claude` run inside the sandbox (while the sandbox is on)?
|
||||
pub fn claude_sandboxed() -> bool {
|
||||
CLAUDE.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
pub fn set_claude_sandboxed(on: bool) {
|
||||
CLAUDE.store(on, Ordering::Relaxed);
|
||||
}
|
||||
|
||||
/// Drop-in for `tokio::process::Command::new` that applies the sandbox when it is on.
|
||||
@@ -561,4 +590,17 @@ mod tests {
|
||||
"base unchanged"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn toggle() {
|
||||
assert!(set_enabled(true).is_err(), "needs init");
|
||||
let _ = init(&Cfg::default(), Path::new("/tmp"));
|
||||
assert!(active().is_none());
|
||||
if set_enabled(true).is_ok() {
|
||||
assert!(active().is_some());
|
||||
set_enabled(false).unwrap();
|
||||
assert!(active().is_none());
|
||||
}
|
||||
assert!(Cfg::default().claude);
|
||||
}
|
||||
}
|
||||
@@ -1384,6 +1384,36 @@ async fn submit(app: &mut App, mut line: String, c: &Ctx<'_>) {
|
||||
}
|
||||
show(app, "skills", l);
|
||||
}
|
||||
"sandbox" => {
|
||||
use hx_core::sandbox as sb;
|
||||
let r = match arg {
|
||||
"" => Ok(()),
|
||||
"on" => sb::set_enabled(true),
|
||||
"off" => sb::set_enabled(false),
|
||||
"claude" => {
|
||||
sb::set_claude_sandboxed(!sb::claude_sandboxed());
|
||||
Ok(())
|
||||
}
|
||||
_ => Err("usage: /sandbox [on|off|claude]".into()),
|
||||
};
|
||||
app.t.push(match r {
|
||||
Ok(()) => Item::Info(format!(
|
||||
"sandbox {} · claude {}{}",
|
||||
if sb::active().is_some() { "on" } else { "off" },
|
||||
if sb::claude_sandboxed() {
|
||||
"sandboxed"
|
||||
} else {
|
||||
"unconfined"
|
||||
},
|
||||
if arg.is_empty() {
|
||||
""
|
||||
} else {
|
||||
" · applies to new processes (restart LSP with /lsp)"
|
||||
}
|
||||
)),
|
||||
Err(e) => Item::Error(e),
|
||||
});
|
||||
}
|
||||
"yolo" => {
|
||||
let on = app
|
||||
.state
|
||||
|
||||
Reference in new issue
Block a user