From e16821116e2180ef4898f75589a65607fd7c53b3 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Tue, 29 Mar 2022 15:46:03 +0000 Subject: [PATCH] Lowering search rate limit. Fixes #2153 (#2154) * Lowering search rate limit. Fixes #2153 * Adding a search rate limit. * Forgot to add the websocket search rate limit * Fix wrong op --- config/defaults.hjson | 3 +++ crates/utils/src/rate_limit/mod.rs | 5 +++++ crates/utils/src/rate_limit/rate_limiter.rs | 1 + crates/utils/src/settings/structs.rs | 5 +++++ crates/websocket/src/chat_server.rs | 1 + src/api_routes.rs | 2 +- 6 files changed, 16 insertions(+), 1 deletion(-) diff --git a/config/defaults.hjson b/config/defaults.hjson index 3458467d2..18a4b2190 100644 --- a/config/defaults.hjson +++ b/config/defaults.hjson @@ -36,6 +36,9 @@ comment: 6 # Interval length for comment limit, in seconds comment_per_second: 600 + search: 6 + # Interval length for search limit, in seconds + search_per_second: 600 } # Settings related to activitypub federation federation: { diff --git a/crates/utils/src/rate_limit/mod.rs b/crates/utils/src/rate_limit/mod.rs index 69bcbcecd..f8acc3aff 100644 --- a/crates/utils/src/rate_limit/mod.rs +++ b/crates/utils/src/rate_limit/mod.rs @@ -57,6 +57,10 @@ impl RateLimit { self.kind(RateLimitType::Comment) } + pub fn search(&self) -> RateLimited { + self.kind(RateLimitType::Search) + } + fn kind(&self, type_: RateLimitType) -> RateLimited { RateLimited { rate_limiter: self.rate_limiter.clone(), @@ -79,6 +83,7 @@ impl RateLimited { RateLimitType::Register => (rate_limit.register, rate_limit.register_per_second), RateLimitType::Image => (rate_limit.image, rate_limit.image_per_second), RateLimitType::Comment => (rate_limit.comment, rate_limit.comment_per_second), + RateLimitType::Search => (rate_limit.search, rate_limit.search_per_second), }; let mut limiter = self.rate_limiter.lock(); diff --git a/crates/utils/src/rate_limit/rate_limiter.rs b/crates/utils/src/rate_limit/rate_limiter.rs index 31d91036e..258d7704a 100644 --- a/crates/utils/src/rate_limit/rate_limiter.rs +++ b/crates/utils/src/rate_limit/rate_limiter.rs @@ -16,6 +16,7 @@ pub(crate) enum RateLimitType { Post, Image, Comment, + Search, } /// Rate limiting based on rate type and IP addr diff --git a/crates/utils/src/settings/structs.rs b/crates/utils/src/settings/structs.rs index ccda734d4..fe4c1509d 100644 --- a/crates/utils/src/settings/structs.rs +++ b/crates/utils/src/settings/structs.rs @@ -170,6 +170,11 @@ pub struct RateLimitConfig { /// Interval length for comment limit, in seconds #[default(600)] pub comment_per_second: i32, + #[default(6)] + pub search: i32, + /// Interval length for search limit, in seconds + #[default(600)] + pub search_per_second: i32, } #[derive(Debug, Deserialize, Serialize, Clone, SmartDefault, Document)] diff --git a/crates/websocket/src/chat_server.rs b/crates/websocket/src/chat_server.rs index 4dafafd8e..4328d2d39 100644 --- a/crates/websocket/src/chat_server.rs +++ b/crates/websocket/src/chat_server.rs @@ -493,6 +493,7 @@ impl ChatServer { let user_operation = UserOperation::from_str(op)?; let passed = match user_operation { UserOperation::GetCaptcha => rate_limiter.post().check(ip), + UserOperation::Search => rate_limiter.search().check(ip), _ => true, }; let fut = (message_handler)(context, msg.id, user_operation, data); diff --git a/src/api_routes.rs b/src/api_routes.rs index 1af9f028f..7a3feb5cb 100644 --- a/src/api_routes.rs +++ b/src/api_routes.rs @@ -29,7 +29,7 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) { ) .service( web::resource("/search") - .wrap(rate_limit.message()) + .wrap(rate_limit.search()) .route(web::get().to(route_get::)), ) .service(