use super::line::quote;
const REDACTED_JSON: &str = "\"[redacted]\"";
pub const SECRETS: &[&str] = &[
"password",
"passwd",
"passphrase",
"secret",
"token",
"credential",
"credentials",
"authorization",
"cookie",
"api key",
"private key",
"secret key",
"access key",
"signing key",
"encryption key",
"connection string",
"dsn",
];
#[derive(Clone, Debug, Default)]
pub(crate) struct Redactor {
names: Vec<String>,
hide_url_passwords: bool,
}
impl Redactor {
pub(crate) fn new<S: AsRef<str>>(names: &[S], keep_url_passwords: bool) -> Redactor {
let names =
names.iter().map(|name| words(name.as_ref()).concat()).filter(|joined| !joined.is_empty()).collect();
Redactor { names, hide_url_passwords: !keep_url_passwords }
}
pub(crate) fn fields(&self, fields: &mut [(String, String)]) {
if self.names.is_empty() && !self.hide_url_passwords {
return;
}
for (key, value) in fields.iter_mut() {
if self.hides(key) {
REDACTED_JSON.clone_into(value);
} else if let Some(hidden) = self.value(value) {
*value = hidden;
}
}
}
fn hides(&self, key: &str) -> bool {
if self.names.is_empty() {
return false;
}
let words = words(key);
self.names.iter().any(|name| joins_to(&words, name))
}
fn value(&self, spelled: &str) -> Option<String> {
let inner = (!self.names.is_empty()).then(|| self.json(spelled)).flatten();
let spelled = inner.as_deref().unwrap_or(spelled);
let urls = self.hide_url_passwords.then(|| hide_url_passwords(spelled)).flatten();
urls.or(inner)
}
fn json(&self, spelled: &str) -> Option<String> {
let trimmed = spelled.trim();
if !trimmed.starts_with(['{', '[']) {
return None;
}
let mut out = String::with_capacity(spelled.len());
let mut walk = Walk { text: trimmed.as_bytes(), at: 0 };
let changed = self.rewrite(&mut walk, &mut out)?;
(changed && walk.at == trimmed.len()).then_some(out)
}
fn rewrite(&self, walk: &mut Walk<'_>, out: &mut String) -> Option<bool> {
walk.space();
let open = walk.peek()?;
if open != b'{' && open != b'[' {
let value = walk.value()?;
out.push_str(value);
return Some(false);
}
walk.at += 1;
out.push(char::from(open));
let (close, mut changed, mut first) = (if open == b'{' { b'}' } else { b']' }, false, true);
loop {
walk.space();
if walk.peek()? == close {
walk.at += 1;
out.push(char::from(close));
return Some(changed);
}
if !first {
walk.expect(b',')?;
out.push(',');
walk.space();
}
first = false;
let hide = if open == b'{' {
let key = walk.string()?;
quote(out, &key);
walk.space();
walk.expect(b':')?;
out.push(':');
self.hides(&key)
} else {
false
};
if hide {
walk.space();
walk.value()?;
out.push_str(REDACTED_JSON);
changed = true;
} else {
changed |= self.rewrite(walk, out)?;
}
}
}
}
fn words(key: &str) -> Vec<String> {
let chars: Vec<char> = key.chars().collect();
let (mut out, mut start) = (Vec::new(), None);
for (at, c) in chars.iter().enumerate() {
if !c.is_alphanumeric() {
if let Some(from) = start.take() {
out.push(lower(&chars[from..at]));
}
continue;
}
if let Some(from) = start
&& crate::text::starts_word(&chars, at)
{
out.push(lower(&chars[from..at]));
start = Some(at);
}
start.get_or_insert(at);
}
if let Some(from) = start {
out.push(lower(&chars[from..]));
}
out
}
fn lower(chars: &[char]) -> String {
chars.iter().collect::<String>().to_lowercase()
}
fn joins_to(words: &[String], name: &str) -> bool {
(0..words.len()).any(|from| {
let mut rest = name;
for word in &words[from..] {
let Some(after) = rest.strip_prefix(word.as_str()) else { return false };
rest = after;
if rest.is_empty() {
return true;
}
}
false
})
}
struct Walk<'a> {
text: &'a [u8],
at: usize,
}
impl<'a> Walk<'a> {
fn space(&mut self) {
while self.text.get(self.at).is_some_and(|byte| byte.is_ascii_whitespace()) {
self.at += 1;
}
}
fn peek(&self) -> Option<u8> {
self.text.get(self.at).copied()
}
fn expect(&mut self, byte: u8) -> Option<()> {
(self.peek()? == byte).then(|| self.at += 1)
}
fn string(&mut self) -> Option<String> {
let start = self.at;
self.skip_string()?;
serde_json::from_slice(&self.text[start..self.at]).ok()
}
fn skip_string(&mut self) -> Option<()> {
self.expect(b'"')?;
loop {
match self.peek()? {
b'"' => {
self.at += 1;
return Some(());
}
b'\\' => self.at += 2,
_ => self.at += 1,
}
}
}
fn value(&mut self) -> Option<&'a str> {
let start = self.at;
match self.peek()? {
b'"' => self.skip_string()?,
b'{' | b'[' => {
let mut depth = 0_usize;
loop {
match self.peek()? {
b'"' => {
self.skip_string()?;
continue;
}
b'{' | b'[' => depth += 1,
b'}' | b']' => {
depth -= 1;
if depth == 0 {
self.at += 1;
break;
}
}
_ => {}
}
self.at += 1;
}
}
_ => {
while self.peek().is_some_and(|byte| !matches!(byte, b',' | b'}' | b']') && !byte.is_ascii_whitespace())
{
self.at += 1;
}
}
}
std::str::from_utf8(&self.text[start..self.at]).ok().filter(|value| !value.is_empty())
}
}
fn hide_url_passwords(spelled: &str) -> Option<String> {
if !spelled.contains("://") && !spelled.contains('\\') {
return None;
}
let bytes = spelled.as_bytes();
let (mut out, mut written, mut at) = (String::new(), 0, 0);
while at < bytes.len() {
if bytes[at] != b'"' {
at += 1;
continue;
}
let mut walk = Walk { text: bytes, at };
let text = walk.string()?;
if let Some(hidden) = hide_url_text_passwords(&text) {
out.push_str(&spelled[written..at]);
quote(&mut out, &hidden);
written = walk.at;
}
at = walk.at;
}
(written > 0).then(|| out + &spelled[written..])
}
fn hide_url_text_passwords(text: &str) -> Option<String> {
let (mut out, mut written, mut from) = (String::new(), 0, 0);
while let Some(found) = text[from..].find("://") {
let scheme_end = from + found;
let (password, at, end) = find_password(text, scheme_end + 3);
from = end;
if password.is_none() || !ends_in_scheme(&text[..scheme_end]) {
continue;
}
let (password, at) = (password?, at?);
out.push_str(&text[written..password]);
out.push_str("[redacted]");
written = at;
}
(written > 0).then(|| out + &text[written..])
}
fn find_password(text: &str, start: usize) -> (Option<usize>, Option<usize>, usize) {
let bytes = text.as_bytes();
let (mut at, mut colon, mut end) = (None, None, start);
while end < bytes.len() {
let c = bytes[end];
if c == b'/' || c == b'?' || c == b'#' || c <= b' ' {
break;
}
if c == b'@' {
at = Some(end);
} else if c == b':' && colon.is_none() {
colon = Some(end);
}
end += 1;
}
match (colon, at) {
(Some(colon), Some(at)) if colon + 1 < at => (Some(colon + 1), Some(at), end),
_ => (None, None, end),
}
}
fn ends_in_scheme(text: &str) -> bool {
let bytes = text.as_bytes();
let mut from = bytes.len();
while from > 0 && (bytes[from - 1].is_ascii_alphanumeric() || matches!(bytes[from - 1], b'+' | b'-' | b'.')) {
from -= 1;
}
from < bytes.len() && bytes[from].is_ascii_alphabetic()
}