keyclaw 0.2.1

Local MITM proxy that keeps secrets out of LLM traffic
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
//! Bundled gitleaks rule loading, compilation, and matching.

use std::collections::HashSet;
use std::path::Path;

use regex::{Regex, RegexBuilder};
use serde::Deserialize;

use crate::allowlist::Allowlist;
use crate::entropy::EntropyConfig;
use crate::errors::KeyclawError;

const ENTROPY_RULE_ID: &str = "entropy";

/// A single compiled gitleaks rule ready for matching.
pub struct Rule {
    /// Stable rule identifier from `gitleaks.toml`.
    pub id: String,
    /// Compiled regex used to find candidate matches.
    pub regex: Regex,
    /// Optional keywords that help prefilter candidate inputs.
    pub keywords: Vec<String>,
    /// Which capture group holds the secret (0 = full match).
    pub secret_group: usize,
    /// Minimum Shannon entropy for the matched secret. If set,
    /// matches below this threshold are discarded as likely false positives.
    pub min_entropy: Option<f64>,
    allowlists: Vec<CompiledAllowlist>,
    stopwords: HashSet<String>,
    specificity: RuleSpecificity,
}

/// All compiled gitleaks rules.
pub struct RuleSet {
    /// Compiled rule list.
    pub rules: Vec<Rule>,
    /// Number of raw TOML rules that were skipped during compilation.
    pub skipped_rules: usize,
    /// Entropy configuration used for entropy-based fallback detection.
    pub entropy_config: EntropyConfig,
    /// Operator-configured allowlist applied after rule matching.
    pub allowlist: Allowlist,
    global_allowlists: Vec<CompiledAllowlist>,
    global_stopwords: HashSet<String>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MatchSource {
    /// Match came from a provider-specific or generic regex rule.
    Regex,
    /// Match came from entropy-based detection.
    Entropy,
}

impl MatchSource {
    /// Return a stable string form used in logs and metadata.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Regex => "regex",
            Self::Entropy => "entropy",
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MatchConfidence {
    /// Weak heuristic confidence.
    Low,
    /// Reasonable but not fully provider-specific confidence.
    Medium,
    /// Strong provider-specific confidence.
    High,
}

impl MatchConfidence {
    fn from_score(score: u8) -> Self {
        match score {
            80..=u8::MAX => Self::High,
            60..=79 => Self::Medium,
            _ => Self::Low,
        }
    }

