Add separate Post check for is_valid_body_field (#3263)
authorNeshura <neshura@protonmail.ch>
Mon, 26 Jun 2023 08:47:01 +0000 (10:47 +0200)
committerGitHub <noreply@github.com>
Mon, 26 Jun 2023 08:47:01 +0000 (10:47 +0200)
* Add separate Post check for is_valid_body_field

* Modify is_valid_body_check for posts only

* Fix check var reinit in validation.rs

* Extra empty line to rerun woodpecker with changes

* Change Option to bool, add false to non-post calls

* Woodpecker trick.. again

* Probable rust_fmt fail fixed

* cargo_clippy changes

* Missing space between = and if

* Remove ; after body length checks

13 files changed:
crates/api/src/community/ban.rs
crates/api/src/local_user/ban_person.rs
crates/api_crud/src/comment/create.rs
crates/api_crud/src/comment/update.rs
crates/api_crud/src/community/create.rs
crates/api_crud/src/community/update.rs
crates/api_crud/src/post/create.rs
crates/api_crud/src/post/update.rs
crates/api_crud/src/private_message/create.rs
crates/api_crud/src/private_message/update.rs
crates/api_crud/src/site/create.rs
crates/api_crud/src/site/update.rs
crates/utils/src/utils/validation.rs

index a0fd7bf187b0036ed9cd892e0da0b18c8c0b9377..330c2c56de9a49d50250399bfa05f8553c90cbd0 100644 (file)
@@ -42,7 +42,7 @@ impl Perform for BanFromCommunity {
 
     // Verify that only mods or admins can ban
     is_mod_or_admin(context.pool(), local_user_view.person.id, community_id).await?;
-    is_valid_body_field(&data.reason)?;
+    is_valid_body_field(&data.reason, false)?;
 
     let community_user_ban_form = CommunityPersonBanForm {
       community_id: data.community_id,
index 452557d2c64ed98d0a451c160edeadc471c1055e..2c2d363e313345250c21308ef44c96fecf665f6b 100644 (file)
@@ -30,7 +30,7 @@ impl Perform for BanPerson {
     // Make sure user is an admin
     is_admin(&local_user_view)?;
 
-    is_valid_body_field(&data.reason)?;
+    is_valid_body_field(&data.reason, false)?;
 
     let ban = data.ban;
     let banned_person_id = data.person_id;
index 4ef8686e2e77f5aebdba299e10cb1a1fc976e2af..b3b1efecd60d2cb025ca1c6204dc01234d05ed0e 100644 (file)
@@ -49,7 +49,7 @@ impl PerformCrud for CreateComment {
       &data.content.clone(),
       &local_site_to_slur_regex(&local_site),
     );
-    is_valid_body_field(&Some(content_slurs_removed.clone()))?;
+    is_valid_body_field(&Some(content_slurs_removed.clone()), false)?;
 
     // Check for a community ban
     let post_id = data.post_id;
index 86bdb52e2101681ea8e8d31913379b036fa1ed24..3504e784d5b189c71e5007edede4832a4c6c6d68 100644 (file)
@@ -64,7 +64,7 @@ impl PerformCrud for EditComment {
       .as_ref()
       .map(|c| remove_slurs(c, &local_site_to_slur_regex(&local_site)));
 
-    is_valid_body_field(&content_slurs_removed)?;
+    is_valid_body_field(&content_slurs_removed, false)?;
 
     let comment_id = data.comment_id;
     let form = CommentUpdateForm::builder()
index 850e9f2f5e65f2a8d011ffe6da9e8efbb91fcdfb..0e55beac9a288f80944b15960050bfbfde137b68 100644 (file)
@@ -67,7 +67,7 @@ impl PerformCrud for CreateCommunity {
     check_slurs_opt(&data.description, &slur_regex)?;
 
     is_valid_actor_name(&data.name, local_site.actor_name_max_length as usize)?;
-    is_valid_body_field(&data.description)?;
+    is_valid_body_field(&data.description, false)?;
 
     // Double check for duplicate community actor_ids
     let community_actor_id = generate_local_apub_endpoint(
index 7494cd342322b5d27372928374c995a36da08db2..dec62865f25c8c8664e19025ee45d3699e823d30 100644 (file)
@@ -39,7 +39,7 @@ impl PerformCrud for EditCommunity {
     let slur_regex = local_site_to_slur_regex(&local_site);
     check_slurs_opt(&data.title, &slur_regex)?;
     check_slurs_opt(&data.description, &slur_regex)?;
-    is_valid_body_field(&data.description)?;
+    is_valid_body_field(&data.description, false)?;
 
     // Verify its a mod (only mods can edit it)
     let community_id = data.community_id;
index cd2cf1c3d8b482f657d5d82f1b695c42a993a18e..8ff1b678aec890a77a20bf8308e0f73e6793b3ce 100644 (file)
@@ -57,7 +57,7 @@ impl PerformCrud for CreatePost {
     let url = data_url.map(clean_url_params).map(Into::into); // TODO no good way to handle a "clear"
 
     is_valid_post_title(&data.name)?;
-    is_valid_body_field(&data.body)?;
+    is_valid_body_field(&data.body, true)?;
 
     check_community_ban(local_user_view.person.id, data.community_id, context.pool()).await?;
     check_community_deleted_or_removed(data.community_id, context.pool()).await?;
index af2c63c5019c53ff7a8296d779fed0ed7e45d752..a540f454f9c3e1d21a7cf7482604198068227bba 100644 (file)
@@ -49,7 +49,7 @@ impl PerformCrud for EditPost {
       is_valid_post_title(name)?;
     }
 
-    is_valid_body_field(&data.body)?;
+    is_valid_body_field(&data.body, true)?;
 
     let post_id = data.post_id;
     let orig_post = Post::read(context.pool(), post_id).await?;
index 3f1d4ef8946339e6ad881a2fb0415bb167a5c2d9..e1a855463d6baae18b735fa20c778f0a82191fdb 100644 (file)
@@ -43,7 +43,7 @@ impl PerformCrud for CreatePrivateMessage {
       &data.content.clone(),
       &local_site_to_slur_regex(&local_site),
     );
-    is_valid_body_field(&Some(content_slurs_removed.clone()))?;
+    is_valid_body_field(&Some(content_slurs_removed.clone()), false)?;
 
     check_person_block(local_user_view.person.id, data.recipient_id, context.pool()).await?;
 
index cc3c377b89b54b3f067485b308c4e06d99c23364..b2d8e48f9987da2cb28721002701bf0300e2f846 100644 (file)
@@ -41,7 +41,7 @@ impl PerformCrud for EditPrivateMessage {
 
     // Doing the update
     let content_slurs_removed = remove_slurs(&data.content, &local_site_to_slur_regex(&local_site));
-    is_valid_body_field(&Some(content_slurs_removed.clone()))?;
+    is_valid_body_field(&Some(content_slurs_removed.clone()), false)?;
 
     let private_message_id = data.private_message_id;
     PrivateMessage::update(
index a1669baef0341569f1590fbb810a2ad6c85c37af..e7486e63a6b58650db3d72deaa2792c6af8a15d9 100644 (file)
@@ -73,7 +73,7 @@ impl PerformCrud for CreateSite {
       site_description_length_check(desc)?;
     }
 
-    is_valid_body_field(&data.sidebar)?;
+    is_valid_body_field(&data.sidebar, false)?;
 
     let application_question = diesel_option_overwrite(&data.application_question);
     check_application_question(
index 6664d549a4f464b96536975e7c4d7919e3bff351..fa800a5a9bd13df3cac8b0f7d18a6f98bd3abab8 100644 (file)
@@ -67,7 +67,7 @@ impl PerformCrud for EditSite {
       site_description_length_check(desc)?;
     }
 
-    is_valid_body_field(&data.sidebar)?;
+    is_valid_body_field(&data.sidebar, false)?;
 
     let application_question = diesel_option_overwrite(&data.application_question);
     check_application_question(
index 41103332c6d7a2a225159344423ffdcd6dfab32c..621543b47ea34f12b016d14b391587223bf496d9 100644 (file)
@@ -18,6 +18,7 @@ static CLEAN_URL_PARAMS_REGEX: Lazy<Regex> = Lazy::new(|| {
     .expect("compile regex")
 });
 const BODY_MAX_LENGTH: usize = 10000;
+const POST_BODY_MAX_LENGTH: usize = 50000;
 const BIO_MAX_LENGTH: usize = 300;
 
 fn has_newline(name: &str) -> bool {
@@ -68,9 +69,14 @@ pub fn is_valid_post_title(title: &str) -> LemmyResult<()> {
 }
 
 /// This could be post bodies, comments, or any description field
-pub fn is_valid_body_field(body: &Option<String>) -> LemmyResult<()> {
+pub fn is_valid_body_field(body: &Option<String>, post: bool) -> LemmyResult<()> {
   if let Some(body) = body {
-    let check = body.chars().count() <= BODY_MAX_LENGTH;
+    let check = if post {
+      body.chars().count() <= POST_BODY_MAX_LENGTH
+    } else {
+      body.chars().count() <= BODY_MAX_LENGTH
+    };
+
     if !check {
       Err(LemmyError::from_message("invalid_body_field"))
     } else {