mod admission;
mod agy;
#[cfg(test)]
mod children_tests;
pub mod cursor;
mod cursor_run;
#[cfg(test)]
mod cursor_tests;
#[cfg(all(test, target_os = "macos"))]
mod cursor_worker_tests;
#[cfg(test)]
mod hardening_tests;
#[cfg(test)]
mod homes_tests;
#[cfg(test)]
mod launch_tests;
#[cfg(test)]
mod live_tests;
mod macos;
pub mod muse;
pub mod pi;
#[cfg(test)]
mod pi_tests;
mod policy;
mod private;
mod profile;
mod state;
mod store;
#[cfg(test)]
mod tests;
pub mod vibe;
use std::ffi::OsStr;
use std::path::Path;
use std::process::Command;
use super::adapter::{Agent, Mode};
use super::text::say;
use admission::presets;
pub use admission::ready;
pub use cursor_run::uid;
pub use policy::{Inputs, Policy};
pub use private::{Private, SHORT_PARENT};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Platform {
Common,
Presets,
}
pub fn platform_for(os: &str) -> Platform {
match os {
"macos" => Platform::Common,
_ => Platform::Presets,
}
}
pub fn platform() -> Platform {
platform_for(std::env::consts::OS)
}
pub fn note(platform: Platform, agent: Agent, mode: Mode) -> Option<String> {
(platform == Platform::Presets).then(|| {
say(
"policy_presets",
&[("agent", agent.name()), ("mode", mode.name())],
)
})
}
pub fn local_store(selection: &[String], directory: &Path) -> bool {
store::check(selection, directory).is_ok_and(|stores| !stores.credential && !stores.login)
}
pub struct Admitted {
pub launch: Launch,
pub notes: Vec<String>,
_private: Option<Private>,
}
pub fn admit(
agent: Agent,
mode: Mode,
selection: &[String],
directory: &Path,
session: Option<&str>,
(child_home, session_dir): (Option<&Path>, Option<&Path>),
) -> Result<Admitted, String> {
if platform() == Platform::Presets {
return Ok(Admitted {
launch: presets(agent, selection, directory)?,
notes: Vec::new(),
_private: None,
});
}
let private = Private::make_for(agent)
.map_err(|error| say("sandbox_refused", &[("error", &error.to_string())]))?;
let turn = Turn {
agent,
mode,
directory,
private: private.path(),
selection,
session,
child_home,
session_dir,
};
let (launch, credential) = confine(&turn, Path::new(macos::FACILITY))?;
let lost = credential && !matches!(agent, Agent::Pi | Agent::Cursor | Agent::Agy | Agent::Muse);
Ok(Admitted {
launch,
notes: lost
.then(|| say("sandbox_no_store", &[]))
.into_iter()
.collect(),
_private: Some(private),
})
}
pub struct Turn<'a> {
pub agent: Agent,
pub mode: Mode,
pub directory: &'a Path,
pub private: &'a Path,
pub selection: &'a [String],
pub session: Option<&'a str>,
pub child_home: Option<&'a Path>,
pub session_dir: Option<&'a Path>,
}
#[derive(Debug)]
pub enum Launch {
Confined {
profile: String,
policy: Box<Policy>,
},
Presets { unset: Vec<String> },
}
pub fn prepare(turn: &Turn) -> Result<Launch, String> {
match platform() {
Platform::Presets => presets(turn.agent, turn.selection, turn.directory),
Platform::Common => confine(turn, Path::new(macos::FACILITY)).map(|(launch, _)| launch),
}
}
fn confine(turn: &Turn, facility: &Path) -> Result<(Launch, bool), String> {
let stores = store::check(turn.selection, turn.directory)?;
let home = super::adapter::home().ok_or_else(|| say("sandbox_home", &[]))?;
let var = |name: &str| std::env::var_os(name);
store::ready(&stores);
let policy = policy::build(&Inputs {
agent: turn.agent,
mode: turn.mode,
directory: turn.directory,
private: turn.private,
home: &home,
stores: &stores,
var: &var,
session: turn.session,
child_home: turn.child_home,
session_dir: turn.session_dir,
persist: &cursor_run::root(),
})?;
let profile = policy.profile();
let target = macos::target(&policy, turn.private, &home)
.ok_or_else(|| say("sandbox_unenforced", &[]))?;
macos::probe(facility, &profile, &target)?;
let policy = Box::new(policy);
Ok((Launch::Confined { profile, policy }, stores.credential))
}
impl Launch {
pub fn confined(&self) -> bool {
matches!(self, Self::Confined { .. })
}
pub fn command(
&self,
program: &Path,
args: &[impl AsRef<OsStr>],
env: &[(String, String)],
) -> Command {
let mut command = match self {
Self::Presets { .. } => Command::new(program),
Self::Confined { profile, .. } => {
macos::command(Path::new(macos::FACILITY), profile, program)
}
};
command.args(args).envs(env.iter().map(|(k, v)| (k, v)));
if let Self::Confined { policy, .. } = self {
command.envs(policy.env.iter().map(|(k, v)| (k, v)));
}
self.strip(&mut command);
command
}
pub fn writable(&self, path: &Path) -> bool {
let Self::Confined { policy, .. } = self else {
return false;
};
let placed = path
.parent()
.and_then(|dir| dir.canonicalize().ok())
.zip(path.file_name())
.map(|(dir, name)| dir.join(name));
[Some(path.to_path_buf()), placed, path.canonicalize().ok()]
.into_iter()
.flatten()
.any(|place| policy.allow.iter().any(|rule| macos::covers(rule, &place)))
}
pub fn strip(&self, command: &mut Command) {
let unset = match self {
Self::Confined { policy, .. } => &policy.unset,
Self::Presets { unset } => unset,
};
for name in unset {
command.env_remove(name);
}
}
}