Use hostname-validator crate for hostname validation

This commit is contained in:
Reylaba 2022-09-22 15:48:51 +02:00
parent 502b55d34b
commit 6a6931e2c3
3 changed files with 9 additions and 18 deletions

7
Cargo.lock generated
View File

@ -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"

View File

@ -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"

View File

@ -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)]