Use hostname-validator crate for hostname validation
This commit is contained in:
parent
502b55d34b
commit
6a6931e2c3
|
@ -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"
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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)]
|
||||
|
|
Loading…
Reference in New Issue