#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Credential {
pub id: &'static str,
pub denied: bool,
owner: Owner,
}
#[derive(Debug, Clone, PartialEq, Eq)]
enum Owner {
Hosts(&'static [&'static str]),
Named(String),
Nobody,
}
impl Credential {
#[must_use]
pub fn owns(&self, host: &str) -> bool {
let host = host.trim_end_matches('.').to_ascii_lowercase();
match &self.owner {
Owner::Hosts(hosts) => hosts.iter().any(|own| {
host == *own
|| host
.strip_suffix(own)
.is_some_and(|rest| rest.ends_with('.'))
}),
Owner::Named(own) => host == *own,
Owner::Nobody => false,
}
}
}
const AWS_HOSTS: &[&str] = &["amazonaws.com", "amazonaws.com.cn", "aws.amazon.com"];
const AWS_SECRET_NAMES: &[&str] = &["secret_access_key", "secret-access-key", "secretaccesskey"];
const AWS_SECRET_LEN: usize = 40;
const DATABASE_SCHEMES: &[&str] = &[
"postgres",
"postgresql",
"mysql",
"mariadb",
"mongodb",
"mongodb+srv",
"redis",
"rediss",
"amqp",
"amqps",
"mssql",
"sqlserver",
"cockroachdb",
];
const JWT_PART_MIN: usize = 10;
#[must_use]
pub fn in_text(text: &str) -> Vec<Credential> {
let mut found = Vec::new();
if aws_secret(text) {
found.push(Credential {
id: "aws-secret",
denied: true,
owner: Owner::Hosts(AWS_HOSTS),
});
}
if private_key(text) {
found.push(Credential {
id: "private-key",
denied: true,
owner: Owner::Nobody,
});
}
if jwt(text) {
found.push(Credential {
id: "jwt",
denied: false,
owner: Owner::Nobody,
});
}
for host in database_hosts(text) {
let credential = Credential {
id: "database-url",
denied: false,
owner: Owner::Named(host),
};
if !found.contains(&credential) {
found.push(credential);
}
}
found
}
fn aws_alphabet(b: u8) -> bool {
b.is_ascii_alphanumeric() || b == b'/' || b == b'+'
}
fn aws_secret(text: &str) -> bool {
let lower = text.to_ascii_lowercase();
let bytes = lower.as_bytes();
let raw = text.as_bytes();
AWS_SECRET_NAMES.iter().any(|name| {
lower.match_indices(name).any(|(at, _)| {
let mut i = at + name.len();
let skip = |i: &mut usize, set: &[u8]| {
while bytes.get(*i).is_some_and(|b| set.contains(b)) {
*i += 1;
}
};
skip(&mut i, b"\"' \t");
if !matches!(bytes.get(i), Some(b'=' | b':')) {
return false;
}
i += 1;
skip(&mut i, b" \t\"'");
let run = raw[i..].iter().take_while(|b| aws_alphabet(**b)).count();
run == AWS_SECRET_LEN && raw.get(i + run) != Some(&b'=')
})
})
}
fn private_key(text: &str) -> bool {
const BEGIN: &str = "-----BEGIN ";
text.match_indices(BEGIN).any(|(at, _)| {
let rest = &text[at + BEGIN.len()..];
let label: String = rest
.chars()
.take_while(|c| c.is_ascii_uppercase() || c.is_ascii_digit() || *c == ' ')
.collect();
let after = &rest[label.len()..];
after.starts_with("-----")
&& (label.ends_with("PRIVATE KEY") || label.ends_with("PRIVATE KEY BLOCK"))
})
}
fn base64url(b: u8) -> bool {
b.is_ascii_alphanumeric() || b == b'_' || b == b'-'
}
fn jwt(text: &str) -> bool {
let bytes = text.as_bytes();
text.match_indices("eyJ").any(|(at, _)| {
if at > 0 && base64url(bytes[at - 1]) {
return false;
}
let part = |from: usize| bytes[from..].iter().take_while(|b| base64url(**b)).count();
let header = part(at);
let payload_at = at + header + 1;
if header < JWT_PART_MIN
|| bytes.get(at + header) != Some(&b'.')
|| !text[payload_at..].starts_with("eyJ")
{
return false;
}
let payload = part(payload_at);
let signature_at = payload_at + payload + 1;
payload >= JWT_PART_MIN
&& bytes.get(payload_at + payload) == Some(&b'.')
&& part(signature_at) >= JWT_PART_MIN
})
}
fn database_hosts(text: &str) -> Vec<String> {
let mut hosts = Vec::new();
for (at, _) in text.match_indices("://") {
let scheme_start = text[..at]
.rfind(|c: char| !(c.is_ascii_alphanumeric() || "+.-".contains(c)))
.map_or(0, |i| i + 1);
let scheme = text[scheme_start..at].to_ascii_lowercase();
if !DATABASE_SCHEMES.contains(&scheme.as_str()) {
continue;
}
let rest = &text[at + 3..];
let end = rest
.find(|c: char| "/?#\"'` \t\n".contains(c))
.unwrap_or(rest.len());
let authority = &rest[..end];
let Some((login, host)) = authority.rsplit_once('@') else {
continue;
};
let Some((_, password)) = login.split_once(':') else {
continue;
};
if password.is_empty() || password.starts_with(['$', '*', '<', '{']) {
continue;
}
let host = host.split(',').next().unwrap_or(host);
let host = host
.rsplit_once(':')
.filter(|(_, port)| port.bytes().all(|b| b.is_ascii_digit()))
.map_or(host, |(name, _)| name);
let host = host.trim_matches(['[', ']']).to_ascii_lowercase();
if !host.is_empty() && !hosts.contains(&host) {
hosts.push(host);
}
}
hosts
}
#[cfg(test)]
mod tests;