Skip to main content

apollo_redaction/strategy/
password.rs

1use crate::RedactionStrategy;
2use std::fmt;
3
4/// Redact passwords with a fixed-length output.
5///
6/// Always outputs `********` regardless of the actual password length.
7/// This prevents leaking information about password length.
8///
9/// ```rust
10/// use apollo_redaction::Redacted;
11/// use apollo_redaction::strategy::PasswordRedactor;
12///
13/// fn password(input: &str) -> String {
14///     Redacted::<_, PasswordRedactor>::new(input).to_string()
15/// }
16///
17/// assert_eq!(password("short"), "********");
18/// assert_eq!(password("a_very_long_password_123!"), "********");
19/// assert_eq!(password("x"), "********");
20/// ```
21#[derive(Default, Clone)]
22pub struct PasswordRedactor;
23
24impl<T> RedactionStrategy<T> for PasswordRedactor {
25    fn display(&self, _value: &T, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        write!(f, "********")
27    }
28}
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33    use crate::Redacted;
34
35    fn password(input: &str) -> String {
36        Redacted::<_, PasswordRedactor>::new(input).to_string()
37    }
38
39    #[test]
40    fn test_fixed_length() {
41        assert_eq!(password("short"), "********");
42        assert_eq!(password("a_very_long_password_123!"), "********");
43        assert_eq!(password("x"), "********");
44        assert_eq!(password(""), "********");
45    }
46}