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

Add separate Post check for is_valid_body_field #3263

Merged
merged 10 commits into from
Jun 26, 2023
2 changes: 1 addition & 1 deletion crates/api/src/community/ban.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion crates/api/src/local_user/ban_person.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion crates/api_crud/src/comment/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion crates/api_crud/src/comment/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion crates/api_crud/src/community/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion crates/api_crud/src/community/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion crates/api_crud/src/post/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;
Expand Down
2 changes: 1 addition & 1 deletion crates/api_crud/src/post/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;
Expand Down
2 changes: 1 addition & 1 deletion crates/api_crud/src/private_message/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;

Expand Down
2 changes: 1 addition & 1 deletion crates/api_crud/src/private_message/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion crates/api_crud/src/site/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion crates/api_crud/src/site/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
10 changes: 8 additions & 2 deletions crates/utils/src/utils/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down