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

feat(core/http_util): Remove sensitive header like Set-Cookie #2664

Merged
merged 2 commits into from
Jul 18, 2023
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
89 changes: 22 additions & 67 deletions core/src/raw/http_util/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,77 +15,11 @@
// specific language governing permissions and limitations
// under the License.

use std::fmt::Display;
use std::fmt::Formatter;

use anyhow::anyhow;
use http::response::Parts;
use http::HeaderMap;
use http::HeaderValue;
use http::Response;
use http::StatusCode;
use http::Uri;

use super::IncomingAsyncBody;
use crate::Error;
use crate::ErrorKind;
use crate::Result;

/// ErrorResponse carries HTTP status code, headers and body.
///
/// This struct should only be used to parse error response which is small.
pub struct ErrorResponse {
parts: Parts,
body: Vec<u8>,
}

impl ErrorResponse {
/// Get http status code
pub fn status_code(&self) -> StatusCode {
self.parts.status
}

/// Get http headers
pub fn headers(&self) -> &HeaderMap<HeaderValue> {
&self.parts.headers
}

/// Get http error response body content (in bytes).
pub fn body(&self) -> &[u8] {
&self.body
}
}

impl Display for ErrorResponse {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"status code: {:?}, headers: {:?}, body: {:?}",
self.status_code(),
self.headers(),
String::from_utf8_lossy(self.body())
)
}
}

/// parse_error_response will parse response into `ErrorResponse`.
///
/// # NOTE
///
/// Please only use this for parsing error response hence it will read the
/// entire body into memory.
pub async fn parse_error_response(resp: Response<IncomingAsyncBody>) -> Result<ErrorResponse> {
let (parts, body) = resp.into_parts();
let bs = body.bytes().await.map_err(|err| {
Error::new(ErrorKind::Unexpected, "reading error response")
.with_operation("http_util::parse_error_response")
.set_source(anyhow!(err))
})?;

Ok(ErrorResponse {
parts,
body: bs.to_vec(),
})
}

