Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore zero values when setting rate limits (fixes #4280) #5029

Merged
merged 3 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions crates/api_crud/src/site/create.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::not_zero;
use crate::site::{application_question_check, site_default_post_listing_type_check};
use activitypub_federation::{config::Data, http_signatures::generate_actor_keypair};
use actix_web::web::Json;
Expand Down Expand Up @@ -117,17 +118,17 @@ pub async fn create_site(

let local_site_rate_limit_form = LocalSiteRateLimitUpdateForm {
message: data.rate_limit_message,
message_per_second: data.rate_limit_message_per_second,
message_per_second: not_zero(data.rate_limit_message_per_second),
post: data.rate_limit_post,
post_per_second: data.rate_limit_post_per_second,
post_per_second: not_zero(data.rate_limit_post_per_second),
register: data.rate_limit_register,
register_per_second: data.rate_limit_register_per_second,
register_per_second: not_zero(data.rate_limit_register_per_second),
image: data.rate_limit_image,
image_per_second: data.rate_limit_image_per_second,
image_per_second: not_zero(data.rate_limit_image_per_second),
comment: data.rate_limit_comment,
comment_per_second: data.rate_limit_comment_per_second,
comment_per_second: not_zero(data.rate_limit_comment_per_second),
search: data.rate_limit_search,
search_per_second: data.rate_limit_search_per_second,
search_per_second: not_zero(data.rate_limit_search_per_second),
..Default::default()
};

Expand Down
16 changes: 15 additions & 1 deletion crates/api_crud/src/site/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,19 @@ pub fn application_question_check(
}
}

fn not_zero(val: Option<i32>) -> Option<i32> {
match val {
Some(0) => None,
v => v,
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be good to coerce a maximum value also, to None

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would you use as maximum? Anyway this can only be set by admins, so I dont see any need for strict verification. Its enough to ensure that it doesnt cause crashes/bugs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like 10k or something, but its not a big deal.


#[cfg(test)]
#[allow(clippy::unwrap_used)]
#[allow(clippy::indexing_slicing)]
mod tests {

use crate::site::{application_question_check, site_default_post_listing_type_check};
use crate::site::{application_question_check, not_zero, site_default_post_listing_type_check};
use lemmy_db_schema::{ListingType, RegistrationMode};

#[test]
Expand Down Expand Up @@ -93,4 +100,11 @@ mod tests {
RegistrationMode::RequireApplication
);
}

#[test]
fn test_not_zero() {
assert_eq!(None, not_zero(None));
assert_eq!(None, not_zero(Some(0)));
assert_eq!(Some(5), not_zero(Some(5)));
}
}
13 changes: 7 additions & 6 deletions crates/api_crud/src/site/update.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::not_zero;
use crate::site::{application_question_check, site_default_post_listing_type_check};
use activitypub_federation::config::Data;
use actix_web::web::Json;
Expand Down Expand Up @@ -130,17 +131,17 @@ pub async fn update_site(

let local_site_rate_limit_form = LocalSiteRateLimitUpdateForm {
message: data.rate_limit_message,
message_per_second: data.rate_limit_message_per_second,
message_per_second: not_zero(data.rate_limit_message_per_second),
post: data.rate_limit_post,
post_per_second: data.rate_limit_post_per_second,
post_per_second: not_zero(data.rate_limit_post_per_second),
register: data.rate_limit_register,
register_per_second: data.rate_limit_register_per_second,
register_per_second: not_zero(data.rate_limit_register_per_second),
image: data.rate_limit_image,
image_per_second: data.rate_limit_image_per_second,
image_per_second: not_zero(data.rate_limit_image_per_second),
comment: data.rate_limit_comment,
comment_per_second: data.rate_limit_comment_per_second,
comment_per_second: not_zero(data.rate_limit_comment_per_second),
search: data.rate_limit_search,
search_per_second: data.rate_limit_search_per_second,
search_per_second: not_zero(data.rate_limit_search_per_second),
..Default::default()
};

Expand Down