Skip to content

Commit

Permalink
Add comment depth check (#2940)
Browse files Browse the repository at this point in the history
* Add comment depth check

* Move comment depth code

* linter fix
  • Loading branch information
makotech222 authored Jun 8, 2023
1 parent 491e197 commit 15c84e2
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions crates/api_crud/src/comment/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use lemmy_utils::{
validation::is_valid_body_field,
},
};
const MAX_COMMENT_DEPTH_LIMIT: usize = 100;

#[async_trait::async_trait(?Send)]
impl PerformCrud for CreateComment {
Expand Down Expand Up @@ -77,6 +78,7 @@ impl PerformCrud for CreateComment {
if parent.post_id != post_id {
return Err(LemmyError::from_message("couldnt_create_comment"));
}
check_comment_depth(parent)?;
}

// if no language is set, copy language from parent post/comment
Expand Down Expand Up @@ -186,3 +188,13 @@ impl PerformCrud for CreateComment {
.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(())
}
}

0 comments on commit 15c84e2

Please sign in to comment.