use super::{
Finding, MAX_VALUE, SecretKind, TYPE_NAME, has_digit, has_random_run, is_placeholder, marker,
record,
};
pub(super) fn redact_assignments(text: &str, findings: &mut Vec<Finding>) -> Option<String> {
let bytes = text.as_bytes();
let mut out = String::new();
let (mut last, mut pos, mut count) = (0, 0, 0);
while let Some((run_start, end)) = next_run(bytes, pos) {
pos = end;
let start = name_start(bytes, run_start, end);
let Some(name) = Name::classify(bytes, start, end) else {
continue;
};
let Some(value) = assigned_value(bytes, &name) else {
continue;
};
if value.end - value.start > MAX_VALUE {
pos = pos.max(value.end);
}
let secret = &text[value.start..value.end];
if value.start < last
|| text[value.end..].starts_with("[REDACTED:")
|| !is_secret(&name, &value, secret)
{
continue;
}
out.push_str(&text[last..value.start]);
out.push_str(&marker(SecretKind::Secret));
last = value.end;
pos = value.end;
count += 1;
}
if count == 0 {
return None;
}
out.push_str(&text[last..]);
record(findings, SecretKind::Secret, count);
Some(out)
}
fn is_name_byte(b: u8) -> bool {
b.is_ascii_alphanumeric() || b == b'_' || b == b'-'
}
fn next_run(b: &[u8], from: usize) -> Option<(usize, usize)> {
let start = from + b.get(from..)?.iter().position(|&c| is_name_byte(c))?;
let len = b[start..].iter().take_while(|&&c| is_name_byte(c)).count();
Some((start, start + len))
}
fn name_start(b: &[u8], start: usize, end: usize) -> usize {
match start.checked_sub(1).map(|i| b[i]) {
Some(b'\\') if matches!(b[start], b'n' | b'r' | b't') => start + 1,
Some(b'%') => match percent_decoded(b, start - 1) {
Some((_, len)) if start - 1 + len < end => start - 1 + len,
_ => start,
},
_ => start,
}
}
fn percent_decoded(b: &[u8], i: usize) -> Option<(u8, usize)> {
let hex = |j: usize| {
let pair = b.get(j..j + 2)?;
let digit = |c: u8| char::from(c).to_digit(16);
Some((digit(pair[0])? * 16 + digit(pair[1])?) as u8)
};
if b.get(i) != Some(&b'%') {
return None;
}
match hex(i + 1)? {
b'%' => Some(hex(i + 3).map_or((b'%', 3), |c| (c, 5))),
c => Some((c, 3)),
}
}
struct Name {
end: usize,
password: bool,
phrase: bool,
flag: bool,
reference_like: bool,
}
fn after_image_key(b: &[u8], start: usize) -> bool {
let mut i = start;
if i > 0 && matches!(b[i - 1], b'"' | b'\'') {
i -= 1;
}
i -= b[..i]
.iter()
.rev()
.take_while(|&&c| c == b' ' || c == b'\t')
.count();
if i == 0 || b[i - 1] != b':' {
return false;
}
i -= 1;
if i > 0 && matches!(b[i - 1], b'"' | b'\'') {
i -= 1;
}
let key = b"image";
i >= key.len()
&& b[i - key.len()..i].eq_ignore_ascii_case(key)
&& (i == key.len() || !is_name_byte(b[i - key.len() - 1]))
}
const PASSWORD_SUFFIXES: [&str; 3] = ["password", "passwd", "passphrase"];
const SECRET_SUFFIXES: [&str; 4] = ["secret", "token", "credential", "credentials"];
const KEY_QUALIFIERS: [&str; 8] = [
"api",
"access",
"private",
"auth",
"encryption",
"signing",
"master",
"secret",
];
impl Name {
fn classify(b: &[u8], start: usize, end: usize) -> Option<Name> {
let run = &b[start..end];
let mentions = ["pass", "pwd", "secret", "token", "credential", "key"]
.iter()
.any(|needle| contains_ignore_case(run, needle.as_bytes()));
if !mentions {
return None;
}
let words = words(run);
let password_word = |w: &Word| is_password_word(&w.text, w.caps);
let secret_word = |i: usize| is_secret_word(&words, i);
let first = (0..words.len()).find(|&i| secret_word(i))?;
if words[first + 1..].iter().any(|w| names_metadata(&w.text))
|| words[..first]
.iter()
.any(|w| is_metadata_qualifier(&w.text))
{
return None;
}
let flag = run[0] == b'-';
Some(Name {
end,
password: words.iter().any(password_word),
phrase: words.last().is_some_and(password_word),
flag,
reference_like: (start > 0 && matches!(b[start - 1], b'/' | b':'))
|| after_image_key(b, start)
|| (!flag && (run.contains(&b'-') || !(first + 1..words.len()).all(secret_word))),
})
}
}
fn forms(w: &str) -> [&str; 2] {
[w, w.strip_suffix('s').unwrap_or(w)]
}
fn names_metadata(w: &str) -> bool {
forms(w).iter().any(|f| is_metadata(f))
}
fn glued(w: &str, needle: &str, before_ok: impl Fn(&str) -> bool) -> bool {
w.match_indices(needle).any(|(p, _)| {
let rest = &w[p + needle.len()..];
before_ok(&w[..p]) && (rest.is_empty() || !names_metadata(rest))
})
}
fn is_password_word(w: &str, caps: bool) -> bool {
forms(w)
.iter()
.any(|f| *f == "pass" || *f == "pwd" || PASSWORD_SUFFIXES.iter().any(|s| f.ends_with(s)))
|| (caps && PASSWORD_SUFFIXES.iter().any(|n| glued(w, n, |_| true)))
}
fn is_secret_word(words: &[Word], i: usize) -> bool {
let Word { text: w, caps } = &words[i];
let qualified = |q: &str| {
let q = if q.is_empty() && i > 0 {
words[i - 1].text.as_str()
} else {
q
};
KEY_QUALIFIERS.iter().any(|k| q.ends_with(k))
};
let qualified_key = |f: &str| f.strip_suffix("key").is_some_and(qualified);
is_password_word(w, *caps)
|| forms(w)
.iter()
.any(|f| SECRET_SUFFIXES.iter().any(|s| f.ends_with(s)) || qualified_key(f))
|| (*caps
&& (SECRET_SUFFIXES.iter().any(|n| glued(w, n, |_| true))
|| glued(w, "key", qualified)))
}
fn contains_ignore_case(hay: &[u8], needle: &[u8]) -> bool {
hay.windows(needle.len())
.any(|w| w.eq_ignore_ascii_case(needle))
}
struct Word {
text: String,
caps: bool,
}
fn words(name: &[u8]) -> Vec<Word> {
let mut words = Vec::new();
let mut word = String::new();
let mut caps = true;
let mut finish = |word: &mut String, caps: &mut bool| {
let trimmed = word.trim_end_matches(|c: char| c.is_ascii_digit());
if !trimmed.is_empty() {
words.push(Word {
text: trimmed.to_string(),
caps: *caps,
});
}
word.clear();
*caps = true;
};
for (i, &c) in name.iter().enumerate() {
if c == b'_' || c == b'-' {
finish(&mut word, &mut caps);
continue;
}
let prev = i.checked_sub(1).map(|p| name[p]);
let next_lower = name.get(i + 1).is_some_and(u8::is_ascii_lowercase);
let boundary = c.is_ascii_uppercase()
&& prev.is_some_and(|p| {
p.is_ascii_lowercase()
|| p.is_ascii_digit()
|| (p.is_ascii_uppercase() && next_lower)
});
if boundary {
finish(&mut word, &mut caps);
}
caps &= !c.is_ascii_lowercase();
word.push(c.to_ascii_lowercase() as char);
}
finish(&mut word, &mut caps);
words
}
fn is_metadata_qualifier(word: &str) -> bool {
matches!(word, "idempotency" | "correlation" | "trace")
}
fn is_metadata(word: &str) -> bool {
matches!(
word,
"url"
| "uri"
| "arn"
| "class"
| "id"
| "ids"
| "type"
| "version"
| "count"
| "file"
| "path"
| "dir"
| "name"
| "expires"
| "expiry"
| "length"
| "len"
| "size"
| "limit"
| "hash"
| "kind"
| "format"
| "prefix"
| "header"
| "endpoint"
| "field"
| "stdin"
| "digest"
| "ref"
| "refs"
| "request"
| "prompt"
| "hint"
| "label"
| "placeholder"
| "message"
| "help"
)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Sep {
Typed,
Assign,
Colon,
Compare,
Space,
}
struct Value {
start: usize,
end: usize,
sep: Sep,
quoted: bool,
closed: bool,
crosses_line: bool,
glued: bool,
code: bool,
}
fn assigned_value(b: &[u8], name: &Name) -> Option<Value> {
let tail = name_tail(b, name.end);
let at = skip_blanks(b, tail);
let rest = &b[at..];
let (sep, after) = if rest.starts_with(b"===") || rest.starts_with(b"!==") {
(Sep::Compare, at + 3)
} else if rest.starts_with(b"==") || rest.starts_with(b"!=") {
(Sep::Compare, at + 2)
} else if rest.starts_with(b":=") || rest.starts_with(b"=>") {
(Sep::Assign, at + 2)
} else if rest.starts_with(b"=") {
(Sep::Assign, at + 1)
} else if rest.starts_with(b"::") {
return None;
} else if rest.starts_with(b":") {
(Sep::Colon, at + 1)
} else if let Some((c @ (b'=' | b':'), len)) = percent_decoded(b, at) {
let sep = if c == b'=' { Sep::Assign } else { Sep::Colon };
(sep, at + len)
} else if name.flag && at > tail {
(Sep::Space, at)
} else {
return None;
};
if sep == Sep::Colon
&& let Some(value) = typed_value(b, after, name.password)
{
return Some(value);
}
let start = skip_gap(b, after, sep != Sep::Space);
let crosses_line = b[after..start].contains(&b'\n');
let glued = start == after;
if let Some(q) = quoted_at(b, start, name.password).or_else(|| encoded_quoted_at(b, start)) {
return Some(Value {
start: q.start,
end: q.end,
sep,
quoted: true,
closed: q.after.is_some(),
crosses_line,
glued,
code: false,
});
}
if sep == Sep::Compare || (sep == Sep::Space && b.get(start) == Some(&b'-')) {
return None;
}
let end = unquoted_end(b, start, None)?;
let call = b.get(end) == Some(&b'(');
if call
&& end - start <= MAX_VALUE
&& let Some(q) = call_literal(b, &b[start..end], end)
{
return Some(Value {
start: q.start,
end: q.end,
sep,
quoted: true,
closed: true,
crosses_line,
glued,
code: false,
});
}
Some(Value {
start,
end,
sep,
quoted: false,
closed: false,
crosses_line,
glued,
code: call
|| then_assigned(b, end)
|| (sep == Sep::Space && is_key_value_spec(&b[start..end])),
})
}
fn is_key_value_spec(value: &[u8]) -> bool {
value
.iter()
.position(|&c| c == b'=')
.is_some_and(|p| p > 0 && value[p + 1..].iter().any(|&c| c != b'='))
}
fn name_tail(b: &[u8], mut i: usize) -> usize {
if b.get(i) == Some(&b'\\') && matches!(b.get(i + 1), Some(b'"' | b'\'')) {
i += 2;
} else if matches!(b.get(i), Some(b'"' | b'\'')) {
i += 1;
} else if let Some((b'"' | b'\'', len)) = percent_decoded(b, i) {
i += len;
}
if b.get(i) == Some(&b']') {
i += 1;
}
if b.get(i) == Some(&b'?') {
i += 1;
}
i
}
fn skip_blanks(b: &[u8], i: usize) -> usize {
i + b[i..]
.iter()
.take_while(|&&c| c == b' ' || c == b'\t')
.count()
}
fn skip_gap(b: &[u8], i: usize, cross_line: bool) -> usize {
let i = skip_blanks(b, i);
if !cross_line {
return i;
}
let rest = &b[i..];
let newline = if rest.starts_with(b"\r\n") {
2
} else {
usize::from(rest.starts_with(b"\n"))
};
if newline == 0 {
i
} else {
skip_blanks(b, i + newline)
}
}
fn typed_value(b: &[u8], after_colon: usize, password: bool) -> Option<Value> {
let ty = skip_blanks(b, after_colon);
if !b
.get(ty)
.is_some_and(|&c| c.is_ascii_alphabetic() || c == b'_' || c == b'&')
{
return None;
}
let len = b[ty..]
.iter()
.take(64)
.take_while(|&&c| c.is_ascii_alphanumeric() || b"_:&'<>[]|,.? \t".contains(&c))
.count();
let eq = ty + len;
if b.get(eq) != Some(&b'=') || matches!(b.get(eq + 1), Some(b'=' | b'>')) {
return None;
}
let start = skip_gap(b, eq + 1, true);
let q = quoted_at(b, start, password)?;
Some(Value {
start: q.start,
end: q.end,
sep: Sep::Typed,
quoted: true,
closed: q.after.is_some(),
crosses_line: b[eq..start].contains(&b'\n'),
glued: false,
code: false,
})
}
struct Quoted {
start: usize,
end: usize,
after: Option<usize>,
}
fn quoted_at(b: &[u8], j: usize, spaces: bool) -> Option<Quoted> {
let escaped = usize::from(b.get(j) == Some(&b'\\'));
let q = *b.get(j + escaped)?;
if q != b'"' && q != b'\'' {
return None;
}
let start = j + escaped + 1;
let mut k = start;
let mut spaced = false;
while k < b.len() {
if spaced && k - start > MAX_VALUE {
break;
}
match b[k] {
c if c == q => {
return Some(Quoted {
start,
end: k,
after: Some(k + 1),
});
}
b'\\' => match b.get(k + 1) {
Some(&c) if c == q => {
return Some(Quoted {
start,
end: k,
after: Some(k + 2),
});
}
Some(&c) if c.is_ascii_whitespace() || matches!(c, b'n' | b'r' | b't') => break,
Some(_) => k += 2,
None => break,
},
b' ' | b'\t' if spaces => {
spaced = true;
k += 1;
}
c if c.is_ascii_whitespace() => break,
_ => k += 1,
}
}
let end = b[start..k]
.iter()
.position(u8::is_ascii_whitespace)
.map_or(k, |p| start + p);
Some(Quoted {
start,
end,
after: None,
})
}
fn is_unquoted_value_byte(b: u8) -> bool {
!b.is_ascii_whitespace() && !b"\"'`,;(){}[]<>|\\&".contains(&b)
}
fn encoded_quoted_at(b: &[u8], mut j: usize) -> Option<Quoted> {
while let Some((b' ', len)) = percent_decoded(b, j) {
j += len;
}
let quote = percent_decoded(b, j).filter(|&(c, _)| c == b'"' || c == b'\'')?;
let start = j + quote.1;
let end = unquoted_end(b, start, Some(quote))?;
Some(Quoted {
start,
end,
after: (percent_decoded(b, end) == Some(quote)).then_some(end + quote.1),
})
}
fn unquoted_end(b: &[u8], j: usize, closing: Option<(u8, usize)>) -> Option<usize> {
let mut k = j;
while k < b.len() {
match b[k] {
b'\\'
if b.get(k + 1).is_some_and(|&c| {
!c.is_ascii_whitespace() && !matches!(c, b'"' | b'\'' | b'n' | b'r' | b't')
}) =>
{
k += 2
}
b'%' if percent_decoded(b, k)
.is_some_and(|escape| escape.0 == b'&' || Some(escape) == closing) =>
{
break;
}
c if is_unquoted_value_byte(c) => k += 1,
_ => break,
}
}
let end = extend_over_ampersands(b, j, k);
(end > j).then_some(end)
}
fn extend_over_ampersands(bytes: &[u8], start: usize, mut end: usize) -> usize {
while bytes.get(end) == Some(&b'&') && end - start < MAX_VALUE {
let segment = &bytes[end + 1..];
let run = segment
.iter()
.take_while(|&&b| is_unquoted_value_byte(b))
.count();
let ident = segment
.iter()
.take_while(|&&b| b.is_ascii_alphanumeric() || b == b'_')
.count();
let next_parameter = ident > 0 && segment.get(ident) == Some(&b'=');
if run == 0 || next_parameter {
break;
}
end += 1 + run;
}
end
}
fn callee(path: &[u8]) -> (&[u8], &[u8]) {
let mut segments = path
.split(|&c| c == b'.' || c == b':')
.filter(|s| !s.is_empty())
.rev();
let last = segments.next().unwrap_or_default();
(segments.next().unwrap_or_default(), last)
}
fn call_literal(b: &[u8], path: &[u8], open: usize) -> Option<Quoted> {
let (owner, callee) = callee(path);
let wanted = match callee {
_ if [&b"env"[..], b"config", b"environ"]
.iter()
.any(|o| owner.eq_ignore_ascii_case(o)) =>
{
1
}
b"from" | b"new" | b"String" | b"str" | b"Some" | b"Secret" | b"SecretStr"
| b"SecretString" => 0,
b"getenv" | b"get" | b"env" | b"config" | b"fetch" | b"setdefault" | b"get_or" => 1,
_ => return None,
};
let (mut depth, mut arg) = (0usize, 0usize);
let mut k = open;
while k < b.len() && k - open <= MAX_VALUE {
match b[k] {
b'(' | b'[' | b'{' => depth += 1,
b')' | b']' | b'}' => {
depth -= 1;
if depth == 0 {
return None;
}
}
b',' if depth == 1 => arg += 1,
b'\n' => return None,
b'"' | b'\'' => {
let q = quoted_at(b, k, depth > 1)?;
let after = q.after?;
if depth == 1 && arg == wanted {
return Some(q);
}
k = after;
continue;
}
_ => {}
}
k += 1;
}
None
}
fn then_assigned(b: &[u8], end: usize) -> bool {
let i = skip_blanks(b, end);
b.get(i) == Some(&b'=') && !matches!(b.get(i + 1), Some(b'=' | b'>'))
}
fn is_secret(name: &Name, value: &Value, s: &str) -> bool {
if value.code
|| s.len() < 8
|| s.contains("[REDACTED:")
|| is_placeholder(s)
|| s.starts_with("#{")
|| s.contains("://")
|| is_timestamp(s)
|| s.starts_with("arn:")
|| is_version(s)
|| is_address(s)
|| is_resource_path(s)
{
return false;
}
if value.crosses_line && (value.sep == Sep::Colon || !value.quoted) {
return false;
}
if value.sep == Sep::Colon && !value.quoted && value.glued && name.reference_like {
return false;
}
if value.sep == Sep::Colon
&& TYPE_NAME.is_match(s)
&& !(name.password && trailing_digits(s) >= 2)
{
return false;
}
if (is_code_path(s) || looks_like_path(s))
&& !has_random_run(s.as_bytes(), 20, |b| b.is_ascii_alphanumeric())
{
return false;
}
if s.contains(' ') {
return name.password
&& value.closed
&& ((name.phrase && is_passphrase(s))
|| s.split(' ')
.any(|w| w.len() >= 8 && has_digit(w) && is_password_like(w)));
}
if name.password && is_password_like(s) {
return true;
}
is_random(s) && !is_word_slug(s)
}
fn is_random(s: &str) -> bool {
(has_digit(s) && s.bytes().any(|b| b.is_ascii_alphabetic()) && shannon_entropy(s) >= 3.0)
|| is_mixed_case_random(s)
}
fn is_password_like(s: &str) -> bool {
is_random(s) || (has_inner_symbol(s) && !mentions_password(s) && !is_selector_or_chain(s))
}
fn mentions_password(s: &str) -> bool {
let lower = s.to_ascii_lowercase();
["password", "passwd", "passphrase"]
.iter()
.any(|w| lower.contains(w))
}
fn is_selector_or_chain(s: &str) -> bool {
let chain = (s.contains("?.") || s.contains("!."))
&& s.bytes().all(|b| is_name_byte(b) || b"?!.".contains(&b));
chain || is_selector(s.as_bytes())
}
const FORM_TAGS: [&[u8]; 6] = [
b"input",
b"button",
b"form",
b"select",
b"textarea",
b"label",
];
const CONTAINER_TAGS: [&[u8]; 10] = [
b"div",
b"span",
b"fieldset",
b"section",
b"main",
b"p",
b"li",
b"ul",
b"a",
b"option",
];
const PSEUDO_CLASSES: [&[u8]; 22] = [
b"focus",
b"focus-visible",
b"focus-within",
b"hover",
b"active",
b"checked",
b"disabled",
b"enabled",
b"invalid",
b"valid",
b"required",
b"optional",
b"placeholder",
b"placeholder-shown",
b"autofill",
b"-webkit-autofill",
b"not",
b"is",
b"where",
b"has",
b"first-child",
b"last-child",
];
fn is_selector(s: &[u8]) -> bool {
let run =
|i: usize, member: fn(u8) -> bool| i + s[i..].iter().take_while(|&&b| member(b)).count();
let mut i = 0;
let mut first = true;
while i < s.len() {
let tag_end = if s[i].is_ascii_lowercase() {
run(i, is_name_byte)
} else {
i
};
let tag = &s[i..tag_end];
let known_tag = FORM_TAGS.contains(&tag) || (!first && CONTAINER_TAGS.contains(&tag));
if !tag.is_empty() && !known_tag {
return false;
}
i = tag_end;
let mut parts = 0;
while let Some(end) = selector_part(s, i) {
if first && parts == 0 && tag.is_empty() && s[i] == b':' {
return false;
}
i = end;
parts += 1;
}
if tag.is_empty() && parts == 0 {
return false;
}
let spaced = run(i, |b| b == b' ');
let combinator = usize::from(s.get(spaced).is_some_and(|b| b">+~,".contains(b)));
let next = run(spaced + combinator, |b| b == b' ');
if next == i && i < s.len() {
return false;
}
if next == s.len() {
return combinator == 0;
}
i = next;
first = false;
}
false
}
fn selector_part(s: &[u8], i: usize) -> Option<usize> {
let within = |from: usize, close: u8, member: fn(u8) -> bool| {
let len = s[from..].iter().take_while(|&&b| member(b)).count();
(len > 0 && s.get(from + len) == Some(&close)).then_some(from + len + 1)
};
match *s.get(i)? {
b'#' | b'.' => {
let len = s[i + 1..].iter().take_while(|&&b| is_name_byte(b)).count();
(len > 0).then_some(i + 1 + len)
}
b'[' => within(i + 1, b']', |b| {
is_name_byte(b) || b"=\"'^$*~| ".contains(&b)
}),
b':' => {
let from = i + 1 + usize::from(s.get(i + 1) == Some(&b':'));
let len = s[from..]
.iter()
.take_while(|&&b| b.is_ascii_lowercase() || b == b'-')
.count();
let end = from + len;
match s.get(end) {
_ if !PSEUDO_CLASSES.contains(&&s[from..end]) => None,
Some(b'(') => within(end + 1, b')', |b| {
is_name_byte(b) || b"#.[]=\"':>+~*, ".contains(&b)
}),
_ => Some(end),
}
}
_ => None,
}
}
fn is_mixed_case_random(s: &str) -> bool {
let upper = s.bytes().filter(u8::is_ascii_uppercase).count();
let flips = s
.as_bytes()
.windows(2)
.filter(|w| w[0].is_ascii_uppercase() != w[1].is_ascii_uppercase())
.count();
s.len() >= 16
&& s.bytes().all(|b| b.is_ascii_alphabetic())
&& upper * 4 >= s.len()
&& upper * 4 <= s.len() * 3
&& flips * 3 >= s.len()
&& shannon_entropy(s) >= 3.5
}
fn is_resource_path(s: &str) -> bool {
s.split('/').count() >= 3
&& s.split('/').all(|part| {
!part.is_empty()
&& part.bytes().all(|b| {
b.is_ascii_lowercase() || b.is_ascii_digit() || matches!(b, b'-' | b'_' | b'.')
})
})
}
fn is_passphrase(s: &str) -> bool {
let words: Vec<&str> = s.split(' ').filter(|w| !w.is_empty()).collect();
let prose = words.iter().filter(|w| PROSE_WORDS.contains(w)).count();
words.len() >= 4
&& words
.iter()
.all(|w| w.bytes().all(|b| b.is_ascii_alphabetic()))
&& !s.starts_with(|c: char| c.is_ascii_uppercase())
&& !mentions_password(s)
&& prose * 2 < words.len()
}
const PROSE_WORDS: [&str; 68] = [
"a",
"an",
"the",
"is",
"are",
"be",
"been",
"not",
"no",
"can",
"cannot",
"must",
"should",
"to",
"of",
"at",
"in",
"on",
"for",
"your",
"you",
"this",
"that",
"and",
"or",
"with",
"does",
"do",
"it",
"has",
"have",
"was",
"will",
"please",
"try",
"again",
"later",
"minimum",
"maximum",
"required",
"character",
"characters",
"invalid",
"wrong",
"incorrect",
"enter",
"too",
"short",
"match",
"least",
"contain",
"contains",
"letter",
"letters",
"number",
"numbers",
"field",
"empty",
"blank",
"weak",
"confirm",
"reset",
"forgot",
"provided",
"missing",
"expired",
"session",
"sign",
];
fn trailing_digits(s: &str) -> usize {
s.bytes().rev().take_while(u8::is_ascii_digit).count()
}
fn has_inner_symbol(s: &str) -> bool {
s.as_bytes()[..s.len() - 1]
.iter()
.any(|&b| b.is_ascii_punctuation() && !b".-_/".contains(&b))
}
fn is_version(s: &str) -> bool {
let s = s.strip_prefix('v').unwrap_or(s);
let major = s.bytes().take_while(u8::is_ascii_digit).count();
major > 0
&& s[major..]
.strip_prefix('.')
.is_some_and(|rest| rest.starts_with(|c: char| c.is_ascii_digit()))
}
fn is_address(s: &str) -> bool {
s.len() == 42 && s.starts_with("0x") && s.bytes().skip(2).all(|b| b.is_ascii_hexdigit())
}
fn shannon_entropy(s: &str) -> f64 {
let mut counts = [0usize; 256];
for b in s.bytes() {
counts[b as usize] += 1;
}
let len = s.len() as f64;
counts
.iter()
.filter(|&&c| c > 0)
.map(|&c| {
let p = c as f64 / len;
-p * p.log2()
})
.sum()
}
fn is_code_path(s: &str) -> bool {
let dotted = s.replace("::", ".");
dotted.contains('.')
&& dotted.split('.').all(|part| {
part.chars()
.next()
.is_some_and(|c| c.is_ascii_alphabetic() || c == '_')
&& part.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
})
}
fn looks_like_path(s: &str) -> bool {
let has_extension = s
.rsplit_once('/')
.and_then(|(_, file)| file.rsplit_once('.'))
.is_some_and(|(_, ext)| (1..=5).contains(&ext.len()));
s.starts_with(['/', '~', '.']) || has_extension
}
fn is_word_slug(s: &str) -> bool {
let dashed = s.contains('-');
s.contains(['-', '_'])
&& s.split(['-', '_']).all(|part| {
!part.is_empty()
&& ((is_cased_word(part)
&& !(dashed && part.starts_with(|c: char| c.is_ascii_uppercase())))
|| part.bytes().all(|b| b.is_ascii_digit())
|| (part.len() <= 3
&& part
.bytes()
.all(|b| b.is_ascii_lowercase() || b.is_ascii_digit())))
})
}
fn is_cased_word(s: &str) -> bool {
let letters = &s.as_bytes()[..s.bytes().take_while(u8::is_ascii_alphabetic).count()];
!letters.is_empty()
&& (letters.iter().all(u8::is_ascii_lowercase)
|| letters.iter().all(u8::is_ascii_uppercase))
&& s.bytes().skip(letters.len()).all(|b| b.is_ascii_digit())
}
fn is_timestamp(s: &str) -> bool {
let b = s.as_bytes();
b.len() >= 10
&& b[..4].iter().all(u8::is_ascii_digit)
&& b[4] == b'-'
&& b[5..7].iter().all(u8::is_ascii_digit)
&& b[7] == b'-'
&& b[8..10].iter().all(u8::is_ascii_digit)
}