From f5dd0a17c9e6152adc3ec1667cdfef34cc5fc81f Mon Sep 17 00:00:00 2001 From: silverpill Date: Wed, 22 Mar 2023 23:37:13 +0000 Subject: [PATCH] Move all validators to validators module --- mitra-cli/src/cli.rs | 2 +- src/activitypub/actors/helpers.rs | 2 +- src/activitypub/handlers/create.rs | 26 ++++++++++--------- src/lib.rs | 1 + src/mastodon_api/accounts/types.rs | 6 +++-- src/mastodon_api/instance/types.rs | 2 +- src/mastodon_api/statuses/views.rs | 10 +++---- src/models/emojis/mod.rs | 1 - src/models/emojis/types.rs | 4 +-- src/models/posts/emojis.rs | 2 +- src/models/posts/mod.rs | 1 - src/models/profiles/mod.rs | 1 - src/models/profiles/types.rs | 2 +- src/models/users/types.rs | 19 -------------- .../validators.rs => validators/emojis.rs} | 0 src/validators/mod.rs | 4 +++ .../validators.rs => validators/posts.rs} | 0 .../validators.rs => validators/profiles.rs} | 2 +- src/validators/users.rs | 25 ++++++++++++++++++ src/webfinger/types.rs | 2 +- 20 files changed, 62 insertions(+), 50 deletions(-) rename src/{models/emojis/validators.rs => validators/emojis.rs} (100%) create mode 100644 src/validators/mod.rs rename src/{models/posts/validators.rs => validators/posts.rs} (100%) rename src/{models/profiles/validators.rs => validators/profiles.rs} (98%) create mode 100644 src/validators/users.rs diff --git a/mitra-cli/src/cli.rs b/mitra-cli/src/cli.rs index e14cd61..89cba99 100644 --- a/mitra-cli/src/cli.rs +++ b/mitra-cli/src/cli.rs @@ -25,7 +25,6 @@ use mitra::models::{ find_unused_remote_emojis, get_emoji_by_name_and_hostname, }, - emojis::validators::EMOJI_LOCAL_MAX_SIZE, oauth::queries::delete_oauth_tokens, posts::queries::{delete_post, find_extraneous_posts, get_post_by_id}, profiles::queries::{ @@ -49,6 +48,7 @@ use mitra::monero::{ helpers::check_expired_invoice, wallet::create_monero_wallet, }; +use mitra::validators::emojis::EMOJI_LOCAL_MAX_SIZE; use mitra_config::Config; use mitra_utils::{ crypto_rsa::{ diff --git a/src/activitypub/actors/helpers.rs b/src/activitypub/actors/helpers.rs index deb6c99..268d535 100644 --- a/src/activitypub/actors/helpers.rs +++ b/src/activitypub/actors/helpers.rs @@ -13,7 +13,6 @@ use crate::activitypub::{ }; use crate::database::DatabaseClient; use crate::models::{ - posts::validators::EMOJIS_MAX_NUM, profiles::queries::{create_profile, update_profile}, profiles::types::{ DbActorProfile, @@ -22,6 +21,7 @@ use crate::models::{ ProfileUpdateData, }, }; +use crate::validators::posts::EMOJIS_MAX_NUM; pub const ACTOR_IMAGE_MAX_SIZE: usize = 5 * 1000 * 1000; // 5 MB diff --git a/src/activitypub/handlers/create.rs b/src/activitypub/handlers/create.rs index 1f23b75..bc9429c 100644 --- a/src/activitypub/handlers/create.rs +++ b/src/activitypub/handlers/create.rs @@ -35,24 +35,26 @@ use crate::models::{ update_emoji, }, emojis::types::{DbEmoji, EmojiImage}, - emojis::validators::{ + posts::{ + hashtags::normalize_hashtag, + queries::create_post, + types::{Post, PostCreateData, Visibility}, + }, + profiles::types::DbActorProfile, + users::queries::get_user_by_name, +}; +use crate::validators::{ + emojis::{ validate_emoji_name, EMOJI_MAX_SIZE, EMOJI_MEDIA_TYPES, }, posts::{ - hashtags::normalize_hashtag, - queries::create_post, - types::{Post, PostCreateData, Visibility}, - validators::{ - content_allowed_classes, - ATTACHMENTS_MAX_NUM, - CONTENT_MAX_SIZE, - EMOJIS_MAX_NUM, - }, + content_allowed_classes, + ATTACHMENTS_MAX_NUM, + CONTENT_MAX_SIZE, + EMOJIS_MAX_NUM, }, - profiles::types::DbActorProfile, - users::queries::get_user_by_name, }; use crate::webfinger::types::ActorAddress; use super::HandlerResult; diff --git a/src/lib.rs b/src/lib.rs index ebc26f8..055c3f2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,5 +15,6 @@ pub mod media; pub mod models; pub mod monero; pub mod nodeinfo; +pub mod validators; pub mod webfinger; pub mod web_client; diff --git a/src/mastodon_api/accounts/types.rs b/src/mastodon_api/accounts/types.rs index 027ff0e..289ca79 100644 --- a/src/mastodon_api/accounts/types.rs +++ b/src/mastodon_api/accounts/types.rs @@ -29,15 +29,17 @@ use crate::models::{ ProfileImage, ProfileUpdateData, }, - profiles::validators::validate_username, subscriptions::types::Subscription, users::types::{ - validate_local_username, Role, Permission, User, }, }; +use crate::validators::{ + profiles::validate_username, + users::validate_local_username, +}; /// https://docs.joinmastodon.org/entities/field/ #[derive(Serialize)] diff --git a/src/mastodon_api/instance/types.rs b/src/mastodon_api/instance/types.rs index ce55c46..f244945 100644 --- a/src/mastodon_api/instance/types.rs +++ b/src/mastodon_api/instance/types.rs @@ -12,7 +12,7 @@ use mitra_utils::markdown::markdown_to_html; use crate::ethereum::contracts::ContractSet; use crate::mastodon_api::MASTODON_API_VERSION; use crate::media::SUPPORTED_MEDIA_TYPES; -use crate::models::posts::validators::ATTACHMENTS_MAX_NUM; +use crate::validators::posts::ATTACHMENTS_MAX_NUM; #[derive(Serialize)] struct InstanceStats { diff --git a/src/mastodon_api/statuses/views.rs b/src/mastodon_api/statuses/views.rs index 08d277a..375b445 100644 --- a/src/mastodon_api/statuses/views.rs +++ b/src/mastodon_api/statuses/views.rs @@ -49,17 +49,17 @@ use crate::models::{ delete_post, }, posts::types::{PostCreateData, Visibility}, - posts::validators::{ - clean_content, - ATTACHMENTS_MAX_NUM, - EMOJIS_MAX_NUM, - }, reactions::queries::{ create_reaction, delete_reaction, }, relationships::queries::get_subscribers, }; +use crate::validators::posts::{ + clean_content, + ATTACHMENTS_MAX_NUM, + EMOJIS_MAX_NUM, +}; use super::helpers::{ build_status, build_status_list, diff --git a/src/models/emojis/mod.rs b/src/models/emojis/mod.rs index 344a039..391d9b9 100644 --- a/src/models/emojis/mod.rs +++ b/src/models/emojis/mod.rs @@ -1,4 +1,3 @@ pub mod helpers; pub mod queries; pub mod types; -pub mod validators; diff --git a/src/models/emojis/types.rs b/src/models/emojis/types.rs index 6c5cf37..98bb822 100644 --- a/src/models/emojis/types.rs +++ b/src/models/emojis/types.rs @@ -4,9 +4,9 @@ use serde::{Deserialize, Serialize}; use uuid::Uuid; use crate::database::json_macro::{json_from_sql, json_to_sql}; -use super::validators::EMOJI_MAX_SIZE; -fn default_emoji_file_size() -> usize { EMOJI_MAX_SIZE } +// Migration +fn default_emoji_file_size() -> usize { 250 * 1000 } #[derive(Clone, Debug, Deserialize, Serialize)] #[cfg_attr(test, derive(Default))] diff --git a/src/models/posts/emojis.rs b/src/models/posts/emojis.rs index 7faf825..aee723c 100644 --- a/src/models/posts/emojis.rs +++ b/src/models/posts/emojis.rs @@ -9,7 +9,7 @@ use crate::models::{ }; use super::links::is_inside_code_block; -// See also: EMOJI_NAME_RE in models::emojis::validators +// See also: EMOJI_NAME_RE in validators::emojis const SHORTCODE_SEARCH_RE: &str = r"(?m):(?P[\w.]+):"; /// Finds emoji shortcodes in text diff --git a/src/models/posts/mod.rs b/src/models/posts/mod.rs index 19cfe13..143bcec 100644 --- a/src/models/posts/mod.rs +++ b/src/models/posts/mod.rs @@ -5,4 +5,3 @@ pub mod links; pub mod mentions; pub mod queries; pub mod types; -pub mod validators; diff --git a/src/models/profiles/mod.rs b/src/models/profiles/mod.rs index 344a039..391d9b9 100644 --- a/src/models/profiles/mod.rs +++ b/src/models/profiles/mod.rs @@ -1,4 +1,3 @@ pub mod helpers; pub mod queries; pub mod types; -pub mod validators; diff --git a/src/models/profiles/types.rs b/src/models/profiles/types.rs index b2682a3..4b0a41c 100644 --- a/src/models/profiles/types.rs +++ b/src/models/profiles/types.rs @@ -22,7 +22,7 @@ use crate::database::{ }; use crate::errors::ValidationError; use crate::models::emojis::types::DbEmoji; -use super::validators::{ +use crate::validators::profiles::{ validate_username, validate_display_name, clean_bio, diff --git a/src/models/users/types.rs b/src/models/users/types.rs index 8379450..df7c7cf 100644 --- a/src/models/users/types.rs +++ b/src/models/users/types.rs @@ -1,6 +1,5 @@ use chrono::{DateTime, Utc}; use postgres_types::FromSql; -use regex::Regex; use uuid::Uuid; use mitra_utils::{ @@ -12,7 +11,6 @@ use crate::database::{ int_enum::{int_enum_from_sql, int_enum_to_sql}, DatabaseTypeError, }; -use crate::errors::ValidationError; use crate::models::profiles::types::DbActorProfile; #[allow(dead_code)] @@ -172,15 +170,6 @@ pub struct UserCreateData { pub role: Role, } -pub fn validate_local_username(username: &str) -> Result<(), ValidationError> { - // The username regexp should not allow domain names and IP addresses - let username_regexp = Regex::new(r"^[a-z0-9_]+$").unwrap(); - if !username_regexp.is_match(username) { - return Err(ValidationError("invalid username")); - } - Ok(()) -} - #[cfg(test)] mod tests { use super::*; @@ -194,12 +183,4 @@ mod tests { let ethereum = Currency::Ethereum; assert_eq!(user.public_wallet_address(ðereum), None); } - - #[test] - fn test_validate_local_username() { - let result_1 = validate_local_username("name_1"); - assert_eq!(result_1.is_ok(), true); - let result_2 = validate_local_username("name&"); - assert_eq!(result_2.is_ok(), false); - } } diff --git a/src/models/emojis/validators.rs b/src/validators/emojis.rs similarity index 100% rename from src/models/emojis/validators.rs rename to src/validators/emojis.rs diff --git a/src/validators/mod.rs b/src/validators/mod.rs new file mode 100644 index 0000000..c659e77 --- /dev/null +++ b/src/validators/mod.rs @@ -0,0 +1,4 @@ +pub mod emojis; +pub mod posts; +pub mod profiles; +pub mod users; diff --git a/src/models/posts/validators.rs b/src/validators/posts.rs similarity index 100% rename from src/models/posts/validators.rs rename to src/validators/posts.rs diff --git a/src/models/profiles/validators.rs b/src/validators/profiles.rs similarity index 98% rename from src/models/profiles/validators.rs rename to src/validators/profiles.rs index a48e97e..ede6cfc 100644 --- a/src/models/profiles/validators.rs +++ b/src/validators/profiles.rs @@ -3,7 +3,7 @@ use regex::Regex; use mitra_utils::html::{clean_html, clean_html_strict}; use crate::errors::ValidationError; -use super::types::ExtraField; +use crate::models::profiles::types::ExtraField; const USERNAME_RE: &str = r"^[a-zA-Z0-9_\.-]+$"; const DISPLAY_NAME_MAX_LENGTH: usize = 200; diff --git a/src/validators/users.rs b/src/validators/users.rs new file mode 100644 index 0000000..8e29e72 --- /dev/null +++ b/src/validators/users.rs @@ -0,0 +1,25 @@ +use regex::Regex; + +use crate::errors::ValidationError; + +pub fn validate_local_username(username: &str) -> Result<(), ValidationError> { + // The username regexp should not allow domain names and IP addresses + let username_regexp = Regex::new(r"^[a-z0-9_]+$").unwrap(); + if !username_regexp.is_match(username) { + return Err(ValidationError("invalid username")); + }; + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_validate_local_username() { + let result_1 = validate_local_username("name_1"); + assert_eq!(result_1.is_ok(), true); + let result_2 = validate_local_username("name&"); + assert_eq!(result_2.is_ok(), false); + } +} diff --git a/src/webfinger/types.rs b/src/webfinger/types.rs index 5a03b3a..0713815 100644 --- a/src/webfinger/types.rs +++ b/src/webfinger/types.rs @@ -11,7 +11,7 @@ use serde::{Serialize, Deserialize}; use crate::errors::ValidationError; use crate::models::profiles::types::DbActorProfile; -// See also: USERNAME_RE in models::profiles::validators +// See also: USERNAME_RE in validators::profiles const ACTOR_ADDRESS_RE: &str = r"^(?P[\w\.-]+)@(?P[\w\.-]+)$"; pub const JRD_CONTENT_TYPE: &str = "application/jrd+json";