cargocrypt 0.1.1

Zero-config cryptographic operations for Rust projects
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
//! Custom rule engine for extensible secret detection
//!
//! This module allows users to define their own detection rules using various
//! rule types including regex patterns, entropy thresholds, and composite rules.

use crate::detection::{SecretType, PatternMatch, EntropyResult};
use crate::error::{CargoCryptError, CryptoResult};
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Types of custom rules
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RuleType {
    /// Simple regex pattern matching
    Regex {
        pattern: String,
        case_sensitive: bool,
    },
    /// Entropy-based detection
    Entropy {
        min_entropy: f64,
        min_length: usize,
        max_length: usize,
    },
    /// Keyword-based detection with context
    Keyword {
        keywords: Vec<String>,
        context_radius: usize,
        require_high_entropy: bool,
    },
    /// Composite rule combining multiple conditions
    Composite {
        rules: Vec<RuleCondition>,
        operator: LogicalOperator,
    },
    /// File-based rules (specific to certain file types)
    FileSpecific {
        file_patterns: Vec<String>,
        rule: Box<RuleType>,
    },
}

/// Logical operators for composite rules
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum LogicalOperator {
    And,
    Or,
    Not,
}

/// A condition within a composite rule
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RuleCondition {
    pub rule_type: RuleType,
    pub weight: f64, // Weight for scoring (0.0 to 1.0)
}

/// A custom detection rule
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CustomRule {
    /// Unique identifier for the rule
    pub id: String,
    /// Human-readable name
    pub name: String,
    /// Description of what this rule detects
    pub description: String,
    /// The rule type and configuration
    pub rule_type: RuleType,
    /// Secret type this rule detects
    pub secret_type: SecretType,
    /// Base confidence score (0.0 to 1.0)
    pub confidence: f64,
    /// Whether this rule is enabled
    pub enabled: bool,
    /// Tags for categorization
    pub tags: Vec<String>,
    /// Whether to validate matches
    pub validate: bool,
}

impl CustomRule {
    /// Create a new custom rule
    pub fn new(
        id: String,
        name: String,
        description: String,
        rule_type: RuleType,
        secret_type: SecretType,
        confidence: f64,
    ) -> Self {
        Self {
            id,
            name,
            description,
            rule_type,
            secret_type,
            confidence,
            enabled: true,
            tags: Vec::new(),
            validate: false,
        }
    }

    /// Add tags to the rule
    pub fn with_tags(mut self, tags: Vec<String>) -> Self {
        self.tags = tags;
        self
    }

    /// Enable validation for this rule
    pub fn with_validation(mut self) -> Self {
        self.validate = true;
        self
    }

    /// Disable this rule
    pub fn disable(mut self) -> Self {
        self.enabled = false;
        self
    }

    /// Check if this rule matches the given text
    pub fn matches(&self, text: &str, file_path: Option<&str>) -> CryptoResult<Vec<RuleMatch>> {
        if !self.enabled {
            return Ok(Vec::new());
        }

        self.check_rule_matches(&self.rule_type, text, file_path)
    }

    /// Internal method to check rule matches
    fn check_rule_matches(
        &self,
        rule_type: &RuleType,
        text: &str,
        file_path: Option<&str>,
    ) -> CryptoResult<Vec<RuleMatch>> {
        match rule_type {
            RuleType::Regex { pattern, case_sensitive } => {
                self.check_regex_matches(pattern, text, *case_sensitive)
            }
            RuleType::Entropy { min_entropy, min_length, max_length } => {
                self.check_entropy_matches(text, *min_entropy, *min_length, *max_length)
            }
            RuleType::Keyword { keywords, context_radius, require_high_entropy } => {
                self.check_keyword_matches(text, keywords, *context_radius, *require_high_entropy)
            }
            RuleType::Composite { rules, operator } => {
                self.check_composite_matches(text, rules, operator, file_path)
            }
            RuleType::FileSpecific { file_patterns, rule } => {
                self.check_file_specific_matches(text, file_patterns, rule, file_path)
            }
        }
    }

