use super::view::{KillSwitchView, RuleView, RuntimeMonitorView};
#[cfg(target_os = "linux")]
use super::{normalize, output};
#[cfg(target_os = "linux")]
use assay_common::MonitorEvent;
use assay_common::{EVENT_EXEC, EVENT_OPENAT};
use assay_core::mcp::runtime_features::{KillMode, MonitorAction, MonitorRuleType};
use assay_evidence::sanitize::sanitize_terminal_line;
#[derive(Debug)]
pub(crate) struct MonitorPolicy {
pub(crate) disabled: bool,
pub(crate) rules: Option<Vec<RuleView>>,
pub(crate) kill: KillPlan,
pub(crate) refusals: Vec<String>,
}
pub(crate) fn read_monitor_policy(
source: &assay_core::mcp::policy::PolicySource,
) -> anyhow::Result<MonitorPolicy> {
let runtime = source.section::<RuntimeMonitorView>("runtime_monitor")?;
if runtime
.value
.as_ref()
.is_some_and(|section| !section.enabled)
{
return Ok(MonitorPolicy {
disabled: true,
rules: None,
kill: KillPlan::none(),
refusals: Vec::new(),
});
}
let kill_switch = source.section::<KillSwitchView>("kill_switch")?;
let kill_switch_off = kill_switch.value.as_ref().is_some_and(|view| !view.enabled);
let kill_switch_unread: &[String] = if kill_switch_off {
&[]
} else {
&kill_switch.unread
};
let mut refusals: Vec<String> = runtime
.unread
.iter()
.chain(kill_switch_unread)
.map(|path| format!("{path:?}: assay monitor does not act on this key; remove it"))
.collect();
let rules = runtime.value.map(|section| section.rules);
let kill = KillPlan::from_view(kill_switch.value.as_ref());
if let Some(rules) = &rules {
refusals.extend(unenforceable_rules(rules, kill.enabled));
}
if let Some(view) = kill_switch.value.as_ref().filter(|_| !kill_switch_off) {
refusals.extend(trigger_refusals(view, rules.as_deref().unwrap_or_default()));
}
let refusals = refusals
.iter()
.map(|refusal| sanitize_terminal_line(refusal, usize::MAX))
.collect();
Ok(MonitorPolicy {
disabled: false,
rules,
kill,
refusals,
})
}
#[derive(Debug, Clone)]
pub(crate) struct KillPlan {
pub(crate) enabled: bool,
pub(crate) grace_period_ms: u64,
mode: KillMode,
triggers: Vec<(String, Option<KillMode>)>,
}
impl KillPlan {
pub(crate) fn none() -> Self {
Self::from_view(None)
}
fn from_view(view: Option<&KillSwitchView>) -> Self {
match view {
Some(view) => Self {
enabled: view.enabled,
grace_period_ms: view.grace_period_ms,
mode: view.mode.clone(),
triggers: view
.triggers
.iter()
.map(|trigger| (trigger.on_rule.clone(), trigger.mode.clone()))
.collect(),
},
None => Self {
enabled: false,
grace_period_ms: 3000,
mode: KillMode::Graceful,
triggers: Vec::new(),
},
}
}
pub(crate) fn mode_for(&self, rule_id: &str) -> KillMode {
self.triggers
.iter()
.find(|(on_rule, _)| on_rule == rule_id)
.and_then(|(_, mode)| mode.clone())
.unwrap_or_else(|| self.mode.clone())
}
}
fn trigger_refusals(kill_switch: &KillSwitchView, rules: &[RuleView]) -> Vec<String> {
let mut seen: Vec<&String> = Vec::new();
let mut refusals = Vec::new();
for trigger in &kill_switch.triggers {
let id = &trigger.on_rule;
let rule = id.escape_debug();
let reason = match rules.iter().find(|candidate| &candidate.id == id) {
None => Some(format!("rule '{rule}' is not a runtime_monitor rule")),
Some(found) if matches!(found.kind, MonitorRuleType::NetConnect) => Some(format!(
"rule '{rule}' is a net_connect rule, which is denied in the kernel and never killed"
)),
Some(found) if found.action != MonitorAction::TriggerKill => {
Some(format!("rule '{rule}' does not use trigger_kill"))
}
Some(_) if seen.contains(&id) => {
Some(format!("rule '{rule}' already has a trigger, and the first one is used"))
}
Some(_) if trigger.mode.is_none() => Some(format!(
"the trigger for rule '{rule}' sets no mode, and kill_switch.mode applies without it"
)),
Some(_) => None,
};
seen.push(id);
if let Some(reason) = reason {
refusals.push(format!(
"kill_switch.triggers: {reason}, so this trigger has no effect"
));
}
}
refusals
}
#[derive(Debug)]
pub(crate) struct ActiveRule {
pub(crate) id: String,
pub(crate) action: MonitorAction,
pub(crate) event_type: u32,
pub(crate) allow: globset::GlobSet,
pub(crate) deny: Option<globset::GlobSet>,
}
pub(crate) fn compile_globset(globs: &[String]) -> anyhow::Result<globset::GlobSet> {
let mut b = globset::GlobSetBuilder::new();
for g in globs {
b.add(globset::Glob::new(g)?);
}
Ok(b.build()?)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Destination {
Cidr,
Port(u16),
Unmatchable,
}
pub(crate) fn classify_destination(dest: &str) -> Destination {
if let Some((ip, prefix)) = dest.split_once('/') {
if ip.parse::<std::net::IpAddr>().is_ok() && prefix.parse::<u8>().is_ok() {
return Destination::Cidr;
}
}
match dest.parse::<u16>() {
Ok(port) => Destination::Port(port),
Err(_) => Destination::Unmatchable,
}
}
fn kind_name(kind: &MonitorRuleType) -> &'static str {
match kind {
MonitorRuleType::FileOpen => "file_open",
MonitorRuleType::ProcExec => "proc_exec",
MonitorRuleType::NetConnect => "net_connect",
}
}
fn plan_rule(rule: &RuleView, kill_switch_enabled: bool) -> Result<Option<ActiveRule>, String> {
let matcher = &rule.matcher;
let kind = kind_name(&rule.kind);
let event_type = match rule.kind {
MonitorRuleType::FileOpen => EVENT_OPENAT,
MonitorRuleType::ProcExec => {
if rule.action == MonitorAction::Deny {
return Err(
"proc_exec cannot be denied: the monitor sees an exec once the new \
program has started. Use trigger_kill, alert or log"
.to_string(),
);
}
if rule.action == MonitorAction::TriggerKill && !kill_switch_enabled {
return Err(
"trigger_kill on proc_exec needs an enabled kill_switch: without one the \
monitor would only log the exec. Enable kill_switch, or use alert or log"
.to_string(),
);
}
EVENT_EXEC
}
MonitorRuleType::NetConnect => {
if !matches!(
rule.action,
MonitorAction::Deny | MonitorAction::TriggerKill
) {
return Err(
"net_connect rules are installed in the kernel as deny rules; \
action log or alert has no effect"
.to_string(),
);
}
if matcher.dest_globs.is_empty() {
return Err("has no dest_globs, so it can never match".to_string());
}
if !matcher.path_globs.is_empty() {
return Err(format!("path_globs has no effect on a {kind} rule"));
}
if matcher.not.is_some() {
return Err(format!("not has no effect on a {kind} rule"));
}
if let Some(dest) = matcher
.dest_globs
.iter()
.find(|dest| classify_destination(dest) == Destination::Unmatchable)
{
return Err(format!(
"dest_globs entry {dest:?} is not an IP CIDR or a port, so the kernel rule \
cannot match it"
));
}
return Ok(None);
}
};
if matcher.path_globs.is_empty() {
return Err("has no path_globs, so it can never match".to_string());
}
if !matcher.dest_globs.is_empty() {
return Err(format!("dest_globs has no effect on a {kind} rule"));
}
if let Some(not) = &matcher.not {
if !not.dest_globs.is_empty() {
return Err(format!("not.dest_globs has no effect on a {kind} rule"));
}
if not.not.is_some() {
return Err("not.not has no effect: an exception has no exception".to_string());
}
}
let allow = compile_globset(&matcher.path_globs)
.map_err(|error| format!("path_globs do not compile: {error}"))?;
let deny = matcher
.not
.as_ref()
.map(|not| compile_globset(¬.path_globs))
.transpose()
.map_err(|error| format!("not.path_globs do not compile: {error}"))?;
Ok(Some(ActiveRule {
id: rule.id.clone(),
action: rule.action.clone(),
event_type,
allow,
deny,
}))
}
pub(crate) fn compile_active_rules(
rules: Option<&[RuleView]>,
kill_switch_enabled: bool,
) -> Vec<ActiveRule> {
rules
.unwrap_or_default()
.iter()
.filter_map(|rule| plan_rule(rule, kill_switch_enabled).ok().flatten())
.collect()
}
fn unenforceable_rules(rules: &[RuleView], kill_switch_enabled: bool) -> Vec<String> {
rules
.iter()
.filter_map(|rule| {
plan_rule(rule, kill_switch_enabled)
.err()
.map(|reason| format!("rule '{}': {reason}", rule.id.escape_debug()))
})
.collect()
}
pub(crate) fn matching_rule<'a>(
event_type: u32,
path: &str,
rules: &'a [ActiveRule],
) -> Option<&'a ActiveRule> {
rules.iter().find(|rule| {
rule.event_type == event_type
&& rule.allow.is_match(path)
&& !rule.deny.as_ref().is_some_and(|deny| deny.is_match(path))
})
}
#[cfg(target_os = "linux")]
pub(crate) fn find_violation_rule<'a>(
event: &MonitorEvent,
rules: &'a [ActiveRule],
) -> Option<&'a ActiveRule> {
if rules.is_empty() || !matches!(event.event_type, EVENT_OPENAT | EVENT_EXEC) {
return None;
}
let raw = output::decode_utf8_cstr(&event.data);
let path = normalize::normalize_path_syntactic(&raw);
matching_rule(event.event_type, &path, rules)
}
#[cfg(test)]
mod tests {
use super::*;
use assay_common::{EVENT_CONNECT, EVENT_EXEC, EVENT_OPENAT};
fn config(rules_yaml: &str) -> Vec<RuleView> {
serde_yaml::from_str::<RuntimeMonitorView>(&format!("enabled: true\nrules:\n{rules_yaml}"))
.expect("monitor config")
.rules
}
fn read(policy: &str) -> MonitorPolicy {
let (_, source) =
assay_core::mcp::policy::McpPolicy::from_slice_with_source(policy.as_bytes())
.expect("policy load");
read_monitor_policy(&source).expect("monitor reading")
}
const EXEC_KILL: &str = " - id: no-curl\n type: proc_exec\n match:\n path_globs: ['**/curl']\n not:\n path_globs: ['/opt/safe/**']\n action: trigger_kill\n";
const KILL_SWITCH_ON: bool = true;
const KILL_SWITCH_OFF: bool = false;
const FILE_DENY: &str = " - id: no-secrets\n type: file_open\n match:\n path_globs: ['/etc/secret/**']\n action: deny\n";
#[test]
fn a_proc_exec_rule_matches_the_exec_it_names_and_only_exec_events() {
let cfg = config(&format!("{EXEC_KILL}{FILE_DENY}"));
assert_eq!(
unenforceable_rules(&cfg, KILL_SWITCH_ON),
Vec::<String>::new()
);
let rules = compile_active_rules(Some(&cfg[..]), KILL_SWITCH_ON);
let hit = matching_rule(EVENT_EXEC, "/usr/bin/curl", &rules).expect("exec of curl matches");
assert_eq!(hit.id, "no-curl");
assert!(matching_rule(EVENT_EXEC, "/usr/bin/true", &rules).is_none());
assert!(matching_rule(EVENT_EXEC, "curl", &rules).is_some());
assert!(matching_rule(EVENT_EXEC, "/opt/safe/curl", &rules).is_none());
assert!(matching_rule(EVENT_OPENAT, "/usr/bin/curl", &rules).is_none());
assert!(matching_rule(EVENT_EXEC, "/etc/secret/key", &rules).is_none());
assert_eq!(
matching_rule(EVENT_OPENAT, "/etc/secret/key", &rules).map(|r| r.id.as_str()),
Some("no-secrets")
);
assert!(matching_rule(EVENT_CONNECT, "/usr/bin/curl", &rules).is_none());
}
#[test]
fn a_rule_the_monitor_cannot_act_on_is_named_at_load() {
let cases = [
(
" - id: exec-deny\n type: proc_exec\n match:\n path_globs: ['**/curl']\n action: deny\n",
"rule 'exec-deny': proc_exec cannot be denied",
),
(
" - id: net-log\n type: net_connect\n match:\n dest_globs: ['10.0.0.0/8']\n action: log\n",
"rule 'net-log': net_connect",
),
(
" - id: exec-empty\n type: proc_exec\n match: {}\n action: alert\n",
"rule 'exec-empty': has no path_globs",
),
(
" - id: file-empty\n type: file_open\n match:\n dest_globs: ['10.0.0.0/8']\n action: deny\n",
"rule 'file-empty': has no path_globs",
),
(
" - id: net-empty\n type: net_connect\n match:\n path_globs: ['/x']\n action: deny\n",
"rule 'net-empty': has no dest_globs",
),
(
" - id: bad-glob\n type: file_open\n match:\n path_globs: ['/etc/[']\n action: log\n",
"rule 'bad-glob': path_globs do not compile",
),
(
" - id: bad-not\n type: file_open\n match:\n path_globs: ['/etc/**']\n not:\n path_globs: ['/etc/[']\n action: trigger_kill\n",
"rule 'bad-not': not.path_globs do not compile",
),
(
" - id: file-dest\n type: file_open\n match:\n path_globs: ['/etc/**']\n dest_globs: ['10.0.0.0/8']\n action: deny\n",
"rule 'file-dest': dest_globs has no effect on a file_open rule",
),
(
" - id: exec-not-dest\n type: proc_exec\n match:\n path_globs: ['**/sh']\n not:\n dest_globs: ['10.0.0.0/8']\n action: log\n",
"rule 'exec-not-dest': not.dest_globs has no effect on a proc_exec rule",
),
(
" - id: file-not-not\n type: file_open\n match:\n path_globs: ['/etc/**']\n not:\n path_globs: ['/etc/ok/**']\n not:\n path_globs: ['/etc/ok/x']\n action: log\n",
"rule 'file-not-not': not.not has no effect",
),
(
" - id: net-path\n type: net_connect\n match:\n dest_globs: ['10.0.0.0/8']\n path_globs: ['/x']\n action: deny\n",
"rule 'net-path': path_globs has no effect on a net_connect rule",
),
(
" - id: net-not\n type: net_connect\n match:\n dest_globs: ['10.0.0.0/8']\n not:\n dest_globs: ['10.1.0.0/16']\n action: deny\n",
"rule 'net-not': not has no effect on a net_connect rule",
),
];
for (yaml, want) in cases {
let refused = unenforceable_rules(&config(yaml), KILL_SWITCH_ON);
assert_eq!(refused.len(), 1, "{yaml} -> {refused:?}");
assert!(
refused[0].starts_with(want),
"{:?} does not start with {want:?}",
refused[0]
);
assert!(compile_active_rules(Some(&config(yaml)[..]), KILL_SWITCH_ON).is_empty());
}
for yaml in [
EXEC_KILL,
FILE_DENY,
" - id: exec-log\n type: proc_exec\n match:\n path_globs: ['**/sh']\n action: log\n",
" - id: net-deny\n type: net_connect\n match:\n dest_globs: ['10.0.0.0/8']\n action: deny\n",
] {
assert_eq!(unenforceable_rules(&config(yaml), KILL_SWITCH_ON), Vec::<String>::new(), "{yaml}");
}
}
#[test]
fn a_proc_exec_kill_rule_needs_the_kill_switch() {
let refused = unenforceable_rules(&config(EXEC_KILL), KILL_SWITCH_OFF);
assert_eq!(refused.len(), 1, "{refused:?}");
assert!(
refused[0].starts_with(
"rule 'no-curl': trigger_kill on proc_exec needs an enabled kill_switch"
),
"{:?}",
refused[0]
);
assert!(compile_active_rules(Some(&config(EXEC_KILL)[..]), KILL_SWITCH_OFF).is_empty());
let file_kill = " - id: f\n type: file_open\n match:\n path_globs: ['/etc/secret/**']\n action: trigger_kill\n";
let exec_alert = " - id: e\n type: proc_exec\n match:\n path_globs: ['**/sh']\n action: alert\n";
for yaml in [file_kill, exec_alert] {
assert_eq!(
unenforceable_rules(&config(yaml), KILL_SWITCH_OFF),
Vec::<String>::new(),
"{yaml}"
);
}
}
#[test]
fn the_first_matching_rule_in_policy_order_wins() {
let two = " - id: first\n type: proc_exec\n match:\n path_globs: ['**/curl']\n action: log\n - id: second\n type: proc_exec\n match:\n path_globs: ['/usr/**']\n action: trigger_kill\n";
let rules = compile_active_rules(Some(&config(two)[..]), KILL_SWITCH_ON);
assert_eq!(
matching_rule(EVENT_EXEC, "/usr/bin/curl", &rules).map(|r| r.id.as_str()),
Some("first")
);
}
#[test]
fn the_kill_switch_is_enabled_only_when_present_and_not_switched_off() {
let rules = "runtime_monitor:\n enabled: true\n rules:\n - id: no-curl\n type: proc_exec\n match:\n path_globs: ['**/curl']\n action: trigger_kill\n";
assert!(!read(rules).kill.enabled, "no kill_switch section");
let off = read(&format!("kill_switch:\n enabled: false\n{rules}"));
assert!(!off.kill.enabled, "switched off");
assert!(
read(&format!("kill_switch:\n enabled: true\n{rules}"))
.kill
.enabled
);
assert!(
read(&format!("kill_switch: {{}}\n{rules}")).kill.enabled,
"present, default on"
);
assert_eq!(off.refusals.len(), 1, "{:?}", off.refusals);
assert!(off.refusals[0].starts_with("rule 'no-curl': trigger_kill on proc_exec needs"));
}
#[test]
fn a_left_out_key_means_what_the_policy_schema_says() {
use assay_core::mcp::runtime_features::{KillSwitchConfig, RuntimeMonitorConfig};
let schema: KillSwitchConfig = serde_yaml::from_str("{}").expect("schema");
let view: KillSwitchView = serde_yaml::from_str("{}").expect("view");
assert_eq!(view.enabled, schema.enabled);
assert_eq!(view.mode, schema.mode);
assert_eq!(view.grace_period_ms, schema.grace_period_ms);
assert!(view.triggers.is_empty() && schema.triggers.is_empty());
let none = KillPlan::none();
assert!(!none.enabled);
assert_eq!(none.mode_for("any"), KillMode::Graceful);
assert_eq!(none.grace_period_ms, schema.grace_period_ms);
let schema: RuntimeMonitorConfig =
serde_yaml::from_str("rules:\n - id: r\n type: file_open\n match: {}\n")
.expect("schema");
let view: RuntimeMonitorView =
serde_yaml::from_str("rules:\n - id: r\n type: file_open\n match: {}\n")
.expect("view");
assert_eq!(view.enabled, schema.enabled);
assert_eq!(view.rules[0].action, schema.rules[0].action);
}
#[test]
fn a_trigger_sets_the_mode_for_its_rule_only() {
let policy = read(
"kill_switch:\n mode: immediate\n grace_period_ms: 250\n triggers:\n - on_rule: soft\n mode: graceful\nruntime_monitor:\n enabled: true\n rules:\n - id: soft\n type: proc_exec\n match:\n path_globs: ['**/a']\n action: trigger_kill\n - id: hard\n type: proc_exec\n match:\n path_globs: ['**/b']\n action: trigger_kill\n",
);
assert!(policy.refusals.is_empty(), "{:?}", policy.refusals);
assert_eq!(policy.kill.mode_for("soft"), KillMode::Graceful);
assert_eq!(policy.kill.mode_for("hard"), KillMode::Immediate);
assert_eq!(policy.kill.grace_period_ms, 250);
assert_eq!(policy.rules.as_deref().map(<[RuleView]>::len), Some(2));
}
#[test]
fn a_trigger_with_no_effect_is_refused() {
let rules = "runtime_monitor:\n enabled: true\n rules:\n - id: kill\n type: file_open\n match:\n path_globs: ['/a']\n action: trigger_kill\n - id: log\n type: file_open\n match:\n path_globs: ['/b']\n action: log\n";
let policy = read(&format!(
"kill_switch:\n triggers:\n - on_rule: kill\n mode: immediate\n - on_rule: kill\n mode: graceful\n - on_rule: log\n mode: immediate\n{rules}"
));
assert_eq!(
policy.refusals,
[
"kill_switch.triggers: rule 'kill' already has a trigger, and the first one is used, so this trigger has no effect",
"kill_switch.triggers: rule 'log' does not use trigger_kill, so this trigger has no effect",
]
);
let no_mode = read(&format!(
"kill_switch:\n triggers:\n - on_rule: kill\n{rules}"
));
assert_eq!(
no_mode.refusals,
["kill_switch.triggers: the trigger for rule 'kill' sets no mode, and kill_switch.mode applies without it, so this trigger has no effect"]
);
let orphan =
read("kill_switch:\n triggers:\n - on_rule: kill\n mode: immediate\n");
assert_eq!(
orphan.refusals,
["kill_switch.triggers: rule 'kill' is not a runtime_monitor rule, so this trigger has no effect"]
);
assert!(orphan.rules.is_none());
}
fn keys_of<T: serde::de::DeserializeOwned>() -> Vec<&'static str> {
assay_core::unread_keys::keys_of::<T>()
.expect("a struct with named fields")
.to_vec()
}
#[test]
fn every_key_the_monitor_reads_is_in_the_policy_schema() {
use crate::cli::commands::monitor::monitor_next::view::{MatchView, TriggerView};
use assay_core::mcp::runtime_features::{
KillSwitchConfig, KillTrigger, MonitorMatch, MonitorRule, RuntimeMonitorConfig,
};
for (section, view, schema) in [
(
"runtime_monitor",
keys_of::<RuntimeMonitorView>(),
keys_of::<RuntimeMonitorConfig>(),
),
(
"runtime_monitor.rules[]",
keys_of::<RuleView>(),
keys_of::<MonitorRule>(),
),
(
"runtime_monitor.rules[].match",
keys_of::<MatchView>(),
keys_of::<MonitorMatch>(),
),
(
"kill_switch",
keys_of::<KillSwitchView>(),
keys_of::<KillSwitchConfig>(),
),
(
"kill_switch.triggers[]",
keys_of::<TriggerView>(),
keys_of::<KillTrigger>(),
),
] {
let unknown: Vec<_> = view.iter().filter(|key| !schema.contains(key)).collect();
assert!(
unknown.is_empty(),
"{section}: the monitor reads {unknown:?}, which the schema does not have"
);
assert!(!view.is_empty() && view.len() <= schema.len(), "{section}");
}
}
#[test]
fn the_guide_lists_exactly_the_keys_the_monitor_reads() {
use crate::cli::commands::monitor::monitor_next::view::{MatchView, TriggerView};
let mut read = std::collections::BTreeSet::new();
for (prefix, keys) in [
("runtime_monitor.", keys_of::<RuntimeMonitorView>()),
("runtime_monitor.rules[].", keys_of::<RuleView>()),
("runtime_monitor.rules[].match.", keys_of::<MatchView>()),
("kill_switch.", keys_of::<KillSwitchView>()),
("kill_switch.triggers[].", keys_of::<TriggerView>()),
] {
read.extend(keys.into_iter().map(|key| format!("{prefix}{key}")));
}
let guide = include_str!("../../../../../../docs/guides/runtime-monitor.md");
let documented: std::collections::BTreeSet<String> = guide
.lines()
.filter_map(|line| line.strip_prefix("| `"))
.filter_map(|rest| rest.split_once("` |").map(|(key, _)| key))
.filter(|key| key.starts_with("runtime_monitor.") || key.starts_with("kill_switch."))
.map(str::to_string)
.collect();
assert_eq!(documented, read);
assert!(
read.contains("runtime_monitor.rules[].type"),
"renames are applied: {read:?}"
);
}
#[test]
fn a_trigger_on_a_rule_that_is_never_killed_is_refused_and_an_off_switch_is_not_read() {
let net = "runtime_monitor:\n enabled: true\n rules:\n - id: net\n type: net_connect\n match:\n dest_globs: ['10.0.0.0/8']\n action: trigger_kill\n";
let policy = read(&format!(
"kill_switch:\n triggers:\n - on_rule: net\n mode: immediate\n{net}"
));
assert_eq!(
policy.refusals,
["kill_switch.triggers: rule 'net' is a net_connect rule, which is denied in the kernel and never killed, so this trigger has no effect"]
);
let off = read(&format!(
"kill_switch:\n enabled: false\n kill_children: true\n triggers:\n - on_rule: net\n{net}"
));
assert!(!off.kill.enabled);
assert!(off.refusals.is_empty(), "{:?}", off.refusals);
}
#[test]
fn a_net_connect_destination_the_kernel_cannot_match_is_refused() {
let rule = |dest: &str| {
format!(" - id: net\n type: net_connect\n match:\n dest_globs: ['{dest}']\n action: deny\n")
};
for dest in ["example.com", "10.0.0.1", "*.internal", "10.0.0.0/33x"] {
let refused = unenforceable_rules(&config(&rule(dest)), KILL_SWITCH_ON);
assert_eq!(
refused,
[format!("rule 'net': dest_globs entry {dest:?} is not an IP CIDR or a port, so the kernel rule cannot match it")],
"{dest}"
);
}
for dest in ["10.0.0.0/8", "10.0.0.1/32", "443", "2001:db8::/32"] {
assert_eq!(
unenforceable_rules(&config(&rule(dest)), KILL_SWITCH_ON),
Vec::<String>::new(),
"{dest}"
);
}
}
#[test]
fn a_rule_id_cannot_add_a_line_to_a_refusal() {
let refused = unenforceable_rules(
&config(" - id: \"a\\nrule 'x': ok\"\n type: proc_exec\n match:\n path_globs: ['**/sh']\n action: deny\n"),
KILL_SWITCH_ON,
);
assert_eq!(refused.len(), 1);
assert!(!refused[0].contains('\n'), "{:?}", refused[0]);
assert!(
refused[0].starts_with("rule 'a\\nrule \\'x\\': ok': proc_exec cannot be denied"),
"{:?}",
refused[0]
);
}
#[test]
fn every_refusal_is_one_line_with_no_terminal_control() {
let policy = read(concat!(
"runtime_monitor:\n enabled: true\n rules:\n",
" - id: bad-glob\n type: file_open\n match:\n",
" path_globs: [\"/etc/[\\e]52;c;cHduZWQ=\\a\\e[31m\\u009b31m\\u202e\\r\\nFATAL: forged\"]\n",
" action: deny\n",
));
assert_eq!(policy.refusals.len(), 1, "{:?}", policy.refusals);
let refusal = &policy.refusals[0];
assert!(
refusal.starts_with("rule 'bad-glob': path_globs do not compile"),
"the control did not reach the glob error: {refusal:?}"
);
for c in refusal.chars() {
assert!(
!c.is_control()
&& !matches!(c, '\u{202a}'..='\u{202e}' | '\u{2066}'..='\u{2069}' | '\u{2028}' | '\u{2029}'),
"{:?} reaches the terminal in {refusal:?}",
c
);
}
}
#[test]
fn a_disabled_monitor_reads_nothing_else() {
let policy = read(
"runtime_monitor:\n enabled: false\n scope: {}\nkill_switch:\n kill_children: true\n",
);
assert!(policy.disabled);
assert!(policy.refusals.is_empty(), "{:?}", policy.refusals);
let absent = read("kill_switch:\n kill_children: true\n");
assert!(!absent.disabled && absent.rules.is_none());
assert_eq!(
absent.refusals,
["\"kill_switch.kill_children\": assay monitor does not act on this key; remove it"]
);
}
}