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

Remove non-ascii header values instead of failing #2848

Merged
merged 1 commit into from
Sep 19, 2024
Merged
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
49 changes: 19 additions & 30 deletions crates/factor-outbound-http/src/spin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,21 +148,7 @@ fn hyper_method(m: Method) -> http::Method {
async fn response_from_hyper(mut resp: crate::Response) -> Result<Response, HttpError> {
let status = resp.status().as_u16();

let headers = resp
.headers()
.into_iter()
.map(|(key, val)| {
Ok((
key.to_string(),
val.to_str()
.map_err(|_| {
tracing::error!("Non-ascii response header {key} = {val:?}");
HttpError::RuntimeError
})?
.to_string(),
))
})
.collect::<Result<Vec<_>, _>>()?;
let headers = headers_from_map(resp.headers());

let body = resp
.body_mut()
Expand Down Expand Up @@ -205,21 +191,7 @@ fn log_reqwest_error(err: reqwest::Error) -> HttpError {
async fn response_from_reqwest(res: reqwest::Response) -> Result<Response, HttpError> {
let status = res.status().as_u16();

let headers = res
.headers()
.into_iter()
.map(|(key, val)| {
Ok((
key.to_string(),
val.to_str()
.map_err(|_| {
tracing::error!("Non-ascii response header {key} = {val:?}");
HttpError::RuntimeError
})?
.to_string(),
))
})
.collect::<Result<Vec<_>, _>>()?;
let headers = headers_from_map(res.headers());

let body = res
.bytes()
Expand All @@ -233,3 +205,20 @@ async fn response_from_reqwest(res: reqwest::Response) -> Result<Response, HttpE
body: Some(body),
})
}

fn headers_from_map(map: &http::HeaderMap) -> Vec<(String, String)> {
map.iter()
.filter_map(|(key, val)| {
Some((
key.to_string(),
val.to_str()
.ok()
.or_else(|| {
tracing::warn!("Non-ascii response header value for {key}");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would there be any use in printing the escaped version of the value?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes but I'm trying to cut down on logging potentially sensitive data 😬

None
})?
.to_string(),
))
})
.collect()
}
Loading