don't allow bad names in names
This commit is contained in:
40
src/main.rs
40
src/main.rs
@@ -3,7 +3,12 @@ use axum::{http::HeaderMap, routing::get, Router};
|
||||
use axum_extra::extract::{cookie::Cookie, CookieJar};
|
||||
use axum_messages::MessagesManagerLayer;
|
||||
use sqlx::{pool::PoolOptions, sqlite::SqliteConnectOptions, SqlitePool};
|
||||
use std::{fmt::Display, str::FromStr, sync::Arc};
|
||||
use std::{
|
||||
collections::HashSet,
|
||||
fmt::Display,
|
||||
str::FromStr,
|
||||
sync::{Arc, LazyLock},
|
||||
};
|
||||
use tower_http::services::ServeDir;
|
||||
use tower_sessions::{MemoryStore, SessionManagerLayer};
|
||||
use uuid::Uuid;
|
||||
@@ -73,6 +78,36 @@ struct Req {
|
||||
pub(crate) enum NameUpdateError {
|
||||
TooLong(usize, usize),
|
||||
TooShort(usize, usize),
|
||||
ContainsBadWord,
|
||||
}
|
||||
|
||||
static BAD_WORDS: LazyLock<HashSet<String>> = LazyLock::new(|| {
|
||||
const BAD_WORDS_FILE: &str = include_str!("../bad/merged_output.txt");
|
||||
|
||||
BAD_WORDS_FILE
|
||||
.lines()
|
||||
.map(|line| line.trim())
|
||||
.filter(|line| !line.is_empty() && !line.starts_with('#')) // Skip empty lines and comments
|
||||
.map(|word| {
|
||||
word.to_lowercase()
|
||||
.chars()
|
||||
.filter(|c| c.is_alphabetic())
|
||||
.collect()
|
||||
})
|
||||
.filter(|word: &String| !word.is_empty())
|
||||
.collect()
|
||||
});
|
||||
|
||||
fn contains_bad_word(text: &str) -> bool {
|
||||
let cleaned_text: String = text
|
||||
.to_lowercase()
|
||||
.chars()
|
||||
.filter(|c| c.is_alphabetic())
|
||||
.collect();
|
||||
|
||||
BAD_WORDS
|
||||
.iter()
|
||||
.any(|bad_word| cleaned_text.contains(bad_word))
|
||||
}
|
||||
|
||||
impl Backend {
|
||||
@@ -105,6 +140,9 @@ impl Backend {
|
||||
if name.len() < 3 {
|
||||
return Err(NameUpdateError::TooShort(3, name.len()));
|
||||
}
|
||||
if contains_bad_word(&name) {
|
||||
return Err(NameUpdateError::ContainsBadWord);
|
||||
}
|
||||
|
||||
match self {
|
||||
Backend::Sqlite(db) => {
|
||||
|
Reference in New Issue
Block a user