const NAMED: usize = 8;
#[cfg_attr(
not(any(target_os = "linux", test)),
expect(
dead_code,
reason = "only Linux has a Lightweight tier with a gap to name"
)
)]
pub const NAMESPACED: &str = "moat run: in the project, the paths the policy denies are \
covered for the session as under `--isolate` (`.env` files hidden, `.git`, `.moat` and \
agents' configuration read-only), but a match created after the start where nothing \
stood (`.env.local`, a `.env` in a new folder) stays open; the hook still checks the \
agent's own tool calls. `openmoat run --isolate` also hides the rest of the machine";
#[cfg_attr(
not(any(target_os = "linux", test)),
expect(
dead_code,
reason = "only Linux has a Lightweight tier with a gap to name"
)
)]
pub fn namespaced_near_top(why: &str) -> String {
format!(
"moat run: the project could not be walked ({why}), so only its top and the folders \
directly in it are covered for the session (`.env` files there hidden, `.git`, `.moat` \
and agents' configuration there read-only); what the policy denies deeper stays open \
to commands; the hook still checks the agent's own tool calls"
)
}
#[cfg_attr(
not(any(target_os = "linux", test)),
expect(
dead_code,
reason = "only Linux has a Lightweight tier with a gap to name"
)
)]
pub fn doctor_refusal(why: &str) -> String {
format!(
"openmoat run without a network namespace it would refuse to start here: the \
project cannot be granted entry by entry ({why}). Install bubblewrap \
(`apt install bubblewrap`)"
)
}
#[cfg_attr(
not(any(target_os = "linux", test)),
expect(
dead_code,
reason = "only Linux has a Lightweight tier with a gap to name"
)
)]
pub fn port_only(open: &Result<Vec<String>, String>) -> String {
format!(
"moat run: without bubblewrap the project is granted entry by entry: commands cannot \
create, rename or remove entries directly in it (create what they need before the \
run), `.env` files there stay unreadable and `.git` and `.moat` read-only. {} With \
bubblewrap (`apt install bubblewrap`) `openmoat run` covers these with mounts and \
needs none of this; or set `sandbox.project_top: writable` in your policy and accept \
that `.env` and git hooks at the top are open to commands. The hook still checks the \
agent's own tool calls",
left_open(open)
)
}
#[cfg_attr(
not(any(target_os = "linux", test)),
expect(
dead_code,
reason = "only Linux has a Lightweight tier with a gap to name"
)
)]
pub const PORT_ONLY_TOP_OPEN: &str = "moat run: without bubblewrap, and with \
`sandbox.project_top: writable` in your policy, the project is granted whole: commands the \
agent starts can read and write `.env`, write `.git/hooks` and `.moat`, and create \
`.envrc` there. This run is recorded in the audit log (`run-project-top-open`). Install \
bubblewrap (`apt install bubblewrap`), or remove the setting, to cover them; the hook \
still checks the agent's own tool calls";
#[cfg_attr(
not(any(target_os = "linux", test)),
expect(
dead_code,
reason = "only Linux has a Lightweight tier with a gap to name"
)
)]
pub const DOCTOR_TOP_OPEN: &str = "openmoat run `sandbox.project_top: writable`: without a \
network namespace the project is granted whole, so commands can read `.env` and write \
`.git/hooks` and `.moat` there; every such run is in the audit log. Install bubblewrap \
(`apt install bubblewrap`) or remove the setting";
#[cfg_attr(
not(any(target_os = "linux", test)),
expect(
dead_code,
reason = "only Linux has a Lightweight tier with a gap to name"
)
)]
pub fn doctor_line(open: &Result<Vec<String>, String>) -> String {
format!(
"openmoat run without a network namespace the project here is granted entry by \
entry: commands cannot create entries directly in it. {} Install bubblewrap \
(`apt install bubblewrap`) to cover them, or set `sandbox.project_top: writable` \
and accept that `.env` and git hooks at the top are open to commands",
left_open(open)
)
}
#[cfg_attr(
not(any(target_os = "linux", test)),
expect(
dead_code,
reason = "only Linux has a Lightweight tier with a gap to name"
)
)]
fn left_open(open: &Result<Vec<String>, String>) -> String {
match open {
Ok(paths) if paths.is_empty() => {
"Nothing else the policy denies in the project is open to commands.".to_owned()
}
Ok(paths) => {
let mut named = paths
.iter()
.take(NAMED)
.cloned()
.collect::<Vec<_>>()
.join(", ");
if paths.len() > NAMED {
named = format!("{named} and {} more", paths.len() - NAMED);
}
format!(
"These paths the policy denies lie in a folder granted whole and stay open to \
commands: {named}."
)
}
Err(why) => format!(
"Which paths the policy denies deeper in the project stay open could not be \
listed ({why})."
),
}
}
#[must_use]
pub fn command(bubblewrap: Option<bool>) -> &'static str {
match bubblewrap {
None => "`openmoat run`",
Some(_) => "`openmoat run --isolate`",
}
}
#[must_use]
pub fn words(bubblewrap: Option<bool>) -> &'static str {
match bubblewrap {
None => "openmoat run",
Some(_) => "openmoat run --isolate",
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn granted_entry_by_entry_names_what_it_covers_the_cost_and_each_open_path() {
let none = port_only(&Ok(Vec::new()));
for said in [
"entry by entry",
"cannot create, rename or remove entries directly in it",
"`.env` files there stay unreadable",
"`.git` and `.moat` read-only",
"Nothing else the policy denies",
"apt install bubblewrap",
"set `sandbox.project_top: writable`",
] {
assert!(none.contains(said), "{said}: {none}");
}
for said in ["`.env`", "`.git/hooks`", "run-project-top-open"] {
assert!(PORT_ONLY_TOP_OPEN.contains(said), "{said}");
}
assert!(DOCTOR_TOP_OPEN.starts_with("openmoat run "));
assert!(DOCTOR_TOP_OPEN.contains("`.git/hooks`"));
assert!(doctor_line(&Ok(Vec::new())).contains("sandbox.project_top: writable"));
let paths: Vec<String> = (0..10).map(|i| format!("/p/sub{i}/.env")).collect();
let some = port_only(&Ok(paths));
assert!(some.contains("/p/sub0/.env, /p/sub1/.env"), "{some}");
assert!(some.contains("/p/sub7/.env and 2 more"), "{some}");
assert!(!some.contains("/p/sub8/.env"), "{some}");
let unknown = port_only(&Err("too many files".into()));
assert!(
unknown.contains("could not be listed (too many files)"),
"{unknown}"
);
let doctor = doctor_line(&Ok(vec!["/p/a/.env".into()]));
assert!(doctor.starts_with("openmoat run "), "{doctor}");
assert!(doctor.contains("/p/a/.env"), "{doctor}");
}
#[test]
fn the_namespace_names_what_stays_open_after_the_start() {
assert!(NAMESPACED.contains("as under `--isolate`"));
assert!(NAMESPACED.contains("created after the start"));
let near = namespaced_near_top("more than 1000000 files");
assert!(
near.contains("could not be walked (more than 1000000 files)"),
"{near}"
);
assert!(near.contains("deeper stays open"), "{near}");
let refused = doctor_refusal("permission denied");
assert!(refused.starts_with("openmoat run "), "{refused}");
assert!(refused.contains("would refuse to start here"), "{refused}");
assert!(refused.contains("(permission denied)"), "{refused}");
}
#[test]
fn the_protection_line_names_isolate_on_linux() {
assert_eq!(command(None), "`openmoat run`");
assert_eq!(command(Some(true)), "`openmoat run --isolate`");
}
}