From edba9c9f5db3001c5f1247c38f589e68e19e0930 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Wed, 1 Dec 2021 17:30:20 +0100 Subject: [PATCH] tokio-xmpp: Remove an unwrap() in DNS name parsing --- tokio-xmpp/src/error.rs | 14 ++++++++++++++ tokio-xmpp/src/starttls.rs | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/tokio-xmpp/src/error.rs b/tokio-xmpp/src/error.rs index 85d0ba2..69e810d 100644 --- a/tokio-xmpp/src/error.rs +++ b/tokio-xmpp/src/error.rs @@ -7,6 +7,8 @@ use std::fmt; use std::io::Error as IoError; use std::str::Utf8Error; #[cfg(feature = "tls-rust")] +use tokio_rustls::rustls::client::InvalidDnsNameError; +#[cfg(feature = "tls-rust")] use tokio_rustls::rustls::Error as TlsError; use trust_dns_proto::error::ProtoError; use trust_dns_resolver::error::ResolveError; @@ -32,6 +34,9 @@ pub enum Error { Auth(AuthError), /// TLS error Tls(TlsError), + #[cfg(feature = "tls-rust")] + /// DNS name parsing error + DnsNameError(InvalidDnsNameError), /// Connection closed Disconnected, /// Shoud never happen @@ -48,6 +53,8 @@ impl fmt::Display for Error { Error::Protocol(e) => write!(fmt, "protocol error: {}", e), Error::Auth(e) => write!(fmt, "authentication error: {}", e), Error::Tls(e) => write!(fmt, "TLS error: {}", e), + #[cfg(feature = "tls-rust")] + Error::DnsNameError(e) => write!(fmt, "DNS name error: {}", e), Error::Disconnected => write!(fmt, "disconnected"), Error::InvalidState => write!(fmt, "invalid state"), } @@ -90,6 +97,13 @@ impl From for Error { } } +#[cfg(feature = "tls-rust")] +impl From for Error { + fn from(e: InvalidDnsNameError) -> Self { + Error::DnsNameError(e) + } +} + /// Causes for stream parsing errors #[derive(Debug)] pub enum ParserError { diff --git a/tokio-xmpp/src/starttls.rs b/tokio-xmpp/src/starttls.rs index 7c91f15..8b20934 100644 --- a/tokio-xmpp/src/starttls.rs +++ b/tokio-xmpp/src/starttls.rs @@ -42,7 +42,7 @@ async fn get_tls_stream( xmpp_stream: XMPPStream, ) -> Result, Error> { let domain = &xmpp_stream.jid.clone().domain(); - let domain = ServerName::try_from(domain.as_str()).unwrap(); + let domain = ServerName::try_from(domain.as_str())?; let stream = xmpp_stream.into_inner(); let mut root_store = RootCertStore::empty(); root_store.add_server_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.0.iter().map(|ta| {