Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

obfuscate auth header #254

Merged
merged 2 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ pub mod ratelimit;
pub mod routing;
pub mod stats;
pub mod tokenizer;
pub mod pii;
44 changes: 44 additions & 0 deletions crates/common/src/pii.rs
Original file line number Diff line number Diff line change
@@ -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()),
]
);
}
}
3 changes: 2 additions & 1 deletion crates/llm_gateway/src/stream_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use common::consts::{
};
use common::errors::ServerError;
use common::llm_providers::LlmProviders;
use common::pii::obfuscate_auth_header;
use common::ratelimit::Header;
use common::{ratelimit, routing, tokenizer};
use http::StatusCode;
Expand Down Expand Up @@ -153,7 +154,7 @@ impl HttpContext for StreamContext {
debug!(
"on_http_request_headers S[{}] req_headers={:?}",
self.context_id,
self.get_http_request_headers()
obfuscate_auth_header(&mut self.get_http_request_headers())
);

self.request_id = self.get_http_request_header(REQUEST_ID_HEADER);
Expand Down
4 changes: 2 additions & 2 deletions crates/prompt_gateway/src/http_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use common::{
HEALTHZ_PATH, REQUEST_ID_HEADER, TOOL_ROLE, TRACE_PARENT_HEADER, USER_ROLE,
},
errors::ServerError,
http::{CallArgs, Client},
http::{CallArgs, Client}, pii::obfuscate_auth_header,
};
use http::StatusCode;
use log::{debug, trace, warn};
Expand Down Expand Up @@ -48,7 +48,7 @@ impl HttpContext for StreamContext {
trace!(
"on_http_request_headers S[{}] req_headers={:?}",
self.context_id,
self.get_http_request_headers()
obfuscate_auth_header(&mut self.get_http_request_headers())
);

self.request_id = self.get_http_request_header(REQUEST_ID_HEADER);
Expand Down
Loading