minos_codex/secret_type/
mod.rs

1use serde::{Deserialize, Serialize};
2use std::collections::HashSet;
3
4#[derive(Debug, Clone, Serialize, Deserialize)]
5pub struct SecretType {
6    pub name: String,
7    pub regex: String,
8    pub description: Option<String>,
9    pub examples: HashSet<String>,
10    #[serde(default)]
11    pub false_positives: HashSet<String>,
12}
13
14impl SecretType {
15    pub fn new(
16        name: String,
17        regex: String,
18        description: Option<String>,
19        examples: HashSet<String>,
20        false_positives: HashSet<String>,
21    ) -> Self {
22        SecretType {
23            name,
24            regex,
25            description,
26            examples,
27            false_positives,
28        }
29    }
30
31    pub fn validate(&self) -> Result<(), String> {
32        // 1. Check if the regex is valid
33        let re = match regex::Regex::new(&self.regex) {
34            Ok(re) => re,
35            Err(e) => return Err(format!("Invalid regex: {}", e)),
36        };
37
38        // 2. Ensure that all examples match the regex
39        for example in &self.examples {
40            if !re.is_match(example) {
41                return Err(format!(
42                    "Example '{}' does not match the regex '{}' for secret type '{}'",
43                    example, self.regex, self.name
44                ));
45            }
46        }
47
48        // 3. Ensure that all false positives do not match the regex
49        for false_positive in &self.false_positives {
50            if re.is_match(false_positive) {
51                return Err(format!(
52                    "False positive '{}' matches the regex '{}' for secret type '{}'",
53                    false_positive,
54                    self.regex,
55                    self.name // End of  Selection
56                ));
57            }
58        }
59
60        Ok(())
61    }
62}