]> Untitled Git - lemmy.git/commitdiff
Implement rate limits on comments
authorlayla <layla@chapo.dev>
Thu, 11 Nov 2021 20:40:25 +0000 (20:40 +0000)
committerlayla <layla@chapo.dev>
Thu, 18 Nov 2021 13:40:16 +0000 (13:40 +0000)
config/defaults.hjson
crates/utils/src/rate_limit/mod.rs
crates/utils/src/rate_limit/rate_limiter.rs
crates/utils/src/settings/structs.rs
crates/websocket/src/chat_server.rs
docker/federation/lemmy_alpha.hjson
docker/federation/lemmy_beta.hjson
docker/federation/lemmy_delta.hjson
docker/federation/lemmy_epsilon.hjson
docker/federation/lemmy_gamma.hjson
src/api_routes.rs

index ff6df36dacb972d4ff4ba4e3b55de557101ce9cb..003adf11a78389864d99cfbd2c85174734a02c22 100644 (file)
     image: 6
     # Interval length for image uploads, in seconds
     image_per_second: 3600
+    # Maximum number of comments created in interval
+    comment: 6
+    # Interval length for comment limit, in seconds
+    comment_per_second: 600
   }
   # Settings related to activitypub federation
   federation: {
index c1a4627c1d9f8ca50913d00ce67f08b90e7b55ec..d56dc0c5e8ea33e64a115efe02af892a160a37f1 100644 (file)
@@ -49,6 +49,10 @@ impl RateLimit {
     self.kind(RateLimitType::Image)
   }
 
+  pub fn comment(&self) -> RateLimited {
+    self.kind(RateLimitType::Comment)
+  }
+
   fn kind(&self, type_: RateLimitType) -> RateLimited {
     RateLimited {
       rate_limiter: self.rate_limiter.clone(),
@@ -115,6 +119,15 @@ impl RateLimited {
             false,
           )?;
         }
+        RateLimitType::Comment => {
+          limiter.check_rate_limit_full(
+            self.type_,
+            &ip_addr,
+            rate_limit.comment,
+            rate_limit.comment_per_second,
+            false,
+          )?;
+        }
       };
     }
 
index 46b6b0c72dd9a0d4d52d1210599a8443af501e13..352d5e66bf122d14ab76d6b13673801beab2b2ff 100644 (file)
@@ -15,6 +15,7 @@ pub(crate) enum RateLimitType {
   Register,
   Post,
   Image,
+  Comment,
 }
 
 /// Rate limiting based on rate type and IP addr
index 300030816757f5b75a6226dca3dc53b577703588..1b8ac812efd3ed0c8507a1978939f99eb42b7d65 100644 (file)
@@ -149,6 +149,12 @@ pub struct RateLimitConfig {
   /// Interval length for image uploads, in seconds
   #[default(3600)]
   pub image_per_second: i32,
+  /// Maximum number of comments created in interval
+  #[default(6)]
+  pub comment: i32,
+  /// Interval length for comment limit, in seconds
+  #[default(600)]
+  pub comment_per_second: i32,
 }
 
 #[derive(Debug, Deserialize, Serialize, Clone, SmartDefault, Document)]
index 3e17b6262cd384507ba0c586cd6990e606ebe028..9fa258ff4fed6039e83b73295b2a998b4a9bd163 100644 (file)
@@ -485,6 +485,7 @@ impl ChatServer {
           UserOperationCrud::Register => rate_limiter.register().wrap(ip, fut).await,
           UserOperationCrud::CreatePost => rate_limiter.post().wrap(ip, fut).await,
           UserOperationCrud::CreateCommunity => rate_limiter.register().wrap(ip, fut).await,
+          UserOperationCrud::CreateComment => rate_limiter.comment().wrap(ip, fut).await,
           _ => rate_limiter.message().wrap(ip, fut).await,
         }
       } else {
index 8c69a4c6f32db7996d5d4afb84fe1a28a050b08a..6c0c54dff5257fde2371109c6b327094546af80c 100644 (file)
@@ -33,5 +33,7 @@
     register_per_second: 3600
     image: 6
     image_per_second: 3600
+    comment: 99999
+    comment_per_second: 600
   }
 }
index efd9458cc0ec31b737adc64e6419bf8f75b49b22..b630dacae46a042a0c4a1ec6c3be445b755e86a5 100644 (file)
@@ -32,5 +32,7 @@
     register_per_second: 3600
     image: 6
     image_per_second: 3600
+    comment: 99999
+    comment_per_second: 600
   }
 }
index 75f3f916314e04b9f0067cd8aa4c9269b77c15c1..8d400fc091d054d7d809fb897c042dfd5941290d 100644 (file)
@@ -32,5 +32,7 @@
     register_per_second: 3600
     image: 6
     image_per_second: 3600
+    comment: 99999
+    comment_per_second: 600
   }
 }
index 1b2cbd34cf6ab35a6653f59d7e1280dd9504fb3c..78b1a687e991defd8e60ad702e05cf02c1379376 100644 (file)
@@ -32,5 +32,7 @@
     register_per_second: 3600
     image: 6
     image_per_second: 3600
+    comment: 99999
+    comment_per_second: 600
   }
 }
index 48b6c1c53437cd59a4f41143711bcc517e19fbba..8d3f097a1fd35ba25bed17c3500ecd24b189e1b0 100644 (file)
@@ -32,5 +32,7 @@
     register_per_second: 3600
     image: 6
     image_per_second: 3600
+    comment: 99999
+    comment_per_second: 600
   }
 }
index 80d507ef83f4c772e80d460dbcdcab5802f5a3cd..0349f518c0253e7ca8a273c5adb1727c68736522 100644 (file)
@@ -101,10 +101,16 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) {
           ),
       )
       // Comment
+      .service(
+        // Handle POST to /comment separately to add the comment() rate limitter
+        web::resource("/comment")
+          .guard(guard::Post())
+          .wrap(rate_limit.comment())
+          .route(web::post().to(route_post_crud::<CreateComment>)),
+      )
       .service(
         web::scope("/comment")
           .wrap(rate_limit.message())
-          .route("", web::post().to(route_post_crud::<CreateComment>))
           .route("", web::put().to(route_post_crud::<EditComment>))
           .route("/delete", web::post().to(route_post_crud::<DeleteComment>))
           .route("/remove", web::post().to(route_post_crud::<RemoveComment>))