mirror of
https://github.com/dani-garcia/vaultwarden.wiki.git
synced 2026-10-29 03:56:47 +02:00
* Fix several scheduled job issues Several jobs could cause a panic, or stop without running through all items because of this panic. This could cause missed items until that panic somehow resolved it self, and probably would never work fully again. This could cause emergency access for example to never work again. Besides this, emergency access was also able to be panic if the wait-time was out-of-bounds and date/time calculation might fail. This is also fixed now by using `checked` functions and continue upon those issues so all awaiting items will be handled. Signed-off-by: BlackDex <black.dex@gmail.com> * Fix several possible panics and overflows Adjust several integer types to be a lower or unsigned type. Most types were too large, only because the function which used this value needed that specific type. But, this allowed too large values which either caused panics or might cause a DoS. This does however might possible cause configurations which have larger values, which are probably way too large anyway to be not loaded or use their defaults. During the config loading, either via env or json, it will show error logs regarding these items. Also fixed an issue with protected actions via OTP, it increased the attempt before it validated if the attempt limit was reached. This could cause an issue when set to `1`, that way it was never allowed, and `3` times would actually be `2`. Also added several config validation checks which otherwise would have prevent Vaultwarden to start, or login, so these will probably not cause issues, else users would already have issues. Signed-off-by: BlackDex <black.dex@gmail.com> * Show config errors on diagnostics page With the previous commits it is now possible to add a warning if their rare issues with loading of the config, either environment variables which we were not able to parse correctly, or if loading the `config.json` failed instead of just showing it wasn't used, while it does exists, which might be confusing. Signed-off-by: BlackDex <black.dex@gmail.com> --------- Signed-off-by: BlackDex <black.dex@gmail.com>
224 lines
7.0 KiB
Rust
224 lines
7.0 KiB
Rust
use chrono::{NaiveDateTime, Utc};
|
|
use derive_more::{AsRef, Deref, Display, From};
|
|
use diesel::prelude::*;
|
|
use serde_json::Value;
|
|
|
|
use crate::{
|
|
api::{ApiResult, EmptyResult},
|
|
crypto::ct_eq,
|
|
db::{DbConn, schema::auth_requests},
|
|
error::MapResult,
|
|
util::format_date,
|
|
};
|
|
use macros::UuidFromParam;
|
|
|
|
use super::{DeviceId, OrganizationId, UserId};
|
|
|
|
#[derive(Identifiable, Queryable, Insertable, AsChangeset, Deserialize, Serialize)]
|
|
#[diesel(table_name = auth_requests)]
|
|
#[diesel(treat_none_as_null = true)]
|
|
#[diesel(primary_key(uuid))]
|
|
pub struct AuthRequest {
|
|
pub uuid: AuthRequestId,
|
|
pub user_uuid: UserId,
|
|
pub organization_uuid: Option<OrganizationId>,
|
|
|
|
pub request_device_identifier: DeviceId,
|
|
pub device_type: i32, // https://github.com/bitwarden/server/blob/9ebe16587175b1c0e9208f84397bb75d0d595510/src/Core/Enums/DeviceType.cs
|
|
|
|
pub request_ip: String,
|
|
pub response_device_id: Option<DeviceId>,
|
|
|
|
pub access_code: String,
|
|
pub public_key: String,
|
|
|
|
pub enc_key: Option<String>,
|
|
|
|
pub master_password_hash: Option<String>,
|
|
pub approved: Option<bool>,
|
|
pub creation_date: NaiveDateTime,
|
|
pub response_date: Option<NaiveDateTime>,
|
|
|
|
pub authentication_date: Option<NaiveDateTime>,
|
|
}
|
|
|
|
impl AuthRequest {
|
|
pub fn new(
|
|
user_uuid: UserId,
|
|
request_device_identifier: DeviceId,
|
|
device_type: i32,
|
|
request_ip: String,
|
|
access_code: String,
|
|
public_key: String,
|
|
) -> Self {
|
|
let now = Utc::now().naive_utc();
|
|
|
|
Self {
|
|
uuid: AuthRequestId(crate::util::get_uuid()),
|
|
user_uuid,
|
|
organization_uuid: None,
|
|
|
|
request_device_identifier,
|
|
device_type,
|
|
request_ip,
|
|
response_device_id: None,
|
|
access_code,
|
|
public_key,
|
|
enc_key: None,
|
|
master_password_hash: None,
|
|
approved: None,
|
|
creation_date: now,
|
|
response_date: None,
|
|
authentication_date: None,
|
|
}
|
|
}
|
|
|
|
pub fn to_json_for_pending_device(&self) -> Value {
|
|
json!({
|
|
"id": self.uuid,
|
|
"creationDate": format_date(&self.creation_date),
|
|
})
|
|
}
|
|
}
|
|
|
|
impl AuthRequest {
|
|
pub async fn save(&self, conn: &DbConn) -> EmptyResult {
|
|
db_run! { conn:
|
|
mysql {
|
|
diesel::insert_into(auth_requests::table)
|
|
.values(self)
|
|
.on_conflict(diesel::dsl::DuplicatedKeys)
|
|
.do_update()
|
|
.set(self)
|
|
.execute(conn)
|
|
.map_res("Error saving auth_request")
|
|
}
|
|
postgresql, sqlite {
|
|
diesel::insert_into(auth_requests::table)
|
|
.values(self)
|
|
.on_conflict(auth_requests::uuid)
|
|
.do_update()
|
|
.set(self)
|
|
.execute(conn)
|
|
.map_res("Error saving auth_request")
|
|
}
|
|
}
|
|
}
|
|
|
|
pub async fn find_by_uuid(uuid: &AuthRequestId, conn: &DbConn) -> Option<Self> {
|
|
conn.run(move |conn| auth_requests::table.filter(auth_requests::uuid.eq(uuid)).first::<Self>(conn).ok()).await
|
|
}
|
|
|
|
pub async fn find_by_uuid_and_user(uuid: &AuthRequestId, user_uuid: &UserId, conn: &DbConn) -> Option<Self> {
|
|
conn.run(move |conn| {
|
|
auth_requests::table
|
|
.filter(auth_requests::uuid.eq(uuid))
|
|
.filter(auth_requests::user_uuid.eq(user_uuid))
|
|
.first::<Self>(conn)
|
|
.ok()
|
|
})
|
|
.await
|
|
}
|
|
|
|
pub async fn find_by_user(user_uuid: &UserId, conn: &DbConn) -> Vec<Self> {
|
|
conn.run(move |conn| {
|
|
auth_requests::table
|
|
.filter(auth_requests::user_uuid.eq(user_uuid))
|
|
.load::<Self>(conn)
|
|
.expect("Error loading auth_requests")
|
|
})
|
|
.await
|
|
}
|
|
|
|
pub async fn find_by_user_and_requested_device(
|
|
user_uuid: &UserId,
|
|
device_uuid: &DeviceId,
|
|
conn: &DbConn,
|
|
) -> Option<Self> {
|
|
conn.run(move |conn| {
|
|
auth_requests::table
|
|
.filter(auth_requests::user_uuid.eq(user_uuid))
|
|
.filter(auth_requests::request_device_identifier.eq(device_uuid))
|
|
.filter(auth_requests::approved.is_null())
|
|
.order_by(auth_requests::creation_date.desc())
|
|
.first::<Self>(conn)
|
|
.ok()
|
|
})
|
|
.await
|
|
}
|
|
|
|
pub async fn find_created_before(dt: &NaiveDateTime, conn: &DbConn) -> Vec<Self> {
|
|
conn.run(move |conn| {
|
|
auth_requests::table
|
|
.filter(auth_requests::creation_date.lt(dt))
|
|
.load::<Self>(conn)
|
|
.expect("Error loading auth_requests")
|
|
})
|
|
.await
|
|
}
|
|
|
|
pub async fn delete(&self, conn: &DbConn) -> EmptyResult {
|
|
conn.run(move |conn| {
|
|
diesel::delete(auth_requests::table.filter(auth_requests::uuid.eq(&self.uuid)))
|
|
.execute(conn)
|
|
.map_res("Error deleting auth request")
|
|
})
|
|
.await
|
|
}
|
|
|
|
pub async fn delete_all_by_user(user_uuid: &UserId, conn: &DbConn) -> EmptyResult {
|
|
conn.run(move |conn| {
|
|
diesel::delete(auth_requests::table.filter(auth_requests::user_uuid.eq(user_uuid)))
|
|
.execute(conn)
|
|
.map_res("Error deleting auth requests for user")
|
|
})
|
|
.await
|
|
}
|
|
|
|
/// Marks the request as used to log in. Returns false when it already was, so it only works once.
|
|
pub async fn set_authentication_date(uuid: &AuthRequestId, conn: &DbConn) -> ApiResult<bool> {
|
|
let now = Utc::now().naive_utc();
|
|
conn.run(move |conn| {
|
|
diesel::update(auth_requests::table)
|
|
.filter(auth_requests::uuid.eq(uuid))
|
|
.filter(auth_requests::authentication_date.is_null())
|
|
.set(auth_requests::authentication_date.eq(now))
|
|
.execute(conn)
|
|
.map(|updated| updated == 1)
|
|
.map_res("Error updating auth request")
|
|
})
|
|
.await
|
|
}
|
|
|
|
pub fn check_access_code(&self, access_code: &str) -> bool {
|
|
ct_eq(&self.access_code, access_code)
|
|
}
|
|
|
|
pub async fn purge_expired_auth_requests(conn: &DbConn) {
|
|
// delete auth requests older than 15 minutes which is functionally equivalent to upstream:
|
|
// https://github.com/bitwarden/server/blob/f8ee2270409f7a13125cd414c450740af605a175/src/Sql/dbo/Auth/Stored%20Procedures/AuthRequest_DeleteIfExpired.sql
|
|
let expiry_time = Utc::now().naive_utc() - chrono::TimeDelta::minutes(15);
|
|
for auth_request in Self::find_created_before(&expiry_time, conn).await {
|
|
auth_request.delete(conn).await.ok();
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(
|
|
Clone,
|
|
Debug,
|
|
AsRef,
|
|
Deref,
|
|
DieselNewType,
|
|
Display,
|
|
From,
|
|
FromForm,
|
|
Hash,
|
|
PartialEq,
|
|
Eq,
|
|
Serialize,
|
|
Deserialize,
|
|
UuidFromParam,
|
|
)]
|
|
pub struct AuthRequestId(String);
|