spark-inquisitor
Ultra-fast compiled native Rust gatekeeper and automated code reviewer
Brought by drakestapleton
spark-inquisitor / src / audit.rs
| 1 | use crate::constitution::{ConstitutionalChecker, ViolationKind}; |
| 2 | |
| 3 | #[derive(Debug, Clone)] |
| 4 | pub struct AuditReport { |
| 5 | pub clean: bool, |
| 6 | pub violations: Vec<String>, |
| 7 | pub telemetry_detected: bool, |
| 8 | pub unslop_violations: Vec<String>, |
| 9 | pub secret_violations: Vec<String>, |
| 10 | } |
| 11 | |
| 12 | impl AuditReport { |
| 13 | pub fn generate_markdown_report(&self) -> String { |
| 14 | if self.clean { |
| 15 | return "#### ✅ Constitutional Diff Audit: PASS\n- **Telemetry Check**: Clean (zero tracking or surveillance hooks detected).\n- **Sovereign Voice & Formatting Check**: Clean (zero em/en dashes, zero banned buzzwords).\n- **Secrets & Credentials Check**: Clean (zero plaintext secrets, hardware vault only).\n- **Status**: Constitutional invariants verified.".to_string(); |
| 16 | } |
| 17 | |
| 18 | let mut out = String::from("#### ❌ Constitutional Diff Audit: FAILED\n\n"); |
| 19 | if !self.violations.is_empty() { |
| 20 | out.push_str("**Telemetry, Surveillance, and Environment Violations:**\n"); |
| 21 | for v in &self.violations { |
| 22 | out.push_str(&format!("* {}\n", v)); |
| 23 | } |
| 24 | out.push('\n'); |
| 25 | } |
| 26 | if !self.secret_violations.is_empty() { |
| 27 | out.push_str("**Plaintext Secrets and Credential Violations:**\n"); |
| 28 | for s in &self.secret_violations { |
| 29 | out.push_str(&format!("* {}\n", s)); |
| 30 | } |
| 31 | out.push('\n'); |
| 32 | } |
| 33 | if !self.unslop_violations.is_empty() { |
| 34 | out.push_str("**Sovereign Voice and Unslop Invariant Violations:**\n"); |
| 35 | for u in &self.unslop_violations { |
| 36 | out.push_str(&format!("* {}\n", u)); |
| 37 | } |
| 38 | out.push('\n'); |
| 39 | } |
| 40 | out.push_str("**Action Required:** Remove all detected tracking hooks, secrets, and forbidden tokens before review can proceed."); |
| 41 | out |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | pub struct DiffAuditor; |
| 46 | |
| 47 | impl DiffAuditor { |
| 48 | pub fn audit_diff(text: &str) -> AuditReport { |
| 49 | Self::audit_text(text) |
| 50 | } |
| 51 | |
| 52 | pub fn audit_text(text: &str) -> AuditReport { |
| 53 | let mut violations = Vec::new(); |
| 54 | let mut telemetry_detected = false; |
| 55 | let mut unslop_violations = Vec::new(); |
| 56 | let mut secret_violations = Vec::new(); |
| 57 | |
| 58 | let telemetry_patterns = [ |
| 59 | "google-analytics", |
| 60 | "segment.io", |
| 61 | "mixpanel", |
| 62 | "sentry.io", |
| 63 | "datadog", |
| 64 | "tracking_id", |
| 65 | "analytics.js", |
| 66 | "amplitude.com", |
| 67 | "hotjar", |
| 68 | "clarity.ms", |
| 69 | "send_telemetry", |
| 70 | "report_telemetry", |
| 71 | "collect_telemetry", |
| 72 | ]; |
| 73 | |
| 74 | let mut current_file = String::new(); |
| 75 | let mut in_binary_patch = false; |
| 76 | |
| 77 | for line in text.lines() { |
| 78 | if line.starts_with("diff --git ") { |
| 79 | in_binary_patch = false; |
| 80 | current_file = Self::extract_target_file(line).unwrap_or_default(); |
| 81 | if ConstitutionalChecker::is_env_file(¤t_file) { |
| 82 | violations.push(format!( |
| 83 | "Forbidden plaintext .env secret file detected in diff header: {}", |
| 84 | current_file |
| 85 | )); |
| 86 | } |
| 87 | continue; |
| 88 | } |
| 89 | |
| 90 | if line.starts_with("+++ ") { |
| 91 | if let Some(target) = Self::extract_plus_file(line) { |
| 92 | current_file = target; |
| 93 | if ConstitutionalChecker::is_env_file(¤t_file) { |
| 94 | violations.push(format!( |
| 95 | "Forbidden plaintext .env secret file added in diff: {}", |
| 96 | current_file |
| 97 | )); |
| 98 | } |
| 99 | } |
| 100 | continue; |
| 101 | } |
| 102 | |
| 103 | if line.starts_with("Binary files ") { |
| 104 | if let Some(target) = Self::extract_binary_target(line) { |
| 105 | if ConstitutionalChecker::is_env_file(&target) { |
| 106 | violations.push(format!( |
| 107 | "Forbidden plaintext .env secret file in binary diff: {}", |
| 108 | target |
| 109 | )); |
| 110 | } |
| 111 | } |
| 112 | continue; |
| 113 | } |
| 114 | |
| 115 | if line.starts_with("GIT binary patch") { |
| 116 | in_binary_patch = true; |
| 117 | continue; |
| 118 | } |
| 119 | |
| 120 | if in_binary_patch { |
| 121 | continue; |
| 122 | } |
| 123 | |
| 124 | // Exclude inquisitor internal pattern definitions from false positives |
| 125 | if current_file.contains("spark-inquisitor") || current_file.contains("constitution.rs") |
| 126 | { |
| 127 | continue; |
| 128 | } |
| 129 | |
| 130 | let lower = line.to_lowercase(); |
| 131 | |
| 132 | // Telemetry pattern scanning |
| 133 | for pattern in &telemetry_patterns { |
| 134 | if lower.contains(pattern) && (line.starts_with('+') && !line.starts_with("+++")) { |
| 135 | violations.push(format!( |
| 136 | "Telemetry pattern '{}' detected in added line: {}", |
| 137 | pattern, |
| 138 | line.trim() |
| 139 | )); |
| 140 | telemetry_detected = true; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // In non-markdown source code, also flag raw telemetry calls |
| 145 | if !current_file.ends_with(".md") |
| 146 | && line.starts_with('+') |
| 147 | && !line.starts_with("+++") |
| 148 | && lower.contains("telemetry") |
| 149 | && !lower.contains("gpu-telemetry") |
| 150 | && !lower.contains("zero telemetry") |
| 151 | && !lower.contains("zero-telemetry") |
| 152 | && !lower.contains("no-telemetry") |
| 153 | && !lower.contains("no telemetry") |
| 154 | && !lower.contains("anti-telemetry") |
| 155 | && !lower.contains("block telemetry") |
| 156 | && !lower.contains("telemetryforbidden") |
| 157 | && !lower.contains("networkpurpose::telemetry") |
| 158 | && !lower.contains("forbid") |
| 159 | && !lower.contains("reject") |
| 160 | { |
| 161 | violations.push(format!( |
| 162 | "Telemetry indicator detected in source line: {}", |
| 163 | line.trim() |
| 164 | )); |
| 165 | telemetry_detected = true; |
| 166 | } |
| 167 | |
| 168 | // Constitutional unslop and secret invariant scanning on added lines |
| 169 | if line.starts_with('+') && !line.starts_with("+++") { |
| 170 | let const_violations = ConstitutionalChecker::check_line(line, ¤t_file); |
| 171 | for cv in const_violations { |
| 172 | match cv { |
| 173 | ViolationKind::Dash(msg) => unslop_violations.push(msg), |
| 174 | ViolationKind::Buzzword(msg) => unslop_violations.push(msg), |
| 175 | ViolationKind::AntithesisTrope(msg) => unslop_violations.push(msg), |
| 176 | ViolationKind::Secret(msg) => secret_violations.push(msg), |
| 177 | ViolationKind::EnvFile(msg) => violations.push(msg), |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | let clean = |
| 184 | violations.is_empty() && unslop_violations.is_empty() && secret_violations.is_empty(); |
| 185 | |
| 186 | AuditReport { |
| 187 | clean, |
| 188 | violations, |
| 189 | telemetry_detected, |
| 190 | unslop_violations, |
| 191 | secret_violations, |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | fn extract_target_file(line: &str) -> Option<String> { |
| 196 | let remainder = line.strip_prefix("diff --git ")?.trim(); |
| 197 | if remainder.contains('"') { |
| 198 | let parts: Vec<&str> = remainder.split("\" \"").collect(); |
| 199 | if parts.len() == 2 { |
| 200 | let target = parts[1].trim_matches('"'); |
| 201 | let stripped = target.strip_prefix("b/").unwrap_or(target); |
| 202 | return Some(stripped.to_string()); |
| 203 | } |
| 204 | } |
| 205 | let tokens: Vec<&str> = remainder.split_whitespace().collect(); |
| 206 | if tokens.len() >= 2 { |
| 207 | let target = tokens[1]; |
| 208 | let stripped = target.strip_prefix("b/").unwrap_or(target); |
| 209 | return Some(stripped.to_string()); |
| 210 | } |
| 211 | None |
| 212 | } |
| 213 | |
| 214 | fn extract_plus_file(line: &str) -> Option<String> { |
| 215 | let path = line.strip_prefix("+++ ")?.trim(); |
| 216 | if path == "/dev/null" { |
| 217 | return None; |
| 218 | } |
| 219 | let clean = path.trim_matches('"'); |
| 220 | let stripped = clean.strip_prefix("b/").unwrap_or(clean); |
| 221 | Some(stripped.to_string()) |
| 222 | } |
| 223 | |
| 224 | fn extract_binary_target(line: &str) -> Option<String> { |
| 225 | // e.g. "Binary files /dev/null and b/.env differ" |
| 226 | let tokens: Vec<&str> = line.split_whitespace().collect(); |
| 227 | if tokens.len() >= 5 { |
| 228 | let target = tokens[4].trim_matches('"'); |
| 229 | let stripped = target.strip_prefix("b/").unwrap_or(target); |
| 230 | return Some(stripped.to_string()); |
| 231 | } |
| 232 | None |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | #[cfg(test)] |
| 237 | mod tests { |
| 238 | use super::*; |
| 239 | |
| 240 | #[test] |
| 241 | fn test_clean_diff() { |
| 242 | let diff = "diff --git a/src/lib.rs b/src/lib.rs\n+pub fn add(a: i32, b: i32) -> i32 {\n+ a + b\n+}"; |
| 243 | let report = DiffAuditor::audit_text(diff); |
| 244 | assert!(report.clean); |
| 245 | let md = report.generate_markdown_report(); |
| 246 | assert!(md.contains("PASS")); |
| 247 | assert!(!md.contains('\u{2014}')); |
| 248 | assert!(!md.contains('\u{2013}')); |
| 249 | } |
| 250 | |
| 251 | #[test] |
| 252 | fn test_telemetry_flagged() { |
| 253 | let diff = "diff --git a/src/net.rs b/src/net.rs\n+ let _ = send_telemetry(\"https://google-analytics.com/collect\");"; |
| 254 | let report = DiffAuditor::audit_text(diff); |
| 255 | assert!(!report.clean); |
| 256 | assert!(report.telemetry_detected); |
| 257 | let md = report.generate_markdown_report(); |
| 258 | assert!(md.contains("FAILED")); |
| 259 | assert!(md.contains("google-analytics")); |
| 260 | } |
| 261 | |
| 262 | #[test] |
| 263 | fn test_unslop_flagged() { |
| 264 | let diff = "diff --git a/src/lib.rs b/src/lib.rs\n+// This is a game-changer feature -- unleash seamless power."; |
| 265 | let report = DiffAuditor::audit_text(diff); |
| 266 | assert!(!report.clean); |
| 267 | assert!(!report.unslop_violations.is_empty()); |
| 268 | } |
| 269 | |
| 270 | #[test] |
| 271 | fn test_secret_flagged() { |
| 272 | let diff = |
| 273 | "diff --git a/src/config.rs b/src/config.rs\n+let key = \"AKIA1234567890ABCDEF\";"; |
| 274 | let report = DiffAuditor::audit_text(diff); |
| 275 | assert!(!report.clean); |
| 276 | assert!(!report.secret_violations.is_empty()); |
| 277 | } |
| 278 | |
| 279 | #[test] |
| 280 | fn test_env_file_flagged() { |
| 281 | let diff = "diff --git a/.env b/.env\n+SECRET_KEY=12345"; |
| 282 | let report = DiffAuditor::audit_text(diff); |
| 283 | assert!(!report.clean); |
| 284 | assert!(!report.violations.is_empty()); |
| 285 | } |
| 286 | } |