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/open-humanity/blob/main/crates/beacon-core/src/firewall.rs # open-humanity Privacy-first peer assistance network for sovereign autonomous agents 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 `open-humanity / crates / beacon-core / src / firewall.rs` 210 lines ยท 7939 bytes Raw Blame History 1 //! Deterministic Outbound Secrets and Personal Data Firewall. 2 //! 3 //! Enforces zero leakage of credentials, hardware tokens, API keys, 4 //! or absolute user directory paths across open-humanity beacons. 5 6 use regex::Regex; 7 use std::sync::LazyLock; 8 use thiserror::Error; 9 10 #[derive(Error, Debug, PartialEq, Eq, Clone)] 11 pub enum FirewallViolation { 12 #[error("Detected AWS Access Key in beacon payload: {0}")] 13 AwsKey(String), 14 #[error("Detected GitHub Personal Access Token in beacon payload: {0}")] 15 GitHubToken(String), 16 #[error("Detected OpenAI API Key in beacon payload: {0}")] 17 OpenAiKey(String), 18 #[error("Detected Anthropic API Key in beacon payload: {0}")] 19 AnthropicKey(String), 20 #[error("Detected Google API Key in payload: {0}")] 21 GoogleApiKey(String), 22 #[error("Detected Stripe API Key in payload: {0}")] 23 StripeKey(String), 24 #[error("Detected HuggingFace Token in payload: {0}")] 25 HuggingFaceToken(String), 26 #[error("Detected Private Key block in beacon payload")] 27 PrivateKeyBlock, 28 #[error("Detected generic high-entropy secret token: {0}")] 29 GenericSecret(String), 30 #[error("Detected relative path traversal attempt: {0}")] 31 PathTraversal(String), 32 } 33 34 static AWS_KEY_REGEX: LazyLock = 35 LazyLock::new(|| Regex::new(r"AKIA[0-9A-Z]{16}").expect("valid regex")); 36 37 static GITHUB_TOKEN_REGEX: LazyLock = 38 LazyLock::new(|| Regex::new(r"(?:ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9_]{36}").expect("valid regex")); 39 40 static OPENAI_KEY_REGEX: LazyLock = 41 LazyLock::new(|| Regex::new(r"sk-(?:proj-)?[A-Za-z0-9_-]{20,}").expect("valid regex")); 42 43 static ANTHROPIC_KEY_REGEX: LazyLock = 44 LazyLock::new(|| Regex::new(r"sk-ant-[A-Za-z0-9_-]{20,}").expect("valid regex")); 45 46 static GOOGLE_KEY_REGEX: LazyLock = 47 LazyLock::new(|| Regex::new(r"AIza[0-9A-Za-z_-]{30,}").expect("valid regex")); 48 49 static STRIPE_KEY_REGEX: LazyLock = 50 LazyLock::new(|| Regex::new(r"(?:sk|rk)_(?:live|test)_[0-9a-zA-Z]{24,}").expect("valid regex")); 51 52 static HUGGINGFACE_TOKEN_REGEX: LazyLock = 53 LazyLock::new(|| Regex::new(r"hf_[a-zA-Z0-9]{34,}").expect("valid regex")); 54 55 static PRIVATE_KEY_REGEX: LazyLock = 56 LazyLock::new(|| Regex::new(r"-----BEGIN [A-Z0-9 ]*PRIVATE KEY-----").expect("valid regex")); 57 58 static GENERIC_SECRET_REGEX: LazyLock = LazyLock::new(|| { 59 Regex::new(r#"(?i)(?:api_key|apikey|secret_key|private_key|auth_token|bearer)\s*[=:]\s*['"][A-Za-z0-9_\-+=/]{16,}['"]"#) 60 .expect("valid regex") 61 }); 62 63 static USER_PATH_REGEX: LazyLock = 64 LazyLock::new(|| Regex::new(r"/(?:home|Users)/[A-Za-z0-9._-]+/").expect("valid regex")); 65 66 static WINDOWS_USER_PATH_REGEX: LazyLock = 67 LazyLock::new(|| Regex::new(r"(?i)[a-z]:\\Users\\[A-Za-z0-9._-]+\\").expect("valid regex")); 68 69 static WINDOWS_USER_PATH_SLASH_REGEX: LazyLock = 70 LazyLock::new(|| Regex::new(r"(?i)[a-z]:/Users/[A-Za-z0-9._-]+/").expect("valid regex")); 71 72 static PATH_TRAVERSAL_REGEX: LazyLock = 73 LazyLock::new(|| Regex::new(r"(?:\.\.[/\\])+").expect("valid regex")); 74 75 pub struct PersonalDataFirewall; 76 77 impl PersonalDataFirewall { 78 pub fn verify_clean(text: &str) -> Result<(), FirewallViolation> { 79 Self::scan_tokens(text)?; 80 81 let normalized = Self::normalize_obfuscation(text); 82 if normalized != text { 83 Self::scan_tokens(&normalized)?; 84 } 85 86 let leet_normalized = Self::normalize_leetspeak(&normalized); 87 if leet_normalized != normalized { 88 if let Some(m) = GENERIC_SECRET_REGEX.find(&leet_normalized) { 89 return Err(FirewallViolation::GenericSecret(m.as_str().to_string())); 90 } 91 } 92 93 if PATH_TRAVERSAL_REGEX.is_match(text) || PATH_TRAVERSAL_REGEX.is_match(&normalized) { 94 return Err(FirewallViolation::PathTraversal( 95 "Detected relative path traversal attempt".to_string(), 96 )); 97 } 98 99 Ok(()) 100 } 101 102 fn scan_tokens(text: &str) -> Result<(), FirewallViolation> { 103 if let Some(m) = AWS_KEY_REGEX.find(text) { 104 return Err(FirewallViolation::AwsKey(m.as_str().to_string())); 105 } 106 107 if let Some(m) = GITHUB_TOKEN_REGEX.find(text) { 108 return Err(FirewallViolation::GitHubToken(m.as_str().to_string())); 109 } 110 111 if let Some(m) = ANTHROPIC_KEY_REGEX.find(text) { 112 return Err(FirewallViolation::AnthropicKey(m.as_str().to_string())); 113 } 114 115 if let Some(m) = OPENAI_KEY_REGEX.find(text) { 116 return Err(FirewallViolation::OpenAiKey(m.as_str().to_string())); 117 } 118 119 if let Some(m) = GOOGLE_KEY_REGEX.find(text) { 120 return Err(FirewallViolation::GoogleApiKey(m.as_str().to_string())); 121 } 122 123 if let Some(m) = STRIPE_KEY_REGEX.find(text) { 124 return Err(FirewallViolation::StripeKey(m.as_str().to_string())); 125 } 126 127 if let Some(m) = HUGGINGFACE_TOKEN_REGEX.find(text) { 128 return Err(FirewallViolation::HuggingFaceToken(m.as_str().to_string())); 129 } 130 131 if PRIVATE_KEY_REGEX.is_match(text) { 132 return Err(FirewallViolation::PrivateKeyBlock); 133 } 134 135 if let Some(m) = GENERIC_SECRET_REGEX.find(text) { 136 return Err(FirewallViolation::GenericSecret(m.as_str().to_string())); 137 } 138 139 Ok(()) 140 } 141 142 pub fn redact_secrets(text: &str) -> String { 143 let mut s = text.to_string(); 144 s = PRIVATE_KEY_REGEX.replace_all(&s, "[REDACTED_PRIVATE_KEY]").to_string(); 145 s = AWS_KEY_REGEX.replace_all(&s, "[REDACTED_AWS_KEY]").to_string(); 146 s = GITHUB_TOKEN_REGEX.replace_all(&s, "[REDACTED_GITHUB_TOKEN]").to_string(); 147 s = ANTHROPIC_KEY_REGEX.replace_all(&s, "[REDACTED_ANTHROPIC_KEY]").to_string(); 148 s = OPENAI_KEY_REGEX.replace_all(&s, "[REDACTED_OPENAI_KEY]").to_string(); 149 s = GOOGLE_KEY_REGEX.replace_all(&s, "[REDACTED_GOOGLE_KEY]").to_string(); 150 s = STRIPE_KEY_REGEX.replace_all(&s, "[REDACTED_STRIPE_KEY]").to_string(); 151 s = HUGGINGFACE_TOKEN_REGEX.replace_all(&s, "[REDACTED_HF_TOKEN]").to_string(); 152 s = GENERIC_SECRET_REGEX.replace_all(&s, "[REDACTED_GENERIC_SECRET]").to_string(); 153 s 154 } 155 156 pub fn sanitize_paths(text: &str) -> String { 157 let s = WINDOWS_USER_PATH_REGEX.replace_all(text, "~\\"); 158 let s = WINDOWS_USER_PATH_SLASH_REGEX.replace_all(&s, "~/"); 159 let s = USER_PATH_REGEX.replace_all(&s, "~/"); 160 let s = PATH_TRAVERSAL_REGEX.replace_all(&s, "./"); 161 s.to_string() 162 } 163 164 pub fn normalize_obfuscation(text: &str) -> String { 165 let stripped: String = text 166 .chars() 167 .filter(|c| { 168 !matches!( 169 *c, 170 '\u{200B}' | '\u{200C}' | '\u{200D}' | '\u{FEFF}' | '\u{2060}' | '\u{00AD}' 171 ) 172 }) 173 .collect(); 174 175 Self::decode_percent(&stripped) 176 } 177 178 pub fn decode_percent(input: &str) -> String { 179 let bytes = input.as_bytes(); 180 let mut out = Vec::with_capacity(bytes.len()); 181 let mut i = 0; 182 while i < bytes.len() { 183 if bytes[i] == b'%' && i + 2 < bytes.len() { 184 if let Ok(val) = u8::from_str_radix(&input[i + 1..i + 3], 16) { 185 out.push(val); 186 i += 3; 187 continue; 188 } 189 } 190 out.push(bytes[i]); 191 i += 1; 192 } 193 String::from_utf8(out).unwrap_or_else(|_| input.to_string()) 194 } 195 196 pub fn normalize_leetspeak(input: &str) -> String { 197 input 198 .chars() 199 .map(|c| match c { 200 '0' => 'o', 201 '1' | '!' => 'i', 202 '3' => 'e', 203 '4' | '@' => 'a', 204 '5' | '$' => 's', 205 '7' => 't', 206 other => other, 207 }) 208 .collect() 209 } 210 }