use super::SandboxExecutor;
use anyhow::{Context, Result};
use std::fs;
use std::path::Path;
use std::process::{Command, Output};
pub struct MacOsSandbox;
impl SandboxExecutor for MacOsSandbox {
fn name(&self) -> &'static str {
"macos-sandbox-exec"
}
fn execute(
&self,
command: &str,
args: &[&str],
work_dir: &Path,
allow_network: bool,
) -> Result<Output> {
let policy_content = format!(
"(version 1)\n(allow default)\n(deny file-write* (regex #\"^/(usr|bin|sbin|System)\"))\n{}",
if allow_network {
""
} else {
"(deny network*)\n"
}
);
let temp_dir = tempfile::tempdir()?;
let policy_path = temp_dir.path().join("policy.sb");
fs::write(&policy_path, policy_content)?;
let mut sandbox_cmd = Command::new("/usr/bin/sandbox-exec");
sandbox_cmd
.arg("-f")
.arg(&policy_path)
.arg(command)
.args(args)
.current_dir(work_dir);
let output = sandbox_cmd
.output()
.with_context(|| format!("macOS sandbox-exec failed for {}", command))?;
Ok(output)
}
}