From 38ed905f476c5dcbf655cc65ead574ed4f6e1906 Mon Sep 17 00:00:00 2001 From: silverpill Date: Tue, 28 Dec 2021 18:34:13 +0000 Subject: [PATCH] Use profile importer in verify_http_signature function --- .cargo/config.toml | 1 + src/activitypub/fetcher/helpers.rs | 8 +++---- src/activitypub/receiver.rs | 18 +++++++-------- src/http_signatures/verify.rs | 36 ++++++++++++------------------ src/mastodon_api/statuses/types.rs | 2 +- src/mastodon_api/statuses/views.rs | 2 +- src/utils/files.rs | 2 +- 7 files changed, 31 insertions(+), 38 deletions(-) diff --git a/.cargo/config.toml b/.cargo/config.toml index d3d4df4..3a7d564 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,6 +1,7 @@ # https://github.com/rust-lang/cargo/issues/5034#issuecomment-927105016 [target.'cfg(feature = "cargo-clippy")'] rustflags = [ + "-Aclippy::len_zero", "-Aclippy::let_and_return", "-Aclippy::map_entry", "-Aclippy::or_fun_call", diff --git a/src/activitypub/fetcher/helpers.rs b/src/activitypub/fetcher/helpers.rs index 5351f51..c441afd 100644 --- a/src/activitypub/fetcher/helpers.rs +++ b/src/activitypub/fetcher/helpers.rs @@ -41,12 +41,12 @@ impl From for HttpError { } } -pub async fn get_or_fetch_profile_by_actor_id( +pub async fn get_or_import_profile_by_actor_id( db_client: &impl GenericClient, instance: &Instance, - actor_id: &str, media_dir: &Path, -) -> Result { + actor_id: &str, +) -> Result { let profile = match get_profile_by_actor_id(db_client, actor_id).await { Ok(profile) => profile, Err(DatabaseError::NotFound(_)) => { @@ -56,7 +56,7 @@ pub async fn get_or_fetch_profile_by_actor_id( .await .map_err(|err| { log::warn!("{}", err); - ValidationError("failed to fetch actor") + err })?; log::info!("fetched profile {}", profile_data.acct); let profile = create_profile(db_client, &profile_data).await?; diff --git a/src/activitypub/receiver.rs b/src/activitypub/receiver.rs index a2ca635..01e182d 100644 --- a/src/activitypub/receiver.rs +++ b/src/activitypub/receiver.rs @@ -45,7 +45,7 @@ use super::fetcher::fetchers::{ fetch_object, }; use super::fetcher::helpers::{ - get_or_fetch_profile_by_actor_id, + get_or_import_profile_by_actor_id, import_profile_by_actor_address, ImportError, }; @@ -211,11 +211,11 @@ pub async fn process_note( .get(0) .ok_or(ValidationError("invalid attributedTo property"))? .to_string(); - let author = get_or_fetch_profile_by_actor_id( + let author = get_or_import_profile_by_actor_id( db_client, &instance, - &author_id, &config.media_dir(), + &author_id, ).await?; let content = object.content .ok_or(ValidationError("no content"))?; @@ -378,11 +378,11 @@ pub async fn receive_activity( Err(DatabaseError::NotFound(_)) => (), Err(other_error) => return Err(other_error.into()), }; - let author = get_or_fetch_profile_by_actor_id( + let author = get_or_import_profile_by_actor_id( db_client, &config.instance(), - &activity.actor, &config.media_dir(), + &activity.actor, ).await?; let object_id = get_object_id(activity.object)?; let post_id = match parse_object_id(&config.instance_url(), &object_id) { @@ -417,11 +417,11 @@ pub async fn receive_activity( NOTE }, (LIKE, _) | (EMOJI_REACT, _) => { - let author = get_or_fetch_profile_by_actor_id( + let author = get_or_import_profile_by_actor_id( db_client, &config.instance(), - &activity.actor, &config.media_dir(), + &activity.actor, ).await?; let object_id = get_object_id(activity.object)?; let post_id = match parse_object_id(&config.instance_url(), &object_id) { @@ -450,11 +450,11 @@ pub async fn receive_activity( NOTE }, (FOLLOW, _) => { - let source_profile = get_or_fetch_profile_by_actor_id( + let source_profile = get_or_import_profile_by_actor_id( db_client, &config.instance(), - &activity.actor, &config.media_dir(), + &activity.actor, ).await?; let source_actor = source_profile.remote_actor().ok().flatten() .ok_or(HttpError::InternalError)?; diff --git a/src/http_signatures/verify.rs b/src/http_signatures/verify.rs index 46cb3da..6814ba5 100644 --- a/src/http_signatures/verify.rs +++ b/src/http_signatures/verify.rs @@ -6,14 +6,13 @@ use actix_web::{ }; use regex::Regex; -use crate::activitypub::fetcher::fetchers::fetch_profile_by_actor_id; +use crate::activitypub::fetcher::helpers::{ + get_or_import_profile_by_actor_id, + ImportError, +}; use crate::config::Config; use crate::database::{Pool, get_database_client}; use crate::errors::DatabaseError; -use crate::models::profiles::queries::{ - get_profile_by_actor_id, - create_profile, -}; use crate::utils::crypto::{deserialize_public_key, verify_signature}; #[derive(thiserror::Error, Debug)] @@ -60,7 +59,7 @@ fn parse_http_signature( let signature_parameter_re = Regex::new(SIGNATURE_PARAMETER_RE).unwrap(); let mut signature_parameters = HashMap::new(); - for item in signature_header.split(",") { + for item in signature_header.split(',') { let caps = signature_parameter_re.captures(item) .ok_or(VerificationError::HeaderError("invalid signature header"))?; let key = caps["key"].to_string(); @@ -123,23 +122,16 @@ pub async fn verify_http_signature( )?; let db_client = &**get_database_client(db_pool).await?; - let actor_profile = match get_profile_by_actor_id(db_client, &signature_data.actor_id).await { + let actor_profile = match get_or_import_profile_by_actor_id( + db_client, + &config.instance(), + &config.media_dir(), + &signature_data.actor_id, + ).await { Ok(profile) => profile, - Err(err) => match err { - DatabaseError::NotFound(_) => { - let profile_data = fetch_profile_by_actor_id( - &config.instance(), - &signature_data.actor_id, - &config.media_dir(), - ).await.map_err(|err| { - VerificationError::ActorError(err.to_string()) - })?; - let profile = create_profile(db_client, &profile_data).await?; - profile - }, - other_error => { - return Err(other_error.into()); - }, + Err(ImportError::DatabaseError(error)) => return Err(error.into()), + Err(other_error) => { + return Err(VerificationError::ActorError(other_error.to_string())); }, }; let actor = actor_profile.remote_actor().ok().flatten() diff --git a/src/mastodon_api/statuses/types.rs b/src/mastodon_api/statuses/types.rs index a4c8996..b4ecc5e 100644 --- a/src/mastodon_api/statuses/types.rs +++ b/src/mastodon_api/statuses/types.rs @@ -82,7 +82,7 @@ impl Status { .map(|item| Mention::from_profile(item, instance_url)) .collect(); let tags: Vec = post.tags.into_iter() - .map(|tag_name| Tag::from_tag_name(tag_name)) + .map(Tag::from_tag_name) .collect(); let account = Account::from_profile(post.author, instance_url); let reblog = if let Some(repost_of) = post.repost_of { diff --git a/src/mastodon_api/statuses/views.rs b/src/mastodon_api/statuses/views.rs index fd7580e..df03fe7 100644 --- a/src/mastodon_api/statuses/views.rs +++ b/src/mastodon_api/statuses/views.rs @@ -395,7 +395,7 @@ async fn unreblog( let activity = create_activity_undo_announce( &config.instance_url(), ¤t_user.profile, - &repost_id, + repost_id, primary_recipient.as_ref(), ); deliver_activity(&config, ¤t_user, activity, recipients); diff --git a/src/utils/files.rs b/src/utils/files.rs index b2226af..d31bd3a 100644 --- a/src/utils/files.rs +++ b/src/utils/files.rs @@ -22,7 +22,7 @@ pub fn save_file(data: Vec, output_dir: &Path) -> Result let digest = Sha256::digest(&data); let mut file_name = hex::encode(digest); let maybe_extension = data.sniff_mime_type() - .and_then(|media_type| get_mime_extensions_str(media_type)) + .and_then(get_mime_extensions_str) .and_then(|extensions| extensions.first()); if let Some(extension) = maybe_extension { // Append extension for known media types