forked from LemmyNet/lemmy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes LemmyNet#2900 - Checks slur regex to see if it is too permissiv…
…e along with small validation organization
- Loading branch information
Showing
4 changed files
with
156 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/// Helper functions for manipulating parts of a site. | ||
use lemmy_utils::error::LemmyError; | ||
use regex::{Regex, RegexBuilder}; | ||
|
||
const SITE_NAME_MAX_LENGTH: usize = 20; | ||
const SITE_DESCRIPTION_MAX_LENGTH: usize = 150; | ||
|
||
/// Attempts to build the regex and checks it for common errors before inserting into the DB. | ||
pub fn build_and_check_regex(regex_str: &Option<&str>) -> Result<Option<Regex>, LemmyError> { | ||
regex_str.map_or_else( | ||
|| Ok(None::<Regex>), | ||
|slurs| { | ||
RegexBuilder::new(slurs) | ||
.case_insensitive(true) | ||
.build() | ||
.map_err(|e| LemmyError::from_error_message(e, "invalid_regex")) | ||
.and_then(|regex| { | ||
// NOTE: It is difficult to know, in the universe of user-crafted regex, which ones | ||
// may match against any string text. To keep it simple, we'll match the regex | ||
// against an innocuous string - a single number - which should help catch a regex | ||
// that accidentally matches against all strings. | ||
if regex.is_match("1") { | ||
return Err(LemmyError::from_message("permissive_slur")); | ||
} | ||
|
||
Ok(Some(regex)) | ||
}) | ||
}, | ||
) | ||
} | ||
|
||
/// Checks the site name length, the limit as defined in the DB. | ||
pub fn site_name_length_check(name: &str) -> Result<(), LemmyError> { | ||
length_check( | ||
name, | ||
SITE_NAME_MAX_LENGTH, | ||
String::from("site_name_length_overflow"), | ||
) | ||
} | ||
|
||
/// Checks the site description length, the limit as defined in the DB. | ||
pub fn site_description_length_check(description: &str) -> Result<(), LemmyError> { | ||
length_check( | ||
description, | ||
SITE_DESCRIPTION_MAX_LENGTH, | ||
String::from("site_description_length_overflow"), | ||
) | ||
} | ||
|
||
fn length_check(item: &str, max_length: usize, msg: String) -> Result<(), LemmyError> { | ||
if item.len() > max_length { | ||
Err(LemmyError::from_message(&msg)) | ||
} else { | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::utils::site_utils::{ | ||
build_and_check_regex, | ||
site_description_length_check, | ||
site_name_length_check, | ||
}; | ||
|
||
#[test] | ||
fn test_valid_slur_regex() { | ||
let valid_regexes = [&None, &Some("(foo|bar)")]; | ||
|
||
valid_regexes.iter().for_each(|regex| { | ||
let result = build_and_check_regex(regex); | ||
|
||
assert!(result.is_ok()); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn test_too_permissive_slur_regex() { | ||
let match_everything_regexes = [ | ||
&Some("["), // Invalid regex | ||
&Some("(foo|bar|)"), // Matches all values | ||
&Some(".*"), // Matches all values | ||
]; | ||
|
||
match_everything_regexes.iter().for_each(|regex| { | ||
let result = build_and_check_regex(regex); | ||
|
||
assert!(result.is_err(), "Testing regex: {:?}", regex); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn test_test_valid_site_name() { | ||
let result = site_name_length_check("awesome.comm"); | ||
|
||
assert!(result.is_ok()) | ||
} | ||
|
||
#[test] | ||
fn test_test_invalid_site_name() { | ||
let result = site_name_length_check("too long community name"); | ||
|
||
assert!(result.err().is_some_and(|error| error | ||
.message | ||
.is_some_and(|msg| msg == "site_name_length_overflow"))); | ||
} | ||
|
||
#[test] | ||
fn test_test_valid_site_description() { | ||
let result = site_description_length_check("cool cats"); | ||
|
||
assert!(result.is_ok()) | ||
} | ||
|
||
#[test] | ||
fn test_test_invalid_site_description() { | ||
let result = site_description_length_check(&(0..10001).map(|_| 'A').collect::<String>()); | ||
|
||
assert!(result.err().is_some_and(|error| error | ||
.message | ||
.is_some_and(|msg| msg == "site_description_length_overflow"))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters