From e5fa36f98e9d36a241ac38b029ce1ef04f1a8bc4 Mon Sep 17 00:00:00 2001 From: Koga Kazuo Date: Sun, 12 Apr 2020 11:00:07 +0900 Subject: [PATCH] Fix panic on invalid authority --- src/lib.rs | 8 ++++++++ src/stream.rs | 3 ++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 274ec3a..8db0fd7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -202,4 +202,12 @@ mod tests { ); assert_eq!("text/html", resp.content_type()); } + + #[test] + #[cfg(feature = "tls")] + fn connect_https_invalid_name() { + let resp = get("https://example.com{REQUEST_URI}/").call(); + assert_eq!(400, resp.status()); + assert!(resp.synthetic()); + } } diff --git a/src/stream.rs b/src/stream.rs index a6b0d45..b4bf0e2 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -156,7 +156,8 @@ pub(crate) fn connect_https(unit: &Unit) -> Result { let hostname = unit.url.host_str().unwrap(); let port = unit.url.port().unwrap_or(443); - let sni = webpki::DNSNameRef::try_from_ascii_str(hostname).unwrap(); + let sni = webpki::DNSNameRef::try_from_ascii_str(hostname) + .map_err(|err| Error::DnsFailed(err.to_string()))?; let sess = rustls::ClientSession::new(&*TLS_CONF, sni); let sock = connect_host(unit, hostname, port)?;