Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding check to description and body length fields. #2805

Merged
merged 4 commits into from
Apr 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion crates/api/src/community/ban.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ use lemmy_db_schema::{
traits::{Bannable, Crud, Followable},
};
use lemmy_db_views_actor::structs::PersonView;
use lemmy_utils::{error::LemmyError, utils::time::naive_from_unix, ConnectionId};
use lemmy_utils::{
error::LemmyError,
utils::{time::naive_from_unix, validation::is_valid_body_field},
ConnectionId,
};

#[async_trait::async_trait(?Send)]
impl Perform for BanFromCommunity {
Expand All @@ -46,6 +50,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)?;

let community_user_ban_form = CommunityPersonBanForm {
community_id: data.community_id,
Expand Down
8 changes: 7 additions & 1 deletion crates/api/src/local_user/ban_person.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ use lemmy_db_schema::{
traits::Crud,
};
use lemmy_db_views_actor::structs::PersonView;
use lemmy_utils::{error::LemmyError, utils::time::naive_from_unix, ConnectionId};
use lemmy_utils::{
error::LemmyError,
utils::{time::naive_from_unix, validation::is_valid_body_field},
ConnectionId,
};

#[async_trait::async_trait(?Send)]
impl Perform for BanPerson {
Expand All @@ -33,6 +37,8 @@ impl Perform for BanPerson {
// Make sure user is an admin
is_admin(&local_user_view)?;

is_valid_body_field(&data.reason)?;

let ban = data.ban;
let banned_person_id = data.person_id;
let expires = data.expires.map(naive_from_unix);
Expand Down
15 changes: 5 additions & 10 deletions crates/api/src/local_user/save_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use lemmy_utils::{
utils::validation::{
build_totp_2fa,
generate_totp_2fa_secret,
is_valid_bio_field,
is_valid_display_name,
is_valid_matrix_id,
},
Expand Down Expand Up @@ -67,24 +68,18 @@ impl Perform for SaveUserSettings {
}

if let Some(Some(bio)) = &bio {
if bio.chars().count() > 300 {
return Err(LemmyError::from_message("bio_length_overflow"));
}
is_valid_bio_field(bio)?;
}

if let Some(Some(display_name)) = &display_name {
if !is_valid_display_name(
is_valid_display_name(
display_name.trim(),
site_view.local_site.actor_name_max_length as usize,
) {
return Err(LemmyError::from_message("invalid_username"));
}
)?;
}

if let Some(Some(matrix_user_id)) = &matrix_user_id {
if !is_valid_matrix_id(matrix_user_id) {
return Err(LemmyError::from_message("invalid_matrix_id"));
}
is_valid_matrix_id(matrix_user_id)?;
}

let local_user_id = local_user_view.local_user.id;
Expand Down
7 changes: 6 additions & 1 deletion crates/api_crud/src/comment/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ use lemmy_db_schema::{
};
use lemmy_utils::{
error::LemmyError,
utils::{mention::scrape_text_for_mentions, slurs::remove_slurs},
utils::{
mention::scrape_text_for_mentions,
slurs::remove_slurs,
validation::is_valid_body_field,
},
ConnectionId,
};

Expand All @@ -50,6 +54,7 @@ impl PerformCrud for CreateComment {
&data.content.clone(),
&local_site_to_slur_regex(&local_site),
);
is_valid_body_field(&Some(content_slurs_removed.clone()))?;

// Check for a community ban
let post_id = data.post_id;
Expand Down
9 changes: 8 additions & 1 deletion crates/api_crud/src/comment/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ use lemmy_db_schema::{
use lemmy_db_views::structs::CommentView;
use lemmy_utils::{
error::LemmyError,
utils::{mention::scrape_text_for_mentions, slurs::remove_slurs},
utils::{
mention::scrape_text_for_mentions,
slurs::remove_slurs,
validation::is_valid_body_field,
},
ConnectionId,
};

Expand Down Expand Up @@ -65,6 +69,9 @@ impl PerformCrud for EditComment {
.content
.as_ref()
.map(|c| remove_slurs(c, &local_site_to_slur_regex(&local_site)));

is_valid_body_field(&content_slurs_removed)?;

let comment_id = data.comment_id;
let form = CommentUpdateForm::builder()
.content(content_slurs_removed)
Expand Down
7 changes: 3 additions & 4 deletions crates/api_crud/src/community/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use lemmy_utils::{
error::LemmyError,
utils::{
slurs::{check_slurs, check_slurs_opt},
validation::is_valid_actor_name,
validation::{is_valid_actor_name, is_valid_body_field},
},
ConnectionId,
};
Expand Down Expand Up @@ -72,9 +72,8 @@ impl PerformCrud for CreateCommunity {
check_slurs(&data.title, &slur_regex)?;
check_slurs_opt(&data.description, &slur_regex)?;

if !is_valid_actor_name(&data.name, local_site.actor_name_max_length as usize) {
return Err(LemmyError::from_message("invalid_community_name"));
}
is_valid_actor_name(&data.name, local_site.actor_name_max_length as usize)?;
is_valid_body_field(&data.description)?;

// Double check for duplicate community actor_ids
let community_actor_id = generate_local_apub_endpoint(
Expand Down
7 changes: 6 additions & 1 deletion crates/api_crud/src/community/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ use lemmy_db_schema::{
utils::{diesel_option_overwrite, diesel_option_overwrite_to_url, naive_now},
};
use lemmy_db_views_actor::structs::CommunityModeratorView;
use lemmy_utils::{error::LemmyError, utils::slurs::check_slurs_opt, ConnectionId};
use lemmy_utils::{
error::LemmyError,
utils::{slurs::check_slurs_opt, validation::is_valid_body_field},
ConnectionId,
};

#[async_trait::async_trait(?Send)]
impl PerformCrud for EditCommunity {
Expand All @@ -41,6 +45,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)?;

// Verify its a mod (only mods can edit it)
let community_id = data.community_id;
Expand Down
7 changes: 3 additions & 4 deletions crates/api_crud/src/post/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use lemmy_utils::{
error::LemmyError,
utils::{
slurs::{check_slurs, check_slurs_opt},
validation::{clean_url_params, is_valid_post_title},
validation::{clean_url_params, is_valid_body_field, is_valid_post_title},
},
ConnectionId,
};
Expand Down Expand Up @@ -62,9 +62,8 @@ impl PerformCrud for CreatePost {
let data_url = data.url.as_ref();
let url = data_url.map(clean_url_params).map(Into::into); // TODO no good way to handle a "clear"

if !is_valid_post_title(&data.name) {
return Err(LemmyError::from_message("invalid_post_title"));
}
is_valid_post_title(&data.name)?;
is_valid_body_field(&data.body)?;

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?;
Expand Down
8 changes: 4 additions & 4 deletions crates/api_crud/src/post/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use lemmy_utils::{
error::LemmyError,
utils::{
slurs::check_slurs_opt,
validation::{clean_url_params, is_valid_post_title},
validation::{clean_url_params, is_valid_body_field, is_valid_post_title},
},
ConnectionId,
};
Expand Down Expand Up @@ -52,11 +52,11 @@ impl PerformCrud for EditPost {
check_slurs_opt(&data.body, &slur_regex)?;

if let Some(name) = &data.name {
if !is_valid_post_title(name) {
return Err(LemmyError::from_message("invalid_post_title"));
}
is_valid_post_title(name)?;
}

is_valid_body_field(&data.body)?;

let post_id = data.post_id;
let orig_post = Post::read(context.pool(), post_id).await?;

Expand Down
7 changes: 6 additions & 1 deletion crates/api_crud/src/private_message/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ use lemmy_db_schema::{
traits::Crud,
};
use lemmy_db_views::structs::LocalUserView;
use lemmy_utils::{error::LemmyError, utils::slurs::remove_slurs, ConnectionId};
use lemmy_utils::{
error::LemmyError,
utils::{slurs::remove_slurs, validation::is_valid_body_field},
ConnectionId,
};

#[async_trait::async_trait(?Send)]
impl PerformCrud for CreatePrivateMessage {
Expand All @@ -43,6 +47,7 @@ impl PerformCrud for CreatePrivateMessage {
&data.content.clone(),
&local_site_to_slur_regex(&local_site),
);
is_valid_body_field(&Some(content_slurs_removed.clone()))?;

check_person_block(local_user_view.person.id, data.recipient_id, context.pool()).await?;

Expand Down
8 changes: 7 additions & 1 deletion crates/api_crud/src/private_message/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ use lemmy_db_schema::{
traits::Crud,
utils::naive_now,
};
use lemmy_utils::{error::LemmyError, utils::slurs::remove_slurs, ConnectionId};
use lemmy_utils::{
error::LemmyError,
utils::{slurs::remove_slurs, validation::is_valid_body_field},
ConnectionId,
};

#[async_trait::async_trait(?Send)]
impl PerformCrud for EditPrivateMessage {
Expand All @@ -40,6 +44,8 @@ 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()))?;

let private_message_id = data.private_message_id;
PrivateMessage::update(
context.pool(),
Expand Down
7 changes: 6 additions & 1 deletion crates/api_crud/src/site/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ use lemmy_db_schema::{
use lemmy_db_views::structs::SiteView;
use lemmy_utils::{
error::LemmyError,
utils::slurs::{check_slurs, check_slurs_opt},
utils::{
slurs::{check_slurs, check_slurs_opt},
validation::is_valid_body_field,
},
ConnectionId,
};
use url::Url;
Expand Down Expand Up @@ -68,6 +71,8 @@ impl PerformCrud for CreateSite {
site_description_length_check(desc)?;
}

is_valid_body_field(&data.sidebar)?;

let application_question = diesel_option_overwrite(&data.application_question);
check_application_question(
&application_question,
Expand Down
8 changes: 7 additions & 1 deletion crates/api_crud/src/site/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ use lemmy_db_schema::{
ListingType,
};
use lemmy_db_views::structs::SiteView;
use lemmy_utils::{error::LemmyError, utils::slurs::check_slurs_opt, ConnectionId};
use lemmy_utils::{
error::LemmyError,
utils::{slurs::check_slurs_opt, validation::is_valid_body_field},
ConnectionId,
};
use std::str::FromStr;

#[async_trait::async_trait(?Send)]
Expand Down Expand Up @@ -60,6 +64,8 @@ impl PerformCrud for EditSite {
site_description_length_check(desc)?;
}

is_valid_body_field(&data.sidebar)?;

let application_question = diesel_option_overwrite(&data.application_question);
check_application_question(
&application_question,
Expand Down
4 changes: 1 addition & 3 deletions crates/api_crud/src/user/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,7 @@ impl PerformCrud for Register {
check_slurs_opt(&data.answer, &slur_regex)?;

let actor_keypair = generate_actor_keypair()?;
if !is_valid_actor_name(&data.username, local_site.actor_name_max_length as usize) {
return Err(LemmyError::from_message("invalid_username"));
}
is_valid_actor_name(&data.username, local_site.actor_name_max_length as usize)?;
let actor_id = generate_local_apub_endpoint(
EndpointType::Person,
&data.username,
Expand Down
Loading