Added basic checks for the account fields
This commit is contained in:
parent
072e030e32
commit
3a9c038090
@ -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),
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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')
|
||||
}
|
||||
|
@ -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)]
|
||||
|
Loading…
Reference in New Issue
Block a user