Skip to content

Commit 7a0e20c

Browse files
authored
Redact the authorization header (#1699)
1 parent a96b6b6 commit 7a0e20c

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

sdk/core/src/headers/mod.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub trait Header {
5151
}
5252

5353
/// A collection of headers
54-
#[derive(Clone, Debug, PartialEq, Eq, Default)]
54+
#[derive(Clone, PartialEq, Eq, Default)]
5555
pub struct Headers(std::collections::HashMap<HeaderName, HeaderValue>);
5656

5757
impl Headers {
@@ -151,6 +151,32 @@ impl Headers {
151151
}
152152
}
153153

154+
fn matching_ignore_ascii_case(a: &str, b: &str) -> bool {
155+
if a.len() != b.len() {
156+
return false;
157+
}
158+
a.chars()
159+
.zip(b.chars())
160+
.all(|(a_c, b_c)| a_c.to_ascii_lowercase() == b_c.to_ascii_lowercase())
161+
}
162+
163+
impl Debug for Headers {
164+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
165+
write!(f, "Headers(")?;
166+
let redacted = HeaderValue::from_static("[redacted]");
167+
f.debug_map()
168+
.entries(self.0.iter().map(|(name, value)| {
169+
if matching_ignore_ascii_case(name.as_str(), AUTHORIZATION.as_str()) {
170+
(name, value)
171+
} else {
172+
(name, &redacted)
173+
}
174+
}))
175+
.finish()?;
176+
write!(f, ")")
177+
}
178+
}
179+
154180
impl IntoIterator for Headers {
155181
type Item = (HeaderName, HeaderValue);
156182

0 commit comments

Comments
 (0)