use openmoat_core::ir::brace_alternatives;
use openmoat_core::rule_id::AGENT_CREDENTIALS;
use openmoat_core::{EvalContext, Kind, Policy};
use openmoat_hosts::Host;
use super::Report;
use super::exposure::within;
use super::patterns::push_unique;
pub const ALLOWANCE: &str = "run.own-login";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Owner {
Agent(Host),
Unknown,
}
impl Owner {
pub fn of(name: &str, program: &str) -> Self {
Host::by_program(name)
.or_else(|| Host::by_program(program))
.map_or(Self::Unknown, |(host, _)| Self::Agent(host))
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Kept {
pub owner: Owner,
pub patterns: Vec<String>,
}
pub fn for_session(
policy: &Policy,
ctx: &EvalContext,
owner: Owner,
writes: &[String],
) -> (Policy, Kept) {
let own = match owner {
Owner::Agent(host) => own_patterns(host),
Owner::Unknown => Vec::new(),
};
let ci = crate::context::CASE_INSENSITIVE_PATHS;
let keep = |pattern: &str| match owner {
Owner::Agent(_) => own.iter().any(|p| p == pattern),
Owner::Unknown => {
!pattern.starts_with('!')
&& ctx.spellings(pattern).iter().any(|spelling| {
let base = literal_base(spelling);
writes.iter().any(|write| within(base, write, ci))
})
}
};
let mut policy = policy.clone();
let mut kept = Vec::new();
for group in policy
.deny
.iter_mut()
.filter(|g| g.id == AGENT_CREDENTIALS && !g.hook_only)
{
for list in [&mut group.fs_read, &mut group.fs_write] {
let expanded: Vec<String> = list.iter().flat_map(|p| brace_alternatives(p)).collect();
let (out, rest): (Vec<String>, Vec<String>) =
expanded.into_iter().partition(|p| keep(p));
for pattern in out {
push_unique(&mut kept, pattern);
}
*list = rest;
}
}
(
policy,
Kept {
owner,
patterns: kept,
},
)
}
pub fn own_patterns(host: Host) -> Vec<String> {
let mut out = Vec::new();
for paths in host.spec().protect.credentials {
for glob in paths.globs.iter().flat_map(|g| brace_alternatives(g)) {
if let Some(dir) = glob.strip_suffix("/**") {
push_unique(&mut out, dir.to_owned());
}
push_unique(&mut out, glob);
}
}
out
}
fn literal_base(pattern: &str) -> &str {
let base = pattern
.split(['*', '?', '[', '{'])
.next()
.unwrap_or(pattern);
match base.trim_end_matches('/') {
"" => base,
trimmed => trimmed,
}
}
pub fn report(report: &mut Report, kept: &Kept, ctx: &EvalContext) {
if kept.patterns.is_empty() {
return;
}
let patterns: Vec<String> = kept
.patterns
.iter()
.flat_map(|p| ctx.spellings(p))
.collect();
let message = match kept.owner {
Owner::Agent(host) => format!(
"the sandbox holds {} itself, which signs in with these files, so they stay \
readable and writable here, for the commands it runs too; every other agent's \
login is denied (`{AGENT_CREDENTIALS}`)",
host.display_name()
),
Owner::Unknown => format!(
"the program is no agent OpenMoat knows, so the agent logins inside its `--write` \
paths stay readable and writable here, for the commands it runs too; every other \
agent's login is denied (`{AGENT_CREDENTIALS}`)"
),
};
for kind in [Kind::FsRead, Kind::FsWrite] {
report.allowance(kind, ALLOWANCE, patterns.clone(), &message);
}
}
#[cfg(test)]
mod tests;