use serde::{Deserialize, Serialize};
use super::trifecta::owned_by_secrets;
use super::{CompiledPolicy, Taint, UntrustedKind};
use crate::action::{Action, AtomicAction};
use crate::pattern::any_match;
use crate::verdict::{Decision, Verdict};
pub const DATA_FLOW_ATOM: &str = "@data-flow";
pub const LABEL_SECRET: &str = "secret";
pub const LABEL_PRIVATE: &str = "private";
pub const LABEL_EXTERNAL: &str = "external";
pub const LABEL_WEB: &str = "web";
pub const LABEL_UNKNOWN_TOOL: &str = "unknown-tool";
pub const LABEL_UNTRUSTED: &str = "untrusted";
pub const LABEL_MCP_PREFIX: &str = "mcp:";
const SINK_PROTECTED_WRITE: &str = "protected-write";
const SAME_SOURCE: &str = "same-source";
const FLOW_TRUSTED: &str = "flow-trusted";
const MAX_LABELS: usize = 32;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Label {
pub name: String,
pub source: String,
}
impl Taint {
pub(super) fn add_label(&mut self, name: &str, source: &str) {
if self.labels.len() < MAX_LABELS && !self.labels.iter().any(|l| l.name == name) {
self.labels.push(Label {
name: name.to_owned(),
source: source.to_owned(),
});
}
}
#[must_use]
pub fn label_set(&self) -> Vec<Label> {
let mut out = self.labels.clone();
let mut implied = |name: &str, source: &str| {
if !out.iter().any(|l| l.name == name) {
out.push(Label {
name: name.to_owned(),
source: source.to_owned(),
});
}
};
if let Some(secret) = self.secrets.first() {
implied(LABEL_SECRET, &secret.source);
}
if let Some(private) = &self.private {
implied(LABEL_PRIVATE, private);
}
if let Some(untrusted) = &self.untrusted
&& !self.labels.iter().any(|l| l.source == *untrusted)
{
implied(LABEL_UNTRUSTED, untrusted);
}
if let Some(seen) = &self.seen
&& !self
.labels
.iter()
.any(|l| l.name == LABEL_EXTERNAL && l.source == *seen)
{
implied(LABEL_EXTERNAL, seen);
if self.untrusted.is_none() {
implied(LABEL_UNTRUSTED, seen);
}
}
out
}
}
pub(super) fn untrusted_label(kind: UntrustedKind, atom: &AtomicAction) -> String {
match (kind, atom) {
(UntrustedKind::Web, _) => LABEL_WEB.to_owned(),
(UntrustedKind::Mcp, AtomicAction::McpTool { name }) => {
format!("{LABEL_MCP_PREFIX}{}", mcp_server(name))
}
(UntrustedKind::UnknownTool, _) => LABEL_UNKNOWN_TOOL.to_owned(),
_ => LABEL_UNTRUSTED.to_owned(),
}
}
fn mcp_server(name: &str) -> &str {
name.split("__").nth(1).unwrap_or(name)
}
impl CompiledPolicy<'_> {
pub(super) fn label_flows(
&self,
action: &Action,
atoms: &[AtomicAction],
taint: &Taint,
) -> Vec<Decision> {
let labels = taint.label_set();
if labels.is_empty() {
return Vec::new();
}
let mut sinks: Vec<(&str, String)> = self
.egress_sinks(action, atoms)
.into_iter()
.map(|(sink, target)| (sink.as_str(), target))
.collect();
for atom in atoms {
if let AtomicAction::FsWrite { path } = atom
&& any_match(&self.protected, path.as_str())
{
sinks.push((SINK_PROTECTED_WRITE, path.as_str().to_owned()));
}
}
let mut out = Vec::new();
for label in &labels {
for (sink, target) in &sinks {
let mut argv = vec![
DATA_FLOW_ATOM.to_owned(),
label.name.clone(),
(*sink).to_owned(),
target.clone(),
];
if same_source(&label.name, target, taint) {
argv.push(SAME_SOURCE.to_owned());
} else if self.flow_trusted(&label.name, target) {
argv.push(FLOW_TRUSTED.to_owned());
}
let Some(hit) = self.flow_rules(&AtomicAction::Shell { argv }) else {
continue;
};
let detail = format!(
"this session holds data from {} ({}), and this call can pass it to {sink} {target}, \
however it was reworded",
describe(&label.name),
label.source
);
let mut named = Decision::new(hit.verdict);
for rule in &hit.rules {
named.push(rule, detail.clone());
}
out.push(named);
}
}
out
}
fn flow_trusted(&self, label: &str, target: &str) -> bool {
let trusted = self.policy.mcp_flow_trusted();
[label, target].into_iter().any(|word| {
word.strip_prefix(LABEL_MCP_PREFIX)
.is_some_and(|server| trusted.iter().any(|t| t == server))
})
}
fn flow_rules(&self, atom: &AtomicAction) -> Option<Decision> {
Self::hit(&self.deny, Verdict::Deny, atom)
.or_else(|| Self::hit(&self.repo_review, Verdict::Review, atom))
.or_else(|| Self::hit(&self.review, Verdict::Review, atom))
}
}
fn same_source(label: &str, target: &str, taint: &Taint) -> bool {
if label.starts_with(LABEL_MCP_PREFIX) {
return label == target;
}
label == LABEL_SECRET && owned_by_secrets(taint, target)
}
fn describe(label: &str) -> String {
match label {
LABEL_SECRET => "secret material".to_owned(),
LABEL_PRIVATE => "private data".to_owned(),
LABEL_EXTERNAL => "content someone else may have written".to_owned(),
LABEL_WEB => "the web".to_owned(),
LABEL_UNKNOWN_TOOL => "a tool OpenMoat does not know".to_owned(),
LABEL_UNTRUSTED => "untrusted content".to_owned(),
other => match other.strip_prefix(LABEL_MCP_PREFIX) {
Some(server) => format!("MCP server {server}"),
None => other.to_owned(),
},
}
}