From eceed94cb9b2dcd2633086c7018d48ac72f37390 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Tue, 23 Nov 2021 09:19:34 -0500 Subject: [PATCH] Adding more newline checks for actors. --- crates/utils/src/utils.rs | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/crates/utils/src/utils.rs b/crates/utils/src/utils.rs index f695b3ac4..122ee4b7d 100644 --- a/crates/utils/src/utils.rs +++ b/crates/utils/src/utils.rs @@ -13,7 +13,7 @@ static MENTIONS_REGEX: Lazy = Lazy::new(|| { static VALID_ACTOR_NAME_REGEX: Lazy = Lazy::new(|| Regex::new(r"^[a-zA-Z0-9_]{3,}$").expect("compile regex")); static VALID_POST_TITLE_REGEX: Lazy = - Lazy::new(|| Regex::new(r".*\S.*").expect("compile regex")); + Lazy::new(|| Regex::new(r".*\S{3,}.*").expect("compile regex")); static VALID_MATRIX_ID_REGEX: Lazy = Lazy::new(|| { Regex::new(r"^@[A-Za-z0-9._=-]+:[A-Za-z0-9.-]+\.[A-Za-z]{2,}$").expect("compile regex") }); @@ -121,7 +121,9 @@ pub fn scrape_text_for_mentions(text: &str) -> Vec { } pub fn is_valid_actor_name(name: &str, actor_name_max_length: usize) -> bool { - name.chars().count() <= actor_name_max_length && VALID_ACTOR_NAME_REGEX.is_match(name) + name.chars().count() <= actor_name_max_length + && VALID_ACTOR_NAME_REGEX.is_match(name) + && !name.contains('\n') } // Can't do a regex here, reverse lookarounds not supported @@ -130,14 +132,15 @@ pub fn is_valid_display_name(name: &str, actor_name_max_length: usize) -> bool { && !name.starts_with('\u{200b}') && name.chars().count() >= 3 && name.chars().count() <= actor_name_max_length + && !name.contains('\n') } pub fn is_valid_matrix_id(matrix_id: &str) -> bool { - VALID_MATRIX_ID_REGEX.is_match(matrix_id) + VALID_MATRIX_ID_REGEX.is_match(matrix_id) && !matrix_id.contains('\n') } pub fn is_valid_post_title(title: &str) -> bool { - VALID_POST_TITLE_REGEX.is_match(title) && title.chars().count() >= 3 && !title.contains('\n') + VALID_POST_TITLE_REGEX.is_match(title) && !title.contains('\n') } pub fn get_ip(conn_info: &ConnectionInfo) -> IpAddr { @@ -166,7 +169,7 @@ pub fn clean_url_params(mut url: Url) -> Url { #[cfg(test)] mod tests { - use crate::utils::clean_url_params; + use crate::utils::{clean_url_params, is_valid_post_title}; use url::Url; #[test] @@ -180,4 +183,13 @@ mod tests { let cleaned = clean_url_params(url.clone()); assert_eq!(url.to_string(), cleaned.to_string()); } + + #[test] + fn regex_checks() { + assert!(!is_valid_post_title("hi")); + assert!(is_valid_post_title("him")); + assert!(!is_valid_post_title("n\n\n\n\nanother")); + assert!(!is_valid_post_title("hello there!\n this is a test.")); + assert!(is_valid_post_title("hello there! this is a test.")); + } }