Remove character limit check

This commit is contained in:
silverpill 2022-10-25 19:03:38 +00:00
parent a3723e2e6d
commit a67a7b9b17
5 changed files with 19 additions and 19 deletions

View file

@ -21,11 +21,14 @@ use crate::activitypub::{
use crate::config::Instance; use crate::config::Instance;
use crate::errors::{ConversionError, DatabaseError, ValidationError}; use crate::errors::{ConversionError, DatabaseError, ValidationError};
use crate::models::attachments::queries::create_attachment; use crate::models::attachments::queries::create_attachment;
use crate::models::posts::hashtags::normalize_hashtag; use crate::models::posts::{
use crate::models::posts::helpers::get_post_by_object_id; hashtags::normalize_hashtag,
use crate::models::posts::mentions::mention_to_address; helpers::get_post_by_object_id,
use crate::models::posts::queries::create_post; mentions::mention_to_address,
use crate::models::posts::types::{Post, PostCreateData, Visibility}; queries::create_post,
types::{Post, PostCreateData, Visibility},
validators::CONTENT_MAX_SIZE,
};
use crate::models::profiles::queries::get_profile_by_acct; use crate::models::profiles::queries::get_profile_by_acct;
use crate::models::profiles::types::DbActorProfile; use crate::models::profiles::types::DbActorProfile;
use crate::models::users::queries::get_user_by_name; use crate::models::users::queries::get_user_by_name;
@ -42,7 +45,6 @@ fn get_note_author_id(object: &Object) -> Result<String, ValidationError> {
Ok(author_id) Ok(author_id)
} }
const CONTENT_MAX_SIZE: usize = 100000;
const ATTACHMENTS_MAX_NUM: usize = 15; const ATTACHMENTS_MAX_NUM: usize = 15;
fn parse_object_url(value: &JsonValue) -> Result<String, ConversionError> { fn parse_object_url(value: &JsonValue) -> Result<String, ConversionError> {

View file

@ -58,7 +58,7 @@ async fn create_status(
let current_user = get_current_user(db_client, auth.token()).await?; let current_user = get_current_user(db_client, auth.token()).await?;
let instance = config.instance(); let instance = config.instance();
let mut post_data = PostCreateData::try_from(status_data.into_inner())?; let mut post_data = PostCreateData::try_from(status_data.into_inner())?;
post_data.clean(config.post_character_limit)?; post_data.clean()?;
// Mentions // Mentions
let mention_map = find_mentioned_profiles( let mention_map = find_mentioned_profiles(
db_client, db_client,

View file

@ -4,4 +4,4 @@ pub mod links;
pub mod mentions; pub mod mentions;
pub mod queries; pub mod queries;
pub mod types; pub mod types;
mod validators; pub mod validators;

View file

@ -261,9 +261,9 @@ impl PostCreateData {
} }
/// Validate and clean post data (only for local posts). /// Validate and clean post data (only for local posts).
pub fn clean(&mut self, character_limit: usize) -> Result<(), ValidationError> { pub fn clean(&mut self) -> Result<(), ValidationError> {
assert!(self.object_id.is_none()); assert!(self.object_id.is_none());
self.content = clean_content(&self.content, character_limit)?; self.content = clean_content(&self.content)?;
Ok(()) Ok(())
} }
} }
@ -277,15 +277,13 @@ pub struct PostUpdateData {
mod tests { mod tests {
use super::*; use super::*;
const POST_CHARACTER_LIMIT: usize = 1000;
#[test] #[test]
fn test_post_data_clean() { fn test_post_data_clean() {
let mut post_data_2 = PostCreateData { let mut post_data_2 = PostCreateData {
content: "test ".to_string(), content: "test ".to_string(),
..Default::default() ..Default::default()
}; };
assert_eq!(post_data_2.clean(POST_CHARACTER_LIMIT).is_ok(), true); assert_eq!(post_data_2.clean().is_ok(), true);
assert_eq!(post_data_2.content.as_str(), "test"); assert_eq!(post_data_2.content.as_str(), "test");
} }
} }

View file

@ -1,6 +1,7 @@
use crate::errors::ValidationError; use crate::errors::ValidationError;
use crate::utils::html::clean_html_strict; use crate::utils::html::clean_html_strict;
pub const CONTENT_MAX_SIZE: usize = 100000;
const CONTENT_ALLOWED_TAGS: [&str; 7] = [ const CONTENT_ALLOWED_TAGS: [&str; 7] = [
"a", "a",
"br", "br",
@ -13,9 +14,10 @@ const CONTENT_ALLOWED_TAGS: [&str; 7] = [
pub fn clean_content( pub fn clean_content(
content: &str, content: &str,
character_limit: usize,
) -> Result<String, ValidationError> { ) -> Result<String, ValidationError> {
if content.chars().count() > character_limit { // Check content size to not exceed the hard limit
// Character limit from config is not enforced at the backend
if content.len() > CONTENT_MAX_SIZE {
return Err(ValidationError("post is too long")); return Err(ValidationError("post is too long"));
}; };
let content_safe = clean_html_strict(content, &CONTENT_ALLOWED_TAGS); let content_safe = clean_html_strict(content, &CONTENT_ALLOWED_TAGS);
@ -30,19 +32,17 @@ pub fn clean_content(
mod tests { mod tests {
use super::*; use super::*;
const POST_CHARACTER_LIMIT: usize = 1000;
#[test] #[test]
fn test_clean_content_empty() { fn test_clean_content_empty() {
let content = " "; let content = " ";
let result = clean_content(content, POST_CHARACTER_LIMIT); let result = clean_content(content);
assert_eq!(result.is_ok(), false); assert_eq!(result.is_ok(), false);
} }
#[test] #[test]
fn test_clean_content_trimming() { fn test_clean_content_trimming() {
let content = "test "; let content = "test ";
let cleaned = clean_content(content, POST_CHARACTER_LIMIT).unwrap(); let cleaned = clean_content(content).unwrap();
assert_eq!(cleaned, "test"); assert_eq!(cleaned, "test");
} }
} }