Added basic checks for the account fields

This commit is contained in:
StNicolay 2023-05-07 19:04:48 +03:00
parent 072e030e32
commit 3a9c038090
Signed by: StNicolay
GPG Key ID: 9693D04DCD962B0D
6 changed files with 66 additions and 7 deletions

View File

@ -29,7 +29,14 @@ async fn get_master_pass(
let db = db.clone();
let name = account.name.clone();
async move {
match spawn_blocking(move || account.into_account(user_id, &master_pass)).await {
let result = spawn_blocking(move || {
if !account.validate() {
return Err(());
}
account.into_account(user_id, &master_pass).map_err(|_| ())
})
.await;
match result {
Ok(Ok(account)) => match account.insert(&db).await {
Ok(_) => (),
Err(_) => failed.lock().await.push(name),

View File

@ -3,7 +3,7 @@ use teloxide::{adaptors::Throttle, prelude::*};
use crate::{
errors::NoMessageText,
handlers::{MainDialogue, PackagedHandler},
handlers::{markups::deletion_markup, utils::validate_field, MainDialogue, PackagedHandler},
};
pub async fn get_account_name(
@ -20,7 +20,17 @@ pub async fn get_account_name(
msg,
db,
dialogue,
|_, _, _, _| Box::pin(async { Ok(true) }),
|bot, msg, _, name| {
Box::pin(async move {
let is_valid = validate_field(name);
if !is_valid {
bot.send_message(msg.chat.id, "Invalid account name")
.reply_markup(deletion_markup())
.await?;
}
Ok(is_valid)
})
},
next,
)
.await

View File

@ -3,7 +3,7 @@ use teloxide::{adaptors::Throttle, prelude::*};
use crate::{
errors::NoMessageText,
handlers::{MainDialogue, PackagedHandler},
handlers::{markups::deletion_markup, utils::validate_field, MainDialogue, PackagedHandler},
};
pub async fn get_login(
@ -20,7 +20,17 @@ pub async fn get_login(
msg,
db,
dialogue,
|_, _, _, _| Box::pin(async { Ok(true) }),
|bot, msg, _, login| {
Box::pin(async move {
let is_valid = validate_field(login);
if !is_valid {
bot.send_message(msg.chat.id, "Invalid login")
.reply_markup(deletion_markup())
.await?;
}
Ok(is_valid)
})
},
next,
)
.await

View File

@ -3,7 +3,7 @@ use teloxide::{adaptors::Throttle, prelude::*};
use crate::{
errors::NoMessageText,
handlers::{MainDialogue, PackagedHandler},
handlers::{markups::deletion_markup, utils::validate_field, MainDialogue, PackagedHandler},
};
pub async fn get_password(
@ -20,7 +20,17 @@ pub async fn get_password(
msg,
db,
dialogue,
|_, _, _, _| Box::pin(async { Ok(true) }),
|bot, msg, _, password| {
Box::pin(async move {
let is_valid = validate_field(password);
if !is_valid {
bot.send_message(msg.chat.id, "Invalid password")
.reply_markup(deletion_markup())
.await?;
}
Ok(is_valid)
})
},
next,
)
.await

View File

@ -31,3 +31,11 @@ pub async fn delete_optional(bot: &Throttle<Bot>, msg: &Option<Message>) {
let _ = bot.delete_message(msg.chat.id, msg.id).await;
}
}
/// Returns true if the field is valid
#[inline]
pub fn validate_field(field: &str) -> bool {
field
.chars()
.all(|char| char != '`' && char != '\\' && char != '\n')
}

View File

@ -27,6 +27,20 @@ impl DecryptedAccount {
let (name, login, password) = (self.name, self.login, self.password);
account::ActiveModel::from_unencrypted(user_id, name, &login, &password, master_pass)
}
/// Returns true if the account is valid
#[inline]
pub fn validate(&self) -> bool {
for string in [&self.name, &self.login, &self.password] {
let is_invalid = string
.chars()
.any(|char| char == '`' || char == '\\' || char == '\n');
if is_invalid {
return false;
}
}
true
}
}
#[derive(Serialize, Deserialize)]