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

fix: redaction #539

Merged
merged 2 commits into from
Feb 23, 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
4 changes: 1 addition & 3 deletions crates/rattler_networking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,4 @@ pub mod retry_policies;

mod redaction;

pub use redaction::{
redact_known_secrets_from_error, redact_known_secrets_from_url, DEFAULT_REDACTION_STR,
};
pub use redaction::{redact_known_secrets_from_url, Redact, DEFAULT_REDACTION_STR};
41 changes: 33 additions & 8 deletions crates/rattler_networking/src/redaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,39 @@ pub fn redact_known_secrets_from_url(url: &Url, redaction: &str) -> Option<Url>
}
}

/// Redacts known secrets from a [`reqwest::Error`].
pub fn redact_known_secrets_from_error(err: reqwest::Error) -> reqwest::Error {
if let Some(url) = err.url() {
let redacted_url = redact_known_secrets_from_url(url, DEFAULT_REDACTION_STR)
.unwrap_or_else(|| url.clone());
err.with_url(redacted_url)
} else {
err
/// A trait to redact known secrets from a type.
pub trait Redact {
/// Redacts any secrets from this instance.
fn redact(self) -> Self;
}

impl Redact for reqwest_middleware::Error {
fn redact(self) -> Self {
if let Some(url) = self.url() {
let redacted_url = redact_known_secrets_from_url(url, DEFAULT_REDACTION_STR)
.unwrap_or_else(|| url.clone());
self.with_url(redacted_url)
} else {
self
}
}
}

impl Redact for reqwest::Error {
fn redact(self) -> Self {
if let Some(url) = self.url() {
let redacted_url = redact_known_secrets_from_url(url, DEFAULT_REDACTION_STR)
.unwrap_or_else(|| url.clone());
self.with_url(redacted_url)
} else {
self
}
}
}

impl Redact for Url {
fn redact(self) -> Self {
redact_known_secrets_from_url(&self, DEFAULT_REDACTION_STR).unwrap_or(self)
}
}

Expand Down
18 changes: 4 additions & 14 deletions crates/rattler_package_streaming/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use std::path::PathBuf;

use rattler_digest::{Md5Hash, Sha256Hash};

#[cfg(feature = "reqwest")]
use rattler_networking::Redact;

pub mod read;
pub mod seek;

Expand Down Expand Up @@ -49,23 +52,10 @@ pub enum ExtractError {
ArchiveMemberParseError(PathBuf, #[source] std::io::Error),
}

#[cfg(feature = "reqwest")]
impl From<::reqwest::Error> for ExtractError {
fn from(err: ::reqwest::Error) -> Self {
Self::ReqwestError(rattler_networking::redact_known_secrets_from_error(err).into())
}
}

#[cfg(feature = "reqwest")]
impl From<::reqwest_middleware::Error> for ExtractError {
fn from(err: ::reqwest_middleware::Error) -> Self {
let err = if let reqwest_middleware::Error::Reqwest(err) = err {
rattler_networking::redact_known_secrets_from_error(err).into()
} else {
err
};

ExtractError::ReqwestError(err)
ExtractError::ReqwestError(err.redact())
}
}

Expand Down
10 changes: 3 additions & 7 deletions crates/rattler_repodata_gateway/src/fetch/jlap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ use blake2::digest::{FixedOutput, Update};
use rattler_digest::{
parse_digest_from_hex, serde::SerializableHash, Blake2b256, Blake2b256Hash, Blake2bMac256,
};
use rattler_networking::redact_known_secrets_from_error;
use rattler_networking::Redact;
use reqwest::{
header::{HeaderMap, HeaderValue},
Response, StatusCode,
Expand Down Expand Up @@ -166,17 +166,13 @@ pub enum JLAPError {

impl From<reqwest_middleware::Error> for JLAPError {
fn from(value: reqwest_middleware::Error) -> Self {
Self::HTTP(if let reqwest_middleware::Error::Reqwest(err) = value {
reqwest_middleware::Error::Reqwest(redact_known_secrets_from_error(err))
} else {
value
})
Self::HTTP(value.redact())
}
}

impl From<reqwest::Error> for JLAPError {
fn from(value: reqwest::Error) -> Self {
Self::HTTP(redact_known_secrets_from_error(value).into())
Self::HTTP(value.redact().into())
}
}

Expand Down
31 changes: 18 additions & 13 deletions crates/rattler_repodata_gateway/src/fetch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ use cache_control::{Cachability, CacheControl};
use futures::{future::ready, FutureExt, TryStreamExt};
use humansize::{SizeFormatter, DECIMAL};
use rattler_digest::{compute_file_digest, Blake2b256, HashingWriter};
use rattler_networking::{
redact_known_secrets_from_error, redact_known_secrets_from_url, DEFAULT_REDACTION_STR,
};
use rattler_networking::Redact;
use reqwest::{
header::{HeaderMap, HeaderValue},
Response, StatusCode,
Expand All @@ -34,7 +32,7 @@ pub type ProgressFunc = Box<dyn FnMut(DownloadProgress) + Send + Sync>;
pub enum RepoDataNotFoundError {
/// There was an error on the Http request
#[error(transparent)]
HttpError(reqwest::Error),
HttpError(reqwest_middleware::Error),

/// There was a file system error
#[error(transparent)]
Expand Down Expand Up @@ -78,15 +76,27 @@ pub enum FetchRepoDataError {
Cancelled,
}

impl From<reqwest_middleware::Error> for FetchRepoDataError {
fn from(err: reqwest_middleware::Error) -> Self {
Self::HttpError(err.redact())
}
}

impl From<reqwest::Error> for FetchRepoDataError {
fn from(err: reqwest::Error) -> Self {
Self::HttpError(redact_known_secrets_from_error(err).into())
Self::HttpError(err.redact().into())
}
}

impl From<reqwest_middleware::Error> for RepoDataNotFoundError {
fn from(err: reqwest_middleware::Error) -> Self {
Self::HttpError(err.redact())
}
}

impl From<reqwest::Error> for RepoDataNotFoundError {
fn from(err: reqwest::Error) -> Self {
Self::HttpError(redact_known_secrets_from_error(err))
Self::HttpError(err.redact().into())
}
}

Expand Down Expand Up @@ -503,7 +513,7 @@ pub async fn fetch_repo_data(
}
Ok(response) => response.error_for_status()?,
Err(e) => {
return Err(FetchRepoDataError::HttpError(e));
return Err(FetchRepoDataError::from(e));
}
};

Expand Down Expand Up @@ -677,12 +687,7 @@ async fn stream_and_decode_to_file(
// Decode, hash and write the data to the file.
let bytes = tokio::io::copy(&mut decoded_repo_data_json_bytes, &mut hashing_file_writer)
.await
.map_err(|e| {
FetchRepoDataError::FailedToDownload(
redact_known_secrets_from_url(&url, DEFAULT_REDACTION_STR).unwrap_or(url),
e,
)
})?;
.map_err(|e| FetchRepoDataError::FailedToDownload(url.redact(), e))?;

// Finalize the hash
let (_, hash) = hashing_file_writer.finalize();
Expand Down