use openmoat_audit::KnownSecrets;
use openmoat_core::{Action, Decision, Notice, Policy, Verdict};
use crate::render::clean;
use crate::report::Paths;
mod builtin;
pub const CALL_CHARS: usize = 100;
const DETAIL_CHARS: usize = 300;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Words<'a> {
pub title: &'a str,
pub why: Option<&'a str>,
pub next: Option<(Verdict, &'a str)>,
}
pub fn words<'a>(policy: Option<&'a Policy>, rule: &str) -> Option<Words<'a>> {
match policy.and_then(|p| p.own_group(rule)) {
Some(group) => group.title.as_deref().map(|title| Words {
title,
why: group.why.as_deref(),
next: None,
}),
None => builtin::words(rule),
}
}
#[derive(Debug, Default)]
pub struct Titles(Vec<(String, Option<Own>)>);
#[derive(Debug)]
struct Own {
title: String,
why: Option<String>,
}
impl Titles {
pub fn of(policy: &Policy, decision: &Decision) -> Self {
Self(
decision
.rules
.iter()
.filter_map(|id| {
let group = policy.own_group(id)?;
let own = group.title.clone().map(|title| Own {
title,
why: group.why.clone(),
});
Some((id.clone(), own))
})
.collect(),
)
}
pub fn get(&self, rule: &str) -> Option<Words<'_>> {
match self.0.iter().find(|(id, _)| id == rule) {
Some((_, own)) => own.as_ref().map(|own| Words {
title: &own.title,
why: own.why.as_deref(),
next: None,
}),
None => builtin::words(rule),
}
}
}
pub fn of(
decision: &Decision,
titles: &Titles,
call: Option<(&str, Option<&Action>)>,
known: &KnownSecrets,
) -> Option<Box<Notice>> {
if !matches!(decision.verdict, Verdict::Deny | Verdict::Review) {
return None;
}
let words = decision.rules.iter().find_map(|r| titles.get(r))?;
let next = words
.next
.filter(|(verdict, _)| *verdict == decision.verdict)
.map(|(_, next)| next.to_owned());
let paths = Paths::detect();
let call = call
.map(|(tool, action)| middle(&shown(&paths, known, &call_text(tool, action)), CALL_CHARS));
let detail = decision
.reasons
.iter()
.map(|reason| middle(&masked(known, reason), DETAIL_CHARS))
.collect::<Vec<_>>()
.join("; ");
Some(Box::new(Notice {
title: clean(words.title).into_owned(),
why: words.why.map(|w| clean(w).into_owned()),
call,
detail: (!detail.is_empty()).then_some(detail),
next,
unasked: None,
}))
}
pub fn shown(paths: &Paths, known: &KnownSecrets, text: &str) -> String {
paths.shorten(&masked(known, text))
}
fn masked(known: &KnownSecrets, text: &str) -> String {
let redacted = known.redact(text);
let one_line = redacted.split_whitespace().collect::<Vec<_>>().join(" ");
clean(&one_line).into_owned()
}
pub fn call_text(tool: &str, action: Option<&Action>) -> String {
match (action.map(Action::shown), target(action)) {
(Some(Action::Shell { command }), _) => command.clone(),
(_, Some(target)) if target != tool => format!("{tool} {target}"),
_ => tool.to_owned(),
}
}
pub fn target(action: Option<&Action>) -> Option<String> {
Some(match action?.shown() {
Action::Shell { command } => command.clone(),
Action::ForeignShell { shell, command } => format!("{shell}: {command}"),
Action::FsRead { path } | Action::FsWrite { path } => path.clone(),
Action::Net { url } | Action::Fetch { url } => url.clone(),
Action::WebSearch { query } => query.clone(),
Action::Patch { writes } => writes.join(", "),
Action::ReadFiles { paths } => paths.join(", "),
Action::McpTool { name, .. } => name.clone(),
_ => return None,
})
}
pub fn middle(text: &str, max: usize) -> String {
let count = text.chars().count();
if count <= max {
return text.to_owned();
}
let keep = max.saturating_sub(1);
let head = keep.div_ceil(2);
let tail = keep - head;
let start: String = text.chars().take(head).collect();
let end: String = text.chars().skip(count - tail).collect();
format!("{start}…{end}")
}
#[cfg(test)]
mod tests;