Onset: version control where nobody waits and nothing gets lost. The on command, its core and onset-server.
| 6 |
| 7 | use regex_lite::Regex; |
| 8 | use serde::Serialize; |
| 9 |
| 10 | use crate::{Id, Result, Source, chunk, diff, tree}; |
| 11 |
| 12 | #[derive(Debug, Clone, Serialize, PartialEq)] |
| 13 | pub struct Hit { |
| 14 | pub path: String, |
| 15 | pub line: usize, |
| 16 | pub kind: &'static str, |
| 17 | } |
| 18 |
| 19 | /// High-confidence formats only: a false alarm on every landing would teach |
| 20 | /// people to pass --allow-secrets. |
| 21 | fn patterns() -> &'static [(&'static str, Regex)] { |
| 22 | static P: OnceLock<Vec<(&'static str, Regex)>> = OnceLock::new(); |
| 23 | P.get_or_init(|| { |
| 24 | [ |
| 25 | ("AWS access key", r"\b(AKIA|ASIA)[0-9A-Z]{16}\b"), |
| 26 | ("GitHub token", r"\b(ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9]{36}\b"), |
| 27 | ("GitHub token", r"\bgithub_pat_[A-Za-z0-9_]{60,}\b"), |
| 28 | ("Stripe live key", r"\b(sk|rk)_live_[A-Za-z0-9]{20,}\b"), |
| 29 | ("Slack token", r"\bxox[abprs]-[A-Za-z0-9-]{20,}\b"), |
| 30 | ("Google API key", r"\bAIza[0-9A-Za-z_-]{35}\b"), |
| 31 | ("private key", r"-----BEGIN (RSA |EC |DSA |OPENSSH |PGP |ENCRYPTED )?PRIVATE KEY( BLOCK)?-----"), |
| 32 | ("OpenAI key", r"\bsk-(proj-)?[A-Za-z0-9_-]{20,}T3BlbkFJ[A-Za-z0-9_-]{20,}\b"), |
| 33 | ("OpenAI key", r"\bsk-proj-[A-Za-z0-9_-]{60,}\b"), |
| 34 | ("Anthropic key", r"\bsk-ant-(api|admin)[0-9]{2}-[A-Za-z0-9_-]{60,}\b"), |
| 35 | ("Upstream token", r"\b(upt|ucs)_[A-Za-z0-9]{24,}\b"), |
| 36 | ("npm token", r"\bnpm_[A-Za-z0-9]{36}\b"), |
| 37 | ("SendGrid key", r"\bSG\.[A-Za-z0-9_-]{22}\.[A-Za-z0-9_-]{43}\b"), |
| 38 | ("Twilio key", r"\bSK[0-9a-f]{32}\b"), |
| 39 | ] |
| 40 | .into_iter() |
| 41 | .map(|(k, p)| (k, Regex::new(p).expect("secret pattern"))) |
| 42 | .collect() |
| 43 | }) |
| 44 | } |
| 45 |
| 46 | /// Secrets in lines `new` adds over `old`. |
| 47 | pub fn scan(src: &dyn Source, old: &Id, new: &Id) -> Result<Vec<Hit>> { |
| 48 | let mut hits = Vec::new(); |
| 49 | for (path, change) in tree::diff_trees(src, old, new)? { |
| 50 | let (o, n) = match change { |
| 51 | tree::Change::Added(e) => (None, e), |
| 52 | tree::Change::Modified(o, n) => (Some(o), n), |
| 53 | tree::Change::Removed(_) => continue, |
| 54 | }; |
| 55 | let new_bytes = chunk::read_file(src, &n.id)?; |
| 56 | if diff::is_binary(&new_bytes) { |
| 57 | continue; |
| 58 | } |
| 59 | let old_bytes = match o { |
| 60 | Some(o) => chunk::read_file(src, &o.id)?, |
| 61 | None => Vec::new(), |
| 62 | }; |
| 63 | let lines = diff::lines(&new_bytes); |
| 64 | for h in diff::hunks(&old_bytes, &new_bytes) { |
| 65 | for i in h.after.clone() { |
| 66 | let text = String::from_utf8_lossy(lines[i as usize]); |
| 67 | if let Some((kind, _)) = patterns().iter().find(|(_, re)| re.is_match(&text)) { |
| 68 | hits.push(Hit { path: path.clone(), line: i as usize + 1, kind }); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | Ok(hits) |
| 74 | } |
| 75 |
| 76 | #[cfg(test)] |
| 77 | mod tests { |
| 78 | use super::*; |
| 79 |
| 80 | #[test] |
| 81 | fn finds_live_keys_only() { |
| 82 | let hit = |s: &str| patterns().iter().any(|(_, re)| re.is_match(s)); |
| 83 | assert!(hit(&format!("const k = \"sk_live_{}\";", "a".repeat(24)))); |
| 84 | assert!(hit("AKIAIOSFODNN7EXAMPLE")); |
| 85 | assert!(hit("-----BEGIN OPENSSH PRIVATE KEY-----")); |
| 86 | assert!(!hit("sk_test_abcdefabcdefabcdefabcdef")); |
| 87 | assert!(!hit("const apiKey = process.env.STRIPE_KEY;")); |
| 88 | } |
| 89 | } |
| 90 |