Cotton code
158 lines · 9.5 KB · RustRaw
//! **EVERY LANE SESSION RUNS UNDER A SEATBELT PROFILE THAT DENIES THE REAL//! DATA FOLDER** (architect, 15 Oct 2026, after a lane's refusal test made a//! folder inside the serving Mac's real data folder). A lane is a Claude or//! Codex session the lane launcher starts in tmux; the launcher//! wraps the agent in `sandbox-exec` with the profile this module writes, so//! the agent and every child it spawns — tests, builds, scripts — can neither//! read nor write the real app's data folder (and its old Application Support//! home) or the person's secrets. The denial is the kernel's, not a rule in a//! brief.//!//! The forbidden list has one source: the real app's data folder is//! [`Flavor::Real`]'s, and the secrets are [`SECRETS_DENIED_IN_HOME`], less what a lane must reach to work — each such//! opening named in [`LANE_OPENED_IN_HOME`] with its reason; the keychain is//! denied, and a lane signs in from [`LANE_CLAUDE_SECRET`] instead. Nothing a lane runs leaves the profile: no entry carries//! `no-sandbox` ([`NO_SANDBOX`]). The profile is written out as the lane sandbox profile file (a test holds//! the file equal to [`lane_profile`]); the launcher puts the person's real//! home in for [`HOME`] and its regex-escaped form for [`HOME_RE`], and adds a//! denial for the resolved target of any denied path that is a symlink at//! [`RESOLVED`].use crate::flavor::Flavor;/// Where the launcher puts the person's home, resolved (`pwd -P`): seatbelt/// matches real paths.pub const HOME: &str = "@HOME@";/// Where the launcher puts the same home, escaped for a seatbelt regex.pub const HOME_RE: &str = "@HOME_RE@";/// The line the launcher replaces with a denial of each symlinked denied/// path's target.pub const RESOLVED: &str = ";; @RESOLVED@";/// How the profile names each denied path, one comment line apiece, so the/// launcher can resolve the ones that are symlinks without a second list.pub const DENIED_MARK: &str = ";; denied-in-home: ";/// The vault secret a Linux lane's Claude signs in with, placed per machine as/// a raw file (a `claude setup-token` login) and handed over by environment./// A Mac lane no longer uses it: it runs on the Mac's own Claude login from the/// keychain ([`LANE_KEYCHAIN_OPENED`]). A lane reaches Cotton by its own token/// and MCP config (`cotton_tools::mcp::lane_scope`), never the claude.ai/// connector a keychain login fetches.pub const LANE_CLAUDE_SECRET: &str = "claude-lane-token";/// The one denied folder a lane may read AND write: the keychain. Owner, 21 Oct/// 2026, reversing lanekeychain (15 Oct): lanes run on the Mac's own Claude/// login so Remote Control works (a setup-token login carries the inference/// scope only and `/rc` fails). The keychain's item ACLs still gate everything/// but Claude's own item; the rest of the agent's secrets stay denied.pub const LANE_KEYCHAIN_OPENED: (&str, &str) = (    "Library/Keychains",    "owner, 21 Oct 2026: Remote Control on lanes — a lane runs on the Mac's own Claude login; item ACLs still gate everything but Claude's own item (reverses lanekeychain, 15 Oct)",);/// What a launch on a Mac with no placement of [`LANE_CLAUDE_SECRET`] says.pub const NO_LANE_TOKEN: &str = "no lane token placed";/// The seatbelt modifier that runs an exec outside every profile, which no/// lane profile entry may carry: a child that escapes the profile can hand/// itself one that opens everything.pub const NO_SANDBOX: &str = "no-sandbox";/// What a lane reads inside a denied folder, each with why. Read only.pub const LANE_OPENED_IN_HOME: [(&str, &str); 3] = [    (".config/git", "git reads its ignore and config on every command; read-only, nothing else under .config (coordinator, 16 Oct 2026)"),    (".cotton-agent/mac-build-policy", "the cargo, rustc, swift and xcodebuild wrappers every Mac build runs through"),    (".cotton-agent/owner-denylist", "the owner-data scan a landing runs; a host without the list does not land"),];/// The app's git route to the code host, under its data folder: a proxy line/// the app writes while it runs, included from the person's `~/.gitconfig`, so/// every git command on the Mac reads it — a lane's included. The one file of/// the data folder a lane may read; it holds a loopback port and nothing else.pub const GIT_ROUTE_IN_DATA: &str = "run/git-route.gitconfig";/// Where the route's proxy keeps its certificate authority's public half,/// under the data folder; each generation puts the file in its own/// [`GIT_ROUTE_CA_DIR_PREFIX`]-named folder beside it, which the route names.pub const GIT_ROUTE_CA_IN_DATA: &str = "run/git-route-ca.pem";/// The start of each route generation's folder's name.pub const GIT_ROUTE_CA_DIR_PREFIX: &str = ".git-route-";/// Where Codex's shared app-server daemon keeps its sockets, one folder per/// user (`/private/tmp/codex-daemon-<uid>`). The daemon is started outside any/// lane (on the serving Mac by launchd), so a lane that reaches it runs its commands/// unsandboxed (security note 7948836e, 15 Oct 2026). A lane's Codex runs with/// its own CODEX_HOME and without the daemon; the profile denies every user's/// daemon folder and connecting to any socket in it.pub const CODEX_DAEMON_SOCKETS: &str = "/private/tmp/codex-daemon-";/// `text` as a seatbelt regex matching it literally.fn regex_literal(text: &str) -> String {    text.chars().map(|c| if "\\.*+?^$()[]{}|".contains(c) { format!("\\{c}") } else { c.to_string() }).collect()}/// The real app's data folders under the person's home: the data folder and/// its old Application Support home.pub fn real_data_in_home() -> [String; 2] {    let name = Flavor::Real.data_dir_name();    [name.to_string(), format!("Library/Application Support/{name}")]}/// The person's secrets a lane is denied besides the data folder, relative to/// their home. `.cotton-credentials` is where vault placements conventionally/// land on a Mac — every credential a machine was granted — so a lane reads/// none of them but what [`LANE_OPENED_IN_HOME`] opens.pub const SECRETS_DENIED_IN_HOME: [&str; 8] = [".ssh", ".config", ".cotton-agent", ".cotton-credentials", ".aws", ".gnupg", ".docker", "Library/Keychains"];/// Every home-relative path a lane may neither read nor write.pub fn lane_denied_in_home() -> Vec<String> {    let secrets = SECRETS_DENIED_IN_HOME.iter().map(|p| p.to_string());    real_data_in_home().into_iter().chain(secrets).collect()}/// `name` matched in any letter case: the Mac's disk is case-insensitive, so a/// folder made as `cottondb` IS the data folder.fn any_case(name: &str) -> String {    name.chars().map(|c| if c.is_ascii_alphabetic() { format!("[{}{}]", c.to_ascii_uppercase(), c.to_ascii_lowercase()) } else { c.to_string() }).collect()}/// The lane's seatbelt profile, with [`HOME`] and [`HOME_RE`] for the launcher/// to fill. Later rules win, so the openings follow the denials they open.pub fn lane_profile() -> String {    let mut p = String::from("(version 1)\n");    p.push_str(";; GENERATED from crates/cotton-core/src/lane_sandbox.rs — do not edit; `COTTON_WRITE_LANE_PROFILE=1 cargo test -p cotton-core lane_sandbox` rewrites it.\n");    p.push_str(";; The lane launcher fills in the home (resolved, and regex-escaped) and denies the target of each denied path that is a symlink.\n");    p.push_str("(allow default)\n");    for path in lane_denied_in_home() {        p.push_str(&format!("{DENIED_MARK}{path}\n"));        p.push_str(&format!("(deny file-read* file-write* (subpath \"{HOME}/{path}\"))\n"));    }    // A data folder that does not exist yet is matched by name only, so a    // folder made under another spelling of it is refused as well.    p.push_str(&format!("(deny file-read* file-write* (regex #\"^{HOME_RE}/{}(/|$)\"))\n", any_case(Flavor::Real.data_dir_name())));    let daemon = regex_literal(CODEX_DAEMON_SOCKETS);    p.push_str(&format!(";; closed: Codex's shared daemon, which runs a client's commands outside the profile\n(deny file-read* file-write* (regex #\"^{daemon}\"))\n(deny network-outbound (remote unix-socket (path-regex #\"^{daemon}\")))\n"));    p.push_str(RESOLVED);    p.push('\n');    for (path, why) in LANE_OPENED_IN_HOME {        p.push_str(&format!(";; open: {why}\n(allow file-read* (subpath \"{HOME}/{path}\"))\n"));    }    let (keychain, why) = LANE_KEYCHAIN_OPENED;    p.push_str(&format!(";; open: {why}\n(allow file-read* file-write* (subpath \"{HOME}/{keychain}\"))\n"));    p.push_str(&format!(";; open: git reads the app's route to the code host from every command, through ~/.gitconfig\n(allow file-read* (literal \"{HOME}/{}/{GIT_ROUTE_IN_DATA}\"))\n", Flavor::Real.data_dir_name()));    let (ca_dir, ca_file) = GIT_ROUTE_CA_IN_DATA.rsplit_once('/').expect("the CA sits in a folder");    p.push_str(&format!(        ";; open: the public half of the route's certificate authority, which the route names\n(allow file-read* (regex #\"^{HOME_RE}/{}/{}/{}[^/]+/{}$\"))\n",        Flavor::Real.data_dir_name(), regex_literal(ca_dir), regex_literal(GIT_ROUTE_CA_DIR_PREFIX), regex_literal(ca_file)    ));    // NOTHING RUNS FREE (architect, 15 Oct 2026, on lanekeychain's finding): a    // child of a lane inherits the lane's profile or does not run. An exec    // opened `(with no-sandbox)` takes whatever profile its caller hands it —    // `sandbox-exec -p '(allow default)'` read every denied folder — so no    // entry carries it. SwiftPM and Xcode skip their own manifest sandboxes    // inside a lane (scripts/lib/mac_disk_policy.py's `lane_args`), and Cotton's    // seatbelt tests skip there, saying so.    p}#[cfg(test)]#[path = "lane_sandbox_tests.rs"]mod tests;