    /// Return a stable string form used in logs and metadata.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Low => "low",
            Self::Medium => "medium",
            Self::High => "high",
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum RuleSpecificity {
    Generic,
    Specific,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum AllowlistRegexTarget {
    Secret,
    Match,
}

#[derive(Debug)]
struct CompiledAllowlist {
    regexes: Vec<Regex>,
    target: AllowlistRegexTarget,
    stopwords: HashSet<String>,
}

// ── TOML deserialization shapes ──────────────────────────────

#[derive(Default, Deserialize)]
struct TomlConfig {
    #[serde(default)]
    rules: Vec<TomlRule>,
    #[serde(default)]
    allowlist: TomlAllowlist,
}

#[derive(Deserialize)]
struct TomlRule {
    id: String,
    #[serde(default)]
    regex: String,
    #[serde(default)]
    keywords: Vec<String>,
    #[serde(default, rename = "secretGroup")]
    secret_group: Option<usize>,
    #[serde(default)]
    entropy: Option<f64>,
    #[serde(default)]
    allowlists: Vec<TomlAllowlist>,
    #[serde(default)]
    stopwords: Vec<String>,
    #[serde(default)]
    path: String,
}

#[derive(Default, Deserialize)]
struct TomlAllowlist {
    #[serde(default)]
    regexes: Vec<String>,
    #[serde(default)]
    stopwords: Vec<String>,
    #[serde(default, rename = "regexTarget")]
    regex_target: Option<String>,
}

// ── Loading ──────────────────────────────────────────────────

impl RuleSet {
    /// Load and compile rules from a gitleaks.toml file.
    pub fn from_file(path: &Path) -> Result<Self, KeyclawError> {
        let content = std::fs::read_to_string(path)
            .map_err(|e| KeyclawError::uncoded(format!("read {}: {e}", path.display())))?;
        Self::from_toml(&content)
    }

    /// Load and compile rules from a TOML string.
    pub fn from_toml(toml_str: &str) -> Result<Self, KeyclawError> {
        let config: TomlConfig = toml::from_str(toml_str)
            .map_err(|e| KeyclawError::uncoded(format!("parse gitleaks.toml: {e}")))?;

        let mut rules = Vec::with_capacity(config.rules.len());
        let mut skipped = 0usize;
        let global_allowlists =
            compile_allowlists(&config.allowlist).map_err(KeyclawError::uncoded)?;
        let global_stopwords = normalize_stopwords(&config.allowlist.stopwords);

        for r in config.rules {
            if r.regex.is_empty() || !r.path.trim().is_empty() {
                skipped += 1;
                continue;
            }
            match RegexBuilder::new(&r.regex)
                .size_limit(50 * 1024 * 1024)
                .build()
            {
                Ok(compiled) => {
                    let allowlists = match compile_rule_allowlists(&r.allowlists) {
                        Ok(allowlists) => allowlists,
                        Err(_) => {
                            skipped += 1;
                            continue;
                        }
                    };
                    rules.push(Rule {
                        specificity: rule_specificity(&r.id),
                        id: r.id,
                        regex: compiled,
                        keywords: r.keywords.iter().map(|k| k.to_lowercase()).collect(),
                        secret_group: r.secret_group.unwrap_or(0),
                        min_entropy: r.entropy,
                        allowlists,
                        stopwords: normalize_stopwords(&r.stopwords),
                    });
                }
                Err(_) => {
                    skipped += 1;
                }
            }
        }

        Ok(RuleSet {
            rules,
            skipped_rules: skipped,
            entropy_config: EntropyConfig::default(),
            allowlist: Allowlist::default(),
            global_allowlists,
            global_stopwords,
        })
    }

    /// Return the bundled default rules (compiled into the binary).
    pub fn bundled() -> Result<Self, KeyclawError> {
        Self::from_toml(include_str!("../gitleaks.toml"))
    }

    /// Find all secret matches in `input`, returning (rule_id, start, end, secret).
    /// Matches inside existing placeholders are skipped.
    pub fn find_secrets<'a>(&'a self, input: &'a str) -> Vec<SecretMatch<'a>> {
        self.find_secrets_with_options(input, true)
    }

    /// Find all secret matches in `input`, optionally including entropy-based
    /// fallback detection.
    pub fn find_secrets_with_options<'a>(
        &'a self,
        input: &'a str,
        include_entropy: bool,
    ) -> Vec<SecretMatch<'a>> {
        let mut candidates: Vec<SecretMatch<'a>> = Vec::new();
        let mut input_lower: Option<String> = None;

        for rule in &self.rules {
            // Fast keyword pre-filter: skip rule if none of its keywords appear
            if !rule.keywords.is_empty() {
                let input_lower = input_lower.get_or_insert_with(|| input.to_ascii_lowercase());
                if !rule.keywords.iter().any(|kw| input_lower.contains(kw)) {
                    continue;
                }
            }

            for caps in rule.regex.captures_iter(input) {
                let full_match = caps.get(0).unwrap();
                let secret_match = if rule.secret_group > 0 {
                    caps.get(rule.secret_group)
                        .unwrap_or_else(|| caps.get(0).unwrap())
                } else {
                    caps.get(0).unwrap()
                };

                let start = secret_match.start();
                let end = secret_match.end();
                let secret = secret_match.as_str();

                // Skip very short matches
                if secret.len() < 8 {
                    continue;
                }

                // Skip if matched secret's entropy is below the rule's threshold
                let entropy = rule
                    .min_entropy
                    .map(|_| crate::entropy::shannon_entropy(secret));
                if let Some(min_entropy) = rule.min_entropy {
                    if entropy.unwrap_or_default() < min_entropy {
                        continue;
                    }
                }

                if self.should_allow_match(rule, secret, full_match.as_str()) {
                    continue;
                }

                // Skip if inside an existing placeholder
                if inside_placeholder(input, start, end) {
                    continue;
                }

                let confidence_score = score_regex_match(rule, secret, entropy);
                candidates.push(SecretMatch {
                    rule_id: &rule.id,
                    start,
                    end,
                    secret,
                    source: MatchSource::Regex,
                    confidence: MatchConfidence::from_score(confidence_score),
                    confidence_score,
                    entropy,
                });
            }
        }

        // Entropy-based detection pass
        if include_entropy && self.entropy_config.enabled {
            for em in crate::entropy::find_high_entropy_tokens(
                input,
                self.entropy_config.min_len,
                self.entropy_config.threshold,
            ) {
                // Skip if inside an existing placeholder
                if inside_placeholder(input, em.start, em.end) {
                    continue;
                }
                if self
                    .global_stopwords
                    .contains(&normalize_secret_key(em.token))
                {
                    continue;
                }
                if self.allowlist.allows(ENTROPY_RULE_ID, em.token) {
                    continue;
                }
                let confidence_score = score_entropy_match(em.token, em.entropy);
                candidates.push(SecretMatch {
                    rule_id: ENTROPY_RULE_ID,
                    start: em.start,
                    end: em.end,
                    secret: em.token,
                    source: MatchSource::Entropy,
                    confidence: MatchConfidence::from_score(confidence_score),
                    confidence_score,
                    entropy: Some(em.entropy),
                });
            }
        }

        select_best_matches(candidates)
    }