/// Create a new error happened during building request.
pub fn new_request_build_error(err: http::Error) -> Error {
Expand All @@ -111,3 +45,24 @@ pub fn new_request_sign_error(err: anyhow::Error) -> Error {
.with_operation("reqsign::Sign")
.set_source(err)
}

/// Add response context to error.
///
/// This helper function will:
///
/// - remove sensitive or useless headers from parts.
/// - fetch uri if parts extensions contains `Uri`.
pub fn with_error_response_context(mut err: Error, mut parts: Parts) -> Error {
if let Some(uri) = parts.extensions.get::<Uri>() {
err = err.with_context("uri", uri.to_string());
}

// The following headers may contains sensitive information.
parts.headers.remove("Set-Cookie");
parts.headers.remove("WWW-Authenticate");
parts.headers.remove("Proxy-Authenticate");

err = err.with_context("response", format!("{parts:?}"));

err
}
3 changes: 1 addition & 2 deletions core/src/raw/http_util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ mod error;
pub use error::new_request_build_error;
pub use error::new_request_credential_error;
pub use error::new_request_sign_error;
pub use error::parse_error_response;
pub use error::ErrorResponse;
pub use error::with_error_response_context;

mod bytes_range;
pub use bytes_range::BytesRange;
Expand Down
5 changes: 4 additions & 1 deletion core/src/services/azblob/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ pub async fn parse_error(resp: Response<IncomingAsyncBody>) -> Result<Error> {
Ok(azblob_err) => format!("{azblob_err:?}"),
Err(_) => String::from_utf8_lossy(&bs).into_owned(),
};

// If there is no body here, fill with error code.
if message.is_empty() {
if let Some(v) = parts.headers.get("x-ms-error-code") {
Expand All @@ -119,7 +120,9 @@ pub async fn parse_error(resp: Response<IncomingAsyncBody>) -> Result<Error> {
}
}

let mut err = Error::new(kind, &message).with_context("response", format!("{parts:?}"));
let mut err = Error::new(kind, &message);

err = with_error_response_context(err, parts);

if retryable {
err = err.set_temporary();
Expand Down
4 changes: 3 additions & 1 deletion core/src/services/azdfs/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ pub async fn parse_error(resp: Response<IncomingAsyncBody>) -> Result<Error> {
}
}

let mut err = Error::new(kind, &message).with_context("response", format!("{parts:?}"));
let mut err = Error::new(kind, &message);

err = with_error_response_context(err, parts);

if retryable {
err = err.set_temporary();
Expand Down
5 changes: 3 additions & 2 deletions core/src/services/cos/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,13 @@ pub async fn parse_error(resp: Response<IncomingAsyncBody>) -> Result<Error> {
Err(_) => String::from_utf8_lossy(&bs).into_owned(),
};

let mut err = Error::new(kind, &message).with_context("response", format!("{parts:?}"));
let mut err = Error::new(kind, &message);

err = with_error_response_context(err, parts);

if retryable {
err = err.set_temporary();
}

Ok(err)
}

Expand Down
9 changes: 3 additions & 6 deletions core/src/services/dropbox/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

use http::Response;
use http::StatusCode;
use http::Uri;
use serde::Deserialize;

use crate::raw::*;
Expand Down Expand Up @@ -55,16 +54,14 @@ pub async fn parse_error(resp: Response<IncomingAsyncBody>) -> Result<Error> {
parse_dropbox_error_summary(&dropbox_err.error_summary).unwrap_or((kind, retryable));
}

let mut err = Error::new(kind, &message).with_context("response", format!("{parts:?}"));
let mut err = Error::new(kind, &message);

err = with_error_response_context(err, parts);

if retryable {
err = err.set_temporary();
}

if let Some(uri) = parts.extensions.get::<Uri>() {
err = err.with_context("uri", uri.to_string());
}

Ok(err)
}

Expand Down
4 changes: 3 additions & 1 deletion core/src/services/gcs/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ pub async fn parse_error(resp: Response<IncomingAsyncBody>) -> Result<Error> {
Err(_) => String::from_utf8_lossy(&bs).into_owned(),
};

let mut err = Error::new(kind, &message).with_context("response", format!("{parts:?}"));
let mut err = Error::new(kind, &message);

err = with_error_response_context(err, parts);

if retryable {
err = err.set_temporary();
Expand Down
7 changes: 5 additions & 2 deletions core/src/services/gdrive/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ pub async fn parse_error(resp: Response<IncomingAsyncBody>) -> Result<Error> {
_ => (ErrorKind::Unexpected, false),
};

let mut err = Error::new(kind, &String::from_utf8_lossy(&bs))
.with_context("response", format!("{parts:?}"));
let message = String::from_utf8_lossy(&bs);

let mut err = Error::new(kind, &message);

err = with_error_response_context(err, parts);

if retryable {
err = err.set_temporary();
Expand Down
7 changes: 5 additions & 2 deletions core/src/services/ghac/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ pub async fn parse_error(resp: Response<IncomingAsyncBody>) -> Result<Error> {
};

let bs = body.bytes().await?;
let mut err = Error::new(kind, &String::from_utf8_lossy(&bs))
.with_context("response", format!("{parts:?}"));
let message = String::from_utf8_lossy(&bs);

let mut err = Error::new(kind, &message);

err = with_error_response_context(err, parts);

if retryable {
err = err.set_temporary();
Expand Down
7 changes: 5 additions & 2 deletions core/src/services/http/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ pub async fn parse_error(resp: Response<IncomingAsyncBody>) -> Result<Error> {
_ => (ErrorKind::Unexpected, false),
};

let mut err = Error::new(kind, &String::from_utf8_lossy(&bs))
.with_context("response", format!("{parts:?}"));
let message = String::from_utf8_lossy(&bs);

let mut err = Error::new(kind, &message);

err = with_error_response_context(err, parts);

if retryable {
err = err.set_temporary();
Expand Down
7 changes: 5 additions & 2 deletions core/src/services/ipfs/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ pub async fn parse_error(resp: Response<IncomingAsyncBody>) -> Result<Error> {
_ => (ErrorKind::Unexpected, false),
};

let mut err = Error::new(kind, &String::from_utf8_lossy(&bs))
.with_context("response", format!("{parts:?}"));
let message = String::from_utf8_lossy(&bs);

let mut err = Error::new(kind, &message);

err = with_error_response_context(err, parts);

if retryable {
err = err.set_temporary();
Expand Down
4 changes: 3 additions & 1 deletion core/src/services/ipmfs/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ pub async fn parse_error(resp: Response<IncomingAsyncBody>) -> Result<Error> {
None => String::from_utf8_lossy(&bs).into_owned(),
};

let mut err = Error::new(kind, &message).with_context("response", format!("{parts:?}"));
let mut err = Error::new(kind, &message);

err = with_error_response_context(err, parts);

if retryable {
err = err.set_temporary();
Expand Down
4 changes: 3 additions & 1 deletion core/src/services/obs/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ pub async fn parse_error(resp: Response<IncomingAsyncBody>) -> Result<Error> {
Err(_) => String::from_utf8_lossy(&bs).into_owned(),
};

let mut err = Error::new(kind, &message).with_context("response", format!("{parts:?}"));
let mut err = Error::new(kind, &message);

err = with_error_response_context(err, parts);

if retryable {
err = err.set_temporary();
Expand Down
7 changes: 5 additions & 2 deletions core/src/services/onedrive/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ pub async fn parse_error(resp: Response<IncomingAsyncBody>) -> Result<Error> {
_ => (ErrorKind::Unexpected, false),
};

let mut err = Error::new(kind, &String::from_utf8_lossy(&bs))
.with_context("response", format!("{parts:?}"));
let message = String::from_utf8_lossy(&bs);

let mut err = Error::new(kind, &message);

err = with_error_response_context(err, parts);

if retryable {
err = err.set_temporary();
Expand Down
4 changes: 3 additions & 1 deletion core/src/services/oss/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ pub async fn parse_error(resp: Response<IncomingAsyncBody>) -> Result<Error> {
Err(_) => String::from_utf8_lossy(&bs).into_owned(),
};

let mut err = Error::new(kind, &message).with_context("response", format!("{parts:?}"));
let mut err = Error::new(kind, &message);

err = with_error_response_context(err, parts);

if retryable {
err = err.set_temporary();
Expand Down
9 changes: 3 additions & 6 deletions core/src/services/s3/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

use bytes::Buf;
use http::Response;
use http::Uri;
use quick_xml::de;
use serde::Deserialize;

Expand Down Expand Up @@ -60,16 +59,14 @@ pub async fn parse_error(resp: Response<IncomingAsyncBody>) -> Result<Error> {
(kind, retryable) = parse_s3_error_code(s3_err.code.as_str()).unwrap_or((kind, retryable));
}

let mut err = Error::new(kind, &message).with_context("response", format!("{parts:?}"));
let mut err = Error::new(kind, &message);

err = with_error_response_context(err, parts);

if retryable {
err = err.set_temporary();
}

if let Some(uri) = parts.extensions.get::<Uri>() {
err = err.with_context("uri", uri.to_string());
}

Ok(err)
}

Expand Down
4 changes: 3 additions & 1 deletion core/src/services/supabase/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ pub async fn parse_error(resp: Response<IncomingAsyncBody>) -> Result<Error> {
})
.unwrap_or_else(|_| (String::from_utf8_lossy(&bs).into_owned(), None));

let mut err = Error::new(kind, &message).with_context("response", format!("{parts:?}"));
let mut err = Error::new(kind, &message);

err = with_error_response_context(err, parts);

if retryable {
err = err.set_temporary();
Expand Down
7 changes: 5 additions & 2 deletions core/src/services/vercel_artifacts/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ pub async fn parse_error(resp: Response<IncomingAsyncBody>) -> Result<Error> {
_ => (ErrorKind::Unexpected, false),
};

let mut err = Error::new(kind, &String::from_utf8_lossy(&bs))
.with_context("response", format!("{parts:?}"));
let message = String::from_utf8_lossy(&bs);

let mut err = Error::new(kind, &message);

err = with_error_response_context(err, parts);

if retryable {
err = err.set_temporary();
Expand Down
4 changes: 3 additions & 1 deletion core/src/services/wasabi/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ pub async fn parse_error(resp: Response<IncomingAsyncBody>) -> Result<Error> {
(kind, retryable) = parse_wasabi_error_code(&wasabi_err.code).unwrap_or((kind, retryable));
}

let mut err = Error::new(kind, &message).with_context("response", format!("{parts:?}"));
let mut err = Error::new(kind, &message);

err = with_error_response_context(err, parts);

if retryable {
err = err.set_temporary();
Expand Down
7 changes: 5 additions & 2 deletions core/src/services/webdav/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ pub async fn parse_error(resp: Response<IncomingAsyncBody>) -> Result<Error> {
_ => (ErrorKind::Unexpected, false),
};

let mut err = Error::new(kind, &String::from_utf8_lossy(&bs))
.with_context("response", format!("{parts:?}"));
let message = String::from_utf8_lossy(&bs);

let mut err = Error::new(kind, &message);

err = with_error_response_context(err, parts);

if retryable {
err = err.set_temporary();
Expand Down
4 changes: 3 additions & 1 deletion core/src/services/webhdfs/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ fn parse_error_msg(parts: Parts, body: &str) -> Result<Error> {
Err(_) => body.to_owned(),
};

let mut err = Error::new(kind, &message).with_context("response", format!("{parts:?}"));
let mut err = Error::new(kind, &message);

err = with_error_response_context(err, parts);

if retryable {
err = err.set_temporary();
Expand Down