mirror of
https://github.com/dani-garcia/vaultwarden.wiki.git
synced 2026-10-25 23:56:47 +02:00
Misc fixes regarding jobs and config (#7858)
* 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>
This commit is contained in:
1 parent
f4696877b9
commit
b71cb7a832
25 files changed
+529
-256
No files matched your search
@@ -197,7 +197,7 @@ impl AuthRequest {
|
||||
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::try_minutes(15).unwrap();
|
||||
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();
|
||||
}
|
||||
|
||||
+16
-6
@@ -494,12 +494,22 @@ impl Cipher {
|
||||
|
||||
/// Purge all ciphers that are old enough to be auto-deleted.
|
||||
pub async fn purge_trash(conn: &DbConn) {
|
||||
if let Some(auto_delete_days) = CONFIG.trash_auto_delete_days() {
|
||||
let now = Utc::now().naive_utc();
|
||||
let dt = now - TimeDelta::try_days(auto_delete_days).unwrap();
|
||||
for cipher in Self::find_deleted_before(&dt, conn).await {
|
||||
cipher.delete(conn).await.ok();
|
||||
}
|
||||
let Some(auto_delete_days) = CONFIG.trash_auto_delete_days().filter(|&d| d != 0) else {
|
||||
debug!("trash_auto_delete_days is not configured or set to 0, skipping");
|
||||
return;
|
||||
};
|
||||
|
||||
let Some(days) = TimeDelta::try_days(i64::from(auto_delete_days)) else {
|
||||
error!("Unable to calculate auto delete days!");
|
||||
return;
|
||||
};
|
||||
let now = Utc::now().naive_utc();
|
||||
let Some(dt) = now.checked_sub_signed(days) else {
|
||||
error!("Unable to calculate trash purge date!");
|
||||
return;
|
||||
};
|
||||
for cipher in Self::find_deleted_before(&dt, conn).await {
|
||||
cipher.delete(conn).await.ok();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use chrono::{NaiveDateTime, Utc};
|
||||
use chrono::{NaiveDateTime, TimeDelta, Utc};
|
||||
use derive_more::{AsRef, Deref, Display, From};
|
||||
use diesel::prelude::*;
|
||||
use serde_json::Value;
|
||||
@@ -112,8 +112,26 @@ impl EmergencyAccess {
|
||||
"object": "emergencyAccessGranteeDetails",
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn deadline_at(&self) -> Result<NaiveDateTime, crate::Error> {
|
||||
if !(EMERGENCY_ACCESS_TIMEOUT_RANGE).contains(&self.wait_time_days) {
|
||||
err!("Emergency access wait time is out-of-bounds!")
|
||||
}
|
||||
|
||||
let initiated_at = self.recovery_initiated_at.map_res("Emergency access initiation date missing!")?;
|
||||
|
||||
TimeDelta::try_days(i64::from(self.wait_time_days))
|
||||
.and_then(|w| initiated_at.checked_add_signed(w))
|
||||
.map_res("Emergency access deadline is invalid")
|
||||
}
|
||||
|
||||
pub fn is_recovery_allowed(&self, now: &NaiveDateTime) -> Result<bool, crate::Error> {
|
||||
Ok(self.deadline_at()?.le(now))
|
||||
}
|
||||
}
|
||||
|
||||
pub const EMERGENCY_ACCESS_TIMEOUT_RANGE: std::ops::RangeInclusive<i32> = 1..=90;
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub enum EmergencyAccessType {
|
||||
View = 0,
|
||||
@@ -169,27 +187,42 @@ impl EmergencyAccess {
|
||||
|
||||
pub async fn update_access_status_and_save(
|
||||
&mut self,
|
||||
status: i32,
|
||||
from_status: i32,
|
||||
to_status: i32,
|
||||
date: &NaiveDateTime,
|
||||
conn: &DbConn,
|
||||
) -> EmptyResult {
|
||||
let uuid = self.uuid.clone();
|
||||
let updated_at = *date;
|
||||
let res = conn
|
||||
.run(move |conn| {
|
||||
crate::util::retry(
|
||||
|| {
|
||||
diesel::update(
|
||||
emergency_access::table
|
||||
.filter(emergency_access::uuid.eq(&uuid))
|
||||
.filter(emergency_access::status.eq(from_status)),
|
||||
)
|
||||
.set((emergency_access::status.eq(to_status), emergency_access::updated_at.eq(updated_at)))
|
||||
.execute(conn)
|
||||
},
|
||||
10,
|
||||
)
|
||||
})
|
||||
.await;
|
||||
|
||||
match res {
|
||||
Ok(1) => {} // One record updated, which is valid
|
||||
Ok(_) => err!("Emergency access status was changed already"),
|
||||
Err(e) => err!("Error updating emergency access status", format!("{e}")),
|
||||
}
|
||||
|
||||
self.status = to_status;
|
||||
self.updated_at = updated_at;
|
||||
|
||||
// Update the grantee so that it will refresh it's status.
|
||||
User::update_uuid_revision(self.grantee_uuid.as_ref().expect("Error getting grantee"), conn).await;
|
||||
self.status = status;
|
||||
date.clone_into(&mut self.updated_at);
|
||||
|
||||
conn.run(move |conn| {
|
||||
crate::util::retry(
|
||||
|| {
|
||||
diesel::update(emergency_access::table.filter(emergency_access::uuid.eq(&self.uuid)))
|
||||
.set((emergency_access::status.eq(status), emergency_access::updated_at.eq(date)))
|
||||
.execute(conn)
|
||||
},
|
||||
10,
|
||||
)
|
||||
.map_res("Error updating emergency access status")
|
||||
})
|
||||
.await
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn update_last_notification_date_and_save(&mut self, date: &NaiveDateTime, conn: &DbConn) -> EmptyResult {
|
||||
|
||||
+17
-11
@@ -347,17 +347,23 @@ impl Event {
|
||||
}
|
||||
|
||||
pub async fn clean_events(conn: &DbConn) -> EmptyResult {
|
||||
if let Some(days_to_retain) = CONFIG.events_days_retain() {
|
||||
let dt = Utc::now().naive_utc() - TimeDelta::try_days(days_to_retain).unwrap();
|
||||
conn.run(move |conn| {
|
||||
diesel::delete(event::table.filter(event::event_date.lt(dt)))
|
||||
.execute(conn)
|
||||
.map_res("Error cleaning old events")
|
||||
})
|
||||
.await
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
let Some(days_to_retain) = CONFIG.events_days_retain().filter(|&d| d != 0) else {
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
let Some(days_to_retain) = TimeDelta::try_days(i64::from(days_to_retain)) else {
|
||||
err!("Unable to calculate the event retention date, check 'EVENTS_DAYS_RETAIN'")
|
||||
};
|
||||
|
||||
let Some(dt) = Utc::now().naive_utc().checked_sub_signed(days_to_retain) else {
|
||||
err!("Unable to calculate the event retention date, check 'EVENTS_DAYS_RETAIN'")
|
||||
};
|
||||
conn.run(move |conn| {
|
||||
diesel::delete(event::table.filter(event::event_date.lt(dt)))
|
||||
.execute(conn)
|
||||
.map_res("Error cleaning old events")
|
||||
})
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -24,7 +24,9 @@ pub use self::auth_request::{AuthRequest, AuthRequestId};
|
||||
pub use self::cipher::{Cipher, CipherId, RepromptType};
|
||||
pub use self::collection::{Collection, CollectionCipher, CollectionId, CollectionUser};
|
||||
pub use self::device::{Device, DeviceId, DeviceType, DeviceWithAuthRequest, PushId};
|
||||
pub use self::emergency_access::{EmergencyAccess, EmergencyAccessId, EmergencyAccessStatus, EmergencyAccessType};
|
||||
pub use self::emergency_access::{
|
||||
EMERGENCY_ACCESS_TIMEOUT_RANGE, EmergencyAccess, EmergencyAccessId, EmergencyAccessStatus, EmergencyAccessType,
|
||||
};
|
||||
pub use self::event::{Event, EventType};
|
||||
pub use self::favorite::Favorite;
|
||||
pub use self::folder::{Folder, FolderCipher, FolderId};
|
||||
|
||||
@@ -37,7 +37,7 @@ impl TwoFactorIncomplete {
|
||||
ip: &ClientIp,
|
||||
conn: &DbConn,
|
||||
) -> EmptyResult {
|
||||
if CONFIG.incomplete_2fa_time_limit() <= 0 || !CONFIG.mail_enabled() {
|
||||
if CONFIG.incomplete_2fa_time_limit() == 0 || !CONFIG.mail_enabled() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
@@ -66,7 +66,7 @@ impl TwoFactorIncomplete {
|
||||
}
|
||||
|
||||
pub async fn mark_complete(user_uuid: &UserId, device_uuid: &DeviceId, conn: &DbConn) -> EmptyResult {
|
||||
if CONFIG.incomplete_2fa_time_limit() <= 0 || !CONFIG.mail_enabled() {
|
||||
if CONFIG.incomplete_2fa_time_limit() == 0 || !CONFIG.mail_enabled() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user