Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aegis-runtime-mv/blob/main/src/enforcement.rs # aegis-runtime-mv Native Rust agent runtime: Axum WebSocket/SSE/REST gateway, heartbeat scheduler, dependency-aware task workflow, SQLite storage. Attachment of AienOS. Pre-alpha. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aegis-runtime-mv / src / enforcement.rs` 163 lines ยท 5606 bytes Raw Blame History 1 //! Universal pre-dispatch enforcement membrane for AEGIS tool execution. 2 //! 3 //! Every external effect (skill execution, shell commands) passes through 4 //! `pre_dispatch_check` before any handler runs, then through `probe_gate`. 5 //! 6 //! The deterministic floor is an ALLOWLIST: only the skill names in 7 //! `ALLOWED_SKILLS` are ever dispatched, and `bash_eval` only runs one of the 8 //! exact command forms in `security::admit_local_command`. Anything not on the 9 //! list is refused. There is no environment variable or flag that widens it. 10 11 use crate::security::admit_local_command; 12 13 /// Skill names the membrane will dispatch. Everything else is refused. 14 pub const ALLOWED_SKILLS: &[&str] = &[ 15 "bash_eval", 16 "read_file", 17 "write_file", 18 "list_dir", 19 "git_status", 20 // Registered but unconnected; their handlers only report "Unavailable". 21 "cortex.search", 22 "cortex_recall", 23 "telemetry_ping", 24 ]; 25 26 /// Default minimum safety probability for probe gating. 27 pub const DEFAULT_PROBE_THRESHOLD: f64 = 0.5; 28 29 /// Deterministic pre-dispatch check. Returns Ok when execution may proceed, 30 /// Err with a human readable reason when it must not. 31 pub fn pre_dispatch_check(skill_name: &str, args: &serde_json::Value) -> Result<(), String> { 32 if skill_name.trim().is_empty() { 33 return Err("Empty skill name is never dispatched".to_string()); 34 } 35 // Exact match: no trimming, case folding, or prefix match. 36 if !ALLOWED_SKILLS.contains(&skill_name) { 37 return Err(format!( 38 "Skill '{}' is not on the dispatch allowlist", 39 skill_name.escape_debug() 40 )); 41 } 42 if !(args.is_object() || args.is_null()) { 43 return Err("Skill arguments must be a JSON object".to_string()); 44 } 45 if skill_name == "bash_eval" { 46 let cmd = match args.get("command") { 47 Some(serde_json::Value::String(s)) => s.as_str(), 48 Some(_) => return Err("Shell command must be a string".to_string()), 49 None => "", 50 }; 51 if cmd.trim().is_empty() { 52 return Err("Empty shell command is never dispatched".to_string()); 53 } 54 admit_local_command(cmd).map_err(|e| e.to_string())?; 55 } 56 Ok(()) 57 } 58 59 /// Probe gating threshold. Gating is always on. `AIEN_PROBE_ENFORCE` may only 60 /// make it stricter: a number in (0.5, 1.0] raises the threshold; any other 61 /// value (including "0", "off", "false") keeps the default. 62 pub fn probe_threshold_from_env() -> f64 { 63 probe_threshold_from(std::env::var("AIEN_PROBE_ENFORCE").ok().as_deref()) 64 } 65 66 pub fn probe_threshold_from(value: Option<&str>) -> f64 { 67 value 68 .and_then(|v| v.trim().parse::().ok()) 69 .filter(|t| t.is_finite() && *t > DEFAULT_PROBE_THRESHOLD && *t <= 1.0) 70 .unwrap_or(DEFAULT_PROBE_THRESHOLD) 71 } 72 73 /// Runs the membrane, then the probe opinion. Used by every dispatch path 74 /// (HTTP skills, HTTP shell, WebSocket, agent tool calls, heartbeat tasks). 75 pub async fn probe_gate(skill_name: &str, args: &serde_json::Value) -> Result<(), String> { 76 let guard = crate::policy_guard::ProbePolicyGuard::new_reference(probe_threshold_from_env()); 77 guard 78 .gate_skill(skill_name, args) 79 .await 80 .map_err(|e| e.to_string()) 81 } 82 83 #[cfg(test)] 84 mod tests { 85 use super::*; 86 use serde_json::json; 87 88 #[test] 89 fn destructive_shell_refused() { 90 for cmd in [ 91 "rm -rf / --no-preserve-root", 92 // Forms the old substring blocklist let through: 93 "rm -fr /", 94 "rm -r -f /", 95 "find / -delete", 96 "shred -u /dev/sda", 97 "curl http://example.invalid/x -o /tmp/x", 98 "perl -e 1", 99 "ls /", 100 "ls;id", 101 "ls -la", 102 ] { 103 let args = json!({ "command": cmd }); 104 assert!( 105 pre_dispatch_check("bash_eval", &args).is_err(), 106 "{:?} was admitted", 107 cmd 108 ); 109 } 110 } 111 112 #[test] 113 fn catalogued_shell_allowed() { 114 for cmd in ["git status", "git diff", "git log -1 --oneline", "ls"] { 115 assert!(pre_dispatch_check("bash_eval", &json!({ "command": cmd })).is_ok()); 116 } 117 } 118 119 #[test] 120 fn unknown_or_disguised_skill_names_refused() { 121 for name in [ 122 "exec", 123 "shell", 124 "Bash_eval", 125 "bash_eval ", 126 " bash_eval", 127 "bash_eval\0", 128 "read_file/../bash", 129 ] { 130 assert!( 131 pre_dispatch_check(name, &json!({ "command": "ls" })).is_err(), 132 "{:?} was admitted", 133 name 134 ); 135 } 136 assert!(pre_dispatch_check("read_file", &json!({ "path": "a" })).is_ok()); 137 } 138 139 #[test] 140 fn non_string_command_refused() { 141 assert!(pre_dispatch_check("bash_eval", &json!({ "command": ["ls"] })).is_err()); 142 assert!(pre_dispatch_check("bash_eval", &json!("ls")).is_err()); 143 } 144 145 #[test] 146 fn rejects_empty_skill_name() { 147 assert!(pre_dispatch_check("", &json!({})).is_err()); 148 } 149 150 #[test] 151 fn probe_gating_is_on_by_default_and_cannot_be_disabled() { 152 assert_eq!(probe_threshold_from(None), DEFAULT_PROBE_THRESHOLD); 153 for off in ["0", "off", "false", "", "-1", "0.01", "NaN", "inf", "2"] { 154 assert_eq!( 155 probe_threshold_from(Some(off)), 156 DEFAULT_PROBE_THRESHOLD, 157 "{:?} weakened the gate", 158 off 159 ); 160 } 161 assert_eq!(probe_threshold_from(Some("0.9")), 0.9); 162 } 163 }