fix once lock for domain regex
This commit is contained in:
parent
2fb79a2524
commit
c16f89d23a
2 changed files with 8 additions and 6 deletions
|
|
@ -26,7 +26,6 @@ use bytes::Bytes;
|
||||||
use derive_builder::Builder;
|
use derive_builder::Builder;
|
||||||
use dyn_clone::{clone_trait_object, DynClone};
|
use dyn_clone::{clone_trait_object, DynClone};
|
||||||
use moka::future::Cache;
|
use moka::future::Cache;
|
||||||
use once_cell::sync::Lazy;
|
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use reqwest::{redirect::Policy, Client, Request};
|
use reqwest::{redirect::Policy, Client, Request};
|
||||||
use reqwest_middleware::{ClientWithMiddleware, RequestBuilder};
|
use reqwest_middleware::{ClientWithMiddleware, RequestBuilder};
|
||||||
|
|
@ -38,6 +37,7 @@ use std::{
|
||||||
sync::{
|
sync::{
|
||||||
atomic::{AtomicU32, Ordering},
|
atomic::{AtomicU32, Ordering},
|
||||||
Arc,
|
Arc,
|
||||||
|
OnceLock,
|
||||||
},
|
},
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
@ -114,8 +114,10 @@ pub struct FederationConfig<T: Clone> {
|
||||||
pub(crate) queue_retry_count: usize,
|
pub(crate) queue_retry_count: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) static DOMAIN_REGEX: Lazy<Regex> =
|
pub(crate) fn domain_regex() -> &'static Regex {
|
||||||
Lazy::new(|| Regex::new(r"^[a-zA-Z0-9.-]*$").expect("compile regex"));
|
static DOMAIN_REGEX: OnceLock<Regex> = OnceLock::new();
|
||||||
|
DOMAIN_REGEX.get_or_init(|| Regex::new(r"^[a-zA-Z0-9.-]*$").expect("compile regex"))
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: Clone> FederationConfig<T> {
|
impl<T: Clone> FederationConfig<T> {
|
||||||
/// Returns a new config builder with default values.
|
/// Returns a new config builder with default values.
|
||||||
|
|
@ -174,7 +176,7 @@ impl<T: Clone> FederationConfig<T> {
|
||||||
let Some(domain) = url.domain() else {
|
let Some(domain) = url.domain() else {
|
||||||
return Err(Error::UrlVerificationError("Url must have a domain"));
|
return Err(Error::UrlVerificationError("Url must have a domain"));
|
||||||
};
|
};
|
||||||
if !DOMAIN_REGEX.is_match(domain) {
|
if !domain_regex().is_match(domain) {
|
||||||
return Err(Error::UrlVerificationError("Invalid characters in domain"));
|
return Err(Error::UrlVerificationError("Invalid characters in domain"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
config::{Data, DOMAIN_REGEX},
|
config::{domain_regex, Data},
|
||||||
error::Error,
|
error::Error,
|
||||||
fetch::{fetch_object_http_with_accept, object_id::ObjectId},
|
fetch::{fetch_object_http_with_accept, object_id::ObjectId},
|
||||||
traits::{Actor, Object},
|
traits::{Actor, Object},
|
||||||
|
|
@ -55,7 +55,7 @@ where
|
||||||
.ok_or(WebFingerError::WrongFormat.into_crate_error())?;
|
.ok_or(WebFingerError::WrongFormat.into_crate_error())?;
|
||||||
|
|
||||||
// For production mode make sure that domain doesnt contain any port or path.
|
// For production mode make sure that domain doesnt contain any port or path.
|
||||||
if !data.config.debug && !DOMAIN_REGEX.is_match(domain) {
|
if !data.config.debug && !domain_regex().is_match(domain) {
|
||||||
return Err(Error::UrlVerificationError("Invalid characters in domain").into());
|
return Err(Error::UrlVerificationError("Invalid characters in domain").into());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue