Check redirect locations are valid Uris (#486)

Closes #484
This commit is contained in:
Sean McArthur
2019-04-01 11:13:01 -07:00
committed by GitHub
parent d62f8c2bbd
commit 5c3494b81d
4 changed files with 52 additions and 8 deletions

View File

@@ -30,7 +30,7 @@ use native_tls::TlsConnector;
use super::request::{Request, RequestBuilder};
use super::response::Response;
use connect::Connector;
use into_url::to_uri;
use into_url::{expect_uri, try_uri};
use redirect::{self, RedirectPolicy, remove_sensitive_headers};
use {IntoUrl, Method, Proxy, StatusCode, Url};
#[cfg(feature = "tls")]
@@ -520,7 +520,7 @@ impl Client {
headers.insert(ACCEPT_ENCODING, HeaderValue::from_static("gzip"));
}
let uri = to_uri(&url);
let uri = expect_uri(&url);
let (reusable, body) = match body {
Some(body) => {
@@ -709,6 +709,17 @@ impl Future for PendingRequest {
self.url.join(str::from_utf8(val.as_bytes()).ok()?).ok()
})();
// Check that the `url` is also a valid `http::Uri`.
//
// If not, just log it and skip the redirect.
let loc = loc.and_then(|url| {
if try_uri(&url).is_some() {
Some(url)
} else {
None
}
});
if loc.is_none() {
debug!("Location header had invalid URI: {:?}", val);
}
@@ -733,7 +744,7 @@ impl Future for PendingRequest {
remove_sensitive_headers(&mut self.headers, &self.url, &self.urls);
debug!("redirecting to {:?} '{}'", self.method, self.url);
let uri = to_uri(&self.url);
let uri = expect_uri(&self.url);
let body = match self.body {
Some(Some(ref body)) => ::hyper::Body::from(body.clone()),
_ => ::hyper::Body::empty(),

View File

@@ -38,10 +38,14 @@ impl<'a> PolyfillTryInto for &'a String {
}
}
pub(crate) fn to_uri(url: &Url) -> ::hyper::Uri {
pub(crate) fn expect_uri(url: &Url) -> ::hyper::Uri {
url.as_str().parse().expect("a parsed Url should always be a valid Uri")
}
pub(crate) fn try_uri(url: &Url) -> Option<::hyper::Uri> {
url.as_str().parse().ok()
}
#[cfg(test)]
mod tests {
use super::*;

View File

@@ -56,7 +56,7 @@ impl Proxy {
/// # fn main() {}
/// ```
pub fn http<U: IntoUrl>(url: U) -> ::Result<Proxy> {
let uri = ::into_url::to_uri(&url.into_url()?);
let uri = ::into_url::expect_uri(&url.into_url()?);
Ok(Proxy::new(Intercept::Http(uri)))
}
@@ -75,7 +75,7 @@ impl Proxy {
/// # fn main() {}
/// ```
pub fn https<U: IntoUrl>(url: U) -> ::Result<Proxy> {
let uri = ::into_url::to_uri(&url.into_url()?);
let uri = ::into_url::expect_uri(&url.into_url()?);
Ok(Proxy::new(Intercept::Https(uri)))
}
@@ -94,7 +94,7 @@ impl Proxy {
/// # fn main() {}
/// ```
pub fn all<U: IntoUrl>(url: U) -> ::Result<Proxy> {
let uri = ::into_url::to_uri(&url.into_url()?);
let uri = ::into_url::expect_uri(&url.into_url()?);
Ok(Proxy::new(Intercept::All(uri)))
}
@@ -200,7 +200,7 @@ impl Proxy {
.parse()
.expect("should be valid Url")
)
.map(|u| into_url::to_uri(&u) )
.map(|u| into_url::expect_uri(&u) )
},
}
}