use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::Path;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct Config {
pub entropy_threshold: f64,
pub min_confidence: f64,
pub enable_validation: bool,
pub scan_git_history: bool,
pub max_git_depth: Option<usize>,
pub respect_gitignore: bool,
pub max_file_size: u64,
pub exclude_tests: bool,
pub exclude_docs: bool,
#[serde(default)]
pub custom_patterns: Vec<CustomPattern>,
#[serde(default)]
pub allowlist: Vec<AllowlistRule>,
#[serde(default = "default_severities")]
pub report_severities: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CustomPattern {
pub name: String,
pub regex: String,
pub severity: String,
pub confidence: f64,
#[serde(default)]
pub description: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AllowlistRule {
#[serde(default)]
pub description: Option<String>,
#[serde(default)]
pub secret_types: Vec<String>,
#[serde(default)]
pub paths: Vec<String>,
#[serde(default)]
pub value_regex: Option<String>,
#[serde(default)]
pub severities: Vec<String>,
}
impl AllowlistRule {
pub fn matches(
&self,
secret_type_name: &str,
file_path: &str,
secret_value: &str,
severity_name: &str,
) -> bool {
if !self.secret_types.is_empty()
&& !self.secret_types.iter().any(|t| t == secret_type_name)
{
return false;
}
if !self.paths.is_empty() {
let matches_any = self.paths.iter().any(|p| {
if glob_match(file_path, p) {
return true;
}
if let Some(idx) = file_path.find(p.trim_end_matches('*').trim_end_matches('/')) {
let tail = &file_path[idx..];
if glob_match(tail, p) {
return true;
}
}
false
});
if !matches_any {
return false;
}
}
if let Some(ref re_str) = self.value_regex {
match regex::Regex::new(re_str) {
Ok(re) => {
if !re.is_match(secret_value) {
return false;
}
}
Err(_) => return false, }
}
if !self.severities.is_empty()
&& !self
.severities
.iter()
.any(|s| s.eq_ignore_ascii_case(severity_name))
{
return false;
}
true
}
}
fn glob_match(text: &str, pattern: &str) -> bool {
if pattern.contains('*') {
let parts: Vec<&str> = pattern.split('*').collect();
if parts.is_empty() {
return false;
}
let mut pos = 0;
for (i, part) in parts.iter().enumerate() {
if i == 0 && !part.is_empty() {
if !text[pos..].starts_with(part) {
return false;
}
pos += part.len();
} else if i == parts.len() - 1 && !part.is_empty() {
return text.ends_with(part);
} else if !part.is_empty() {
if let Some(found_pos) = text[pos..].find(part) {
pos += found_pos + part.len();
} else {
return false;
}
}
}
true
} else {
text.contains(pattern)
}
}
fn default_severities() -> Vec<String> {
vec![
"CRITICAL".to_string(),
"HIGH".to_string(),
"MEDIUM".to_string(),
"LOW".to_string(),
]
}
impl Default for Config {
fn default() -> Self {
Self {
entropy_threshold: 3.5,
min_confidence: 0.6,
enable_validation: false,
scan_git_history: true,
max_git_depth: None,
respect_gitignore: true,
max_file_size: 1024 * 1024, exclude_tests: false,
exclude_docs: false,
custom_patterns: Vec::new(),
allowlist: Vec::new(),
report_severities: default_severities(),
}
}
}
impl Config {
pub fn from_toml_file(path: &Path) -> Result<Self> {
let content = fs::read_to_string(path)?;
let config: Config = toml::from_str(&content)?;
Ok(config)
}
pub fn from_yaml_file(path: &Path) -> Result<Self> {
let content = fs::read_to_string(path)?;
let config: Config = serde_yaml::from_str(&content)?;
Ok(config)
}
pub fn to_toml_file(&self, path: &Path) -> Result<()> {
let content = toml::to_string_pretty(self)?;
fs::write(path, content)?;
Ok(())
}
pub fn to_yaml_file(&self, path: &Path) -> Result<()> {
let content = serde_yaml::to_string(self)?;
fs::write(path, content)?;
Ok(())
}
pub fn load_from_current_dir() -> Result<Self> {
let config_names = [".leaktor.toml", ".leaktor.yaml", ".leaktor.yml"];
for name in &config_names {
let path = Path::new(name);
if path.exists() {
if name.ends_with(".toml") {
return Self::from_toml_file(path);
} else {
return Self::from_yaml_file(path);
}
}
}
Ok(Self::default())
}
pub fn compiled_allowlist(&self) -> &[AllowlistRule] {
&self.allowlist
}
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::TempDir;
#[test]
fn test_default_config() {
let config = Config::default();
assert_eq!(config.entropy_threshold, 3.5);
assert_eq!(config.min_confidence, 0.6);
assert!(config.scan_git_history);
assert!(config.allowlist.is_empty());
assert!(config.custom_patterns.is_empty());
}
#[test]
fn test_config_serialization() -> Result<()> {
let config = Config::default();
let toml_str = toml::to_string(&config)?;
assert!(toml_str.contains("entropy_threshold"));
Ok(())
}
#[test]
fn test_config_save_and_load() -> Result<()> {
let temp_dir = TempDir::new()?;
let config_path = temp_dir.path().join("test.toml");
let config = Config::default();
config.to_toml_file(&config_path)?;
let loaded = Config::from_toml_file(&config_path)?;
assert_eq!(loaded.entropy_threshold, config.entropy_threshold);
Ok(())
}
#[test]
fn test_custom_patterns_round_trip() -> Result<()> {
let temp_dir = TempDir::new()?;
let config_path = temp_dir.path().join("test.toml");
let mut config = Config::default();
config.custom_patterns.push(CustomPattern {
name: "Internal Key".to_string(),
regex: "int_key_[a-f0-9]{32}".to_string(),
severity: "HIGH".to_string(),
confidence: 0.85,
description: Some("Company internal key".to_string()),
});
config.to_toml_file(&config_path)?;
let loaded = Config::from_toml_file(&config_path)?;
assert_eq!(loaded.custom_patterns.len(), 1);
assert_eq!(loaded.custom_patterns[0].name, "Internal Key");
assert_eq!(loaded.custom_patterns[0].confidence, 0.85);
Ok(())
}
#[test]
fn test_allowlist_round_trip() -> Result<()> {
let temp_dir = TempDir::new()?;
let config_path = temp_dir.path().join("test.toml");
let mut config = Config::default();
config.allowlist.push(AllowlistRule {
description: Some("Skip Sentry DSN".to_string()),
secret_types: vec!["Sentry DSN".to_string()],
paths: vec![],
value_regex: None,
severities: vec![],
});
config.to_toml_file(&config_path)?;
let loaded = Config::from_toml_file(&config_path)?;
assert_eq!(loaded.allowlist.len(), 1);
assert_eq!(loaded.allowlist[0].secret_types, vec!["Sentry DSN"]);
Ok(())
}
#[test]
fn test_allowlist_rule_matches_type() {
let rule = AllowlistRule {
description: None,
secret_types: vec!["Sentry DSN".to_string()],
paths: vec![],
value_regex: None,
severities: vec![],
};
assert!(rule.matches("Sentry DSN", "any/path", "any_value", "MEDIUM"));
assert!(!rule.matches("GitHub PAT", "any/path", "any_value", "CRITICAL"));
}
#[test]
fn test_allowlist_rule_matches_path() {
let rule = AllowlistRule {
description: None,
secret_types: vec![],
paths: vec!["tests/fixtures/*".to_string()],
value_regex: None,
severities: vec![],
};
assert!(rule.matches("Any", "tests/fixtures/secrets.env", "val", "HIGH"));
assert!(!rule.matches("Any", "src/main.rs", "val", "HIGH"));
}
#[test]
fn test_allowlist_rule_matches_value_regex() {
let rule = AllowlistRule {
description: None,
secret_types: vec![],
paths: vec![],
value_regex: Some("AKIAIOSFODNN7EXAMPLE".to_string()),
severities: vec![],
};
assert!(rule.matches("AWS", "file.env", "AKIAIOSFODNN7EXAMPLE", "CRITICAL"));
assert!(!rule.matches("AWS", "file.env", "AKIAREALKEY12345678", "CRITICAL"));
}
#[test]
fn test_allowlist_rule_matches_severity() {
let rule = AllowlistRule {
description: None,
secret_types: vec![],
paths: vec![],
value_regex: None,
severities: vec!["LOW".to_string(), "MEDIUM".to_string()],
};
assert!(rule.matches("Any", "any", "val", "LOW"));
assert!(rule.matches("Any", "any", "val", "MEDIUM"));
assert!(!rule.matches("Any", "any", "val", "CRITICAL"));
}
#[test]
fn test_allowlist_rule_multi_criteria() {
let rule = AllowlistRule {
description: None,
secret_types: vec!["Sentry DSN".to_string()],
paths: vec!["tests/*".to_string()],
value_regex: None,
severities: vec![],
};
assert!(rule.matches("Sentry DSN", "tests/fixtures/env", "val", "MEDIUM"));
assert!(!rule.matches("Sentry DSN", "src/main.rs", "val", "MEDIUM"));
assert!(!rule.matches("GitHub PAT", "tests/foo", "val", "CRITICAL"));
}
#[test]
fn test_glob_match() {
assert!(glob_match("tests/fixtures/secret.env", "tests/*"));
assert!(glob_match("src/main.test.rs", "*.test.*"));
assert!(glob_match("foo/bar/baz.js", "**/baz.js"));
assert!(!glob_match("src/main.rs", "*.py"));
}
}