    fn should_allow_match(&self, rule: &Rule, secret: &str, matched: &str) -> bool {
        self.allowlist.allows(&rule.id, secret)
            || self
                .global_stopwords
                .contains(&normalize_secret_key(secret))
            || rule.stopwords.contains(&normalize_secret_key(secret))
            || matches_allowlists(&self.global_allowlists, secret, matched)
            || matches_allowlists(&rule.allowlists, secret, matched)
    }
}

#[derive(Debug)]
pub struct SecretMatch<'a> {
    /// Rule ID that produced the match.
    pub rule_id: &'a str,
    /// Byte offset where the matched secret starts.
    pub start: usize,
    /// Byte offset where the matched secret ends.
    pub end: usize,
    /// Matched secret text.
    pub secret: &'a str,
    /// Whether the match came from regex or entropy detection.
    pub source: MatchSource,
    /// Bucketed confidence for operator-facing reporting.
    pub confidence: MatchConfidence,
    /// Raw confidence score used for match prioritization.
    pub confidence_score: u8,
    /// Shannon entropy score, when available.
    pub entropy: Option<f64>,
}

impl SecretMatch<'_> {
    fn priority(&self) -> (u8, u8, usize) {
        let source_rank = match self.source {
            MatchSource::Regex => 2,
            MatchSource::Entropy => 1,
        };
        (self.confidence_score, source_rank, self.len())
    }

    fn len(&self) -> usize {
        self.end.saturating_sub(self.start)
    }
}

/// Check if a position in the input falls inside an existing placeholder.
fn inside_placeholder(input: &str, start: usize, end: usize) -> bool {
    let search_start = start.saturating_sub(crate::placeholder::MAX_PLACEHOLDER_LEN);
    for (rel, ch) in input[search_start..start].char_indices() {
        if ch != '{' {
            continue;
        }

        let abs_pos = search_start + rel;
        if let Some(len) = crate::placeholder::complete_placeholder_len(&input[abs_pos..]) {
            let placeholder_end = abs_pos + len;
            if abs_pos < end && start < placeholder_end {
                return true;
            }
        }
    }

    false
}

fn compile_rule_allowlists(raw: &[TomlAllowlist]) -> Result<Vec<CompiledAllowlist>, String> {
    raw.iter()
        .map(compile_allowlist)
        .filter_map(|result| match result {
            Ok(Some(allowlist)) => Some(Ok(allowlist)),
            Ok(None) => None,
            Err(_) => None,
        })
        .collect()
}

fn compile_allowlists(raw: &TomlAllowlist) -> Result<Vec<CompiledAllowlist>, String> {
    match compile_allowlist(raw)? {
        Some(allowlist) => Ok(vec![allowlist]),
        None => Ok(Vec::new()),
    }
}