    fn check_regex_matches(
        &self,
        pattern: &str,
        text: &str,
        case_sensitive: bool,
    ) -> CryptoResult<Vec<RuleMatch>> {
        let regex_pattern = if case_sensitive {
            pattern.to_string()
        } else {
            format!("(?i){}", pattern)
        };

        let regex = Regex::new(&regex_pattern)
            .map_err(|e| CargoCryptError::detection_error(&format!("Invalid regex pattern: {}", e)))?;

        let matches = regex
            .find_iter(text)
            .map(|m| RuleMatch {
                matched_text: m.as_str().to_string(),
                start: m.start(),
                end: m.end(),
                confidence: self.confidence,
                rule_id: self.id.clone(),
                match_type: MatchType::Regex,
                metadata: HashMap::new(),
            })
            .collect();

        Ok(matches)
    }

    fn check_entropy_matches(
        &self,
        text: &str,
        min_entropy: f64,
        min_length: usize,
        max_length: usize,
    ) -> CryptoResult<Vec<RuleMatch>> {
        use crate::detection::entropy::EntropyAnalyzer;
        
        let analyzer = EntropyAnalyzer::new();
        let candidates = analyzer.extract_high_entropy_substrings(text, min_length);

        let matches = candidates
            .into_iter()
            .filter(|(substring, result)| {
                substring.len() <= max_length && result.shannon_entropy >= min_entropy
            })
            .map(|(substring, entropy_result)| {
                let start = text.find(&substring).unwrap_or(0);
                let end = start + substring.len();
                
                let mut metadata = HashMap::new();
                metadata.insert("shannon_entropy".to_string(), entropy_result.shannon_entropy.to_string());
                metadata.insert("normalized_entropy".to_string(), entropy_result.normalized_entropy.to_string());
                metadata.insert("charset_size".to_string(), entropy_result.charset_size.to_string());

                RuleMatch {
                    matched_text: substring,
                    start,
                    end,
                    confidence: self.confidence * entropy_result.confidence,
                    rule_id: self.id.clone(),
                    match_type: MatchType::Entropy,
                    metadata,
                }
            })
            .collect();

        Ok(matches)
    }

    fn check_keyword_matches(
        &self,
        text: &str,
        keywords: &[String],
        context_radius: usize,
        require_high_entropy: bool,
    ) -> CryptoResult<Vec<RuleMatch>> {
        use crate::detection::entropy::utils::has_high_entropy;
        
        let mut matches = Vec::new();
        let text_lower = text.to_lowercase();

        for keyword in keywords {
            let keyword_lower = keyword.to_lowercase();
            let mut start_pos = 0;

            while let Some(pos) = text_lower[start_pos..].find(&keyword_lower) {
                let actual_pos = start_pos + pos;
                let context_start = actual_pos.saturating_sub(context_radius);
                let context_end = std::cmp::min(actual_pos + keyword.len() + context_radius, text.len());
                let context = &text[context_start..context_end];

                // Check if high entropy is required
                if require_high_entropy && !has_high_entropy(context) {
                    start_pos = actual_pos + 1;
                    continue;
                }

                let mut metadata = HashMap::new();
                metadata.insert("keyword".to_string(), keyword.clone());
                metadata.insert("context".to_string(), context.to_string());

                matches.push(RuleMatch {
                    matched_text: context.to_string(),
                    start: context_start,
                    end: context_end,
                    confidence: self.confidence,
                    rule_id: self.id.clone(),
                    match_type: MatchType::Keyword,
                    metadata,
                });

                start_pos = actual_pos + 1;
            }
        }

        Ok(matches)
    }

    fn check_composite_matches(
        &self,
        text: &str,
        rules: &[RuleCondition],
        operator: &LogicalOperator,
        file_path: Option<&str>,
    ) -> CryptoResult<Vec<RuleMatch>> {
        let mut all_matches = Vec::new();
        let mut rule_results = Vec::new();

        // Collect matches from all sub-rules
        for rule_condition in rules {
            let matches = self.check_rule_matches(&rule_condition.rule_type, text, file_path)?;
            rule_results.push((matches.clone(), rule_condition.weight));
            all_matches.extend(matches);
        }

        // Apply logical operator
        let filtered_matches = match operator {
            LogicalOperator::And => {
                // All rules must have matches
                if rule_results.iter().all(|(matches, _)| !matches.is_empty()) {
                    all_matches
                } else {
                    Vec::new()
                }
            }
            LogicalOperator::Or => {
                // Any rule can have matches
                all_matches
            }
            LogicalOperator::Not => {
                // No rules should have matches (useful for exclusions)
                if all_matches.is_empty() {
                    // Create a dummy match to indicate this rule fired
                    vec![RuleMatch {
                        matched_text: "negative_match".to_string(),
                        start: 0,
                        end: 0,
                        confidence: self.confidence,
                        rule_id: self.id.clone(),
                        match_type: MatchType::Composite,
                        metadata: HashMap::new(),
                    }]
                } else {
                    Vec::new()
                }
            }
        };

        Ok(filtered_matches)
    }

