From 0c963da1fd8a92cb3a09e9446b8406d563e354df Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Tue, 14 Feb 2017 23:10:36 -0800 Subject: [PATCH] Fix IPv6 address parsing and validation. --- lib/src/config/config.rs | 9 ++++----- lib/src/config/mod.rs | 16 ++++++++++++---- lib/src/lib.rs | 1 + 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/lib/src/config/config.rs b/lib/src/config/config.rs index 9131352a..65076166 100644 --- a/lib/src/config/config.rs +++ b/lib/src/config/config.rs @@ -1,5 +1,5 @@ use std::collections::HashMap; -use std::net::ToSocketAddrs; +use std::net::{IpAddr, lookup_host}; use std::path::{Path, PathBuf}; use std::sync::RwLock; use std::convert::AsRef; @@ -268,19 +268,18 @@ impl Config { /// # fn config_test() -> Result<(), ConfigError> { /// let mut config = Config::new(Environment::Staging)?; /// assert!(config.set_address("localhost").is_ok()); + /// assert!(config.set_address("::").is_ok()); /// assert!(config.set_address("?").is_err()); /// # Ok(()) /// # } /// ``` pub fn set_address>(&mut self, address: A) -> config::Result<()> { let address = address.into(); - if address.contains(':') { - return Err(self.bad_type("address", "string", "a hostname or IP with no port")); - } else if format!("{}:{}", address, 80).to_socket_addrs().is_err() { + if address.parse::().is_err() && lookup_host(&address).is_err() { return Err(self.bad_type("address", "string", "a valid hostname or IP")); } - self.address = address.into(); + self.address = address; Ok(()) } diff --git a/lib/src/config/mod.rs b/lib/src/config/mod.rs index cc609a3c..6bdbab95 100644 --- a/lib/src/config/mod.rs +++ b/lib/src/config/mod.rs @@ -638,11 +638,19 @@ mod test { default_config(Development).address("localhost") }); + + check_config!(RocketConfig::parse(r#" + [development] + address = "::" + "#.to_string(), TEST_CONFIG_FILENAME), { + default_config(Development).address("::") + }); + check_config!(RocketConfig::parse(r#" [dev] - address = "127.0.0.1" + address = "2001:db8::370:7334" "#.to_string(), TEST_CONFIG_FILENAME), { - default_config(Development).address("127.0.0.1") + default_config(Development).address("2001:db8::370:7334") }); check_config!(RocketConfig::parse(r#" @@ -930,9 +938,9 @@ mod test { check_config!(RocketConfig::parse(format!(r#" [{}] - address = "7.6.5.4" + address = "::1" "#, GLOBAL_ENV_NAME), TEST_CONFIG_FILENAME), { - default_config(*env).address("7.6.5.4") + default_config(*env).address("::1") }); check_config!(RocketConfig::parse(format!(r#" diff --git a/lib/src/lib.rs b/lib/src/lib.rs index a39e9ef1..06df88b8 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -5,6 +5,7 @@ #![feature(const_fn)] #![feature(type_ascription)] #![feature(pub_restricted)] +#![feature(lookup_host)] //! # Rocket - Core API Documentation //!