Skip to content

Commit

Permalink
more domain validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Nutomic committed Jan 13, 2025
1 parent 4e2c5c1 commit dc82698
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ use bytes::Bytes;
use derive_builder::Builder;
use dyn_clone::{clone_trait_object, DynClone};
use moka::future::Cache;
use once_cell::sync::Lazy;
use regex::Regex;
use reqwest::Request;
use reqwest_middleware::{ClientWithMiddleware, RequestBuilder};
use rsa::{pkcs8::DecodePrivateKey, RsaPrivateKey};
Expand Down Expand Up @@ -107,6 +109,9 @@ pub struct FederationConfig<T: Clone> {
pub(crate) queue_retry_count: usize,
}

pub(crate) static DOMAIN_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r"^[a-zA-Z0-9.-]*$").expect("compile regex"));

impl<T: Clone> FederationConfig<T> {
/// Returns a new config builder with default values.
pub fn builder() -> FederationConfigBuilder<T> {
Expand Down Expand Up @@ -164,6 +169,9 @@ impl<T: Clone> FederationConfig<T> {
let Some(domain) = url.domain() else {
return Err(Error::UrlVerificationError("Url must have a domain"));
};
if !DOMAIN_REGEX.is_match(domain) {
return Err(Error::UrlVerificationError("Invalid characters in domain").into());
}

// Extra checks only for production mode
if !self.debug {
Expand Down
5 changes: 1 addition & 4 deletions src/fetch/webfinger.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
config::Data,
config::{Data, DOMAIN_REGEX},
error::Error,
fetch::{fetch_object_http_with_accept, object_id::ObjectId},
traits::{Actor, Object},
Expand Down Expand Up @@ -53,9 +53,6 @@ where
for<'de2> <Kind as Object>::Kind: serde::Deserialize<'de2>,
<Kind as Object>::Error: From<crate::error::Error> + Send + Sync + Display,
{
static DOMAIN_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r"^[a-zA-Z0-9.-]*$").expect("compile regex"));

let (_, domain) = identifier
.splitn(2, '@')
.collect_tuple()
Expand Down

0 comments on commit dc82698

Please sign in to comment.