Avoid crash when handling urls without domain

This commit is contained in:
Felix Ableitner 2024-04-30 00:29:49 +02:00
parent 492d8f1b01
commit 664603489b
5 changed files with 35 additions and 8 deletions

View File

@ -39,7 +39,10 @@ use lemmy_db_schema::{
},
traits::{Bannable, Crud, Followable},
};
use lemmy_utils::error::{LemmyError, LemmyResult};
use lemmy_utils::{
error::{LemmyError, LemmyResult},
LemmyErrorType,
};
use url::Url;
impl BlockUser {
@ -129,7 +132,11 @@ impl ActivityHandler for BlockUser {
verify_is_public(&self.to, &self.cc)?;
match self.target.dereference(context).await? {
SiteOrCommunity::Site(site) => {
let domain = self.object.inner().domain().expect("url needs domain");
let domain = self
.object
.inner()
.domain()
.ok_or(LemmyErrorType::UrlWithoutDomain)?;
if context.settings().hostname == domain {
return Err(
anyhow!("Site bans from remote instance can't affect user's home instance").into(),

View File

@ -78,7 +78,10 @@ impl UrlVerifier for VerifyUrlData {
/// - URL not being in the blocklist (if it is active)
#[tracing::instrument(skip(local_site_data))]
fn check_apub_id_valid(apub_id: &Url, local_site_data: &LocalSiteData) -> LemmyResult<()> {
let domain = apub_id.domain().expect("apud id has domain").to_string();
let domain = apub_id
.domain()
.ok_or(LemmyErrorType::UrlWithoutDomain)?
.to_string();
if !local_site_data
.local_site
@ -158,7 +161,10 @@ pub(crate) async fn check_apub_id_valid_with_strictness(
is_strict: bool,
context: &LemmyContext,
) -> LemmyResult<()> {
let domain = apub_id.domain().expect("apud id has domain").to_string();
let domain = apub_id
.domain()
.ok_or(LemmyErrorType::UrlWithoutDomain)?
.to_string();
let local_instance = context
.settings()
.get_hostname_without_port()
@ -185,7 +191,10 @@ pub(crate) async fn check_apub_id_valid_with_strictness(
.expect("local hostname is valid");
allowed_and_local.push(local_instance);
let domain = apub_id.domain().expect("apud id has domain").to_string();
let domain = apub_id
.domain()
.ok_or(LemmyErrorType::UrlWithoutDomain)?
.to_string();
if !allowed_and_local.contains(&domain) {
Err(LemmyErrorType::FederationDisabledByStrictAllowList)?
}

View File

@ -54,7 +54,10 @@ pub async fn collect_non_local_mentions(
name: Some(format!(
"@{}@{}",
&parent_creator.name,
&parent_creator.id().domain().expect("has domain")
&parent_creator
.id()
.domain()
.ok_or(LemmyErrorType::UrlWithoutDomain)?
)),
kind: MentionType::Mention,
};

View File

@ -45,6 +45,7 @@ use lemmy_utils::{
markdown::markdown_to_html,
slurs::{check_slurs, check_slurs_opt},
},
LemmyErrorType,
};
use std::ops::Deref;
use tracing::debug;
@ -137,7 +138,11 @@ impl Object for ApubSite {
#[tracing::instrument(skip_all)]
async fn from_json(apub: Self::Kind, context: &Data<Self::DataType>) -> LemmyResult<Self> {
let domain = apub.id.inner().domain().expect("group id has domain");
let domain = apub
.id
.inner()
.domain()
.ok_or(LemmyErrorType::UrlWithoutDomain)?;
let instance = DbInstance::read_or_create(&mut context.pool(), domain.to_string()).await?;
let local_site = LocalSite::read(&mut context.pool()).await.ok();
@ -210,7 +215,9 @@ pub(in crate::objects) async fn fetch_instance_actor_for_object<T: Into<Url> + C
Err(e) => {
// Failed to fetch instance actor, its probably not a lemmy instance
debug!("Failed to dereference site for {}: {}", &instance_id, e);
let domain = instance_id.domain().expect("has domain");
let domain = instance_id
.domain()
.ok_or(LemmyErrorType::UrlWithoutDomain)?;
Ok(
DbInstance::read_or_create(&mut context.pool(), domain.to_string())
.await?

View File

@ -176,6 +176,7 @@ pub enum LemmyErrorType {
InvalidUnixTime,
InvalidBotAction,
CantBlockLocalInstance,
UrlWithoutDomain,
Unknown(String),
}