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'%') if end - start > 2 && b[start..start + 2].iter().all(u8::is_ascii_hexdigit) => {
start + 2
}
_ => start,
}
}
struct Name {
end: usize,
password: bool,
phrase: bool,
flag: bool,
reference_like: bool,
}
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 all_caps = !run.iter().any(u8::is_ascii_lowercase);
let password_word = |w: &str| is_password_word(w, all_caps);
let secret_word = |i: usize| is_secret_word(&words, i, all_caps);
let first = (0..words.len()).find(|&i| secret_word(i))?;
if words[first + 1..].iter().any(|w| names_metadata(w))
|| words[..first].iter().any(|w| is_metadata_qualifier(w))
{
return None;
}
let flag = run[0] == b'-';
Some(Name {
end,
password: words.iter().any(|w| password_word(w)),
phrase: words.last().is_some_and(|w| password_word(w)),
flag,
reference_like: (start > 0 && matches!(b[start - 1], b'/' | b':'))
|| (!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, all_caps: bool) -> bool {
forms(w)
.iter()
.any(|f| *f == "pass" || *f == "pwd" || PASSWORD_SUFFIXES.iter().any(|s| f.ends_with(s)))
|| (all_caps && PASSWORD_SUFFIXES.iter().any(|n| glued(w, n, |_| true)))
}
fn is_secret_word(words: &[String], i: usize, all_caps: bool) -> bool {
let w = words[i].as_str();
let qualified = |q: &str| {
let q = if q.is_empty() && i > 0 {
words[i - 1].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, all_caps)
|| forms(w)
.iter()
.any(|f| SECRET_SUFFIXES.iter().any(|s| f.ends_with(s)) || qualified_key(f))
|| (all_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))
}
fn words(name: &[u8]) -> Vec<String> {
let mut words = Vec::new();
let mut word = String::new();
let mut finish = |word: &mut String| {
let trimmed = word.trim_end_matches(|c: char| c.is_ascii_digit());
if !trimmed.is_empty() {
words.push(trimmed.to_string());
}
word.clear();
};
for (i, &c) in name.iter().enumerate() {
if c == b'_' || c == b'-' {
finish(&mut word);
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);
}
word.push(c.to_ascii_lowercase() as char);
}
finish(&mut word);
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 starts_with_ignore_case(rest, b"%3d") {
(Sep::Assign, at + 3)
} else if starts_with_ignore_case(rest, b"%3a") {
(Sep::Colon, at + 3)
} 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) {
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)?;
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;
}
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 starts_with_ignore_case(hay: &[u8], prefix: &[u8]) -> bool {
hay.len() >= prefix.len() && hay[..prefix.len()].eq_ignore_ascii_case(prefix)
}
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 unquoted_end(b: &[u8], j: 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 b
.get(k + 1..k + 3)
.is_some_and(|w| w.eq_ignore_ascii_case(b"26")) =>
{
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 matches!(owner, b"env" | b"config" | b"environ") => 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 ident = |b: u8| b.is_ascii_alphanumeric() || b == b'_' || b == b'-';
let tag = ["input", "button", "form", "select", "textarea", "label"]
.iter()
.find(|t| s.starts_with(*t))
.map_or(0, |t| t.len());
let rest = &s[tag..];
let selector = rest.starts_with(['#', '.', '['])
&& rest.bytes().all(|b| ident(b) || b"#.[]=\"' ".contains(&b));
let chain = (s.contains("?.") || s.contains("!."))
&& s.bytes().all(|b| ident(b) || b"?!.".contains(&b));
selector || chain
}
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();
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)
&& !words.iter().any(|w| PROSE_WORDS.contains(w))
}
const PROSE_WORDS: [&str; 65] = [
"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",
];
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)
}