]> Untitled Git - lemmy.git/commitdiff
Actor name length config dess (#1672)
authorDessalines <dessalines@users.noreply.github.com>
Fri, 23 Jul 2021 01:53:44 +0000 (21:53 -0400)
committerGitHub <noreply@github.com>
Fri, 23 Jul 2021 01:53:44 +0000 (21:53 -0400)
* Make length of user/community name configurable (fixes #1306)

* use single config value, fix migrations, increase displayname length

* remove debug statements

* add helper method for setting actor_name_max_length

* move function

* Fixing some defaults

Co-authored-by: Felix Ableitner <me@nutomic.com>
config/config.hjson
crates/api_crud/src/community/create.rs
crates/api_crud/src/user/create.rs
crates/utils/src/lib.rs
crates/utils/src/settings/defaults.rs
crates/utils/src/settings/mod.rs
crates/utils/src/settings/structs.rs
crates/utils/src/test.rs
crates/utils/src/utils.rs
migrations/2021-07-20-102033_actor_name_length/down.sql [new file with mode: 0644]
migrations/2021-07-20-102033_actor_name_length/up.sql [new file with mode: 0644]

index 461efa0d36f843623204750eff5baeda11a4dc80..236751f4e8d06837ae2e3935504c6413dcaf0923 100644 (file)
@@ -39,6 +39,8 @@
   pictrs_url: "http://pictrs:8080"
   # address where iframely is available
   iframely_url: "http://iframely"
+  # maximum length of local community and user names
+  actor_name_max_length: 20
   # rate limits for various user actions, by user ip
   rate_limit: {
     # maximum number of messages created in interval
index 5c55e149e5e407849569c0a554a31595c0b90463..ecaa9058dd88964ad4b312401cbc611bf82cb1ab 100644 (file)
@@ -28,7 +28,7 @@ use lemmy_db_schema::source::{
 use lemmy_db_views_actor::community_view::CommunityView;
 use lemmy_utils::{
   apub::generate_actor_keypair,
-  utils::{check_slurs, check_slurs_opt, is_valid_community_name},
+  utils::{check_slurs, check_slurs_opt, is_valid_actor_name},
   ApiError,
   ConnectionId,
   LemmyError,
@@ -56,7 +56,7 @@ impl PerformCrud for CreateCommunity {
     check_slurs(&data.title)?;
     check_slurs_opt(&data.description)?;
 
-    if !is_valid_community_name(&data.name) {
+    if !is_valid_actor_name(&data.name) {
       return Err(ApiError::err("invalid_community_name").into());
     }
 
index 82788b6dad4384da7a6e5cb64818674ec0f03e70..8047369fa722e24bb272650871fd647a35e111e6 100644 (file)
@@ -30,7 +30,7 @@ use lemmy_utils::{
   apub::generate_actor_keypair,
   claims::Claims,
   settings::structs::Settings,
-  utils::{check_slurs, is_valid_username},
+  utils::{check_slurs, is_valid_actor_name},
   ApiError,
   ConnectionId,
   LemmyError,
@@ -91,7 +91,7 @@ impl PerformCrud for Register {
     check_slurs(&data.username)?;
 
     let actor_keypair = generate_actor_keypair()?;
-    if !is_valid_username(&data.username) {
+    if !is_valid_actor_name(&data.username) {
       return Err(ApiError::err("invalid_username").into());
     }
     let actor_id = generate_apub_endpoint(EndpointType::Person, &data.username)?;
index f917d93890d5cad7cbf902719702038c82e5aa25..ca544bd49aed0375014a991ec160dbed6c244157 100644 (file)
@@ -89,12 +89,12 @@ impl actix_web::error::ResponseError for LemmyError {
 
 lazy_static! {
   pub static ref WEBFINGER_COMMUNITY_REGEX: Regex = Regex::new(&format!(
-    "^group:([a-z0-9_]{{3, 20}})@{}$",
+    "^group:([a-z0-9_]{{3,}})@{}$",
     Settings::get().hostname()
   ))
   .expect("compile webfinger regex");
   pub static ref WEBFINGER_USERNAME_REGEX: Regex = Regex::new(&format!(
-    "^acct:([a-z0-9_]{{3, 20}})@{}$",
+    "^acct:([a-z0-9_]{{3,}})@{}$",
     Settings::get().hostname()
   ))
   .expect("compile webfinger regex");
index 2dddd3f3bb8756d3c7782a4de06529dd82a57f27..1333ebe3d7acb6ccd7ec149052dcbd6451bdaaf0 100644 (file)
@@ -18,6 +18,7 @@ impl Default for Settings {
       pictrs_url: Some("http://pictrs:8080".into()),
       iframely_url: Some("http://iframely".into()),
       additional_slurs: None,
+      actor_name_max_length: Some(20),
     }
   }
 }
index 8c9f82ccfddf4fbaaa9114f645ed2f2cf51b338c..b247bae93697ce9cdc76e66c441f8473b40f0f77 100644 (file)
@@ -132,29 +132,52 @@ impl Settings {
     Ok(Self::read_config_file()?)
   }
 
-  pub fn database(&self) -> DatabaseConfig {
-    self.database.to_owned().unwrap_or_default()
-  }
   pub fn hostname(&self) -> String {
-    self.hostname.to_owned().unwrap_or_default()
+    self.hostname.to_owned().expect("No hostname given")
   }
   pub fn bind(&self) -> IpAddr {
     self.bind.expect("return bind address")
   }
   pub fn port(&self) -> u16 {
-    self.port.unwrap_or_default()
+    self
+      .port
+      .unwrap_or_else(|| Settings::default().port.expect("missing port"))
   }
   pub fn tls_enabled(&self) -> bool {
-    self.tls_enabled.unwrap_or_default()
+    self.tls_enabled.unwrap_or_else(|| {
+      Settings::default()
+        .tls_enabled
+        .expect("missing tls_enabled")
+    })
   }
   pub fn jwt_secret(&self) -> String {
-    self.jwt_secret.to_owned().unwrap_or_default()
+    self
+      .jwt_secret
+      .to_owned()
+      .unwrap_or_else(|| Settings::default().jwt_secret.expect("missing jwt_secret"))
   }
   pub fn pictrs_url(&self) -> String {
-    self.pictrs_url.to_owned().unwrap_or_default()
+    self
+      .pictrs_url
+      .to_owned()
+      .unwrap_or_else(|| Settings::default().pictrs_url.expect("missing pictrs_url"))
   }
   pub fn iframely_url(&self) -> String {
-    self.iframely_url.to_owned().unwrap_or_default()
+    self.iframely_url.to_owned().unwrap_or_else(|| {
+      Settings::default()
+        .iframely_url
+        .expect("missing iframely_url")
+    })
+  }
+  pub fn actor_name_max_length(&self) -> usize {
+    self.actor_name_max_length.unwrap_or_else(|| {
+      Settings::default()
+        .actor_name_max_length
+        .expect("missing actor name length")
+    })
+  }
+  pub fn database(&self) -> DatabaseConfig {
+    self.database.to_owned().unwrap_or_default()
   }
   pub fn rate_limit(&self) -> RateLimitConfig {
     self.rate_limit.to_owned().unwrap_or_default()
index 1a7a74343228471d69c1f9ef62bc12a9dc1cc304..1046bd147d6987b13bc8ebea543630cf70e52ffd 100644 (file)
@@ -18,6 +18,7 @@ pub struct Settings {
   pub(crate) email: Option<EmailConfig>,
   pub(crate) setup: Option<SetupConfig>,
   pub(crate) additional_slurs: Option<String>,
+  pub(crate) actor_name_max_length: Option<usize>,
 }
 
 #[derive(Debug, Deserialize, Clone)]
index 2d9d417f399ec8c3f1eba27f41741aa528c1ab63..33bf38700ca9904a0598a96c25895e4c09af415f 100644 (file)
@@ -1,9 +1,8 @@
 use crate::utils::{
-  is_valid_community_name,
+  is_valid_actor_name,
   is_valid_display_name,
   is_valid_matrix_id,
   is_valid_post_title,
-  is_valid_username,
   remove_slurs,
   scrape_text_for_mentions,
   slur_check,
@@ -21,12 +20,12 @@ fn test_mentions_regex() {
 }
 
 #[test]
-fn test_valid_register_username() {
-  assert!(is_valid_username("Hello_98"));
-  assert!(is_valid_username("ten"));
-  assert!(!is_valid_username("Hello-98"));
-  assert!(!is_valid_username("a"));
-  assert!(!is_valid_username(""));
+fn test_valid_actor_name() {
+  assert!(is_valid_actor_name("Hello_98"));
+  assert!(is_valid_actor_name("ten"));
+  assert!(!is_valid_actor_name("Hello-98"));
+  assert!(!is_valid_actor_name("a"));
+  assert!(!is_valid_actor_name(""));
 }
 
 #[test]
@@ -41,15 +40,6 @@ fn test_valid_display_name() {
   )));
 }
 
-#[test]
-fn test_valid_community_name() {
-  assert!(is_valid_community_name("example"));
-  assert!(is_valid_community_name("example_community"));
-  assert!(!is_valid_community_name("Example"));
-  assert!(!is_valid_community_name("Ex"));
-  assert!(!is_valid_community_name(""));
-}
-
 #[test]
 fn test_valid_post_title() {
   assert!(is_valid_post_title("Post Title"));
index ee02891432e71b483a3370201b61a44b9a9288e4..d6e1f25d4c2b34dc8a470e47346149f50cd504c3 100644 (file)
@@ -22,8 +22,7 @@ lazy_static! {
   // 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._:-]+)").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_ACTOR_NAME_REGEX: Regex = Regex::new(r"^[a-zA-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
@@ -116,8 +115,9 @@ pub fn scrape_text_for_mentions(text: &str) -> Vec<MentionData> {
   out.into_iter().unique().collect()
 }
 
-pub fn is_valid_username(name: &str) -> bool {
-  VALID_USERNAME_REGEX.is_match(name)
+pub fn is_valid_actor_name(name: &str) -> bool {
+  name.chars().count() <= Settings::get().actor_name_max_length()
+    && VALID_ACTOR_NAME_REGEX.is_match(name)
 }
 
 // Can't do a regex here, reverse lookarounds not supported
@@ -125,17 +125,13 @@ pub fn is_valid_display_name(name: &str) -> bool {
   !name.starts_with('@')
     && !name.starts_with('\u{200b}')
     && name.chars().count() >= 3
-    && name.chars().count() <= 20
+    && name.chars().count() <= Settings::get().actor_name_max_length()
 }
 
 pub fn is_valid_matrix_id(matrix_id: &str) -> bool {
   VALID_MATRIX_ID_REGEX.is_match(matrix_id)
 }
 
-pub fn is_valid_community_name(name: &str) -> bool {
-  VALID_COMMUNITY_NAME_REGEX.is_match(name)
-}
-
 pub fn is_valid_post_title(title: &str) -> bool {
   VALID_POST_TITLE_REGEX.is_match(title)
 }
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 (file)
index 0000000..76cec4c
--- /dev/null
@@ -0,0 +1,10 @@
+DROP VIEW person_alias_1;
+DROP VIEW person_alias_2;
+
+ALTER TABLE community ALTER COLUMN name TYPE varchar(20);
+ALTER TABLE community ALTER COLUMN title TYPE varchar(100);
+ALTER TABLE person ALTER COLUMN name TYPE varchar(20);
+ALTER TABLE person ALTER COLUMN display_name TYPE varchar(20);
+
+create view person_alias_1 as select * from person;
+create view person_alias_2 as select * from person;
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 (file)
index 0000000..2e7bc9d
--- /dev/null
@@ -0,0 +1,10 @@
+DROP VIEW person_alias_1;
+DROP VIEW person_alias_2;
+
+ALTER TABLE community ALTER COLUMN name TYPE varchar(255);
+ALTER TABLE community ALTER COLUMN title TYPE varchar(255);
+ALTER TABLE person ALTER COLUMN name TYPE varchar(255);
+ALTER TABLE person ALTER COLUMN display_name TYPE varchar(255);
+
+create view person_alias_1 as select * from person;
+create view person_alias_2 as select * from person;