|
| 1 | +use std::error::Error; |
| 2 | + |
| 3 | +use lazy_static::lazy_static; |
| 4 | +use regex::{Captures, Regex, Replacer}; |
| 5 | +use url::Url; |
| 6 | + |
| 7 | +/// Custom wrapper for `reqwest::Error` to avoid displaying the url in error |
| 8 | +/// message that could contain sensitive information such as API keys. |
| 9 | +#[derive(thiserror::Error, Debug)] |
| 10 | +pub struct ReqwestError(#[from] reqwest::Error); |
| 11 | + |
| 12 | +impl From<ReqwestError> for reqwest::Error { |
| 13 | + fn from(value: ReqwestError) -> Self { |
| 14 | + value.0 |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +// Matches the `Display` implementation for `reqwest::Error` except where noted |
| 19 | +impl std::fmt::Display for ReqwestError { |
| 20 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 21 | + if self.0.is_builder() { |
| 22 | + f.write_str("builder self")?; |
| 23 | + } else if self.0.is_request() { |
| 24 | + f.write_str("self sending request")?; |
| 25 | + } else if self.0.is_body() { |
| 26 | + f.write_str("request or response body self")?; |
| 27 | + } else if self.0.is_decode() { |
| 28 | + f.write_str("self decoding response body")?; |
| 29 | + } else if self.0.is_redirect() { |
| 30 | + f.write_str("self following redirect")?; |
| 31 | + } else if self.0.is_status() { |
| 32 | + let code = self.0.status().expect("Error is status"); |
| 33 | + let prefix = if code.is_client_error() { |
| 34 | + "HTTP status client self" |
| 35 | + } else { |
| 36 | + debug_assert!(code.is_server_error()); |
| 37 | + "HTTP status server self" |
| 38 | + }; |
| 39 | + write!(f, "{prefix} ({code})")?; |
| 40 | + } else { |
| 41 | + // It might be an upgrade, but `reqwest` doesn't expose checking that on the |
| 42 | + // self type. |
| 43 | + f.write_str("unknown self")?; |
| 44 | + } |
| 45 | + |
| 46 | + // This is changed from the original code |
| 47 | + if let Some(host) = self.0.url().and_then(|url| url.host_str()) { |
| 48 | + write!(f, " for host ({host})")?; |
| 49 | + } |
| 50 | + |
| 51 | + if let Some(e) = self.0.source() { |
| 52 | + write!(f, ": {e}")?; |
| 53 | + } |
| 54 | + |
| 55 | + Ok(()) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/// Custom wrapper for `reqwest_middleware::Error` to avoid displaying the url |
| 60 | +/// in error message that could contain sensitive information such as API keys. |
| 61 | +#[derive(thiserror::Error, Debug)] |
| 62 | +pub enum MiddlewareError { |
| 63 | + /// There was an error running some middleware |
| 64 | + Middleware(#[from] anyhow::Error), |
| 65 | + /// Error from the underlying reqwest client |
| 66 | + Reqwest(#[from] ReqwestError), |
| 67 | +} |
| 68 | + |
| 69 | +impl From<reqwest_middleware::Error> for MiddlewareError { |
| 70 | + fn from(value: reqwest_middleware::Error) -> Self { |
| 71 | + match value { |
| 72 | + reqwest_middleware::Error::Middleware(middleware) => { |
| 73 | + MiddlewareError::Middleware(middleware) |
| 74 | + } |
| 75 | + reqwest_middleware::Error::Reqwest(reqwest) => MiddlewareError::Reqwest(reqwest.into()), |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +impl std::fmt::Display for MiddlewareError { |
| 81 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 82 | + match self { |
| 83 | + MiddlewareError::Middleware(e) => { |
| 84 | + let s = e.to_string(); |
| 85 | + let replaced = URL_REGEX.replace_all(&s, UrlReplacer); |
| 86 | + f.write_str(&replaced) |
| 87 | + } |
| 88 | + MiddlewareError::Reqwest(e) => e.fmt(f), |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +lazy_static! { |
| 94 | + static ref URL_REGEX: Regex = Regex::new(r"(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)").expect("Test checks panic"); |
| 95 | +} |
| 96 | + |
| 97 | +/// Replaces urls in strings with the host part only. |
| 98 | +struct UrlReplacer; |
| 99 | + |
| 100 | +impl Replacer for UrlReplacer { |
| 101 | + fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut String) { |
| 102 | + if let Some(host) = caps.get(0).and_then(|url| { |
| 103 | + Url::parse(url.as_str()) |
| 104 | + .ok() |
| 105 | + .and_then(|url| url.host_str().map(ToString::to_string)) |
| 106 | + }) { |
| 107 | + dst.push_str(&host); |
| 108 | + } else { |
| 109 | + dst.push_str("<unknown host>"); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +#[cfg(test)] |
| 115 | +mod tests { |
| 116 | + use super::*; |
| 117 | + |
| 118 | + #[test] |
| 119 | + fn test_display_middleware_error() -> anyhow::Result<()> { |
| 120 | + let error = MiddlewareError::Middleware(anyhow::anyhow!( |
| 121 | + "Some middleware error occurred for url: http://subdomain.example.com:1234/secret something else" |
| 122 | + )); |
| 123 | + assert_eq!( |
| 124 | + error.to_string(), |
| 125 | + "Some middleware error occurred for url: subdomain.example.com something else" |
| 126 | + ); |
| 127 | + |
| 128 | + let error = MiddlewareError::Middleware(anyhow::anyhow!( |
| 129 | + "Some middleware error occurred for url: https://subdomain.example.com/path?query=secret something else" |
| 130 | + )); |
| 131 | + assert_eq!( |
| 132 | + error.to_string(), |
| 133 | + "Some middleware error occurred for url: subdomain.example.com something else" |
| 134 | + ); |
| 135 | + |
| 136 | + Ok(()) |
| 137 | + } |
| 138 | +} |
0 commit comments