spark-rsi-mv
Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger
Brought by drakestapleton
spark-rsi-mv / src / evaluator / layers / security.rs
| 1 | use super::LayerResult; |
| 2 | use crate::isolation::RootOfTrust; |
| 3 | use serde::{Deserialize, Serialize}; |
| 4 | |
| 5 | #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] |
| 6 | pub struct SecurityEvaluation { |
| 7 | pub root_of_trust_compliant: bool, |
| 8 | pub zero_disk_secrets_clean: bool, |
| 9 | pub network_isolation_compliant: bool, |
| 10 | pub passed: bool, |
| 11 | pub violations: Vec<String>, |
| 12 | } |
| 13 | |
| 14 | impl SecurityEvaluation { |
| 15 | pub fn to_layer_result(&self) -> LayerResult { |
| 16 | let score = if self.passed { 1.0 } else { 0.0 }; |
| 17 | let summary = format!( |
| 18 | "Security: root_of_trust={}, zero_disk_secrets={}, network_isolation={}", |
| 19 | if self.root_of_trust_compliant { |
| 20 | "PASS" |
| 21 | } else { |
| 22 | "FAIL" |
| 23 | }, |
| 24 | if self.zero_disk_secrets_clean { |
| 25 | "PASS" |
| 26 | } else { |
| 27 | "FAIL" |
| 28 | }, |
| 29 | if self.network_isolation_compliant { |
| 30 | "PASS" |
| 31 | } else { |
| 32 | "FAIL" |
| 33 | } |
| 34 | ); |
| 35 | |
| 36 | LayerResult { |
| 37 | layer_name: "Security".to_string(), |
| 38 | is_hard_invariant: true, |
| 39 | passed: self.passed, |
| 40 | score, |
| 41 | summary, |
| 42 | violations: self.violations.clone(), |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | pub struct SecurityLayer; |
| 48 | |
| 49 | impl SecurityLayer { |
| 50 | fn secret_signatures() -> Vec<String> { |
| 51 | vec![ |
| 52 | "AKIA".to_string(), |
| 53 | ["gh", "p_"].join(""), |
| 54 | ["github", "_pat_"].join(""), |
| 55 | ["xox", "b-"].join(""), |
| 56 | ["xox", "p-"].join(""), |
| 57 | ["-----BEGIN ", "PRIVATE KEY-----"].join(""), |
| 58 | ["-----BEGIN ", "RSA PRIVATE KEY-----"].join(""), |
| 59 | ["-----BEGIN ", "OPENSSH PRIVATE KEY-----"].join(""), |
| 60 | "eyJhbGciOi".to_string(), |
| 61 | ] |
| 62 | } |
| 63 | |
| 64 | const NETWORK_RISK_PATTERNS: &'static [&'static str] = &[ |
| 65 | "std::net::TcpStream::connect", |
| 66 | "std::net::TcpListener::bind", |
| 67 | "reqwest::Client::new", |
| 68 | "curl http", |
| 69 | "wget http", |
| 70 | ]; |
| 71 | |
| 72 | pub fn evaluate_candidate( |
| 73 | declared_target_files: &[String], |
| 74 | patch_diff: &str, |
| 75 | candidate_tier: u8, |
| 76 | ) -> SecurityEvaluation { |
| 77 | let mut violations = Vec::new(); |
| 78 | |
| 79 | // 1. Validate root-of-trust protection |
| 80 | let root_check = |
| 81 | RootOfTrust::validate_declared_files(declared_target_files, candidate_tier); |
| 82 | let root_of_trust_compliant = match root_check { |
| 83 | Ok(_) => true, |
| 84 | Err(e) => { |
| 85 | violations.push(format!("Root-of-trust violation: {}", e.join(", "))); |
| 86 | false |
| 87 | } |
| 88 | }; |
| 89 | |
| 90 | // 2. Validate zero disk secrets |
| 91 | let patterns = Self::secret_signatures(); |
| 92 | let mut zero_disk_secrets_clean = true; |
| 93 | for pattern in &patterns { |
| 94 | if patch_diff.contains(pattern) { |
| 95 | violations.push(format!("Hardcoded secret signature detected: {}", pattern)); |
| 96 | zero_disk_secrets_clean = false; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // 3. Validate network isolation compliance |
| 101 | let mut network_isolation_compliant = true; |
| 102 | for &net_pat in Self::NETWORK_RISK_PATTERNS { |
| 103 | if patch_diff.contains(net_pat) { |
| 104 | violations.push(format!( |
| 105 | "Unauthorized network primitive detected: {}", |
| 106 | net_pat |
| 107 | )); |
| 108 | network_isolation_compliant = false; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | let passed = |
| 113 | root_of_trust_compliant && zero_disk_secrets_clean && network_isolation_compliant; |
| 114 | |
| 115 | SecurityEvaluation { |
| 116 | root_of_trust_compliant, |
| 117 | zero_disk_secrets_clean, |
| 118 | network_isolation_compliant, |
| 119 | passed, |
| 120 | violations, |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | #[cfg(test)] |
| 126 | mod tests { |
| 127 | use super::*; |
| 128 | |
| 129 | #[test] |
| 130 | fn test_security_clean_candidate() { |
| 131 | let files = vec!["src/observe.rs".to_string(), "src/propose.rs".to_string()]; |
| 132 | let diff = "+ let x = 42;\n+ let y = x + 1;"; |
| 133 | let eval = SecurityLayer::evaluate_candidate(&files, diff, 0); |
| 134 | |
| 135 | assert!(eval.passed); |
| 136 | assert!(eval.zero_disk_secrets_clean); |
| 137 | assert!(eval.root_of_trust_compliant); |
| 138 | assert!(eval.network_isolation_compliant); |
| 139 | assert_eq!(eval.violations.len(), 0); |
| 140 | |
| 141 | let lr = eval.to_layer_result(); |
| 142 | assert!(lr.passed); |
| 143 | assert!((lr.score - 1.0).abs() < 1e-6); |
| 144 | } |
| 145 | |
| 146 | #[test] |
| 147 | fn test_security_secret_leak() { |
| 148 | let files = vec!["src/observe.rs".to_string()]; |
| 149 | let fake_token = ["gh", "p_1234567890abcdef1234567890abcdef1234"].join(""); |
| 150 | let diff = format!("+ let key = \"{}\";", fake_token); |
| 151 | let eval = SecurityLayer::evaluate_candidate(&files, &diff, 0); |
| 152 | |
| 153 | assert!(!eval.passed); |
| 154 | assert!(!eval.zero_disk_secrets_clean); |
| 155 | assert!(eval.violations[0].contains("Hardcoded secret signature")); |
| 156 | } |
| 157 | |
| 158 | #[test] |
| 159 | fn test_security_root_of_trust_tampering() { |
| 160 | let files = vec!["Cargo.toml".to_string()]; |
| 161 | let diff = "+ serde = \"1.0\""; |
| 162 | let eval = SecurityLayer::evaluate_candidate(&files, diff, 0); |
| 163 | |
| 164 | assert!(!eval.passed); |
| 165 | assert!(!eval.root_of_trust_compliant); |
| 166 | assert!(eval.violations[0] |
| 167 | .contains("Target path 'Cargo.toml' is a protected root-of-trust file")); |
| 168 | } |
| 169 | |
| 170 | #[test] |
| 171 | fn test_security_network_escape_attempt() { |
| 172 | let files = vec!["src/propose.rs".to_string()]; |
| 173 | let diff = "+ let stream = std::net::TcpStream::connect(\"1.1.1.1:80\");"; |
| 174 | let eval = SecurityLayer::evaluate_candidate(&files, diff, 0); |
| 175 | |
| 176 | assert!(!eval.passed); |
| 177 | assert!(!eval.network_isolation_compliant); |
| 178 | assert!(eval.violations[0].contains("Unauthorized network primitive")); |
| 179 | } |
| 180 | } |