Adding more newline checks for actors.

This commit is contained in:
Dessalines 2021-11-23 09:19:34 -05:00
parent 778c86af04
commit eceed94cb9

View file

@ -13,7 +13,7 @@ static MENTIONS_REGEX: Lazy<Regex> = Lazy::new(|| {
static VALID_ACTOR_NAME_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r"^[a-zA-Z0-9_]{3,}$").expect("compile regex"));
static VALID_POST_TITLE_REGEX: Lazy<Regex> =
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<Regex> = 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<MentionData> {
}
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."));
}
}