]> Untitled Git - lemmy.git/blob - crates/utils/src/utils/validation.rs
Ensure site can only either be a private instance or federated when creating or editi...
[lemmy.git] / crates / utils / src / utils / validation.rs
1 use crate::error::{LemmyError, LemmyResult};
2 use itertools::Itertools;
3 use once_cell::sync::Lazy;
4 use regex::Regex;
5 use totp_rs::{Secret, TOTP};
6 use url::Url;
7
8 static VALID_ACTOR_NAME_REGEX: Lazy<Regex> =
9   Lazy::new(|| Regex::new(r"^[a-zA-Z0-9_]{3,}$").expect("compile regex"));
10 static VALID_POST_TITLE_REGEX: Lazy<Regex> =
11   Lazy::new(|| Regex::new(r".*\S{3,}.*").expect("compile regex"));
12 static VALID_MATRIX_ID_REGEX: Lazy<Regex> = Lazy::new(|| {
13   Regex::new(r"^@[A-Za-z0-9._=-]+:[A-Za-z0-9.-]+\.[A-Za-z]{2,}$").expect("compile regex")
14 });
15 // taken from https://en.wikipedia.org/wiki/UTM_parameters
16 static CLEAN_URL_PARAMS_REGEX: Lazy<Regex> = Lazy::new(|| {
17   Regex::new(r"^utm_source|utm_medium|utm_campaign|utm_term|utm_content|gclid|gclsrc|dclid|fbclid$")
18     .expect("compile regex")
19 });
20 const BODY_MAX_LENGTH: usize = 10000;
21 const BIO_MAX_LENGTH: usize = 300;
22
23 fn has_newline(name: &str) -> bool {
24   name.contains('\n')
25 }
26
27 pub fn is_valid_actor_name(name: &str, actor_name_max_length: usize) -> LemmyResult<()> {
28   let check = name.chars().count() <= actor_name_max_length
29     && VALID_ACTOR_NAME_REGEX.is_match(name)
30     && !has_newline(name);
31   if !check {
32     Err(LemmyError::from_message("invalid_name"))
33   } else {
34     Ok(())
35   }
36 }
37
38 // Can't do a regex here, reverse lookarounds not supported
39 pub fn is_valid_display_name(name: &str, actor_name_max_length: usize) -> LemmyResult<()> {
40   let check = !name.starts_with('@')
41     && !name.starts_with('\u{200b}')
42     && name.chars().count() >= 3
43     && name.chars().count() <= actor_name_max_length
44     && !has_newline(name);
45   if !check {
46     Err(LemmyError::from_message("invalid_username"))
47   } else {
48     Ok(())
49   }
50 }
51
52 pub fn is_valid_matrix_id(matrix_id: &str) -> LemmyResult<()> {
53   let check = VALID_MATRIX_ID_REGEX.is_match(matrix_id) && !has_newline(matrix_id);
54   if !check {
55     Err(LemmyError::from_message("invalid_matrix_id"))
56   } else {
57     Ok(())
58   }
59 }
60
61 pub fn is_valid_post_title(title: &str) -> LemmyResult<()> {
62   let check = VALID_POST_TITLE_REGEX.is_match(title) && !has_newline(title);
63   if !check {
64     Err(LemmyError::from_message("invalid_post_title"))
65   } else {
66     Ok(())
67   }
68 }
69
70 /// This could be post bodies, comments, or any description field
71 pub fn is_valid_body_field(body: &Option<String>) -> LemmyResult<()> {
72   if let Some(body) = body {
73     let check = body.chars().count() <= BODY_MAX_LENGTH;
74     if !check {
75       Err(LemmyError::from_message("invalid_body_field"))
76     } else {
77       Ok(())
78     }
79   } else {
80     Ok(())
81   }
82 }
83
84 pub fn is_valid_bio_field(bio: &str) -> LemmyResult<()> {
85   let check = bio.chars().count() <= BIO_MAX_LENGTH;
86   if !check {
87     Err(LemmyError::from_message("bio_length_overflow"))
88   } else {
89     Ok(())
90   }
91 }
92
93 pub fn clean_url_params(url: &Url) -> Url {
94   let mut url_out = url.clone();
95   if url.query().is_some() {
96     let new_query = url
97       .query_pairs()
98       .filter(|q| !CLEAN_URL_PARAMS_REGEX.is_match(&q.0))
99       .map(|q| format!("{}={}", q.0, q.1))
100       .join("&");
101     url_out.set_query(Some(&new_query));
102   }
103   url_out
104 }
105
106 pub fn check_totp_2fa_valid(
107   totp_secret: &Option<String>,
108   totp_token: &Option<String>,
109   site_name: &str,
110   username: &str,
111 ) -> LemmyResult<()> {
112   // Check only if they have a totp_secret in the DB
113   if let Some(totp_secret) = totp_secret {
114     // Throw an error if their token is missing
115     let token = totp_token
116       .as_deref()
117       .ok_or_else(|| LemmyError::from_message("missing_totp_token"))?;
118
119     let totp = build_totp_2fa(site_name, username, totp_secret)?;
120
121     let check_passed = totp.check_current(token)?;
122     if !check_passed {
123       return Err(LemmyError::from_message("incorrect_totp token"));
124     }
125   }
126
127   Ok(())
128 }
129
130 pub fn generate_totp_2fa_secret() -> String {
131   Secret::generate_secret().to_string()
132 }
133
134 pub fn build_totp_2fa(site_name: &str, username: &str, secret: &str) -> Result<TOTP, LemmyError> {
135   let sec = Secret::Raw(secret.as_bytes().to_vec());
136   let sec_bytes = sec
137     .to_bytes()
138     .map_err(|_| LemmyError::from_message("Couldnt parse totp secret"))?;
139
140   TOTP::new(
141     totp_rs::Algorithm::SHA256,
142     6,
143     1,
144     30,
145     sec_bytes,
146     Some(site_name.to_string()),
147     username.to_string(),
148   )
149   .map_err(|e| LemmyError::from_error_message(e, "Couldnt generate TOTP"))
150 }
151
152 pub fn check_site_visibility_valid(
153   current_private_instance: bool,
154   current_federation_enabled: bool,
155   new_private_instance: &Option<bool>,
156   new_federation_enabled: &Option<bool>,
157 ) -> LemmyResult<()> {
158   let private_instance = new_private_instance.unwrap_or(current_private_instance);
159   let federation_enabled = new_federation_enabled.unwrap_or(current_federation_enabled);
160
161   if private_instance && federation_enabled {
162     return Err(LemmyError::from_message(
163       "cant_enable_private_instance_and_federation_together",
164     ));
165   }
166
167   Ok(())
168 }
169
170 #[cfg(test)]
171 mod tests {
172   use super::build_totp_2fa;
173   use crate::utils::validation::{
174     check_site_visibility_valid,
175     clean_url_params,
176     generate_totp_2fa_secret,
177     is_valid_actor_name,
178     is_valid_display_name,
179     is_valid_matrix_id,
180     is_valid_post_title,
181   };
182   use url::Url;
183
184   #[test]
185   fn test_clean_url_params() {
186     let url = Url::parse("https://example.com/path/123?utm_content=buffercf3b2&utm_medium=social&username=randomuser&id=123").unwrap();
187     let cleaned = clean_url_params(&url);
188     let expected = Url::parse("https://example.com/path/123?username=randomuser&id=123").unwrap();
189     assert_eq!(expected.to_string(), cleaned.to_string());
190
191     let url = Url::parse("https://example.com/path/123").unwrap();
192     let cleaned = clean_url_params(&url);
193     assert_eq!(url.to_string(), cleaned.to_string());
194   }
195
196   #[test]
197   fn regex_checks() {
198     assert!(is_valid_post_title("hi").is_err());
199     assert!(is_valid_post_title("him").is_ok());
200     assert!(is_valid_post_title("n\n\n\n\nanother").is_err());
201     assert!(is_valid_post_title("hello there!\n this is a test.").is_err());
202     assert!(is_valid_post_title("hello there! this is a test.").is_ok());
203   }
204
205   #[test]
206   fn test_valid_actor_name() {
207     let actor_name_max_length = 20;
208     assert!(is_valid_actor_name("Hello_98", actor_name_max_length).is_ok());
209     assert!(is_valid_actor_name("ten", actor_name_max_length).is_ok());
210     assert!(is_valid_actor_name("Hello-98", actor_name_max_length).is_err());
211     assert!(is_valid_actor_name("a", actor_name_max_length).is_err());
212     assert!(is_valid_actor_name("", actor_name_max_length).is_err());
213   }
214
215   #[test]
216   fn test_valid_display_name() {
217     let actor_name_max_length = 20;
218     assert!(is_valid_display_name("hello @there", actor_name_max_length).is_ok());
219     assert!(is_valid_display_name("@hello there", actor_name_max_length).is_err());
220
221     // Make sure zero-space with an @ doesn't work
222     assert!(
223       is_valid_display_name(&format!("{}@my name is", '\u{200b}'), actor_name_max_length).is_err()
224     );
225   }
226
227   #[test]
228   fn test_valid_post_title() {
229     assert!(is_valid_post_title("Post Title").is_ok());
230     assert!(is_valid_post_title("   POST TITLE ðŸ˜ƒðŸ˜ƒðŸ˜ƒðŸ˜ƒðŸ˜ƒ").is_ok());
231     assert!(is_valid_post_title("\n \n \n \n                    ").is_err()); // tabs/spaces/newlines
232   }
233
234   #[test]
235   fn test_valid_matrix_id() {
236     assert!(is_valid_matrix_id("@dess:matrix.org").is_ok());
237     assert!(is_valid_matrix_id("dess:matrix.org").is_err());
238     assert!(is_valid_matrix_id(" @dess:matrix.org").is_err());
239     assert!(is_valid_matrix_id("@dess:matrix.org t").is_err());
240   }
241
242   #[test]
243   fn test_build_totp() {
244     let generated_secret = generate_totp_2fa_secret();
245     let totp = build_totp_2fa("lemmy", "my_name", &generated_secret);
246     assert!(totp.is_ok());
247   }
248
249   #[test]
250   fn test_check_site_visibility_valid() {
251     assert!(check_site_visibility_valid(true, true, &None, &None).is_err());
252     assert!(check_site_visibility_valid(true, false, &None, &Some(true)).is_err());
253     assert!(check_site_visibility_valid(false, true, &Some(true), &None).is_err());
254     assert!(check_site_visibility_valid(false, false, &Some(true), &Some(true)).is_err());
255     assert!(check_site_visibility_valid(true, false, &None, &None).is_ok());
256     assert!(check_site_visibility_valid(false, true, &None, &None).is_ok());
257     assert!(check_site_visibility_valid(false, false, &Some(true), &None).is_ok());
258     assert!(check_site_visibility_valid(false, false, &None, &Some(true)).is_ok());
259   }
260 }