fixed javalsai's mess
This commit is contained in:
+19
-10
@@ -28,6 +28,7 @@ use futures::StreamExt as _;
|
||||
use rand::random_bool;
|
||||
use serde::{Deserialize, Serialize, de};
|
||||
use serde_json::json;
|
||||
use regex::Regex;
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug, Ord, Eq, PartialEq, PartialOrd, Clone)]
|
||||
struct Entry
|
||||
@@ -372,15 +373,14 @@ async fn handle_socket
|
||||
|
||||
let Some(name) = socket.next().await else
|
||||
{
|
||||
eprintln!("user gave no username");
|
||||
eprintln!("No username");
|
||||
return;
|
||||
};
|
||||
let name: Arc<str> = match name.expect("failed to recv socket msg")
|
||||
{
|
||||
Message::Text(text)
|
||||
if validate_name(&text.to_string()) =>
|
||||
Message::Text(text) =>
|
||||
{
|
||||
Arc::from(text.to_string().into_boxed_str())
|
||||
Arc::from(validate_name(text.to_string()))
|
||||
}
|
||||
_ => Arc::from("anon"),
|
||||
};
|
||||
@@ -442,16 +442,25 @@ async fn handle_socket
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_name(input: &str) -> bool {
|
||||
fn validate_name(input: String) -> String {
|
||||
let input = input.trim();
|
||||
|
||||
// Length check
|
||||
if input.is_empty() || input.len() > 32
|
||||
if input == "null"
|
||||
{
|
||||
return false;
|
||||
return "anon".to_string();
|
||||
}
|
||||
// Length check
|
||||
if input.is_empty() || input.len() > 32 {
|
||||
return "anon".to_string();
|
||||
}
|
||||
|
||||
input.chars().all(|c| c.is_ascii_alphanumeric())
|
||||
// Allow only letters, numbers, _ and -
|
||||
let re = Regex::new(r"^[a-zA-Z0-9_-]+$").unwrap();
|
||||
|
||||
if re.is_match(input) {
|
||||
input.to_string()
|
||||
} else {
|
||||
"anon".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
// static routes
|
||||
|
||||
Reference in New Issue
Block a user