pub(crate) const REDACTED: &str = "[REDACTED]";
#[derive(Debug, Clone)]
pub(crate) struct StreamRedactor {
secrets: Vec<String>,
window_chars: usize,
pending: String,
}
impl StreamRedactor {
pub(crate) fn new(secrets: impl IntoIterator<Item = String>) -> Self {
let mut unique: Vec<String> = secrets
.into_iter()
.filter(|secret| !secret.is_empty())
.collect();
unique.sort_by(|left, right| right.len().cmp(&left.len()).then_with(|| left.cmp(right)));
unique.dedup();
let window_chars = unique
.iter()
.map(|secret| secret.chars().count())
.max()
.unwrap_or(0)
.saturating_sub(1);
Self {
secrets: unique,
window_chars,
pending: String::new(),
}
}
pub(crate) fn push(&mut self, chunk: &str) -> String {
if self.secrets.is_empty() {
return chunk.to_owned();
}
self.pending.push_str(chunk);
self.redact_pending();
let total = self.pending.chars().count();
if total <= self.window_chars {
return String::new();
}
let emit_chars = total - self.window_chars;
let split_byte = self
.pending
.char_indices()
.nth(emit_chars)
.map_or(self.pending.len(), |(index, _)| index);
let tail = self.pending.split_off(split_byte);
std::mem::replace(&mut self.pending, tail)
}
pub(crate) fn flush(&mut self) -> String {
if self.secrets.is_empty() {
return std::mem::take(&mut self.pending);
}
self.redact_pending();
std::mem::take(&mut self.pending)
}
pub(crate) fn redact_all(&self, text: &str) -> String {
if self.secrets.is_empty() || text.is_empty() {
return text.to_owned();
}
let mut copy = Self {
secrets: self.secrets.clone(),
window_chars: self.window_chars,
pending: String::new(),
};
let mut out = copy.push(text);
out.push_str(©.flush());
out
}
fn redact_pending(&mut self) {
for secret in &self.secrets {
if self.pending.contains(secret.as_str()) {
self.pending = self.pending.replace(secret.as_str(), REDACTED);
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn stream_redactor_preserves_lines_and_catches_cross_chunk_secrets() {
let mut redactor = StreamRedactor::new(["super-secret-token-123".to_owned()]);
let mut output = String::new();
for chunk in ["line1\nkey=super-", "secret-", "token-123\nline3"] {
output.push_str(&redactor.push(chunk));
}
output.push_str(&redactor.flush());
assert_eq!(output, "line1\nkey=[REDACTED]\nline3");
}
}