Make verify apub url function async (#2514)

* Make verify apub url function async

* cleanup

* use dep from crates.io

* dont use unwrap
This commit is contained in:
Nutomic 2022-10-28 13:38:22 +00:00 committed by GitHub
parent 235cc8b228
commit b0c3185663
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 49 additions and 127 deletions

11
Cargo.lock generated
View file

@ -4,9 +4,9 @@ version = 3
[[package]]
name = "activitypub_federation"
version = "0.2.2"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "690ed975ab70b883b4f0776f60fd7f23a7484a49f4257e7672e64d0990e95771"
checksum = "dd9ae511df7135c271dca3ef3751f5528891c965e47d8d7a70fed9d2f1e5b6b1"
dependencies = [
"activitypub_federation_derive",
"actix-web",
@ -16,6 +16,7 @@ dependencies = [
"base64",
"chrono",
"derive_builder 0.11.2",
"dyn-clone",
"http",
"http-signature-normalization-actix",
"http-signature-normalization-reqwest",
@ -1219,6 +1220,12 @@ dependencies = [
"syn 1.0.100",
]
[[package]]
name = "dyn-clone"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4f94fa09c2aeea5b8839e414b7b841bf429fd25b9c522116ac97ee87856d88b2"
[[package]]
name = "either"
version = "1.8.0"

View file

@ -49,7 +49,7 @@ lemmy_db_schema = { version = "=0.16.5", path = "./crates/db_schema" }
lemmy_api_common = { version = "=0.16.5", path = "crates/api_common" }
lemmy_websocket = { version = "=0.16.5", path = "./crates/websocket" }
lemmy_routes = { version = "=0.16.5", path = "./crates/routes" }
activitypub_federation = "0.2.0"
activitypub_federation = "0.2.3"
diesel = "2.0.0"
diesel_migrations = "2.0.0"
serde = { version = "1.0.145", features = ["derive"] }

View file

@ -22,7 +22,7 @@ lemmy_db_views_moderator = { version = "=0.16.5", path = "../db_views_moderator"
lemmy_db_views_actor = { version = "=0.16.5", path = "../db_views_actor", features = ["full"] }
lemmy_api_common = { version = "=0.16.5", path = "../api_common", features = ["full"] }
lemmy_websocket = { version = "=0.16.5", path = "../websocket" }
activitypub_federation = { version = "0.2.2" }
activitypub_federation = "0.2.3"
diesel = "2.0.0"
bcrypt = "0.13.0"
chrono = { version = "0.4.22", features = ["serde"], default-features = false }

View file

@ -16,7 +16,7 @@ lemmy_db_views = { version = "=0.16.5", path = "../db_views", features = ["full"
lemmy_db_views_actor = { version = "=0.16.5", path = "../db_views_actor", features = ["full"] }
lemmy_api_common = { version = "=0.16.5", path = "../api_common", features = ["full"] }
lemmy_websocket = { version = "=0.16.5", path = "../websocket" }
activitypub_federation = { version = "0.2.2" }
activitypub_federation = "0.2.3"
bcrypt = "0.13.0"
serde_json = { version = "1.0.85", features = ["preserve_order"] }
serde = { version = "1.0.145", features = ["derive"] }

View file

@ -20,7 +20,7 @@ lemmy_db_views = { version = "=0.16.5", path = "../db_views", features = ["full"
lemmy_db_views_actor = { version = "=0.16.5", path = "../db_views_actor", features = ["full"] }
lemmy_api_common = { version = "=0.16.5", path = "../api_common", features = ["full"] }
lemmy_websocket = { version = "=0.16.5", path = "../websocket" }
activitypub_federation = { version = "0.2.2" }
activitypub_federation = "0.2.3"
diesel = "2.0.0"
activitystreams-kinds = "0.2.1"
chrono = { version = "0.4.22", features = ["serde"], default-features = false }

View file

@ -9,8 +9,6 @@ use crate::{
verify_person_in_community,
},
activity_lists::AnnouncableActivities,
check_apub_id_valid,
fetch_local_site_data,
local_instance,
objects::{community::ApubCommunity, instance::remote_instance_inboxes, person::ApubPerson},
protocol::activities::block::block_user::BlockUser,
@ -125,10 +123,6 @@ impl ActivityHandler for BlockUser {
context: &Data<LemmyContext>,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let local_site_data = blocking(context.pool(), fetch_local_site_data).await??;
check_apub_id_valid(self.id(), &local_site_data, context.settings())
.map_err(LemmyError::from_message)?;
verify_is_public(&self.to, &self.cc)?;
match self
.target

View file

@ -1,19 +1,3 @@
use crate::{
activities::{
block::{generate_cc, SiteOrCommunity},
community::{announce::GetCommunity, send_activity_in_community},
generate_activity_id,
send_lemmy_activity,
verify_is_public,
},
activity_lists::AnnouncableActivities,
check_apub_id_valid,
fetch_local_site_data,
local_instance,
objects::{community::ApubCommunity, instance::remote_instance_inboxes, person::ApubPerson},
protocol::activities::block::{block_user::BlockUser, undo_block_user::UndoBlockUser},
ActorType,
};
use activitypub_federation::{
core::object_id::ObjectId,
data::Data,
@ -34,6 +18,21 @@ use lemmy_utils::error::LemmyError;
use lemmy_websocket::LemmyContext;
use url::Url;
use crate::{
activities::{
block::{generate_cc, SiteOrCommunity},
community::{announce::GetCommunity, send_activity_in_community},
generate_activity_id,
send_lemmy_activity,
verify_is_public,
},
activity_lists::AnnouncableActivities,
local_instance,
objects::{community::ApubCommunity, instance::remote_instance_inboxes, person::ApubPerson},
protocol::activities::block::{block_user::BlockUser, undo_block_user::UndoBlockUser},
ActorType,
};
impl UndoBlockUser {
#[tracing::instrument(skip_all)]
pub async fn send(
@ -92,10 +91,6 @@ impl ActivityHandler for UndoBlockUser {
context: &Data<LemmyContext>,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let local_site_data = blocking(context.pool(), fetch_local_site_data).await??;
check_apub_id_valid(self.id(), &local_site_data, context.settings())
.map_err(LemmyError::from_message)?;
verify_is_public(&self.to, &self.cc)?;
verify_domains_match(self.actor.inner(), self.object.actor.inner())?;
self.object.verify(context, request_counter).await?;

View file

@ -12,8 +12,6 @@ use crate::{
verify_person_in_community,
},
activity_lists::AnnouncableActivities,
check_apub_id_valid,
fetch_local_site_data,
generate_moderators_url,
local_instance,
objects::{community::ApubCommunity, person::ApubPerson},
@ -86,10 +84,6 @@ impl ActivityHandler for AddMod {
context: &Data<LemmyContext>,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let local_site_data = blocking(context.pool(), fetch_local_site_data).await??;
check_apub_id_valid(self.id(), &local_site_data, context.settings())
.map_err(LemmyError::from_message)?;
verify_is_public(&self.to, &self.cc)?;
let community = self.get_community(context, request_counter).await?;
verify_person_in_community(&self.actor, &community, context, request_counter).await?;

View file

@ -1,8 +1,6 @@
use crate::{
activities::{generate_activity_id, send_lemmy_activity, verify_is_public},
activity_lists::AnnouncableActivities,
check_apub_id_valid,
fetch_local_site_data,
insert_activity,
objects::community::ApubCommunity,
protocol::{
@ -13,7 +11,6 @@ use crate::{
};
use activitypub_federation::{core::object_id::ObjectId, data::Data, traits::ActivityHandler};
use activitystreams_kinds::{activity::AnnounceType, public};
use lemmy_api_common::utils::blocking;
use lemmy_utils::error::LemmyError;
use lemmy_websocket::LemmyContext;
use tracing::debug;
@ -87,13 +84,9 @@ impl ActivityHandler for AnnounceActivity {
#[tracing::instrument(skip_all)]
async fn verify(
&self,
context: &Data<LemmyContext>,
_context: &Data<LemmyContext>,
_request_counter: &mut i32,
) -> Result<(), LemmyError> {
let local_site_data = blocking(context.pool(), fetch_local_site_data).await??;
check_apub_id_valid(self.id(), &local_site_data, context.settings())
.map_err(LemmyError::from_message)?;
verify_is_public(&self.to, &self.cc)?;
Ok(())
}

View file

@ -12,8 +12,6 @@ use crate::{
verify_person_in_community,
},
activity_lists::AnnouncableActivities,
check_apub_id_valid,
fetch_local_site_data,
generate_moderators_url,
local_instance,
objects::{community::ApubCommunity, person::ApubPerson},
@ -86,10 +84,6 @@ impl ActivityHandler for RemoveMod {
context: &Data<LemmyContext>,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let local_site_data = blocking(context.pool(), fetch_local_site_data).await??;
check_apub_id_valid(self.id(), &local_site_data, context.settings())
.map_err(LemmyError::from_message)?;
verify_is_public(&self.to, &self.cc)?;
let community = self.get_community(context, request_counter).await?;
verify_person_in_community(&self.actor, &community, context, request_counter).await?;

View file

@ -1,7 +1,5 @@
use crate::{
activities::{generate_activity_id, send_lemmy_activity, verify_person_in_community},
check_apub_id_valid,
fetch_local_site_data,
local_instance,
objects::{community::ApubCommunity, person::ApubPerson},
protocol::activities::community::report::Report,
@ -76,10 +74,6 @@ impl ActivityHandler for Report {
context: &Data<LemmyContext>,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let local_site_data = blocking(context.pool(), fetch_local_site_data).await??;
check_apub_id_valid(self.id(), &local_site_data, context.settings())
.map_err(LemmyError::from_message)?;
let community = self.to[0]
.dereference(context, local_instance(context), request_counter)
.await?;

View file

@ -7,8 +7,6 @@ use crate::{
verify_person_in_community,
},
activity_lists::AnnouncableActivities,
check_apub_id_valid,
fetch_local_site_data,
local_instance,
objects::{community::ApubCommunity, person::ApubPerson},
protocol::activities::community::update::UpdateCommunity,
@ -71,9 +69,6 @@ impl ActivityHandler for UpdateCommunity {
context: &Data<LemmyContext>,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let local_site_data = blocking(context.pool(), fetch_local_site_data).await??;
check_apub_id_valid(self.id(), &local_site_data, context.settings())
.map_err(LemmyError::from_message)?;
verify_is_public(&self.to, &self.cc)?;
let community = self.get_community(context, request_counter).await?;
verify_person_in_community(&self.actor, &community, context, request_counter).await?;

View file

@ -8,8 +8,6 @@ use crate::{
verify_person_in_community,
},
activity_lists::AnnouncableActivities,
check_apub_id_valid,
fetch_local_site_data,
local_instance,
mentions::MentionOrValue,
objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson},
@ -117,10 +115,6 @@ impl ActivityHandler for CreateOrUpdateComment {
context: &Data<LemmyContext>,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let local_site_data = blocking(context.pool(), fetch_local_site_data).await??;
check_apub_id_valid(self.id(), &local_site_data, context.settings())
.map_err(LemmyError::from_message)?;
verify_is_public(&self.to, &self.cc)?;
let post = self.object.get_parents(context, request_counter).await?.0;
let community = self.get_community(context, request_counter).await?;

View file

@ -8,8 +8,6 @@ use crate::{
verify_person_in_community,
},
activity_lists::AnnouncableActivities,
check_apub_id_valid,
fetch_local_site_data,
objects::{community::ApubCommunity, person::ApubPerson, post::ApubPost},
protocol::activities::{create_or_update::post::CreateOrUpdatePost, CreateOrUpdateType},
ActorType,
@ -95,9 +93,6 @@ impl ActivityHandler for CreateOrUpdatePost {
context: &Data<LemmyContext>,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let local_site_data = blocking(context.pool(), fetch_local_site_data).await??;
check_apub_id_valid(self.id(), &local_site_data, context.settings())
.map_err(LemmyError::from_message)?;
verify_is_public(&self.to, &self.cc)?;
let community = self.get_community(context, request_counter).await?;
verify_person_in_community(&self.actor, &community, context, request_counter).await?;

View file

@ -1,7 +1,5 @@
use crate::{
activities::{generate_activity_id, send_lemmy_activity, verify_person},
check_apub_id_valid,
fetch_local_site_data,
objects::{person::ApubPerson, private_message::ApubPrivateMessage},
protocol::activities::{
create_or_update::private_message::CreateOrUpdatePrivateMessage,
@ -71,10 +69,6 @@ impl ActivityHandler for CreateOrUpdatePrivateMessage {
context: &Data<LemmyContext>,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let local_site_data = blocking(context.pool(), fetch_local_site_data).await??;
check_apub_id_valid(self.id(), &local_site_data, context.settings())
.map_err(LemmyError::from_message)?;
verify_person(&self.actor, context, request_counter).await?;
verify_domains_match(self.actor.inner(), self.object.id.inner())?;
verify_domains_match(self.to[0].inner(), self.object.to[0].inner())?;

View file

@ -4,8 +4,6 @@ use crate::{
deletion::{receive_delete_action, verify_delete_activity, DeletableObjects},
generate_activity_id,
},
check_apub_id_valid,
fetch_local_site_data,
local_instance,
objects::{community::ApubCommunity, person::ApubPerson},
protocol::{activities::deletion::delete::Delete, IdOrNestedObject},
@ -57,9 +55,6 @@ impl ActivityHandler for Delete {
context: &Data<LemmyContext>,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let local_site_data = blocking(context.pool(), fetch_local_site_data).await??;
check_apub_id_valid(self.id(), &local_site_data, context.settings())
.map_err(LemmyError::from_message)?;
verify_delete_activity(self, self.summary.is_some(), context, request_counter).await?;
Ok(())
}

View file

@ -1,7 +1,5 @@
use crate::{
activities::{generate_activity_id, send_lemmy_activity, verify_is_public, verify_person},
check_apub_id_valid,
fetch_local_site_data,
local_instance,
objects::{instance::remote_instance_inboxes, person::ApubPerson},
protocol::activities::deletion::delete_user::DeleteUser,
@ -13,7 +11,7 @@ use activitypub_federation::{
utils::verify_urls_match,
};
use activitystreams_kinds::{activity::DeleteType, public};
use lemmy_api_common::utils::{blocking, delete_user_account};
use lemmy_api_common::utils::delete_user_account;
use lemmy_utils::error::LemmyError;
use lemmy_websocket::LemmyContext;
use url::Url;
@ -38,9 +36,6 @@ impl ActivityHandler for DeleteUser {
context: &Data<LemmyContext>,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let local_site_data = blocking(context.pool(), fetch_local_site_data).await??;
check_apub_id_valid(self.id(), &local_site_data, context.settings())
.map_err(LemmyError::from_message)?;
verify_is_public(&self.to, &[])?;
verify_person(&self.actor, context, request_counter).await?;
verify_urls_match(self.actor.inner(), self.object.inner())?;

View file

@ -4,8 +4,6 @@ use crate::{
deletion::{receive_delete_action, verify_delete_activity, DeletableObjects},
generate_activity_id,
},
check_apub_id_valid,
fetch_local_site_data,
local_instance,
objects::{community::ApubCommunity, person::ApubPerson},
protocol::activities::deletion::{delete::Delete, undo_delete::UndoDelete},
@ -56,9 +54,6 @@ impl ActivityHandler for UndoDelete {
context: &Data<LemmyContext>,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let local_site_data = blocking(context.pool(), fetch_local_site_data).await??;
check_apub_id_valid(self.id(), &local_site_data, context.settings())
.map_err(LemmyError::from_message)?;
self.object.verify(context, request_counter).await?;
verify_delete_activity(
&self.object,

View file

@ -1,7 +1,5 @@
use crate::{
activities::{generate_activity_id, send_lemmy_activity},
check_apub_id_valid,
fetch_local_site_data,
local_instance,
protocol::activities::following::{accept::AcceptFollowCommunity, follow::FollowCommunity},
ActorType,
@ -69,10 +67,6 @@ impl ActivityHandler for AcceptFollowCommunity {
context: &Data<LemmyContext>,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let local_site_data = blocking(context.pool(), fetch_local_site_data).await??;
check_apub_id_valid(self.id(), &local_site_data, context.settings())
.map_err(LemmyError::from_message)?;
verify_urls_match(self.actor.inner(), self.object.object.inner())?;
self.object.verify(context, request_counter).await?;
Ok(())

View file

@ -5,8 +5,6 @@ use crate::{
verify_person,
verify_person_in_community,
},
check_apub_id_valid,
fetch_local_site_data,
local_instance,
objects::{community::ApubCommunity, person::ApubPerson},
protocol::activities::following::{accept::AcceptFollowCommunity, follow::FollowCommunity},
@ -86,9 +84,6 @@ impl ActivityHandler for FollowCommunity {
context: &Data<LemmyContext>,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let local_site_data = blocking(context.pool(), fetch_local_site_data).await??;
check_apub_id_valid(self.id(), &local_site_data, context.settings())
.map_err(LemmyError::from_message)?;
verify_person(&self.actor, context, request_counter).await?;
let community = self
.object

View file

@ -1,7 +1,5 @@
use crate::{
activities::{generate_activity_id, send_lemmy_activity, verify_person},
check_apub_id_valid,
fetch_local_site_data,
local_instance,
objects::{community::ApubCommunity, person::ApubPerson},
protocol::activities::following::{follow::FollowCommunity, undo_follow::UndoFollowCommunity},
@ -65,9 +63,6 @@ impl ActivityHandler for UndoFollowCommunity {
context: &Data<LemmyContext>,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let local_site_data = blocking(context.pool(), fetch_local_site_data).await??;
check_apub_id_valid(self.id(), &local_site_data, context.settings())
.map_err(LemmyError::from_message)?;
verify_urls_match(self.actor.inner(), self.object.actor.inner())?;
verify_person(&self.actor, context, request_counter).await?;
self.object.verify(context, request_counter).await?;

View file

@ -6,8 +6,6 @@ use crate::{
voting::{undo_vote_comment, undo_vote_post},
},
activity_lists::AnnouncableActivities,
check_apub_id_valid,
fetch_local_site_data,
local_instance,
objects::{community::ApubCommunity, person::ApubPerson},
protocol::activities::voting::{
@ -84,9 +82,6 @@ impl ActivityHandler for UndoVote {
context: &Data<LemmyContext>,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let local_site_data = blocking(context.pool(), fetch_local_site_data).await??;
check_apub_id_valid(self.id(), &local_site_data, context.settings())
.map_err(LemmyError::from_message)?;
let community = self.get_community(context, request_counter).await?;
verify_person_in_community(&self.actor, &community, context, request_counter).await?;
verify_urls_match(self.actor.inner(), self.object.actor.inner())?;

View file

@ -6,8 +6,6 @@ use crate::{
voting::{vote_comment, vote_post},
},
activity_lists::AnnouncableActivities,
check_apub_id_valid,
fetch_local_site_data,
local_instance,
objects::{community::ApubCommunity, person::ApubPerson},
protocol::activities::voting::vote::{Vote, VoteType},
@ -83,9 +81,6 @@ impl ActivityHandler for Vote {
context: &Data<LemmyContext>,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let local_site_data = blocking(context.pool(), fetch_local_site_data).await??;
check_apub_id_valid(self.id(), &local_site_data, context.settings())
.map_err(LemmyError::from_message)?;
let community = self.get_community(context, request_counter).await?;
verify_person_in_community(&self.actor, &community, context, request_counter).await?;
let enable_downvotes = blocking(context.pool(), LocalSite::read)

View file

@ -4,8 +4,10 @@ use activitypub_federation::{
traits::{Actor, ApubObject},
InstanceSettings,
LocalInstance,
UrlVerifier,
};
use anyhow::Context;
use async_trait::async_trait;
use diesel::PgConnection;
use lemmy_api_common::utils::blocking;
use lemmy_db_schema::{
@ -59,9 +61,8 @@ fn local_instance(context: &LemmyContext) -> &'static LocalInstance {
.http_fetch_retry_limit(http_fetch_retry_limit)
.worker_count(worker_count)
.debug(federation_debug)
// TODO No idea why, but you can't pass context.settings() to the verify_url_function closure
// without the value getting captured.
.http_signature_compat(true)
.url_verifier(Box::new(VerifyUrlData(context.clone())))
.build()
.expect("configure federation");
LocalInstance::new(
@ -72,6 +73,20 @@ fn local_instance(context: &LemmyContext) -> &'static LocalInstance {
})
}
#[derive(Clone)]
struct VerifyUrlData(LemmyContext);
#[async_trait]
impl UrlVerifier for VerifyUrlData {
async fn verify(&self, url: &Url) -> Result<(), &'static str> {
let local_site_data = blocking(self.0.pool(), fetch_local_site_data)
.await
.expect("read local site data")
.expect("read local site data");
check_apub_id_valid(url, &local_site_data, self.0.settings())
}
}
/// Checks if the ID is allowed for sending or receiving.
///
/// In particular, it checks for:
@ -83,7 +98,6 @@ fn local_instance(context: &LemmyContext) -> &'static LocalInstance {
/// `use_strict_allowlist` should be true only when parsing a remote community, or when parsing a
/// post/comment in a local community.
#[tracing::instrument(skip(settings, local_site_data))]
// TODO This function needs to be called by incoming activities
fn check_apub_id_valid(
apub_id: &Url,
local_site_data: &LocalSiteData,

View file

@ -24,7 +24,7 @@ url = { version = "2.3.1", features = ["serde"] }
strum = "0.24.1"
strum_macros = "0.24.3"
serde_json = { version = "1.0.85", features = ["preserve_order"], optional = true }
activitypub_federation = { version = "0.2.2", optional = true }
activitypub_federation = { version = "0.2.3", optional = true }
lemmy_utils = { version = "=0.16.5", path = "../utils", optional = true }
bcrypt = { version = "0.13.0", optional = true }
diesel = { version = "2.0.0", features = ["postgres","chrono","r2d2","serde_json"], optional = true }

@ -1 +1 @@
Subproject commit 454debaede4cc932ac15fea9bf620cf1daf1ae4c
Subproject commit f5d6f0eabafd559417bf8f203fd655f7858bffcf