Add comment depth check (#2940)

* Add comment depth check

* Move comment depth code

* linter fix
pull/2963/head
Anon 2023-06-08 09:31:26 -05:00 committed by GitHub
parent 491e197529
commit 15c84e2f7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 0 deletions

View File

@ -33,6 +33,7 @@ use lemmy_utils::{
validation::is_valid_body_field, validation::is_valid_body_field,
}, },
}; };
const MAX_COMMENT_DEPTH_LIMIT: usize = 100;
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl PerformCrud for CreateComment { impl PerformCrud for CreateComment {
@ -77,6 +78,7 @@ impl PerformCrud for CreateComment {
if parent.post_id != post_id { if parent.post_id != post_id {
return Err(LemmyError::from_message("couldnt_create_comment")); return Err(LemmyError::from_message("couldnt_create_comment"));
} }
check_comment_depth(parent)?;
} }
// if no language is set, copy language from parent post/comment // if no language is set, copy language from parent post/comment
@ -186,3 +188,13 @@ impl PerformCrud for CreateComment {
.await .await
} }
} }
pub fn check_comment_depth(comment: &Comment) -> Result<(), LemmyError> {
let path = &comment.path.0;
let length = path.split('.').collect::<Vec<&str>>().len();
if length > MAX_COMMENT_DEPTH_LIMIT {
Err(LemmyError::from_message("max_comment_depth_reached"))
} else {
Ok(())
}
}