diff --git a/config/config.hjson b/config/config.hjson index 461efa0d3..814976de2 100644 --- a/config/config.hjson +++ b/config/config.hjson @@ -39,6 +39,10 @@ pictrs_url: "http://pictrs:8080" # address where iframely is available iframely_url: "http://iframely" + # maximum length of local community names (between 3 and 255) + community_name_max_length: 20 + # maximum length of local user names (between 3 and 255) + user_name_max_length: 20 # rate limits for various user actions, by user ip rate_limit: { # maximum number of messages created in interval diff --git a/crates/utils/src/settings/defaults.rs b/crates/utils/src/settings/defaults.rs index 2dddd3f3b..386be3587 100644 --- a/crates/utils/src/settings/defaults.rs +++ b/crates/utils/src/settings/defaults.rs @@ -18,6 +18,8 @@ impl Default for Settings { pictrs_url: Some("http://pictrs:8080".into()), iframely_url: Some("http://iframely".into()), additional_slurs: None, + community_name_max_length: Some(20), + user_name_max_length: Some(20), } } } diff --git a/crates/utils/src/settings/structs.rs b/crates/utils/src/settings/structs.rs index 1a7a74343..11fc8ecbc 100644 --- a/crates/utils/src/settings/structs.rs +++ b/crates/utils/src/settings/structs.rs @@ -18,6 +18,8 @@ pub struct Settings { pub(crate) email: Option, pub(crate) setup: Option, pub(crate) additional_slurs: Option, + pub(crate) community_name_max_length: Option, + pub(crate) user_name_max_length: Option, } #[derive(Debug, Deserialize, Clone)] diff --git a/crates/utils/src/utils.rs b/crates/utils/src/utils.rs index ee0289143..10460dc46 100644 --- a/crates/utils/src/utils.rs +++ b/crates/utils/src/utils.rs @@ -22,8 +22,8 @@ lazy_static! { // TODO keep this old one, it didn't work with port well tho // static ref MENTIONS_REGEX: Regex = Regex::new(r"@(?P[\w.]+)@(?P[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)").expect("compile regex"); static ref MENTIONS_REGEX: Regex = Regex::new(r"@(?P[\w.]+)@(?P[a-zA-Z0-9._:-]+)").expect("compile regex"); - static ref VALID_USERNAME_REGEX: Regex = Regex::new(r"^[a-zA-Z0-9_]{3,20}$").expect("compile regex"); - static ref VALID_COMMUNITY_NAME_REGEX: Regex = Regex::new(r"^[a-z0-9_]{3,20}$").expect("compile regex"); + static ref VALID_USERNAME_REGEX: Regex = Regex::new(r"^[a-zA-Z0-9_]{3,}$").expect("compile regex"); + static ref VALID_COMMUNITY_NAME_REGEX: Regex = Regex::new(r"^[a-z0-9_]{3,}$").expect("compile regex"); static ref VALID_POST_TITLE_REGEX: Regex = Regex::new(r".*\S.*").expect("compile regex"); static ref VALID_MATRIX_ID_REGEX: Regex = Regex::new(r"^@[A-Za-z0-9._=-]+:[A-Za-z0-9.-]+\.[A-Za-z]{2,}$").expect("compile regex"); // taken from https://en.wikipedia.org/wiki/UTM_parameters @@ -117,7 +117,10 @@ pub fn scrape_text_for_mentions(text: &str) -> Vec { } pub fn is_valid_username(name: &str) -> bool { - VALID_USERNAME_REGEX.is_match(name) + let max_length = Settings::get() + .user_name_max_length + .unwrap_or_else(|| Settings::default().user_name_max_length.unwrap()); + name.chars().count() <= max_length && VALID_USERNAME_REGEX.is_match(name) } // Can't do a regex here, reverse lookarounds not supported @@ -133,7 +136,10 @@ pub fn is_valid_matrix_id(matrix_id: &str) -> bool { } pub fn is_valid_community_name(name: &str) -> bool { - VALID_COMMUNITY_NAME_REGEX.is_match(name) + let max_length = Settings::get() + .community_name_max_length + .unwrap_or_else(|| Settings::default().community_name_max_length.unwrap()); + name.chars().count() <= max_length && VALID_COMMUNITY_NAME_REGEX.is_match(name) } pub fn is_valid_post_title(title: &str) -> bool { diff --git a/migrations/2021-07-20-102033_actor_name_length/down.sql b/migrations/2021-07-20-102033_actor_name_length/down.sql new file mode 100644 index 000000000..3deda6786 --- /dev/null +++ b/migrations/2021-07-20-102033_actor_name_length/down.sql @@ -0,0 +1,2 @@ +ALTER TABLE community ALTER COLUMN name TYPE varchar(20); +ALTER TABLE person ALTER COLUMN name TYPE varchar(20); diff --git a/migrations/2021-07-20-102033_actor_name_length/up.sql b/migrations/2021-07-20-102033_actor_name_length/up.sql new file mode 100644 index 000000000..dfc58b793 --- /dev/null +++ b/migrations/2021-07-20-102033_actor_name_length/up.sql @@ -0,0 +1,2 @@ +ALTER TABLE community ALTER COLUMN name TYPE varchar(255); +ALTER TABLE person ALTER COLUMN name TYPE varchar(255);