    fn check_file_specific_matches(
        &self,
        text: &str,
        file_patterns: &[String],
        rule: &RuleType,
        file_path: Option<&str>,
    ) -> CryptoResult<Vec<RuleMatch>> {
        // Check if file path matches any of the patterns
        if let Some(path) = file_path {
            let matches_pattern = file_patterns.iter().any(|pattern| {
                // Simple glob-like matching
                if pattern.contains('*') {
                    let pattern_regex = pattern.replace("*", ".*");
                    Regex::new(&pattern_regex)
                        .map(|r| r.is_match(path))
                        .unwrap_or(false)
                } else {
                    path.ends_with(pattern)
                }
            });

            if matches_pattern {
                self.check_rule_matches(rule, text, Some(path))
            } else {
                Ok(Vec::new())
            }
        } else {
            Ok(Vec::new())
        }
    }
}

/// A match result from a custom rule
#[derive(Debug, Clone)]
pub struct RuleMatch {
    /// The matched text
    pub matched_text: String,
    /// Start position in the source
    pub start: usize,
    /// End position in the source
    pub end: usize,
    /// Confidence score for this match
    pub confidence: f64,
    /// ID of the rule that produced this match
    pub rule_id: String,
    /// Type of match
    pub match_type: MatchType,
    /// Additional metadata
    pub metadata: HashMap<String, String>,
}

/// Types of rule matches
#[derive(Debug, Clone)]
pub enum MatchType {
    Regex,
    Entropy,
    Keyword,
    Composite,
    FileSpecific,
}

/// Rule engine for managing and executing custom rules
#[derive(Debug, Default, Clone)]
pub struct RuleEngine {
    rules: HashMap<String, CustomRule>,
    enabled: bool,
}

impl RuleEngine {
    /// Create a new rule engine
    pub fn new() -> Self {
        Self {
            rules: HashMap::new(),
            enabled: true,
        }
    }

    /// Add a rule to the engine
    pub fn add_rule(&mut self, rule: CustomRule) {
        self.rules.insert(rule.id.clone(), rule);
    }

    /// Remove a rule from the engine
    pub fn remove_rule(&mut self, rule_id: &str) -> Option<CustomRule> {
        self.rules.remove(rule_id)
    }

    /// Get a rule by ID
    pub fn get_rule(&self, rule_id: &str) -> Option<&CustomRule> {
        self.rules.get(rule_id)
    }

    /// Get all rules
    pub fn rules(&self) -> Vec<&CustomRule> {
        self.rules.values().collect()
    }

    /// Get enabled rules only
    pub fn enabled_rules(&self) -> Vec<&CustomRule> {
        self.rules
            .values()
            .filter(|rule| rule.enabled)
            .collect()
    }

    /// Execute all enabled rules against text
    pub fn execute_rules(&self, text: &str, file_path: Option<&str>) -> CryptoResult<Vec<RuleMatch>> {
        if !self.enabled {
            return Ok(Vec::new());
        }

        let mut all_matches = Vec::new();

        for rule in self.enabled_rules() {
            let matches = rule.matches(text, file_path)?;
            all_matches.extend(matches);
        }

        // Sort by position
        all_matches.sort_by_key(|m| m.start);

        Ok(all_matches)
    }

    /// Load rules from configuration
    pub fn load_rules_from_config(&mut self, config: &RuleConfig) -> CryptoResult<()> {
        for rule_config in &config.rules {
            let rule = self.create_rule_from_config(rule_config)?;
            self.add_rule(rule);
        }
        Ok(())
    }

