Use hostname-validator crate for hostname validation
This commit is contained in:
parent
502b55d34b
commit
6a6931e2c3
|
@ -154,6 +154,7 @@ dependencies = [
|
||||||
"educe",
|
"educe",
|
||||||
"fs-mistrust",
|
"fs-mistrust",
|
||||||
"futures",
|
"futures",
|
||||||
|
"hostname-validator",
|
||||||
"humantime-serde",
|
"humantime-serde",
|
||||||
"libc",
|
"libc",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
|
@ -1489,6 +1490,12 @@ dependencies = [
|
||||||
"digest 0.10.3",
|
"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]]
|
[[package]]
|
||||||
name = "http"
|
name = "http"
|
||||||
version = "0.2.8"
|
version = "0.2.8"
|
||||||
|
|
|
@ -60,6 +60,7 @@ directories = "4"
|
||||||
educe = "0.4.6"
|
educe = "0.4.6"
|
||||||
fs-mistrust = { path = "../fs-mistrust", version = "0.5.0", features = ["serde"] }
|
fs-mistrust = { path = "../fs-mistrust", version = "0.5.0", features = ["serde"] }
|
||||||
futures = "0.3.14"
|
futures = "0.3.14"
|
||||||
|
hostname-validator = "1.1.1"
|
||||||
humantime-serde = "1.1.1"
|
humantime-serde = "1.1.1"
|
||||||
libc = "0.2"
|
libc = "0.2"
|
||||||
pin-project = "1"
|
pin-project = "1"
|
||||||
|
|
|
@ -363,25 +363,8 @@ impl DangerouslyIntoTorAddr for SocketAddrV6 {
|
||||||
/// Check whether `hostname` is a valid hostname or not.
|
/// Check whether `hostname` is a valid hostname or not.
|
||||||
///
|
///
|
||||||
/// (Note that IPv6 addresses don't follow these rules.)
|
/// (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 {
|
fn is_valid_hostname(hostname: &str) -> bool {
|
||||||
/// Check if we have the valid characters for a hostname
|
hostname_validator::is_valid(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())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
Loading…
Reference in New Issue