Make length of user/community name configurable (fixes #1306)

This commit is contained in:
Felix Ableitner 2021-07-20 13:08:00 +02:00
parent 5fe7de2bff
commit e812ba10d3
6 changed files with 22 additions and 4 deletions

View file

@ -39,6 +39,10 @@
pictrs_url: "http://pictrs:8080" pictrs_url: "http://pictrs:8080"
# address where iframely is available # address where iframely is available
iframely_url: "http://iframely" 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 limits for various user actions, by user ip
rate_limit: { rate_limit: {
# maximum number of messages created in interval # maximum number of messages created in interval

View file

@ -18,6 +18,8 @@ impl Default for Settings {
pictrs_url: Some("http://pictrs:8080".into()), pictrs_url: Some("http://pictrs:8080".into()),
iframely_url: Some("http://iframely".into()), iframely_url: Some("http://iframely".into()),
additional_slurs: None, additional_slurs: None,
community_name_max_length: Some(20),
user_name_max_length: Some(20),
} }
} }
} }

View file

@ -18,6 +18,8 @@ pub struct Settings {
pub(crate) email: Option<EmailConfig>, pub(crate) email: Option<EmailConfig>,
pub(crate) setup: Option<SetupConfig>, pub(crate) setup: Option<SetupConfig>,
pub(crate) additional_slurs: Option<String>, pub(crate) additional_slurs: Option<String>,
pub(crate) community_name_max_length: Option<usize>,
pub(crate) user_name_max_length: Option<usize>,
} }
#[derive(Debug, Deserialize, Clone)] #[derive(Debug, Deserialize, Clone)]

View file

@ -22,8 +22,8 @@ lazy_static! {
// TODO keep this old one, it didn't work with port well tho // TODO keep this old one, it didn't work with port well tho
// static ref MENTIONS_REGEX: Regex = Regex::new(r"@(?P<name>[\w.]+)@(?P<domain>[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)").expect("compile regex"); // static ref MENTIONS_REGEX: Regex = Regex::new(r"@(?P<name>[\w.]+)@(?P<domain>[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)").expect("compile regex");
static ref MENTIONS_REGEX: Regex = Regex::new(r"@(?P<name>[\w.]+)@(?P<domain>[a-zA-Z0-9._:-]+)").expect("compile regex"); static ref MENTIONS_REGEX: Regex = Regex::new(r"@(?P<name>[\w.]+)@(?P<domain>[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_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,20}$").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_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"); 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 // taken from https://en.wikipedia.org/wiki/UTM_parameters
@ -117,7 +117,10 @@ pub fn scrape_text_for_mentions(text: &str) -> Vec<MentionData> {
} }
pub fn is_valid_username(name: &str) -> bool { 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 // 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 { 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 { pub fn is_valid_post_title(title: &str) -> bool {

View file

@ -0,0 +1,2 @@
ALTER TABLE community ALTER COLUMN name TYPE varchar(20);
ALTER TABLE person ALTER COLUMN name TYPE varchar(20);

View file

@ -0,0 +1,2 @@
ALTER TABLE community ALTER COLUMN name TYPE varchar(255);
ALTER TABLE person ALTER COLUMN name TYPE varchar(255);