fn compile_allowlist(raw: &TomlAllowlist) -> Result<Option<CompiledAllowlist>, String> {
    if raw.regexes.is_empty() && raw.stopwords.is_empty() {
        return Ok(None);
    }

    let target = match raw
        .regex_target
        .as_deref()
        .map(str::trim)
        .map(str::to_ascii_lowercase)
        .as_deref()
    {
        Some("match") => AllowlistRegexTarget::Match,
        _ => AllowlistRegexTarget::Secret,
    };

    let mut regexes = Vec::with_capacity(raw.regexes.len());
    for pattern in &raw.regexes {
        if let Ok(compiled) = RegexBuilder::new(pattern)
            .size_limit(50 * 1024 * 1024)
            .build()
        {
            regexes.push(compiled);
        }
    }

    if regexes.is_empty() && raw.stopwords.is_empty() {
        return Ok(None);
    }

    Ok(Some(CompiledAllowlist {
        regexes,
        target,
        stopwords: normalize_stopwords(&raw.stopwords),
    }))
}

fn matches_allowlists(allowlists: &[CompiledAllowlist], secret: &str, matched: &str) -> bool {
    allowlists.iter().any(|allowlist| {
        if allowlist.stopwords.contains(&normalize_secret_key(secret)) {
            return true;
        }
        let candidate = match allowlist.target {
            AllowlistRegexTarget::Secret => secret,
            AllowlistRegexTarget::Match => matched,
        };
        allowlist
            .regexes
            .iter()
            .any(|regex| regex.is_match(candidate))
    })
}

fn normalize_stopwords(stopwords: &[String]) -> HashSet<String> {
    stopwords
        .iter()
        .map(|word| normalize_secret_key(word))
        .collect()
}

fn normalize_secret_key(secret: &str) -> String {
    secret.trim().to_ascii_lowercase()
}

fn rule_specificity(rule_id: &str) -> RuleSpecificity {
    if rule_id.trim().to_ascii_lowercase().starts_with("generic-") {
        RuleSpecificity::Generic
    } else {
        RuleSpecificity::Specific
    }
}

fn score_regex_match(rule: &Rule, secret: &str, entropy: Option<f64>) -> u8 {
    let mut score = match rule.specificity {
        RuleSpecificity::Specific => 88u8,
        RuleSpecificity::Generic => 66u8,
    };

    if let (Some(min_entropy), Some(entropy)) = (rule.min_entropy, entropy) {
        let bonus = ((entropy - min_entropy).max(0.0) * 8.0).round() as u8;
        score = score.saturating_add(bonus.min(10));
    }

    if secret.len() >= 32 {
        score = score.saturating_add(3);
    }

    score.min(99)
}

fn score_entropy_match(secret: &str, entropy: f64) -> u8 {
    let len_bonus = (secret.len() / 16).min(8) as u8;
    let entropy_bonus = ((entropy - 3.5).max(0.0) * 10.0).round() as u8;
    42u8.saturating_add(len_bonus)
        .saturating_add(entropy_bonus.min(12))
        .min(70)
}

fn select_best_matches<'a>(mut candidates: Vec<SecretMatch<'a>>) -> Vec<SecretMatch<'a>> {
    candidates.sort_by(|left, right| {
        left.start
            .cmp(&right.start)
            .then_with(|| right.priority().cmp(&left.priority()))
    });

    let mut selected: Vec<SecretMatch<'a>> = Vec::new();

    'candidate: for candidate in candidates {
        let mut idx = 0usize;
        while idx < selected.len() {
            if selected[idx].start < candidate.end && candidate.start < selected[idx].end {
                if candidate.priority() > selected[idx].priority() {
                    selected.remove(idx);
                    continue;
                }
                continue 'candidate;
            }
            idx += 1;
        }
        selected.push(candidate);
    }

    selected.sort_by_key(|m| m.start);
    selected
}

#[cfg(test)]
mod tests {
    use std::collections::HashSet;

    use super::RuleSet;
    use crate::allowlist::Allowlist;
    use crate::placeholder::make;

