Skip to content

Commit 0d31c50

Browse files
authored
hosts: when binding to unspecified address allow requests from localhost (#549)
1 parent 5f1e693 commit 0d31c50

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

server-utils/src/hosts.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,20 @@ pub fn is_host_valid(host: Option<&str>, allowed_hosts: &Option<Vec<Host>>) -> b
160160

161161
/// Updates given list of hosts with the address.
162162
pub fn update(hosts: Option<Vec<Host>>, address: &SocketAddr) -> Option<Vec<Host>> {
163+
use std::net::{IpAddr, Ipv4Addr};
164+
163165
hosts.map(|current_hosts| {
164166
let mut new_hosts = current_hosts.into_iter().collect::<HashSet<_>>();
165-
let address = address.to_string();
166-
new_hosts.insert(address.clone().into());
167-
new_hosts.insert(address.replace("127.0.0.1", "localhost").into());
167+
let address_string = address.to_string();
168+
169+
if address.ip() == IpAddr::V4(Ipv4Addr::UNSPECIFIED) {
170+
new_hosts.insert(address_string.replace("0.0.0.0", "127.0.0.1").into());
171+
new_hosts.insert(address_string.replace("0.0.0.0", "localhost").into());
172+
} else if address.ip() == IpAddr::V4(Ipv4Addr::LOCALHOST) {
173+
new_hosts.insert(address_string.replace("127.0.0.1", "localhost").into());
174+
}
175+
176+
new_hosts.insert(address_string.into());
168177
new_hosts.into_iter().collect()
169178
})
170179
}

0 commit comments

Comments
 (0)