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

Add the ability to customize behavior for filtering headers #2015

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 6 additions & 2 deletions src/async_impl/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use crate::dns::trust_dns::TrustDnsResolver;
use crate::dns::{gai::GaiResolver, DnsResolverWithOverrides, DynResolver, Resolve};
use crate::error;
use crate::into_url::{expect_uri, try_uri};
use crate::redirect::{self, remove_sensitive_headers};
use crate::redirect::{self};
#[cfg(feature = "__tls")]
use crate::tls::{self, TlsBackend};
#[cfg(feature = "__tls")]
Expand Down Expand Up @@ -2378,7 +2378,11 @@ impl Future for PendingRequest {
let mut headers =
std::mem::replace(self.as_mut().headers(), HeaderMap::new());

remove_sensitive_headers(&mut headers, &self.url, &self.urls);
self.client.redirect_policy.handle_sensitive_headers(
&mut headers,
&self.url,
&self.urls,
);
let uri = expect_uri(&self.url);
let body = match self.body {
Some(Some(ref body)) => Body::reusable(body.clone()),
Expand Down
75 changes: 58 additions & 17 deletions src/redirect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,41 @@ use crate::Url;
/// - `custom` can be used to create a customized policy.
pub struct Policy {
inner: PolicyKind,
filter: Box<dyn Filter + Send + Sync>,
}

/// A trait that controls the handling of sensitive headers.
pub trait Filter {
/// Handle sensitive headers.
fn handle_sensitive_headers(&self, headers: &mut HeaderMap, next: &Url, previous: &[Url]);
}

/// Default implementation for the handling of sensitive headers.
///
/// This implementation will exclude the authorization and cookie headers.
#[derive(Debug)]
pub struct DefaultFilter {}

impl Default for DefaultFilter {
fn default() -> Self {
Self {}
}
}

impl Filter for DefaultFilter {
fn handle_sensitive_headers(&self, headers: &mut HeaderMap, next: &Url, previous: &[Url]) {
if let Some(previous) = previous.last() {
let cross_host = next.host_str() != previous.host_str()
|| next.port_or_known_default() != previous.port_or_known_default();
if cross_host {
headers.remove(AUTHORIZATION);
headers.remove(COOKIE);
headers.remove("cookie2");
headers.remove(PROXY_AUTHORIZATION);
headers.remove(WWW_AUTHENTICATE);
}
}
}
}

/// A type that holds information on the next request and previous requests
Expand All @@ -47,13 +82,15 @@ impl Policy {
pub fn limited(max: usize) -> Self {
Self {
inner: PolicyKind::Limit(max),
filter: Box::new(DefaultFilter::default()),
}
}

/// Create a `Policy` that does not follow any redirect.
pub fn none() -> Self {
Self {
inner: PolicyKind::None,
filter: Box::new(DefaultFilter::default()),
}
}

Expand Down Expand Up @@ -101,6 +138,7 @@ impl Policy {
{
Self {
inner: PolicyKind::Custom(Box::new(policy)),
filter: Box::new(DefaultFilter::default()),
}
}

Expand Down Expand Up @@ -150,6 +188,21 @@ impl Policy {
pub(crate) fn is_default(&self) -> bool {
matches!(self.inner, PolicyKind::Limit(10))
}

/// Set filter for the handling of sensitive headers.
pub fn set_filter(&mut self, filter: Box<dyn Filter + Send + Sync>) {
self.filter = filter;
}

pub(crate) fn handle_sensitive_headers(
&self,
headers: &mut HeaderMap,
next: &Url,
previous: &[Url],
) {
self.filter
.handle_sensitive_headers(headers, next, previous);
}
}

impl Default for Policy {
Expand Down Expand Up @@ -231,20 +284,6 @@ pub(crate) enum ActionKind {
Error(Box<dyn StdError + Send + Sync>),
}

pub(crate) fn remove_sensitive_headers(headers: &mut HeaderMap, next: &Url, previous: &[Url]) {
if let Some(previous) = previous.last() {
let cross_host = next.host_str() != previous.host_str()
|| next.port_or_known_default() != previous.port_or_known_default();
if cross_host {
headers.remove(AUTHORIZATION);
headers.remove(COOKIE);
headers.remove("cookie2");
headers.remove(PROXY_AUTHORIZATION);
headers.remove(WWW_AUTHENTICATE);
}
}
}

#[derive(Debug)]
struct TooManyRedirects;

Expand Down Expand Up @@ -313,7 +352,7 @@ fn test_redirect_policy_custom() {
}

#[test]
fn test_remove_sensitive_headers() {
fn test_handle_sensitive_headers() {
use hyper::header::{HeaderValue, ACCEPT, AUTHORIZATION, COOKIE};

let mut headers = HeaderMap::new();
Expand All @@ -325,13 +364,15 @@ fn test_remove_sensitive_headers() {
let mut prev = vec![Url::parse("http://initial-domain.com/new_path").unwrap()];
let mut filtered_headers = headers.clone();

remove_sensitive_headers(&mut headers, &next, &prev);
let filter = DefaultFilter::default();

filter.handle_sensitive_headers(&mut headers, &next, &prev);
assert_eq!(headers, filtered_headers);

prev.push(Url::parse("http://new-domain.com/path").unwrap());
filtered_headers.remove(AUTHORIZATION);
filtered_headers.remove(COOKIE);

remove_sensitive_headers(&mut headers, &next, &prev);
filter.handle_sensitive_headers(&mut headers, &next, &prev);
assert_eq!(headers, filtered_headers);
}