Skip to content

Commit

Permalink
Adding local site settings to reject federated upvotes or downvotes.
Browse files Browse the repository at this point in the history
- Should help defend against downvote spamming instances.
- Fixes #4086
  • Loading branch information
dessalines committed Sep 20, 2024
1 parent 8cdfc14 commit f0adbaa
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 10 deletions.
10 changes: 8 additions & 2 deletions crates/api_common/src/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ pub struct CreateSite {
pub registration_mode: Option<RegistrationMode>,
pub oauth_registration: Option<bool>,
pub content_warning: Option<String>,
pub reject_federated_upvotes: Option<bool>,
pub reject_federated_downvotes: Option<bool>,
}

#[skip_serializing_none]
Expand Down Expand Up @@ -287,13 +289,17 @@ pub struct EditSite {
/// A list of blocked URLs
pub blocked_urls: Option<Vec<String>>,
pub registration_mode: Option<RegistrationMode>,
/// Whether or not external auth methods can auto-register users.
pub oauth_registration: Option<bool>,
/// Whether to email admins for new reports.
pub reports_email_admins: Option<bool>,
/// If present, nsfw content is visible by default. Should be displayed by frontends/clients
/// when the site is first opened by a user.
pub content_warning: Option<String>,
/// Whether or not external auth methods can auto-register users.
pub oauth_registration: Option<bool>,
/// If enabled, your site rejects federated upvotes.
pub reject_federated_upvotes: Option<bool>,
/// If enabled, your site rejects federated downvotes.
pub reject_federated_downvotes: Option<bool>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
Expand Down
2 changes: 2 additions & 0 deletions crates/api_crud/src/site/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ pub async fn create_site(
captcha_enabled: data.captcha_enabled,
captcha_difficulty: data.captcha_difficulty.clone(),
default_post_listing_mode: data.default_post_listing_mode,
reject_federated_upvotes: data.reject_federated_upvotes,
reject_federated_downvotes: data.reject_federated_downvotes,
..Default::default()
};

Expand Down
2 changes: 2 additions & 0 deletions crates/api_crud/src/site/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ pub async fn update_site(
reports_email_admins: data.reports_email_admins,
default_post_listing_mode: data.default_post_listing_mode,
oauth_registration: data.oauth_registration,
reject_federated_upvotes: data.reject_federated_upvotes,
reject_federated_downvotes: data.reject_federated_downvotes,
..Default::default()
};

Expand Down
21 changes: 16 additions & 5 deletions crates/apub/src/activities/voting/vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,23 @@ impl ActivityHandler for Vote {

check_bot_account(&actor.0)?;

let enable_downvotes = LocalSite::read(&mut context.pool())
.await
.map(|l| l.enable_downvotes)
// Check for enabled federation votes
let local_site = LocalSite::read(&mut context.pool()).await;
let enable_federated_downvotes = local_site
.as_ref()
.map(|l| l.enable_downvotes && !l.reject_federated_downvotes)
.unwrap_or(true);
if self.kind == VoteType::Dislike && !enable_downvotes {
// If this is a downvote but downvotes are ignored, only undo any existing vote

let enable_federated_upvotes = local_site
.as_ref()
.map(|l| !l.reject_federated_upvotes)
.unwrap_or(true);

let reject_vote_check = (self.kind == VoteType::Dislike && !enable_federated_downvotes)
|| (self.kind == VoteType::Like && !enable_federated_upvotes);

if reject_vote_check {
// If this is a rejection, undo the vote
match object {
PostOrComment::Post(p) => undo_vote_post(actor, &p, context).await,
PostOrComment::Comment(c) => undo_vote_comment(actor, &c, context).await,
Expand Down
2 changes: 2 additions & 0 deletions crates/db_schema/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,8 @@ diesel::table! {
default_post_sort_type -> PostSortTypeEnum,
default_comment_sort_type -> CommentSortTypeEnum,
oauth_registration -> Bool,
reject_federated_upvotes -> Bool,
reject_federated_downvotes -> Bool,
}
}

Expand Down
16 changes: 13 additions & 3 deletions crates/db_schema/src/source/local_site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ pub struct LocalSite {
pub default_comment_sort_type: CommentSortType,
/// Whether or not external auth methods can auto-register users.
pub oauth_registration: bool,
/// If enabled, your site rejects federated upvotes.
pub reject_federated_upvotes: bool,
/// If enabled, your site rejects federated downvotes.
pub reject_federated_downvotes: bool,
}

#[derive(Clone, derive_new::new)]
Expand Down Expand Up @@ -114,8 +118,6 @@ pub struct LocalSiteInsertForm {
#[new(default)]
pub registration_mode: Option<RegistrationMode>,
#[new(default)]
pub oauth_registration: Option<bool>,
#[new(default)]
pub reports_email_admins: Option<bool>,
#[new(default)]
pub federation_signed_fetch: Option<bool>,
Expand All @@ -125,6 +127,12 @@ pub struct LocalSiteInsertForm {
pub default_post_sort_type: Option<PostSortType>,
#[new(default)]
pub default_comment_sort_type: Option<CommentSortType>,
#[new(default)]
pub oauth_registration: Option<bool>,
#[new(default)]
pub reject_federated_upvotes: Option<bool>,
#[new(default)]
pub reject_federated_downvotes: Option<bool>,
}

#[derive(Clone, Default)]
Expand All @@ -148,11 +156,13 @@ pub struct LocalSiteUpdateForm {
pub captcha_enabled: Option<bool>,
pub captcha_difficulty: Option<String>,
pub registration_mode: Option<RegistrationMode>,
pub oauth_registration: Option<bool>,
pub reports_email_admins: Option<bool>,
pub updated: Option<Option<DateTime<Utc>>>,
pub federation_signed_fetch: Option<bool>,
pub default_post_listing_mode: Option<PostListingMode>,
pub default_post_sort_type: Option<PostSortType>,
pub default_comment_sort_type: Option<CommentSortType>,
pub oauth_registration: Option<bool>,
pub reject_federated_upvotes: Option<bool>,
pub reject_federated_downvotes: Option<bool>,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ALTER TABLE local_site
DROP COLUMN reject_federated_upvotes,
DROP COLUMN reject_federated_downvotes;

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ALTER TABLE local_site
ADD COLUMN reject_federated_upvotes boolean DEFAULT FALSE NOT NULL,
ADD COLUMN reject_federated_downvotes boolean DEFAULT FALSE NOT NULL;

0 comments on commit f0adbaa

Please sign in to comment.