chore: add additional sanitisation

This commit is contained in:
Paul Makles
2023-06-11 12:26:18 +01:00
parent 0578a05a05
commit c8d5128b0c
4 changed files with 14 additions and 1 deletions

7
Cargo.lock generated
View File

@@ -806,6 +806,12 @@ dependencies = [
"uuid", "uuid",
] ]
[[package]]
name = "decancer"
version = "1.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "808127a7de612079ec37bfc1abc48ed77a6015a971a8bd7d4178d79147cbc839"
[[package]] [[package]]
name = "derivative" name = "derivative"
version = "2.2.0" version = "2.2.0"
@@ -2974,6 +2980,7 @@ dependencies = [
"bson", "bson",
"dashmap", "dashmap",
"deadqueue", "deadqueue",
"decancer",
"dotenv", "dotenv",
"futures", "futures",
"impl_ops", "impl_ops",

View File

@@ -64,6 +64,7 @@ nanoid = "0.4.0"
linkify = "0.8.1" linkify = "0.8.1"
dotenv = "0.15.0" dotenv = "0.15.0"
indexmap = "1.9.1" indexmap = "1.9.1"
decancer = "1.6.2"
impl_ops = "0.1.1" impl_ops = "0.1.1"
num_enum = "0.5.6" num_enum = "0.5.6"
reqwest = "0.11.10" reqwest = "0.11.10"

View File

@@ -177,6 +177,11 @@ impl User {
// Copy the username for validation // Copy the username for validation
let username_lowercase = username.to_lowercase(); let username_lowercase = username.to_lowercase();
// Block homoglyphs
if decancer::cure(&username_lowercase).into_str() != username_lowercase {
return Err(Error::InvalidUsername);
}
// Ensure the username itself isn't blocked // Ensure the username itself isn't blocked
const BLOCKED_USERNAMES: &[&str] = &["admin", "revolt"]; const BLOCKED_USERNAMES: &[&str] = &["admin", "revolt"];

View File

@@ -8,6 +8,6 @@ pub fn prefix_keys<T: Serialize>(t: &T, prefix: &str) -> HashMap<String, serde_j
let v: HashMap<String, serde_json::Value> = serde_json::from_str(&v).unwrap(); let v: HashMap<String, serde_json::Value> = serde_json::from_str(&v).unwrap();
v.into_iter() v.into_iter()
.filter(|(_k, v)| !v.is_null()) .filter(|(_k, v)| !v.is_null())
.map(|(k, v)| (prefix.to_owned() + &k, v)) .map(|(k, v)| (format!("{}{}", prefix.to_owned(), k), v))
.collect() .collect()
} }