obfuscate auth header (#254)

This commit is contained in:
Adil Hafeez 2024-11-08 15:17:39 -06:00 committed by GitHub
parent 88d0f99866
commit 9081eb0f7f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 49 additions and 3 deletions

44
crates/common/src/pii.rs Normal file
View file

@ -0,0 +1,44 @@
pub fn obfuscate_auth_header(headers: &mut [(String, String)]) -> &[(String, String)] {
headers.iter_mut().for_each(|(key, value)| {
if key.to_lowercase() == "authorization" {
if value.starts_with("Bearer ") {
*value = "Bearer ***".to_string();
} else {
*value = "***".to_string();
}
}
});
headers
}
#[cfg(test)]
mod test {
use crate::pii::obfuscate_auth_header;
#[test]
pub fn test_obfuscate_auth_header() {
let mut headers = vec![("Authorization".to_string(), "Bearer 1234".to_string())];
obfuscate_auth_header(&mut headers);
assert_eq!(
headers,
vec![("Authorization".to_string(), "Bearer ***".to_string())]
);
}
#[test]
pub fn test_obfuscate_no_auth_header_found() {
let mut headers = vec![
(":path".to_string(), "/healthz".to_string()),
(":method".to_string(), "POST".to_string()),
];
obfuscate_auth_header(&mut headers);
assert_eq!(
headers,
vec![
(":path".to_string(), "/healthz".to_string()),
(":method".to_string(), "POST".to_string()),
]
);
}
}