    #[test]
    fn find_secrets_skips_real_placeholders_but_not_marker_shaped_invalid_text() {
        let rules = RuleSet::from_toml(
            r#"
[[rules]]
id = "placeholder-hash"
regex = '[a-f0-9]{16}'
"#,
        )
        .expect("ruleset");

        let valid = format!("prefix {}", make("*_0123456789abcdef"));
        let invalid = "prefix {{KEYCLAW_SECRET_prefixx_0123456789abcdef}}";

        assert!(
            rules.find_secrets(&valid).is_empty(),
            "valid placeholder should be ignored"
        );
        assert_eq!(
            rules.find_secrets(invalid).len(),
            1,
            "invalid marker-shaped text should still be scanned"
        );
    }

    #[test]
    fn find_secrets_includes_entropy_matches() {
        use crate::entropy::EntropyConfig;
        let rules = RuleSet {
            rules: Vec::new(),
            skipped_rules: 0,
            entropy_config: EntropyConfig::default(),
            allowlist: Allowlist::default(),
            global_allowlists: Vec::new(),
            global_stopwords: HashSet::new(),
        };
        let input = "token=aB3dE5fG7hI9jK1lM3nO5pQ7rS9tU1v";
        let matches = rules.find_secrets(input);
        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].rule_id, "entropy");
        assert_eq!(matches[0].secret, "aB3dE5fG7hI9jK1lM3nO5pQ7rS9tU1v");
    }

    #[test]
    fn find_secrets_entropy_disabled() {
        use crate::entropy::EntropyConfig;
        let rules = RuleSet {
            rules: Vec::new(),
            skipped_rules: 0,
            entropy_config: EntropyConfig {
                enabled: false,
                ..Default::default()
            },
            allowlist: Allowlist::default(),
            global_allowlists: Vec::new(),
            global_stopwords: HashSet::new(),
        };
        let input = "token=aB3dE5fG7hI9jK1lM3nO5pQ7rS9tU1v";
        let matches = rules.find_secrets(input);
        assert!(matches.is_empty());
    }

    #[test]
    fn find_secrets_respects_per_rule_entropy_threshold() {
        // Rule requires entropy >= 3.0; a low-entropy match should be skipped
        let rules = RuleSet::from_toml(
            r#"
[[rules]]
id = "high-entropy-only"
regex = '[a-zA-Z0-9]{20,}'
entropy = 3.0
"#,
        )
        .expect("ruleset");

        // Low entropy: repeated pattern
        let low = "abcabcabcabcabcabcabcabc";
        assert!(
            rules.find_secrets(low).is_empty(),
            "low-entropy match should be filtered by per-rule threshold"
        );

        // High entropy: random-looking
        let high = "aB3dE5fG7hI9jK1lM3nO5pQ7rS9tU1v";
        let matches = rules.find_secrets(high);
        assert_eq!(matches.len(), 1, "high-entropy match should pass threshold");
        assert_eq!(matches[0].rule_id, "high-entropy-only");
    }

    #[test]
    fn find_secrets_no_entropy_threshold_accepts_all_matches() {
        // Rule without entropy field — all matches accepted
        let rules = RuleSet::from_toml(
            r#"
[[rules]]
id = "no-threshold"
regex = '[a-zA-Z]{20,}'
"#,
        )
        .expect("ruleset");

        let input = "abcabcabcabcabcabcabcabc";
        let matches = rules.find_secrets(input);
        assert_eq!(
            matches.len(),
            1,
            "without entropy threshold, all matches pass"
        );
    }

    #[test]
    fn bundled_rules_parse_entropy_field() {
        let rules = RuleSet::bundled().expect("bundled rules");
        let with_entropy = rules
            .rules
            .iter()
            .filter(|r| r.min_entropy.is_some())
            .count();
        assert!(
            with_entropy >= 100,
            "expected at least 100 rules with entropy thresholds, found {with_entropy}"
        );
    }
    #[test]
    fn bundled_rules_report_unsupported_entries_as_skipped() {
        let rules = RuleSet::bundled().expect("bundled rules");
        assert!(
            rules.skipped_rules >= 1,
            "payload scanning should skip unsupported path-scoped entries"
        );
    }
}