From 0d31c5031082f09fc0ed63846ad1587d531fbb44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Silva?= <123550+andresilva@users.noreply.github.com> Date: Fri, 22 May 2020 10:53:35 +0100 Subject: [PATCH] hosts: when binding to unspecified address allow requests from localhost (#549) --- server-utils/src/hosts.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/server-utils/src/hosts.rs b/server-utils/src/hosts.rs index 53f7a9702..ab2270ea6 100644 --- a/server-utils/src/hosts.rs +++ b/server-utils/src/hosts.rs @@ -160,11 +160,20 @@ pub fn is_host_valid(host: Option<&str>, allowed_hosts: &Option>) -> b /// Updates given list of hosts with the address. pub fn update(hosts: Option>, address: &SocketAddr) -> Option> { + use std::net::{IpAddr, Ipv4Addr}; + hosts.map(|current_hosts| { let mut new_hosts = current_hosts.into_iter().collect::>(); - let address = address.to_string(); - new_hosts.insert(address.clone().into()); - new_hosts.insert(address.replace("127.0.0.1", "localhost").into()); + let address_string = address.to_string(); + + if address.ip() == IpAddr::V4(Ipv4Addr::UNSPECIFIED) { + new_hosts.insert(address_string.replace("0.0.0.0", "127.0.0.1").into()); + new_hosts.insert(address_string.replace("0.0.0.0", "localhost").into()); + } else if address.ip() == IpAddr::V4(Ipv4Addr::LOCALHOST) { + new_hosts.insert(address_string.replace("127.0.0.1", "localhost").into()); + } + + new_hosts.insert(address_string.into()); new_hosts.into_iter().collect() }) }