spark-rsi-mv
Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger
Brought by drakestapleton
spark-rsi-mv / src / isolation / root_of_trust.rs
| 1 | use std::path::Path; |
| 2 | |
| 3 | pub struct RootOfTrust; |
| 4 | |
| 5 | impl RootOfTrust { |
| 6 | pub const PROTECTED_PATHS: &'static [&'static str] = &[ |
| 7 | "Cargo.toml", |
| 8 | "Cargo.lock", |
| 9 | ".cargo", |
| 10 | "src/isolation", |
| 11 | "src/evaluator", |
| 12 | "src/ledger", |
| 13 | "src/governance", |
| 14 | "src/supervisor", |
| 15 | "src/daemon.rs", |
| 16 | "tests", |
| 17 | ".github", |
| 18 | "CONSTITUTION.md", |
| 19 | "LICENSE", |
| 20 | "agent.json", |
| 21 | ".rsi/ledger.db", |
| 22 | ".rsi/active", |
| 23 | ".rsi/holdouts", |
| 24 | ]; |
| 25 | |
| 26 | /// Lexical normalization: drops leading `/`, `.` and empty components and |
| 27 | /// resolves `..`. A path that climbs above the repository root, or holds a |
| 28 | /// backslash or NUL, comes back as `..` so callers fail closed. |
| 29 | pub fn normalize_path(path: &str) -> String { |
| 30 | match Self::normalize_strict(path) { |
| 31 | Ok(p) => p, |
| 32 | Err(_) => "..".to_string(), |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | /// Strict normalization. Refuses backslashes, NUL, and any `..` that climbs |
| 37 | /// above the repository root. |
| 38 | pub fn normalize_strict(path: &str) -> Result<String, String> { |
| 39 | if path.contains('\\') || path.contains('\0') { |
| 40 | return Err(format!("path {:?} contains a backslash or NUL", path)); |
| 41 | } |
| 42 | let mut stack: Vec<&str> = Vec::new(); |
| 43 | for comp in path.split('/') { |
| 44 | match comp { |
| 45 | "" | "." => {} |
| 46 | ".." => { |
| 47 | if stack.pop().is_none() { |
| 48 | return Err(format!("path {:?} climbs above the repository root", path)); |
| 49 | } |
| 50 | } |
| 51 | c => stack.push(c), |
| 52 | } |
| 53 | } |
| 54 | Ok(stack.join("/")) |
| 55 | } |
| 56 | |
| 57 | pub fn is_protected_path(path: &str) -> bool { |
| 58 | let normalized = match Self::normalize_strict(path) { |
| 59 | Ok(p) => p, |
| 60 | // Anything that cannot be normalized is treated as protected. |
| 61 | Err(_) => return true, |
| 62 | }; |
| 63 | if normalized.is_empty() { |
| 64 | return true; |
| 65 | } |
| 66 | let path_obj = Path::new(&normalized); |
| 67 | |
| 68 | for protected in Self::PROTECTED_PATHS { |
| 69 | let prot_obj = Path::new(protected); |
| 70 | if path_obj == prot_obj || path_obj.starts_with(prot_obj) { |
| 71 | return true; |
| 72 | } |
| 73 | } |
| 74 | false |
| 75 | } |
| 76 | |
| 77 | pub fn assert_patch_permitted(target_file: &str, tier: u8) -> Result<(), String> { |
| 78 | let normalized = Self::normalize_strict(target_file) |
| 79 | .map_err(|e| format!("SECURITY VIOLATION: {}", e))?; |
| 80 | if Self::is_protected_path(&normalized) && tier < 2 { |
| 81 | return Err(format!( |
| 82 | "SECURITY VIOLATION: Target path '{}' is a protected root-of-trust file. Required tier: >= 2, candidate tier: {}", |
| 83 | normalized, tier |
| 84 | )); |
| 85 | } |
| 86 | Ok(()) |
| 87 | } |
| 88 | |
| 89 | pub fn validate_declared_files(declared_files: &[String], tier: u8) -> Result<(), Vec<String>> { |
| 90 | let mut violations = Vec::new(); |
| 91 | for file in declared_files { |
| 92 | if let Err(msg) = Self::assert_patch_permitted(file, tier) { |
| 93 | violations.push(msg); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | if violations.is_empty() { |
| 98 | Ok(()) |
| 99 | } else { |
| 100 | Err(violations) |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | #[cfg(test)] |
| 106 | mod tests { |
| 107 | use super::*; |
| 108 | |
| 109 | #[test] |
| 110 | fn test_protected_paths_detected() { |
| 111 | assert!(RootOfTrust::is_protected_path("Cargo.toml")); |
| 112 | assert!(RootOfTrust::is_protected_path("./Cargo.toml")); |
| 113 | assert!(RootOfTrust::is_protected_path("src/isolation/container.rs")); |
| 114 | assert!(RootOfTrust::is_protected_path("src/evaluator/stats.rs")); |
| 115 | assert!(RootOfTrust::is_protected_path(".github/workflows/ci.yml")); |
| 116 | assert!(RootOfTrust::is_protected_path("CONSTITUTION.md")); |
| 117 | assert!(RootOfTrust::is_protected_path(".rsi/ledger.db")); |
| 118 | } |
| 119 | |
| 120 | #[test] |
| 121 | fn test_unprotected_target_paths_allowed() { |
| 122 | assert!(!RootOfTrust::is_protected_path("src/observe.rs")); |
| 123 | assert!(!RootOfTrust::is_protected_path("src/propose.rs")); |
| 124 | assert!(!RootOfTrust::is_protected_path("src/models.rs")); |
| 125 | assert!(!RootOfTrust::is_protected_path("docs/PHILOSOPHY.md")); |
| 126 | assert!(!RootOfTrust::is_protected_path("README.md")); |
| 127 | } |
| 128 | |
| 129 | #[test] |
| 130 | fn test_tier_enforcement() { |
| 131 | assert!(RootOfTrust::assert_patch_permitted("src/observe.rs", 0).is_ok()); |
| 132 | assert!(RootOfTrust::assert_patch_permitted("src/isolation/root_of_trust.rs", 0).is_err()); |
| 133 | assert!(RootOfTrust::assert_patch_permitted("src/isolation/root_of_trust.rs", 1).is_err()); |
| 134 | assert!(RootOfTrust::assert_patch_permitted("src/isolation/root_of_trust.rs", 2).is_ok()); |
| 135 | } |
| 136 | |
| 137 | #[test] |
| 138 | fn test_dotdot_and_escape_forms_cannot_bypass_protection() { |
| 139 | // Before the fix these normalized to themselves and were not protected. |
| 140 | assert!(RootOfTrust::is_protected_path("src/../Cargo.toml")); |
| 141 | assert!(RootOfTrust::is_protected_path( |
| 142 | "docs/../.github/workflows/ci.yml" |
| 143 | )); |
| 144 | assert!(RootOfTrust::is_protected_path( |
| 145 | "src//isolation/container.rs" |
| 146 | )); |
| 147 | assert!(RootOfTrust::is_protected_path("../outside.rs")); |
| 148 | assert!(RootOfTrust::is_protected_path( |
| 149 | "src\\isolation\\container.rs" |
| 150 | )); |
| 151 | assert!(RootOfTrust::assert_patch_permitted("src/../Cargo.toml", 0).is_err()); |
| 152 | // Climbing out of the repository is refused even at tier 2. |
| 153 | assert!(RootOfTrust::assert_patch_permitted("../../etc/passwd", 2).is_err()); |
| 154 | assert!(RootOfTrust::assert_patch_permitted("docs/../README.md", 0).is_ok()); |
| 155 | } |
| 156 | |
| 157 | #[test] |
| 158 | fn test_validate_declared_files_batch() { |
| 159 | let valid = vec!["src/observe.rs".to_string(), "README.md".to_string()]; |
| 160 | assert!(RootOfTrust::validate_declared_files(&valid, 0).is_ok()); |
| 161 | |
| 162 | let invalid = vec!["src/observe.rs".to_string(), "Cargo.toml".to_string()]; |
| 163 | let err = RootOfTrust::validate_declared_files(&invalid, 0).unwrap_err(); |
| 164 | assert_eq!(err.len(), 1); |
| 165 | } |
| 166 | } |