fix: stricter URLs
This commit is contained in:
parent
a9714fe51a
commit
ee7c67dfbf
26
src/main.rs
26
src/main.rs
|
@ -231,6 +231,13 @@ mod service {
|
||||||
is_https: bool,
|
is_https: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Validator)]
|
||||||
|
#[validator(domain(ipv4(Allow), local(NotAllow), at_least_two_labels(Must), port(Allow)))]
|
||||||
|
pub struct Domain {
|
||||||
|
domain: String,
|
||||||
|
port: Option<u16>,
|
||||||
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for HttpUrl {
|
impl std::fmt::Display for HttpUrl {
|
||||||
fn fmt(&self, f: &mut validators_prelude::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut validators_prelude::Formatter<'_>) -> std::fmt::Result {
|
||||||
self.url.fmt(f)
|
self.url.fmt(f)
|
||||||
|
@ -238,19 +245,16 @@ mod service {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HttpUrl {
|
impl HttpUrl {
|
||||||
/// Transform this into an `Err(())` if the url does not match more
|
/// Transform this into an `Err(())` if the url is not a `Domain`.
|
||||||
/// criteria.
|
|
||||||
pub fn strict(self) -> Result<Self, ()> {
|
pub fn strict(self) -> Result<Self, ()> {
|
||||||
// Don't even bother with URLs that don't have hosts.
|
match self.url.domain() {
|
||||||
if !self.url.has_host() {
|
None => return Err(()),
|
||||||
return Err(());
|
Some(domain) => {
|
||||||
|
if Domain::parse_string(domain).is_err() {
|
||||||
|
return Err(());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// URLs that cannot be a base are weird (UNIX sockets, data types)
|
|
||||||
if self.url.cannot_be_a_base() {
|
|
||||||
return Err(())
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue