From ecd3061d822457da4cccc0b2038771444637641e Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Thu, 14 Jan 2021 22:06:21 +0100 Subject: [PATCH] Don't send activities to ourselves --- lemmy_apub/src/activity_queue.rs | 9 ++++++++- lemmy_apub/src/lib.rs | 8 +------- lemmy_utils/src/settings.rs | 17 +++++++++++++++++ 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/lemmy_apub/src/activity_queue.rs b/lemmy_apub/src/activity_queue.rs index a8f2ab180..ff792c4da 100644 --- a/lemmy_apub/src/activity_queue.rs +++ b/lemmy_apub/src/activity_queue.rs @@ -219,6 +219,13 @@ where return Ok(()); } + // Don't send anything to ourselves + let hostname = Settings::get().get_hostname_without_port()?; + let inboxes: Vec<&Url> = inboxes + .iter() + .filter(|i| i.domain().unwrap() != hostname) + .collect(); + let activity = activity.into_any_base()?; let serialised_activity = serde_json::to_string(&activity)?; @@ -232,7 +239,7 @@ where for i in inboxes { let message = SendActivityTask { activity: serialised_activity.to_owned(), - inbox: i, + inbox: i.to_owned(), actor_id: actor.actor_id()?, private_key: actor.private_key().context(location_info!())?, }; diff --git a/lemmy_apub/src/lib.rs b/lemmy_apub/src/lib.rs index bbf1bbbc7..d5d682a7d 100644 --- a/lemmy_apub/src/lib.rs +++ b/lemmy_apub/src/lib.rs @@ -61,13 +61,7 @@ pub static APUB_JSON_CONTENT_TYPE: &str = "application/activity+json"; fn check_is_apub_id_valid(apub_id: &Url) -> Result<(), LemmyError> { let settings = Settings::get(); let domain = apub_id.domain().context(location_info!())?.to_string(); - let local_instance = settings - .hostname - .split(':') - .collect::>() - .first() - .context(location_info!())? - .to_string(); + let local_instance = settings.get_hostname_without_port()?; if !settings.federation.enabled { return if domain == local_instance { diff --git a/lemmy_utils/src/settings.rs b/lemmy_utils/src/settings.rs index 4ca87f282..4877d46b3 100644 --- a/lemmy_utils/src/settings.rs +++ b/lemmy_utils/src/settings.rs @@ -1,3 +1,5 @@ +use crate::location_info; +use anyhow::Context; use config::{Config, ConfigError, Environment, File}; use serde::Deserialize; use std::{env, fs, io::Error, net::IpAddr, path::PathBuf, sync::RwLock}; @@ -178,6 +180,21 @@ impl Settings { format!("{}://{}", self.get_protocol_string(), self.hostname) } + /// When running the federation test setup in `api_tests/` or `docker/federation`, the `hostname` + /// variable will be like `lemmy-alpha:8541`. This method removes the port and returns + /// `lemmy-alpha` instead. It has no effect in production. + pub fn get_hostname_without_port(&self) -> Result { + Ok( + self + .hostname + .split(':') + .collect::>() + .first() + .context(location_info!())? + .to_string(), + ) + } + pub fn save_config_file(data: &str) -> Result { fs::write(CONFIG_FILE, data)?;