    /// Create a rule from configuration
    fn create_rule_from_config(&self, config: &RuleConfigItem) -> CryptoResult<CustomRule> {
        let rule_type = match config.rule_type.as_str() {
            "regex" => {
                let pattern = config.pattern.as_ref()
                    .ok_or_else(|| CargoCryptError::detection_error("Regex rule requires 'pattern' field"))?;
                
                RuleType::Regex {
                    pattern: pattern.clone(),
                    case_sensitive: config.case_sensitive.unwrap_or(true),
                }
            }
            "entropy" => {
                RuleType::Entropy {
                    min_entropy: config.min_entropy.unwrap_or(4.0),
                    min_length: config.min_length.unwrap_or(8),
                    max_length: config.max_length.unwrap_or(100),
                }
            }
            "keyword" => {
                let keywords = config.keywords.as_ref()
                    .ok_or_else(|| CargoCryptError::detection_error("Keyword rule requires 'keywords' field"))?;
                
                RuleType::Keyword {
                    keywords: keywords.clone(),
                    context_radius: config.context_radius.unwrap_or(20),
                    require_high_entropy: config.require_high_entropy.unwrap_or(false),
                }
            }
            _ => {
                return Err(CargoCryptError::detection_error(&format!(
                    "Unknown rule type: {}", config.rule_type
                )));
            }
        };

        let secret_type = if let Some(custom_type) = &config.secret_type {
            SecretType::Custom(custom_type.clone())
        } else {
            SecretType::Custom("unknown".to_string())
        };

        Ok(CustomRule::new(
            config.id.clone(),
            config.name.clone(),
            config.description.clone().unwrap_or_default(),
            rule_type,
            secret_type,
            config.confidence.unwrap_or(0.7),
        ))
    }

    /// Enable or disable the rule engine
    pub fn set_enabled(&mut self, enabled: bool) {
        self.enabled = enabled;
    }
}

/// Configuration for loading rules
#[derive(Debug, Serialize, Deserialize)]
pub struct RuleConfig {
    pub rules: Vec<RuleConfigItem>,
}

/// Configuration for a single rule
#[derive(Debug, Serialize, Deserialize)]
pub struct RuleConfigItem {
    pub id: String,
    pub name: String,
    pub description: Option<String>,
    pub rule_type: String,
    pub secret_type: Option<String>,
    pub confidence: Option<f64>,
    
    // Regex rule fields
    pub pattern: Option<String>,
    pub case_sensitive: Option<bool>,
    
    // Entropy rule fields
    pub min_entropy: Option<f64>,
    pub min_length: Option<usize>,
    pub max_length: Option<usize>,
    
    // Keyword rule fields
    pub keywords: Option<Vec<String>>,
    pub context_radius: Option<usize>,
    pub require_high_entropy: Option<bool>,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_regex_rule() {
        let rule = CustomRule::new(
            "test_regex".to_string(),
            "Test Regex".to_string(),
            "Test regex rule".to_string(),
            RuleType::Regex {
                pattern: r"test_\d+".to_string(),
                case_sensitive: true,
            },
            SecretType::Custom("test".to_string()),
            0.8,
        );

        let matches = rule.matches("This is test_123 and test_456", None).unwrap();
        assert_eq!(matches.len(), 2);
        assert_eq!(matches[0].matched_text, "test_123");
        assert_eq!(matches[1].matched_text, "test_456");
    }

    #[test]
    fn test_entropy_rule() {
        let rule = CustomRule::new(
            "test_entropy".to_string(),
            "Test Entropy".to_string(),
            "Test entropy rule".to_string(),
            RuleType::Entropy {
                min_entropy: 3.0,
                min_length: 8,
                max_length: 50,
            },
            SecretType::Custom("high_entropy".to_string()),
            0.7,
        );

        let matches = rule.matches("hello AKIAIOSFODNN7EXAMPLE world", None).unwrap();
        assert!(!matches.is_empty());
    }

    #[test]
    fn test_keyword_rule() {
        let rule = CustomRule::new(
            "test_keyword".to_string(),
            "Test Keyword".to_string(),
            "Test keyword rule".to_string(),
            RuleType::Keyword {
                keywords: vec!["password".to_string(), "secret".to_string()],
                context_radius: 10,
                require_high_entropy: false,
            },
            SecretType::Custom("keyword".to_string()),
            0.6,
        );

        let matches = rule.matches("The password is very secure", None).unwrap();
        assert_eq!(matches.len(), 1);
        assert!(matches[0].matched_text.contains("password"));
    }

    #[test]
    fn test_rule_engine() {
        let mut engine = RuleEngine::new();
        
        let rule = CustomRule::new(
            "test_rule".to_string(),
            "Test Rule".to_string(),
            "Test rule".to_string(),
            RuleType::Regex {
                pattern: r"secret_\w+".to_string(),
                case_sensitive: false,
            },
            SecretType::Custom("test".to_string()),
            0.8,
        );

        engine.add_rule(rule);
        
        let matches = engine.execute_rules("This is secret_key and SECRET_TOKEN", None).unwrap();
        assert_eq!(matches.len(), 2);
    }
}