Merge pull request #1907 from not-layla/main

Implement rate limits on comments
This commit is contained in:
Dessalines 2021-11-18 11:22:38 -05:00 committed by GitHub
commit a56853be05
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 42 additions and 1 deletions

View file

@ -32,6 +32,10 @@
image: 6 image: 6
# Interval length for image uploads, in seconds # Interval length for image uploads, in seconds
image_per_second: 3600 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 # Settings related to activitypub federation
federation: { federation: {

View file

@ -49,6 +49,10 @@ impl RateLimit {
self.kind(RateLimitType::Image) self.kind(RateLimitType::Image)
} }
pub fn comment(&self) -> RateLimited {
self.kind(RateLimitType::Comment)
}
fn kind(&self, type_: RateLimitType) -> RateLimited { fn kind(&self, type_: RateLimitType) -> RateLimited {
RateLimited { RateLimited {
rate_limiter: self.rate_limiter.clone(), rate_limiter: self.rate_limiter.clone(),
@ -115,6 +119,15 @@ impl RateLimited {
false, false,
)?; )?;
} }
RateLimitType::Comment => {
limiter.check_rate_limit_full(
self.type_,
&ip_addr,
rate_limit.comment,
rate_limit.comment_per_second,
false,
)?;
}
}; };
} }

View file

@ -15,6 +15,7 @@ pub(crate) enum RateLimitType {
Register, Register,
Post, Post,
Image, Image,
Comment,
} }
/// Rate limiting based on rate type and IP addr /// Rate limiting based on rate type and IP addr

View file

@ -149,6 +149,12 @@ pub struct RateLimitConfig {
/// Interval length for image uploads, in seconds /// Interval length for image uploads, in seconds
#[default(3600)] #[default(3600)]
pub image_per_second: i32, 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)] #[derive(Debug, Deserialize, Serialize, Clone, SmartDefault, Document)]

View file

@ -485,6 +485,7 @@ impl ChatServer {
UserOperationCrud::Register => rate_limiter.register().wrap(ip, fut).await, UserOperationCrud::Register => rate_limiter.register().wrap(ip, fut).await,
UserOperationCrud::CreatePost => rate_limiter.post().wrap(ip, fut).await, UserOperationCrud::CreatePost => rate_limiter.post().wrap(ip, fut).await,
UserOperationCrud::CreateCommunity => rate_limiter.register().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, _ => rate_limiter.message().wrap(ip, fut).await,
} }
} else { } else {

View file

@ -33,5 +33,7 @@
register_per_second: 3600 register_per_second: 3600
image: 6 image: 6
image_per_second: 3600 image_per_second: 3600
comment: 99999
comment_per_second: 600
} }
} }

View file

@ -32,5 +32,7 @@
register_per_second: 3600 register_per_second: 3600
image: 6 image: 6
image_per_second: 3600 image_per_second: 3600
comment: 99999
comment_per_second: 600
} }
} }

View file

@ -32,5 +32,7 @@
register_per_second: 3600 register_per_second: 3600
image: 6 image: 6
image_per_second: 3600 image_per_second: 3600
comment: 99999
comment_per_second: 600
} }
} }

View file

@ -32,5 +32,7 @@
register_per_second: 3600 register_per_second: 3600
image: 6 image: 6
image_per_second: 3600 image_per_second: 3600
comment: 99999
comment_per_second: 600
} }
} }

View file

@ -32,5 +32,7 @@
register_per_second: 3600 register_per_second: 3600
image: 6 image: 6
image_per_second: 3600 image_per_second: 3600
comment: 99999
comment_per_second: 600
} }
} }

View file

@ -101,10 +101,16 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) {
), ),
) )
// Comment // 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( .service(
web::scope("/comment") web::scope("/comment")
.wrap(rate_limit.message()) .wrap(rate_limit.message())
.route("", web::post().to(route_post_crud::<CreateComment>))
.route("", web::put().to(route_post_crud::<EditComment>)) .route("", web::put().to(route_post_crud::<EditComment>))
.route("/delete", web::post().to(route_post_crud::<DeleteComment>)) .route("/delete", web::post().to(route_post_crud::<DeleteComment>))
.route("/remove", web::post().to(route_post_crud::<RemoveComment>)) .route("/remove", web::post().to(route_post_crud::<RemoveComment>))