Skip to main content

lean_ctx/core/
secret_detection.rs

1use std::sync::OnceLock;
2
3use regex::Regex;
4
5use crate::core::config::{Config, SecretDetectionConfig};
6
7macro_rules! static_regex {
8    ($pattern:expr_2021) => {{
9        static RE: OnceLock<Regex> = OnceLock::new();
10        RE.get_or_init(|| Regex::new($pattern).expect(concat!("invalid regex: ", $pattern)))
11    }};
12}
13
14#[derive(Debug, Clone)]
15pub struct SecretMatch {
16    pub pattern_name: &'static str,
17    pub line_number: usize,
18    pub redacted_preview: String,
19}
20
21fn aws_key_re() -> &'static Regex {
22    static_regex!(r"AKIA[0-9A-Z]{16}")
23}
24
25fn private_key_re() -> &'static Regex {
26    static_regex!(r"-----BEGIN (?:RSA |EC |DSA |OPENSSH )?PRIVATE KEY-----")
27}
28
29fn github_token_re() -> &'static Regex {
30    static_regex!(r"gh[ps]_[A-Za-z0-9_]{36,}")
31}
32
33fn anthropic_key_re() -> &'static Regex {
34    static_regex!(r"sk-ant-[A-Za-z0-9_\-]{20,}")
35}
36
37fn openai_key_re() -> &'static Regex {
38    static_regex!(r"sk-[A-Za-z0-9]{20,}")
39}
40
41// #718 word boundaries for the generic key/value patterns: the keyword must
42// not be a camelCase subword (`superuserPassword`), while SNAKE_CASE env
43// names (`MY_TOKEN=…`) keep matching — `_` stays a permitted predecessor.
44// Group 1 = predecessor + key prefix (kept on redaction), group 2 = value
45// (guarded: identifier references and placeholders are benign).
46fn generic_api_key_re() -> &'static Regex {
47    static_regex!(
48        r#"(?im)((?:^|[^a-z0-9])(?:api[_-]?key|secret[_-]?key|token|password|passwd|access[_-]?token|client[_-]?secret)\s*[=:]\s*)(['"]?[a-zA-Z0-9_\-]{20,})"#
49    )
50}
51
52fn high_entropy_b64_re() -> &'static Regex {
53    static_regex!(
54        r#"(?im)((?:^|[^a-z0-9])(?:key|token|secret|password|credential|auth)\s*[=:]\s*)(['"]?[A-Za-z0-9+/=\-_]{40,})"#
55    )
56}
57
58fn gitlab_pat_re() -> &'static Regex {
59    static_regex!(r"glpat-[A-Za-z0-9_\-]{20,}")
60}
61
62fn jwt_re() -> &'static Regex {
63    static_regex!(r"eyJ[A-Za-z0-9_\-]{10,}\.[A-Za-z0-9_\-]{10,}")
64}
65
66fn slack_token_re() -> &'static Regex {
67    static_regex!(r"xox[bpas]-[0-9a-zA-Z\-]{10,}")
68}
69
70fn stripe_key_re() -> &'static Regex {
71    static_regex!(r"[sr]k_live_[0-9a-zA-Z]{10,}")
72}
73
74fn db_url_re() -> &'static Regex {
75    static_regex!(r"(?:postgres|mysql|mongodb|redis)://[^\s]+:[^\s]+@")
76}
77
78fn npm_token_re() -> &'static Regex {
79    static_regex!(r"npm_[A-Za-z0-9]{10,}")
80}
81
82fn github_fine_grained_re() -> &'static Regex {
83    static_regex!(r"github_pat_[A-Za-z0-9_]{20,}")
84}
85
86const BUILTIN_PATTERNS: &[(&str, fn() -> &'static Regex)] = &[
87    ("aws_key", aws_key_re),
88    ("private_key", private_key_re),
89    ("github_token", github_token_re),
90    ("github_fine_grained", github_fine_grained_re),
91    ("anthropic_key", anthropic_key_re),
92    ("openai_key", openai_key_re),
93    ("gitlab_pat", gitlab_pat_re),
94    ("jwt", jwt_re),
95    ("slack_token", slack_token_re),
96    ("stripe_key", stripe_key_re),
97    ("db_url", db_url_re),
98    ("npm_token", npm_token_re),
99];
100
101/// Key/value patterns with a kept prefix (group 1) and a guarded value
102/// (group 2): identifier references, placeholders and language literals are
103/// benign (#718) and neither reported nor redacted.
104const GUARDED_PATTERNS: &[(&str, fn() -> &'static Regex)] = &[
105    ("generic_api_key", generic_api_key_re),
106    ("high_entropy_secret", high_entropy_b64_re),
107];
108
109/// True when a guarded-pattern match should be skipped: the value is benign
110/// (#718) — shares the benign-value heuristics with `core::redaction` so the
111/// two layers cannot drift.
112fn guarded_match_is_benign(caps: &regex::Captures) -> bool {
113    caps.get(2)
114        .is_some_and(|value| crate::core::redaction::is_benign_secret_value(value.as_str()))
115}
116
117/// Compile the subtractive `exclude_patterns` from config (#718). Invalid
118/// regexes are skipped — a broken exclude must never disable detection.
119fn compile_excludes(patterns: &[String]) -> Vec<Regex> {
120    patterns.iter().filter_map(|p| Regex::new(p).ok()).collect()
121}
122
123fn excluded(excludes: &[Regex], matched: &str) -> bool {
124    excludes.iter().any(|ex| ex.is_match(matched))
125}
126
127fn make_redacted_preview(matched: &str) -> String {
128    let chars: Vec<char> = matched.chars().collect();
129    if chars.len() <= 6 {
130        return "***".to_string();
131    }
132    let prefix: String = chars[..4].iter().collect();
133    let suffix: String = chars[chars.len() - 2..].iter().collect();
134    format!("{prefix}***{suffix}")
135}
136
137fn collect_matches(
138    content: &str,
139    custom_patterns: &[String],
140    excludes: &[Regex],
141) -> Vec<SecretMatch> {
142    let mut matches = Vec::new();
143
144    let line_offsets: Vec<usize> = std::iter::once(0)
145        .chain(content.match_indices('\n').map(|(i, _)| i + 1))
146        .collect();
147
148    let offset_to_line = |byte_offset: usize| -> usize {
149        match line_offsets.binary_search(&byte_offset) {
150            Ok(i) => i + 1,
151            Err(i) => i,
152        }
153    };
154
155    for &(name, regex_fn) in BUILTIN_PATTERNS {
156        let re = regex_fn();
157        for m in re.find_iter(content) {
158            if excluded(excludes, m.as_str()) {
159                continue;
160            }
161            matches.push(SecretMatch {
162                pattern_name: name,
163                line_number: offset_to_line(m.start()),
164                redacted_preview: make_redacted_preview(m.as_str()),
165            });
166        }
167    }
168
169    for &(name, regex_fn) in GUARDED_PATTERNS {
170        let re = regex_fn();
171        for caps in re.captures_iter(content) {
172            let whole = caps.get(0).map_or("", |m| m.as_str());
173            if guarded_match_is_benign(&caps) || excluded(excludes, whole) {
174                continue;
175            }
176            let start = caps.get(0).map_or(0, |m| m.start());
177            matches.push(SecretMatch {
178                pattern_name: name,
179                line_number: offset_to_line(start),
180                redacted_preview: make_redacted_preview(whole),
181            });
182        }
183    }
184
185    for pattern_str in custom_patterns {
186        if let Ok(re) = Regex::new(pattern_str) {
187            for m in re.find_iter(content) {
188                if excluded(excludes, m.as_str()) {
189                    continue;
190                }
191                matches.push(SecretMatch {
192                    pattern_name: "custom_pattern",
193                    line_number: offset_to_line(m.start()),
194                    redacted_preview: make_redacted_preview(m.as_str()),
195                });
196            }
197        }
198    }
199
200    matches
201}
202
203pub fn detect_secrets(content: &str) -> Vec<SecretMatch> {
204    collect_matches(content, &[], &[])
205}
206
207pub fn detect_secrets_with_custom(content: &str, custom_patterns: &[String]) -> Vec<SecretMatch> {
208    collect_matches(content, custom_patterns, &[])
209}
210
211pub fn scan_and_redact(
212    content: &str,
213    config: &SecretDetectionConfig,
214) -> (String, Vec<SecretMatch>) {
215    if !config.enabled {
216        return (content.to_string(), Vec::new());
217    }
218
219    let excludes = compile_excludes(&config.exclude_patterns);
220    let matches = collect_matches(content, &config.custom_patterns, &excludes);
221
222    if matches.is_empty() || !config.redact {
223        return (content.to_string(), matches);
224    }
225
226    let mut redacted = content.to_string();
227    for &(name, regex_fn) in BUILTIN_PATTERNS {
228        let re = regex_fn();
229        redacted = re
230            .replace_all(&redacted, |caps: &regex::Captures| {
231                let whole = caps.get(0).map_or("", |m| m.as_str());
232                if excluded(&excludes, whole) {
233                    return whole.to_string();
234                }
235                format!("[REDACTED:{name}]")
236            })
237            .to_string();
238    }
239
240    for &(name, regex_fn) in GUARDED_PATTERNS {
241        let re = regex_fn();
242        redacted = re
243            .replace_all(&redacted, |caps: &regex::Captures| {
244                let whole = caps.get(0).map_or("", |m| m.as_str());
245                if guarded_match_is_benign(caps) || excluded(&excludes, whole) {
246                    return whole.to_string();
247                }
248                let prefix = caps.get(1).map_or("", |m| m.as_str());
249                format!("{prefix}[REDACTED:{name}]")
250            })
251            .to_string();
252    }
253
254    for pattern_str in &config.custom_patterns {
255        if let Ok(re) = Regex::new(pattern_str) {
256            redacted = re
257                .replace_all(&redacted, |caps: &regex::Captures| {
258                    let whole = caps.get(0).map_or("", |m| m.as_str());
259                    if excluded(&excludes, whole) {
260                        return whole.to_string();
261                    }
262                    "[REDACTED:custom_pattern]".to_string()
263                })
264                .to_string();
265        }
266    }
267
268    (redacted, matches)
269}
270
271pub fn scan_and_redact_from_config(content: &str) -> (String, Vec<SecretMatch>) {
272    let cfg = Config::load();
273    scan_and_redact(content, &cfg.secret_detection)
274}
275
276#[cfg(test)]
277pub mod tests {
278    use super::*;
279
280    #[test]
281    fn detects_aws_key() {
282        let input = concat!("aws_key = AK", "IAIOSFODNN7EXAMPLE");
283        let matches = detect_secrets(input);
284        assert!(matches.iter().any(|m| m.pattern_name == "aws_key"));
285    }
286
287    #[test]
288    fn detects_private_key_header() {
289        let input = concat!("-----BEGIN RSA PRIVATE", " KEY-----\nMIIEpAIB...");
290        let matches = detect_secrets(input);
291        assert!(matches.iter().any(|m| m.pattern_name == "private_key"));
292    }
293
294    #[test]
295    fn detects_github_token() {
296        let input = concat!(
297            "export GITHUB_TOKEN=gh",
298            "p_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijk"
299        );
300        let matches = detect_secrets(input);
301        assert!(matches.iter().any(|m| m.pattern_name == "github_token"));
302    }
303
304    #[test]
305    fn detects_anthropic_key() {
306        let input = concat!("ANTHROPIC_API_KEY=sk-ant-api03-", "abcdef1234567890ABCD");
307        let matches = detect_secrets(input);
308        assert!(matches.iter().any(|m| m.pattern_name == "anthropic_key"));
309    }
310
311    #[test]
312    fn detects_openai_key() {
313        let input = concat!("OPENAI_API_KEY=sk-", "abcdefghijklmnopqrstuvwx");
314        let matches = detect_secrets(input);
315        assert!(matches.iter().any(|m| m.pattern_name == "openai_key"));
316    }
317
318    #[test]
319    fn detects_gitlab_pat() {
320        let input = concat!("token = glpat-", "xxxxxxxxxxxxxxxxxxxx");
321        let matches = detect_secrets(input);
322        assert!(matches.iter().any(|m| m.pattern_name == "gitlab_pat"));
323    }
324
325    #[test]
326    fn detects_generic_api_key() {
327        let input = concat!("api_key = abcdefghijklmnop", "qrstuvwxyz1234567890");
328        let matches = detect_secrets(input);
329        assert!(matches.iter().any(
330            |m| m.pattern_name == "generic_api_key" || m.pattern_name == "high_entropy_secret"
331        ));
332    }
333
334    #[test]
335    fn clean_content_returns_empty() {
336        let input = "fn main() { println!(\"hello world\"); }";
337        let matches = detect_secrets(input);
338        assert!(matches.is_empty());
339    }
340
341    #[test]
342    fn detects_jwt() {
343        let input = concat!("token = eyJhbGciOiJIU", "zI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkw");
344        let matches = detect_secrets(input);
345        assert!(matches.iter().any(|m| m.pattern_name == "jwt"));
346    }
347
348    #[test]
349    fn detects_slack_token() {
350        let input = concat!("SLACK_TOKEN=xoxb-12345", "67890-abcdefghij");
351        let matches = detect_secrets(input);
352        assert!(matches.iter().any(|m| m.pattern_name == "slack_token"));
353    }
354
355    #[test]
356    fn detects_stripe_key() {
357        let input = concat!("stripe_key = sk_", "live_abcdefghij1234567890");
358        let matches = detect_secrets(input);
359        assert!(matches.iter().any(|m| m.pattern_name == "stripe_key"));
360    }
361
362    #[test]
363    fn detects_db_url() {
364        let input = concat!(
365            "DATABASE_URL=postgres://user:pass",
366            "word@localhost:5432/db"
367        );
368        let matches = detect_secrets(input);
369        assert!(matches.iter().any(|m| m.pattern_name == "db_url"));
370    }
371
372    #[test]
373    fn detects_npm_token() {
374        let input = concat!("NPM_TOKEN=npm_abcd", "efghij1234567890");
375        let matches = detect_secrets(input);
376        assert!(matches.iter().any(|m| m.pattern_name == "npm_token"));
377    }
378
379    #[test]
380    fn detects_github_fine_grained() {
381        let input = concat!("token = github_pat_ABC", "DEFGHIJKLMNOPQRSTuv");
382        let matches = detect_secrets(input);
383        assert!(
384            matches
385                .iter()
386                .any(|m| m.pattern_name == "github_fine_grained")
387        );
388    }
389
390    #[test]
391    fn redacted_preview_format() {
392        let preview = make_redacted_preview(concat!("AK", "IAIOSFODNN7EXAMPLE"));
393        assert!(preview.starts_with("AKIA"));
394        assert!(preview.ends_with("LE"));
395        assert!(preview.contains("***"));
396    }
397
398    #[test]
399    fn redacted_preview_short_string() {
400        let preview = make_redacted_preview("short");
401        assert_eq!(preview, "***");
402    }
403
404    #[test]
405    fn scan_and_redact_replaces_secrets() {
406        let cfg = SecretDetectionConfig {
407            enabled: true,
408            redact: true,
409            ..Default::default()
410        };
411        let input = concat!("key = AK", "IAIOSFODNN7EXAMPLE");
412        let (redacted, matches) = scan_and_redact(input, &cfg);
413        assert!(!matches.is_empty());
414        assert!(redacted.contains("[REDACTED:aws_key]"));
415        assert!(!redacted.contains(concat!("AK", "IAIOSFODNN7EXAMPLE")));
416    }
417
418    #[test]
419    fn scan_without_redact_preserves_content() {
420        let cfg = SecretDetectionConfig {
421            enabled: true,
422            redact: false,
423            ..Default::default()
424        };
425        let input = concat!("key = AK", "IAIOSFODNN7EXAMPLE");
426        let (output, matches) = scan_and_redact(input, &cfg);
427        assert!(!matches.is_empty());
428        assert_eq!(output, input);
429    }
430
431    #[test]
432    fn disabled_detection_returns_unchanged() {
433        let cfg = SecretDetectionConfig {
434            enabled: false,
435            redact: true,
436            ..Default::default()
437        };
438        let input = concat!("key = AK", "IAIOSFODNN7EXAMPLE");
439        let (output, matches) = scan_and_redact(input, &cfg);
440        assert!(matches.is_empty());
441        assert_eq!(output, input);
442    }
443
444    #[test]
445    fn custom_pattern_detected() {
446        let cfg = SecretDetectionConfig {
447            enabled: true,
448            redact: true,
449            custom_patterns: vec![r"MYCORP_[A-Z]{10,}".to_string()],
450            ..Default::default()
451        };
452        let input = "value is MYCORP_ABCDEFGHIJKLMNO here";
453        let (redacted, matches) = scan_and_redact(input, &cfg);
454        assert!(matches.iter().any(|m| m.pattern_name == "custom_pattern"));
455        assert!(redacted.contains("[REDACTED:custom_pattern]"));
456    }
457
458    #[test]
459    fn line_numbers_are_correct() {
460        let input = concat!("line1\nline2\nAK", "IAIOSFODNN7EXAMPLE\nline4");
461        let matches = detect_secrets(input);
462        assert!(!matches.is_empty());
463        assert_eq!(matches[0].line_number, 3);
464    }
465
466    #[test]
467    fn multiple_secrets_on_different_lines() {
468        let input = concat!(
469            "AK",
470            "IAIOSFODNN7EXAMPLE\nclean\nsk-abcdef",
471            "ghijklmnopqrstuvwx"
472        );
473        let matches = detect_secrets(input);
474        assert!(matches.len() >= 2);
475        let aws = matches
476            .iter()
477            .find(|m| m.pattern_name == "aws_key")
478            .unwrap();
479        assert_eq!(aws.line_number, 1);
480        let oai = matches
481            .iter()
482            .find(|m| m.pattern_name == "openai_key")
483            .unwrap();
484        assert_eq!(oai.line_number, 3);
485    }
486
487    #[test]
488    fn ec_private_key_detected() {
489        let input = concat!("-----BEGIN EC PRIVATE", " KEY-----");
490        let matches = detect_secrets(input);
491        assert!(matches.iter().any(|m| m.pattern_name == "private_key"));
492    }
493
494    #[test]
495    fn openssh_private_key_detected() {
496        let input = concat!("-----BEGIN OPENSSH PRIVATE", " KEY-----");
497        let matches = detect_secrets(input);
498        assert!(matches.iter().any(|m| m.pattern_name == "private_key"));
499    }
500}