151 行
4.5 KiB
Rust
151 行
4.5 KiB
Rust
|
|
use std::sync::OnceLock;
|
||
|
|
|
||
|
|
use regex::{Captures, Regex};
|
||
|
|
|
||
|
|
pub const REDACTED: &str = "[REDACTED]";
|
||
|
|
|
||
|
|
#[derive(Clone, Debug, Default)]
|
||
|
|
pub struct Redactor {
|
||
|
|
known_secrets: Vec<String>,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl Redactor {
|
||
|
|
pub fn new<I, S>(known_secrets: I) -> Self
|
||
|
|
where
|
||
|
|
I: IntoIterator<Item = S>,
|
||
|
|
S: AsRef<str>,
|
||
|
|
{
|
||
|
|
let mut secrets = known_secrets
|
||
|
|
.into_iter()
|
||
|
|
.map(|value| value.as_ref().to_owned())
|
||
|
|
.filter(|value| value.chars().count() >= 4)
|
||
|
|
.collect::<Vec<_>>();
|
||
|
|
secrets.sort_by_key(|value| std::cmp::Reverse(value.len()));
|
||
|
|
secrets.dedup();
|
||
|
|
Self {
|
||
|
|
known_secrets: secrets,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn redact(&self, input: &str) -> String {
|
||
|
|
let mut output = input.to_owned();
|
||
|
|
for secret in &self.known_secrets {
|
||
|
|
output = output.replace(secret, REDACTED);
|
||
|
|
}
|
||
|
|
|
||
|
|
output = pem_private_key_regex()
|
||
|
|
.replace_all(&output, REDACTED)
|
||
|
|
.into_owned();
|
||
|
|
output = url_credentials_regex()
|
||
|
|
.replace_all(&output, "${scheme}[REDACTED]@")
|
||
|
|
.into_owned();
|
||
|
|
output = bearer_regex()
|
||
|
|
.replace_all(&output, "${prefix}[REDACTED]")
|
||
|
|
.into_owned();
|
||
|
|
key_value_regex()
|
||
|
|
.replace_all(&output, |captures: &Captures<'_>| {
|
||
|
|
format!("{}{}", &captures["prefix"], REDACTED)
|
||
|
|
})
|
||
|
|
.into_owned()
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn redact_value_for_key(&self, key: &str, value: &str) -> String {
|
||
|
|
if is_sensitive_key(key) {
|
||
|
|
REDACTED.to_owned()
|
||
|
|
} else {
|
||
|
|
self.redact(value)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn is_sensitive_key(key: &str) -> bool {
|
||
|
|
let normalized = key
|
||
|
|
.chars()
|
||
|
|
.filter(|character| character.is_ascii_alphanumeric())
|
||
|
|
.flat_map(char::to_lowercase)
|
||
|
|
.collect::<String>();
|
||
|
|
|
||
|
|
const SENSITIVE_SUFFIXES: &[&str] = &[
|
||
|
|
"token",
|
||
|
|
"accesstoken",
|
||
|
|
"refreshtoken",
|
||
|
|
"apikey",
|
||
|
|
"secret",
|
||
|
|
"clientsecret",
|
||
|
|
"password",
|
||
|
|
"passwd",
|
||
|
|
"privatekey",
|
||
|
|
"cookie",
|
||
|
|
"authorization",
|
||
|
|
];
|
||
|
|
|
||
|
|
SENSITIVE_SUFFIXES
|
||
|
|
.iter()
|
||
|
|
.any(|suffix| normalized == *suffix || normalized.ends_with(suffix))
|
||
|
|
}
|
||
|
|
|
||
|
|
fn key_value_regex() -> &'static Regex {
|
||
|
|
static INSTANCE: OnceLock<Regex> = OnceLock::new();
|
||
|
|
INSTANCE.get_or_init(|| {
|
||
|
|
Regex::new(
|
||
|
|
r#"(?im)(?P<prefix>[\"']?(?:access[_-]?token|refresh[_-]?token|api[_-]?key|client[_-]?secret|private[_-]?key|password|passwd|secret|cookie|authorization)[\"']?\s*[:=]\s*)(?:\"[^\"\r\n]*\"|'[^'\r\n]*'|[^\s,;#\r\n]+)"#,
|
||
|
|
)
|
||
|
|
.expect("the sensitive key regex is valid")
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
fn bearer_regex() -> &'static Regex {
|
||
|
|
static INSTANCE: OnceLock<Regex> = OnceLock::new();
|
||
|
|
INSTANCE.get_or_init(|| {
|
||
|
|
Regex::new(r"(?i)\b(?P<prefix>bearer\s+)[A-Za-z0-9._~+/=-]{8,}")
|
||
|
|
.expect("the bearer regex is valid")
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
fn url_credentials_regex() -> &'static Regex {
|
||
|
|
static INSTANCE: OnceLock<Regex> = OnceLock::new();
|
||
|
|
INSTANCE.get_or_init(|| {
|
||
|
|
Regex::new(r"(?i)\b(?P<scheme>[a-z][a-z0-9+.-]*://)[^/@\s:]+:[^/@\s]+@")
|
||
|
|
.expect("the URL credential regex is valid")
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
fn pem_private_key_regex() -> &'static Regex {
|
||
|
|
static INSTANCE: OnceLock<Regex> = OnceLock::new();
|
||
|
|
INSTANCE.get_or_init(|| {
|
||
|
|
Regex::new(
|
||
|
|
r"(?is)-----BEGIN(?: [A-Z0-9]+)* PRIVATE KEY-----.*?-----END(?: [A-Z0-9]+)* PRIVATE KEY-----",
|
||
|
|
)
|
||
|
|
.expect("the PEM private key regex is valid")
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
#[cfg(test)]
|
||
|
|
mod tests {
|
||
|
|
use super::*;
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn redacts_known_and_pattern_based_secrets() {
|
||
|
|
let canary = format!("canary-{}", uuid::Uuid::new_v4());
|
||
|
|
let input = format!(
|
||
|
|
"token={canary}\nAuthorization: Bearer abcdefghijklmnop\nurl=https://alice:password@example.test/path\npassword = hunter2"
|
||
|
|
);
|
||
|
|
let redacted = Redactor::new([&canary]).redact(&input);
|
||
|
|
|
||
|
|
assert!(!redacted.contains(&canary));
|
||
|
|
assert!(!redacted.contains("abcdefghijklmnop"));
|
||
|
|
assert!(!redacted.contains("alice:password"));
|
||
|
|
assert!(!redacted.contains("hunter2"));
|
||
|
|
assert!(redacted.contains(REDACTED));
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn recognizes_normalized_sensitive_keys() {
|
||
|
|
assert!(is_sensitive_key("database_password"));
|
||
|
|
assert!(is_sensitive_key("api-key"));
|
||
|
|
assert!(is_sensitive_key("Refresh.Token"));
|
||
|
|
assert!(!is_sensitive_key("token_count"));
|
||
|
|
assert!(!is_sensitive_key("username"));
|
||
|
|
}
|
||
|
|
}
|