use std::path::{Path, PathBuf};
pub const SENSITIVE_PATTERNS: &[&str] = &[
".git",
".ssh",
".gnupg",
".aws",
".azure",
".docker",
".kube",
".pulumi",
".terraform",
".env",
".envrc",
".dev.vars",
".key",
".pem",
".aws/credentials",
".netrc",
".docker/config.json",
"credentials",
"id_rsa",
"id_ed25519",
".npmrc",
".pypirc",
".htpasswd",
".git-credentials",
"secrets",
];
pub const SECRET_READ_PATTERNS: &[&str] = &[
".ssh",
".gnupg",
".aws",
".azure",
".kube",
".env",
".envrc",
".dev.vars",
".key",
".pem",
".aws/credentials",
".netrc",
".docker/config.json",
"credentials",
"id_rsa",
"id_ed25519",
".npmrc",
".pypirc",
".htpasswd",
".git-credentials",
"secrets",
];
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct PathClassifier {
protected: Vec<PathBuf>,
}
impl PathClassifier {
pub fn new(protected: Vec<PathBuf>) -> Self {
Self { protected }
}
pub const fn empty() -> Self {
Self {
protected: Vec::new(),
}
}
pub fn protected_paths(&self) -> &[PathBuf] {
&self.protected
}
pub fn is_protected(&self, path: &Path) -> bool {
self.protected.iter().any(|own| same_file(own, path))
}
pub fn is_secret(&self, path: &Path) -> bool {
self.is_secret_ignoring_name_shape(path) || has_secret_name_shape(path)
}
pub fn is_secret_ignoring_name_shape(&self, path: &Path) -> bool {
matches_patterns(path, SECRET_READ_PATTERNS) || self.is_protected(path)
}
pub fn is_sensitive(&self, path: &Path) -> bool {
matches_patterns(path, SENSITIVE_PATTERNS)
|| self.is_protected(path)
|| has_secret_name_shape(path)
}
}
pub fn same_file(a: &Path, b: &Path) -> bool {
a == b
|| matches!(
(dunce::canonicalize(a), dunce::canonicalize(b)),
(Ok(x), Ok(y)) if x == y
)
}
fn matches_patterns(path: &Path, patterns: &[&str]) -> bool {
let path_lower = path.to_string_lossy().to_ascii_lowercase();
patterns.iter().any(|pattern| {
if pattern.contains('/') {
path_lower.contains(&pattern.to_ascii_lowercase())
} else {
path.components().any(|c| {
c.as_os_str()
.to_string_lossy()
.eq_ignore_ascii_case(pattern)
}) || path
.file_name()
.is_some_and(|f| f.to_string_lossy().eq_ignore_ascii_case(pattern))
}
})
}
fn has_secret_name_shape(path: &Path) -> bool {
path.components().any(|component| {
let component = component.as_os_str().to_string_lossy().to_ascii_lowercase();
component.starts_with(".env.")
|| component.starts_with(".dev.vars.")
|| component.ends_with(".key")
|| component.ends_with(".pem")
|| component.ends_with(".p12")
|| component.ends_with(".pfx")
|| component.ends_with(".jks")
|| component.ends_with(".keystore")
|| component == "terraform.tfstate"
|| component.starts_with("terraform.tfstate.")
})
}
#[derive(Debug, Clone, Default)]
pub struct DaemonPaths {
classifier: std::sync::Arc<PathClassifier>,
}
impl DaemonPaths {
pub fn new(protected: Vec<PathBuf>) -> Self {
Self {
classifier: std::sync::Arc::new(PathClassifier::new(protected)),
}
}
pub fn is_secret(&self, path: &Path) -> bool {
self.classifier.is_secret(path) || is_configured_env_file(path)
}
pub fn is_secret_ignoring_name_shape(&self, path: &Path) -> bool {
self.classifier.is_secret_ignoring_name_shape(path) || is_configured_env_file(path)
}
pub fn is_sensitive(&self, path: &Path) -> bool {
self.classifier.is_sensitive(path) || is_configured_env_file(path)
}
pub fn is_own_file(&self, path: &Path) -> bool {
self.classifier.is_protected(path)
}
}
fn is_configured_env_file(path: &Path) -> bool {
["AIDAEMON_ENV_FILE", "AIDAEMON_RUNTIME_ENV_FILE"]
.iter()
.filter_map(std::env::var_os)
.filter(|value| !value.is_empty())
.any(|value| same_file(Path::new(&value), path))
}
#[cfg(test)]
mod tests {
use super::*;
fn path(p: &str) -> &Path {
Path::new(p)
}
#[test]
fn secrets_and_sensitive_paths() {
let classifier = PathClassifier::default();
for secret in [
"/home/synthetic/.ssh/id_ed25519",
"/srv/app/.env",
"/srv/app/.env.production",
"/srv/app/server.key",
"/home/synthetic/.aws/credentials",
] {
assert!(classifier.is_secret(path(secret)), "{secret}");
assert!(classifier.is_sensitive(path(secret)), "{secret}");
}
assert!(classifier.is_sensitive(path("/srv/app/.git/config")));
assert!(!classifier.is_secret(path("/srv/app/.git/config")));
assert!(classifier.is_sensitive(path("/srv/infra/terraform.tfstate")));
assert!(!classifier.is_sensitive(path("/srv/app/src/main.rs")));
assert!(!classifier.is_sensitive(path("/srv/app/monkey.txt")));
}
#[test]
fn name_shape_rules_can_be_ignored_for_templates() {
let classifier = PathClassifier::default();
let template = path("/srv/app/config/.env.example");
assert!(classifier.is_secret(template));
assert!(!classifier.is_secret_ignoring_name_shape(template));
}
#[test]
fn the_apps_own_files_are_protected() {
let own = PathBuf::from("/srv/synthetic-app/app.toml");
let classifier = PathClassifier::new(vec![own.clone()]);
assert!(classifier.is_protected(&own));
assert!(classifier.is_secret(&own));
assert!(classifier.is_sensitive(&own));
assert!(!PathClassifier::default().is_sensitive(&own));
}
}