From 6a6931e2c338f6a4ebb52aa7149a1ac688c866a5 Mon Sep 17 00:00:00 2001 From: Reylaba Date: Thu, 22 Sep 2022 15:48:51 +0200 Subject: [PATCH] Use hostname-validator crate for hostname validation --- Cargo.lock | 7 +++++++ crates/arti-client/Cargo.toml | 1 + crates/arti-client/src/address.rs | 19 +------------------ 3 files changed, 9 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fbed7cdfa..8b10c2511 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -154,6 +154,7 @@ dependencies = [ "educe", "fs-mistrust", "futures", + "hostname-validator", "humantime-serde", "libc", "once_cell", @@ -1489,6 +1490,12 @@ dependencies = [ "digest 0.10.3", ] +[[package]] +name = "hostname-validator" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f558a64ac9af88b5ba400d99b579451af0d39c6d360980045b91aac966d705e2" + [[package]] name = "http" version = "0.2.8" diff --git a/crates/arti-client/Cargo.toml b/crates/arti-client/Cargo.toml index d6601f9d8..6bbb5f39e 100644 --- a/crates/arti-client/Cargo.toml +++ b/crates/arti-client/Cargo.toml @@ -60,6 +60,7 @@ directories = "4" educe = "0.4.6" fs-mistrust = { path = "../fs-mistrust", version = "0.5.0", features = ["serde"] } futures = "0.3.14" +hostname-validator = "1.1.1" humantime-serde = "1.1.1" libc = "0.2" pin-project = "1" diff --git a/crates/arti-client/src/address.rs b/crates/arti-client/src/address.rs index 08cd71fe5..a68b8381b 100644 --- a/crates/arti-client/src/address.rs +++ b/crates/arti-client/src/address.rs @@ -363,25 +363,8 @@ impl DangerouslyIntoTorAddr for SocketAddrV6 { /// Check whether `hostname` is a valid hostname or not. /// /// (Note that IPv6 addresses don't follow these rules.) -/// -/// TODO: Check whether the rules given here are in fact the same rules -/// as Tor follows, and whether they conform to anything. fn is_valid_hostname(hostname: &str) -> bool { - /// Check if we have the valid characters for a hostname - fn is_valid_char(byte: u8) -> bool { - ((b'a'..=b'z').contains(&byte)) - || ((b'A'..=b'Z').contains(&byte)) - || ((b'0'..=b'9').contains(&byte)) - || byte == b'-' - || byte == b'.' - } - - !(hostname.bytes().any(|byte| !is_valid_char(byte)) - || hostname.ends_with('-') - || hostname.starts_with('-') - || hostname.ends_with('.') - || hostname.starts_with('.') - || hostname.is_empty()) + hostname_validator::is_valid(hostname) } #[cfg(test)]