rework following verify(), fix tests and test manually

This commit is contained in:
Felix Ableitner 2021-07-10 01:33:18 +02:00
parent 184517c969
commit ddf480d6e2
10 changed files with 76 additions and 56 deletions

View file

@ -31,7 +31,7 @@ impl ActivityHandlerNew for LikeComment {
request_counter: &mut i32, request_counter: &mut i32,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
like_or_dislike_comment( like_or_dislike_comment(
-1, 1,
&self.common.actor, &self.common.actor,
&self.object, &self.object,
context, context,

View file

@ -1,11 +1,11 @@
use crate::activities::following::follow::FollowCommunity; use crate::activities::{following::follow::FollowCommunity, verify_activity, verify_community};
use activitystreams::activity::kind::AcceptType; use activitystreams::activity::kind::AcceptType;
use lemmy_api_common::blocking; use lemmy_api_common::blocking;
use lemmy_apub::{ use lemmy_apub::fetcher::{
check_is_apub_id_valid, community::get_or_fetch_and_upsert_community,
fetcher::{community::get_or_fetch_and_upsert_community, person::get_or_fetch_and_upsert_person}, person::get_or_fetch_and_upsert_person,
}; };
use lemmy_apub_lib::{verify_domains_match, ActivityCommonFields, ActivityHandlerNew}; use lemmy_apub_lib::{verify_urls_match, ActivityCommonFields, ActivityHandlerNew};
use lemmy_db_queries::Followable; use lemmy_db_queries::Followable;
use lemmy_db_schema::source::community::CommunityFollower; use lemmy_db_schema::source::community::CommunityFollower;
use lemmy_utils::LemmyError; use lemmy_utils::LemmyError;
@ -31,9 +31,12 @@ impl ActivityHandlerNew for AcceptFollowCommunity {
context: &LemmyContext, context: &LemmyContext,
request_counter: &mut i32, request_counter: &mut i32,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
verify_domains_match(&self.common.actor, self.common.id_unchecked())?; verify_activity(self.common())?;
check_is_apub_id_valid(&self.common.actor, false)?; verify_urls_match(&self.to, &self.object.common.actor)?;
self.object.verify(context, request_counter).await verify_urls_match(&self.common.actor, &self.object.to)?;
verify_community(&self.common.actor, context, request_counter).await?;
self.object.verify(context, request_counter).await?;
Ok(())
} }
async fn receive( async fn receive(

View file

@ -1,3 +1,4 @@
use crate::activities::{verify_activity, verify_person};
use activitystreams::{ use activitystreams::{
activity::{kind::FollowType, Follow}, activity::{kind::FollowType, Follow},
base::{AnyBase, ExtendsExt}, base::{AnyBase, ExtendsExt},
@ -5,11 +6,10 @@ use activitystreams::{
use anyhow::Context; use anyhow::Context;
use lemmy_api_common::blocking; use lemmy_api_common::blocking;
use lemmy_apub::{ use lemmy_apub::{
check_is_apub_id_valid,
fetcher::{community::get_or_fetch_and_upsert_community, person::get_or_fetch_and_upsert_person}, fetcher::{community::get_or_fetch_and_upsert_community, person::get_or_fetch_and_upsert_person},
CommunityType, CommunityType,
}; };
use lemmy_apub_lib::{verify_domains_match, ActivityCommonFields, ActivityHandlerNew}; use lemmy_apub_lib::{verify_urls_match, ActivityCommonFields, ActivityHandlerNew};
use lemmy_db_queries::Followable; use lemmy_db_queries::Followable;
use lemmy_db_schema::source::community::{CommunityFollower, CommunityFollowerForm}; use lemmy_db_schema::source::community::{CommunityFollower, CommunityFollowerForm};
use lemmy_utils::{location_info, LemmyError}; use lemmy_utils::{location_info, LemmyError};
@ -19,20 +19,25 @@ use url::Url;
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct FollowCommunity { pub struct FollowCommunity {
to: Url, pub(in crate::activities::following) to: Url,
pub(in crate::activities::following) object: Url, pub(in crate::activities::following) object: Url,
#[serde(rename = "type")] #[serde(rename = "type")]
kind: FollowType, kind: FollowType,
#[serde(flatten)] #[serde(flatten)]
common: ActivityCommonFields, pub(in crate::activities::following) common: ActivityCommonFields,
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl ActivityHandlerNew for FollowCommunity { impl ActivityHandlerNew for FollowCommunity {
async fn verify(&self, _context: &LemmyContext, _: &mut i32) -> Result<(), LemmyError> { async fn verify(
verify_domains_match(&self.common.actor, self.common.id_unchecked())?; &self,
verify_domains_match(&self.to, &self.object)?; context: &LemmyContext,
check_is_apub_id_valid(&self.common.actor, false) request_counter: &mut i32,
) -> Result<(), LemmyError> {
verify_activity(self.common())?;
verify_urls_match(&self.to, &self.object)?;
verify_person(&self.common.actor, context, request_counter).await?;
Ok(())
} }
async fn receive( async fn receive(

View file

@ -1,11 +1,11 @@
use crate::activities::following::follow::FollowCommunity; use crate::activities::{following::follow::FollowCommunity, verify_activity, verify_person};
use activitystreams::activity::kind::UndoType; use activitystreams::activity::kind::UndoType;
use lemmy_api_common::blocking; use lemmy_api_common::blocking;
use lemmy_apub::{ use lemmy_apub::fetcher::{
check_is_apub_id_valid, community::get_or_fetch_and_upsert_community,
fetcher::{community::get_or_fetch_and_upsert_community, person::get_or_fetch_and_upsert_person}, person::get_or_fetch_and_upsert_person,
}; };
use lemmy_apub_lib::{verify_domains_match, ActivityCommonFields, ActivityHandlerNew}; use lemmy_apub_lib::{verify_urls_match, ActivityCommonFields, ActivityHandlerNew};
use lemmy_db_queries::Followable; use lemmy_db_queries::Followable;
use lemmy_db_schema::source::community::{CommunityFollower, CommunityFollowerForm}; use lemmy_db_schema::source::community::{CommunityFollower, CommunityFollowerForm};
use lemmy_utils::LemmyError; use lemmy_utils::LemmyError;
@ -30,10 +30,12 @@ impl ActivityHandlerNew for UndoFollowCommunity {
context: &LemmyContext, context: &LemmyContext,
request_counter: &mut i32, request_counter: &mut i32,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
verify_domains_match(&self.common.actor, self.common.id_unchecked())?; verify_activity(self.common())?;
verify_domains_match(&self.to, &self.object.object)?; verify_urls_match(&self.to, &self.object.object)?;
check_is_apub_id_valid(&self.common.actor, false)?; verify_urls_match(&self.common.actor, &self.object.common.actor)?;
self.object.verify(context, request_counter).await verify_person(&self.common.actor, context, request_counter).await?;
self.object.verify(context, request_counter).await?;
Ok(())
} }
async fn receive( async fn receive(

View file

@ -1,5 +1,10 @@
use anyhow::anyhow; use anyhow::anyhow;
use lemmy_api_common::blocking; use lemmy_api_common::blocking;
use lemmy_apub::{
check_is_apub_id_valid,
fetcher::{community::get_or_fetch_and_upsert_community, person::get_or_fetch_and_upsert_person},
};
use lemmy_apub_lib::{verify_domains_match, ActivityCommonFields};
use lemmy_db_queries::ApubObject; use lemmy_db_queries::ApubObject;
use lemmy_db_schema::source::{community::Community, person::Person}; use lemmy_db_schema::source::{community::Community, person::Person};
use lemmy_db_views_actor::community_view::CommunityView; use lemmy_db_views_actor::community_view::CommunityView;
@ -13,6 +18,36 @@ pub mod following;
pub mod post; pub mod post;
pub mod private_message; pub mod private_message;
/// Checks that the specified Url actually identifies a Person (by fetching it), and that the person
/// doesn't have a site ban.
async fn verify_person(
person_id: &Url,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let person = get_or_fetch_and_upsert_person(person_id, context, request_counter).await?;
if person.banned {
return Err(anyhow!("Person {} is banned", person_id).into());
}
Ok(())
}
/// Simply check that the url actually refers to a valid group.
async fn verify_community(
community_id: &Url,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
get_or_fetch_and_upsert_community(community_id, context, request_counter).await?;
Ok(())
}
fn verify_activity(common: &ActivityCommonFields) -> Result<(), LemmyError> {
check_is_apub_id_valid(&common.actor, false)?;
verify_domains_match(common.id_unchecked(), &common.actor)?;
Ok(())
}
async fn verify_mod_action( async fn verify_mod_action(
actor_id: Url, actor_id: Url,
activity_cc: Url, activity_cc: Url,

View file

@ -1,4 +1,4 @@
use crate::activities::private_message::{send_websocket_message, verify_activity, verify_person}; use crate::activities::{private_message::send_websocket_message, verify_activity, verify_person};
use activitystreams::{activity::kind::CreateType, base::BaseExt}; use activitystreams::{activity::kind::CreateType, base::BaseExt};
use lemmy_apub::{objects::FromApub, NoteExt}; use lemmy_apub::{objects::FromApub, NoteExt};
use lemmy_apub_lib::{verify_domains_match_opt, ActivityCommonFields, ActivityHandlerNew}; use lemmy_apub_lib::{verify_domains_match_opt, ActivityCommonFields, ActivityHandlerNew};

View file

@ -1,4 +1,4 @@
use crate::activities::private_message::{send_websocket_message, verify_activity, verify_person}; use crate::activities::{private_message::send_websocket_message, verify_activity, verify_person};
use activitystreams::activity::kind::DeleteType; use activitystreams::activity::kind::DeleteType;
use lemmy_api_common::blocking; use lemmy_api_common::blocking;
use lemmy_apub_lib::{verify_domains_match, ActivityCommonFields, ActivityHandlerNew}; use lemmy_apub_lib::{verify_domains_match, ActivityCommonFields, ActivityHandlerNew};

View file

@ -1,38 +1,14 @@
use anyhow::anyhow;
use lemmy_api_common::{blocking, person::PrivateMessageResponse}; use lemmy_api_common::{blocking, person::PrivateMessageResponse};
use lemmy_apub::{check_is_apub_id_valid, fetcher::person::get_or_fetch_and_upsert_person};
use lemmy_apub_lib::{verify_domains_match, ActivityCommonFields};
use lemmy_db_schema::PrivateMessageId; use lemmy_db_schema::PrivateMessageId;
use lemmy_db_views::{local_user_view::LocalUserView, private_message_view::PrivateMessageView}; use lemmy_db_views::{local_user_view::LocalUserView, private_message_view::PrivateMessageView};
use lemmy_utils::LemmyError; use lemmy_utils::LemmyError;
use lemmy_websocket::{messages::SendUserRoomMessage, LemmyContext, UserOperationCrud}; use lemmy_websocket::{messages::SendUserRoomMessage, LemmyContext, UserOperationCrud};
use url::Url;
pub mod create; pub mod create;
pub mod delete; pub mod delete;
pub mod undo_delete; pub mod undo_delete;
pub mod update; pub mod update;
/// Checks that the specified Url actually identifies a Person (by fetching it), and that the person
/// doesn't have a site ban.
async fn verify_person(
person_id: &Url,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let person = get_or_fetch_and_upsert_person(person_id, context, request_counter).await?;
if person.banned {
return Err(anyhow!("Person {} is banned", person_id).into());
}
Ok(())
}
fn verify_activity(common: &ActivityCommonFields) -> Result<(), LemmyError> {
check_is_apub_id_valid(&common.actor, false)?;
verify_domains_match(common.id_unchecked(), &common.actor)?;
Ok(())
}
async fn send_websocket_message( async fn send_websocket_message(
private_message_id: PrivateMessageId, private_message_id: PrivateMessageId,
op: UserOperationCrud, op: UserOperationCrud,

View file

@ -1,6 +1,5 @@
use crate::activities::private_message::{ use crate::activities::{
delete::DeletePrivateMessage, private_message::{delete::DeletePrivateMessage, send_websocket_message},
send_websocket_message,
verify_activity, verify_activity,
verify_person, verify_person,
}; };

View file

@ -1,4 +1,4 @@
use crate::activities::private_message::{send_websocket_message, verify_activity, verify_person}; use crate::activities::{private_message::send_websocket_message, verify_activity, verify_person};
use activitystreams::{activity::kind::UpdateType, base::BaseExt}; use activitystreams::{activity::kind::UpdateType, base::BaseExt};
use lemmy_apub::{objects::FromApub, NoteExt}; use lemmy_apub::{objects::FromApub, NoteExt};
use lemmy_apub_lib::{verify_domains_match_opt, ActivityCommonFields, ActivityHandlerNew}; use lemmy_apub_lib::{verify_domains_match_opt, ActivityCommonFields, ActivityHandlerNew};