fix(backup): verify scheduled export destinations before writing
This commit is contained in:
1 parent
a8f966dcfd
commit
4ce27f8ae5
12 files changed
+652
-7
No files matched your search
@@ -1630,6 +1630,195 @@ fn emit_database_export_running(
|
||||
});
|
||||
}
|
||||
|
||||
fn export_destination_state_key(dir: &std::path::Path) -> String {
|
||||
format!("database_export_destination:{}", dir.to_string_lossy())
|
||||
}
|
||||
|
||||
/// Records the destination identity for a scheduled backup as soon as it is
|
||||
/// configured, not just after its first successful export. Scheduled plans
|
||||
/// live in the frontend and may not run for hours after being saved; without
|
||||
/// an eager record here, a destination whose mount disappears before its
|
||||
/// very first run is indistinguishable from a brand-new local folder to
|
||||
/// `ensure_export_destination_dir` (both have no recorded state) and gets
|
||||
/// silently recreated on the wrong filesystem. The schedule editor only
|
||||
/// accepts directories selected from the filesystem, so a missing path here
|
||||
/// means the destination vanished before its identity could be recorded and
|
||||
/// the schedule must not be saved. See #6327.
|
||||
pub async fn record_export_destination_identity(
|
||||
state: &crate::connection::AppState,
|
||||
dir: &std::path::Path,
|
||||
) -> Result<(), String> {
|
||||
if !dir.is_dir() {
|
||||
return Err(format!(
|
||||
"Backup directory {} does not exist or is not a directory. Select an existing destination before saving the schedule.",
|
||||
dir.display()
|
||||
));
|
||||
}
|
||||
save_export_destination_dir_device_id(state, dir).await
|
||||
}
|
||||
|
||||
async fn save_export_destination_dir_device_id(
|
||||
state: &crate::connection::AppState,
|
||||
dir: &std::path::Path,
|
||||
) -> Result<(), String> {
|
||||
let value = match export_destination_device_id_for_path(dir) {
|
||||
Some(dev) => dev.to_le_bytes().to_vec(),
|
||||
None => Vec::new(),
|
||||
};
|
||||
state.storage.save_state(&export_destination_state_key(dir), &value, "application/octet-stream").await
|
||||
}
|
||||
|
||||
/// Ensures `dir` exists for an export destination, without ever silently
|
||||
/// recreating a directory that previously produced a successful export (or
|
||||
/// was recorded via [`record_export_destination_identity`]) and has since
|
||||
/// disappeared. Auto-creating on every run is what the original fix for
|
||||
/// #6109 did, but that is unsafe for a destination on a removable or network
|
||||
/// drive: if the mount is temporarily gone when a run executes, blindly
|
||||
/// recreating the path resurrects it on the local root filesystem and the
|
||||
/// export "succeeds" while silently writing to the wrong disk. A directory
|
||||
/// dbx has never seen before is safe to create (normal first-time
|
||||
/// configuration of a local folder); a directory dbx has seen before but
|
||||
/// that is now missing, or that now resolves to a different filesystem than
|
||||
/// last time, is refused instead. See #6327.
|
||||
///
|
||||
/// Returns the device identity that was just verified (or recorded for a
|
||||
/// newly created directory), if the current platform can determine one, so
|
||||
/// the caller can re-verify it against the file it actually opens -- the
|
||||
/// directory check here and the later `File::create` are separate
|
||||
/// operations, and the mount can change in between.
|
||||
async fn ensure_export_destination_dir(
|
||||
state: &crate::connection::AppState,
|
||||
dir: &std::path::Path,
|
||||
) -> Result<Option<u64>, String> {
|
||||
let key = export_destination_state_key(dir);
|
||||
let recorded_dev: Option<Option<u64>> = state
|
||||
.storage
|
||||
.load_state(&key)
|
||||
.await?
|
||||
.map(|(bytes, _content_type)| <[u8; 8]>::try_from(bytes.as_slice()).ok().map(u64::from_le_bytes));
|
||||
|
||||
if dir.is_dir() {
|
||||
if let Some(Some(recorded_dev)) = recorded_dev {
|
||||
if let Some(current_dev) = export_destination_device_id_for_path(dir) {
|
||||
if current_dev != recorded_dev {
|
||||
return Err(format!(
|
||||
"Backup directory {} now resolves to a different filesystem than the last \
|
||||
successful export to this location. Refusing to write here automatically -- \
|
||||
if this directory is on a removable or network drive, make sure the correct \
|
||||
drive is connected before running the backup.",
|
||||
dir.display()
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if recorded_dev.is_some() {
|
||||
return Err(format!(
|
||||
"Backup directory {} is missing. It was configured or previously used for exports to \
|
||||
this location, so dbx will not recreate it automatically -- if this is on a removable \
|
||||
or network drive, reconnect it and try again.",
|
||||
dir.display()
|
||||
));
|
||||
}
|
||||
std::fs::create_dir_all(dir).map_err(|e| format!("Failed to create backup directory: {e}"))?;
|
||||
}
|
||||
|
||||
save_export_destination_dir_device_id(state, dir).await?;
|
||||
Ok(export_destination_device_id_for_path(dir))
|
||||
}
|
||||
|
||||
/// Whether a destination's identity, checked once via [`ensure_export_destination_dir`]
|
||||
/// and then again against the file dbx actually opened, indicates the mount
|
||||
/// changed in between. An unknown expected identity has nothing to compare,
|
||||
/// but once an expected identity is known, failing to identify the opened
|
||||
/// handle must fail closed rather than allowing an unverified write.
|
||||
fn export_destination_identity_mismatch(expected: Option<u64>, actual: Option<u64>) -> bool {
|
||||
expected.is_some_and(|expected| actual != Some(expected))
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn export_destination_device_id_for_path(dir: &std::path::Path) -> Option<u64> {
|
||||
use std::os::unix::fs::MetadataExt;
|
||||
std::fs::metadata(dir).ok().map(|metadata| metadata.dev())
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn export_destination_device_id_for_file(file: &std::fs::File) -> Option<u64> {
|
||||
use std::os::unix::fs::MetadataExt;
|
||||
file.metadata().ok().map(|metadata| metadata.dev())
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn export_destination_device_id_for_path(dir: &std::path::Path) -> Option<u64> {
|
||||
windows_export_destination::device_id_for_path(dir)
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn export_destination_device_id_for_file(file: &std::fs::File) -> Option<u64> {
|
||||
windows_export_destination::device_id_for_handle(file)
|
||||
}
|
||||
|
||||
#[cfg(not(any(unix, windows)))]
|
||||
fn export_destination_device_id_for_path(_dir: &std::path::Path) -> Option<u64> {
|
||||
None
|
||||
}
|
||||
|
||||
#[cfg(not(any(unix, windows)))]
|
||||
fn export_destination_device_id_for_file(_file: &std::fs::File) -> Option<u64> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Windows has no stable `std` API for a directory or file's volume identity
|
||||
/// (`MetadataExt::volume_serial_number` is still gated behind the unstable
|
||||
/// `windows_by_handle` feature), so this queries `dwVolumeSerialNumber` from
|
||||
/// `BY_HANDLE_FILE_INFORMATION` directly. Using the *handle* rather than
|
||||
/// re-resolving the path is what makes `export_destination_device_id_for_file`
|
||||
/// safe to call on an already-open `File`: it reports the volume the handle
|
||||
/// was actually opened against, not whatever currently sits at that path.
|
||||
#[cfg(windows)]
|
||||
mod windows_export_destination {
|
||||
use std::os::windows::ffi::OsStrExt;
|
||||
use std::os::windows::io::AsRawHandle;
|
||||
use std::path::Path;
|
||||
use windows_sys::Win32::Foundation::{CloseHandle, HANDLE, INVALID_HANDLE_VALUE};
|
||||
use windows_sys::Win32::Storage::FileSystem::{
|
||||
CreateFileW, GetFileInformationByHandle, BY_HANDLE_FILE_INFORMATION, FILE_FLAG_BACKUP_SEMANTICS,
|
||||
FILE_SHARE_DELETE, FILE_SHARE_READ, FILE_SHARE_WRITE, OPEN_EXISTING,
|
||||
};
|
||||
|
||||
pub(super) fn device_id_for_path(path: &Path) -> Option<u64> {
|
||||
let mut wide: Vec<u16> = path.as_os_str().encode_wide().collect();
|
||||
wide.push(0);
|
||||
let handle = unsafe {
|
||||
CreateFileW(
|
||||
wide.as_ptr(),
|
||||
0,
|
||||
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
||||
std::ptr::null(),
|
||||
OPEN_EXISTING,
|
||||
FILE_FLAG_BACKUP_SEMANTICS,
|
||||
std::ptr::null_mut(),
|
||||
)
|
||||
};
|
||||
if handle == INVALID_HANDLE_VALUE {
|
||||
return None;
|
||||
}
|
||||
let result = volume_serial_number(handle);
|
||||
unsafe { CloseHandle(handle) };
|
||||
result
|
||||
}
|
||||
|
||||
pub(super) fn device_id_for_handle(file: &std::fs::File) -> Option<u64> {
|
||||
volume_serial_number(file.as_raw_handle() as HANDLE)
|
||||
}
|
||||
|
||||
fn volume_serial_number(handle: HANDLE) -> Option<u64> {
|
||||
let mut info: BY_HANDLE_FILE_INFORMATION = unsafe { std::mem::zeroed() };
|
||||
let ok = unsafe { GetFileInformationByHandle(handle, &mut info) };
|
||||
(ok != 0).then_some(info.dwVolumeSerialNumber as u64)
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn export_database_sql_core(
|
||||
state: &crate::connection::AppState,
|
||||
request: &DatabaseExportRequest,
|
||||
@@ -1673,7 +1862,28 @@ pub async fn export_database_sql_core(
|
||||
)
|
||||
.await?;
|
||||
// 4. Create file
|
||||
let mut expected_destination_dev = None;
|
||||
if let Some(parent) = std::path::Path::new(&request.file_path).parent() {
|
||||
if !parent.as_os_str().is_empty() {
|
||||
expected_destination_dev = ensure_export_destination_dir(state, parent).await?;
|
||||
}
|
||||
}
|
||||
let file = std::fs::File::create(&request.file_path).map_err(|e| format!("Failed to write file: {e}"))?;
|
||||
// The directory check above and this `File::create` are separate
|
||||
// operations: the mount can disappear and be replaced by something else
|
||||
// at the same path in between. Re-check the identity of the handle we
|
||||
// actually opened, not just the path, and refuse to keep a backup that
|
||||
// landed on the wrong filesystem. See #6327.
|
||||
if export_destination_identity_mismatch(expected_destination_dev, export_destination_device_id_for_file(&file)) {
|
||||
drop(file);
|
||||
let _ = std::fs::remove_file(&request.file_path);
|
||||
return Err(format!(
|
||||
"Backup destination for {} changed while opening the output file -- the directory now \
|
||||
resolves to a different filesystem than the one just verified. If a removable or network \
|
||||
drive was disconnected and reconnected, retry the backup.",
|
||||
request.file_path
|
||||
));
|
||||
}
|
||||
let mut file = BufWriter::new(file);
|
||||
|
||||
let create_database_preamble = if request.include_create_database && matches!(db_type, DatabaseType::Mysql) {
|
||||
@@ -2519,19 +2729,22 @@ fn build_database_export_object_source_sql(
|
||||
mod tests {
|
||||
use super::{
|
||||
build_database_export_object_source_sql, build_database_sql_export, build_export_insert_statements,
|
||||
database_export_select_sql, database_export_total_objects, drop_table_if_exists_sql, filter_export_table_infos,
|
||||
database_export_select_sql, database_export_total_objects, drop_table_if_exists_sql,
|
||||
ensure_export_destination_dir, export_destination_identity_mismatch, filter_export_table_infos,
|
||||
format_export_sql_literal, format_export_table_ddl, format_mysql_spatial_export_literal,
|
||||
generate_postgres_extension_ddl, generate_postgres_sequence_create_ddl, generate_postgres_sequence_owner_ddl,
|
||||
generate_postgres_sequence_setval_sql, is_postgres_extension_member_routine, mysql_database_export_preamble,
|
||||
mysql_view_dependencies_from_rows, mysql_view_dependencies_sql, normalize_export_table_ddl,
|
||||
record_export_error, replace_database_export_select_list, sort_export_views_by_dependencies,
|
||||
write_database_export_rows, BuildDatabaseSqlExportOptions, BuildExportInsertStatementsOptions,
|
||||
DatabaseExportObjectCounts, DatabaseExportRequest, DdlNormalizeOptions, ExportedTableSql,
|
||||
PostgresExportExtension, PostgresExportSequence, PostgresExtensionMembers, DATABASE_EXPORT_INSERT_BATCH_SIZE,
|
||||
DATABASE_EXPORT_ROW_LIMIT,
|
||||
record_export_destination_identity, record_export_error, replace_database_export_select_list,
|
||||
sort_export_views_by_dependencies, write_database_export_rows, BuildDatabaseSqlExportOptions,
|
||||
BuildExportInsertStatementsOptions, DatabaseExportObjectCounts, DatabaseExportRequest, DdlNormalizeOptions,
|
||||
ExportedTableSql, PostgresExportExtension, PostgresExportSequence, PostgresExtensionMembers,
|
||||
DATABASE_EXPORT_INSERT_BATCH_SIZE, DATABASE_EXPORT_ROW_LIMIT,
|
||||
};
|
||||
use super::{concurrent_metadata_prefetch_allowed, database_export_metadata_prefetch_concurrency};
|
||||
use crate::connection::AppState;
|
||||
use crate::models::connection::DatabaseType;
|
||||
use crate::storage::Storage;
|
||||
use crate::types::{ObjectInfo, ObjectSourceKind, TableInfo};
|
||||
use serde_json::{json, Value};
|
||||
|
||||
@@ -3926,4 +4139,161 @@ mod tests {
|
||||
assert!(std::fs::read_to_string(&path).unwrap().is_empty());
|
||||
let _ = std::fs::remove_file(path);
|
||||
}
|
||||
|
||||
async fn test_app_state(scratch_dir: &std::path::Path) -> AppState {
|
||||
let storage = Storage::open(&scratch_dir.join("storage.db")).await.unwrap();
|
||||
AppState::new(storage)
|
||||
}
|
||||
|
||||
// Regression tests for #6327: create_dir_all on every export run is unsafe
|
||||
// when the destination is on a removable/network drive, because a mount
|
||||
// that is temporarily gone at write time would be silently recreated on
|
||||
// the local root filesystem, and the backup would "succeed" while writing
|
||||
// to the wrong disk.
|
||||
|
||||
#[tokio::test]
|
||||
async fn ensure_export_destination_dir_creates_a_never_before_seen_local_directory() {
|
||||
let scratch = std::env::temp_dir().join(format!("dbx-export-dest-new-{}", uuid::Uuid::new_v4()));
|
||||
std::fs::create_dir_all(&scratch).unwrap();
|
||||
let state = test_app_state(&scratch).await;
|
||||
|
||||
let destination = scratch.join("backups").join("mydb");
|
||||
assert!(!destination.exists());
|
||||
|
||||
ensure_export_destination_dir(&state, &destination)
|
||||
.await
|
||||
.expect("first-time local directory should be created");
|
||||
assert!(destination.is_dir());
|
||||
|
||||
let _ = std::fs::remove_dir_all(&scratch);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn ensure_export_destination_dir_refuses_to_recreate_a_destination_that_disappeared() {
|
||||
let scratch = std::env::temp_dir().join(format!("dbx-export-dest-vanished-{}", uuid::Uuid::new_v4()));
|
||||
std::fs::create_dir_all(&scratch).unwrap();
|
||||
let state = test_app_state(&scratch).await;
|
||||
|
||||
// Simulates a previously configured, already-used backup destination
|
||||
// (e.g. on an external or network drive) by successfully exporting to
|
||||
// it once first.
|
||||
let destination = scratch.join("mounted-drive").join("mydb");
|
||||
ensure_export_destination_dir(&state, &destination).await.expect("initial export should create the directory");
|
||||
assert!(destination.is_dir());
|
||||
|
||||
// Simulates the mount disappearing (unplugged drive, unmounted share,
|
||||
// etc.) before the next run.
|
||||
std::fs::remove_dir_all(scratch.join("mounted-drive")).unwrap();
|
||||
assert!(!destination.exists());
|
||||
|
||||
let result = ensure_export_destination_dir(&state, &destination).await;
|
||||
|
||||
assert!(result.is_err(), "a destination that existed before should not be silently recreated");
|
||||
assert!(!destination.exists(), "the backup directory must not be resurrected on the wrong filesystem");
|
||||
|
||||
let _ = std::fs::remove_dir_all(&scratch);
|
||||
}
|
||||
|
||||
// Regression test for review feedback on #6327: `ensure_export_destination_dir`
|
||||
// only remembered a destination *after* a successful export, so a mount
|
||||
// that was present when a schedule was configured but vanished before its
|
||||
// very first run looked identical to a brand-new local folder (no
|
||||
// recorded state either way) and got silently recreated on the local
|
||||
// disk. `record_export_destination_identity` closes that gap by letting
|
||||
// the schedule-configuration flow record the destination eagerly.
|
||||
#[tokio::test]
|
||||
async fn record_export_destination_identity_protects_a_mount_that_vanishes_before_its_first_run() {
|
||||
let scratch = std::env::temp_dir().join(format!("dbx-export-dest-eager-{}", uuid::Uuid::new_v4()));
|
||||
std::fs::create_dir_all(&scratch).unwrap();
|
||||
let state = test_app_state(&scratch).await;
|
||||
|
||||
// Simulates the user picking an already-mounted external/network
|
||||
// drive in the schedule editor and saving the schedule -- the
|
||||
// directory exists at configuration time, but no export has run yet.
|
||||
let destination = scratch.join("mounted-drive").join("mydb");
|
||||
std::fs::create_dir_all(&destination).unwrap();
|
||||
record_export_destination_identity(&state, &destination)
|
||||
.await
|
||||
.expect("configuring the schedule should succeed");
|
||||
|
||||
// The mount disappears before the scheduler ever runs this schedule
|
||||
// for the first time.
|
||||
std::fs::remove_dir_all(scratch.join("mounted-drive")).unwrap();
|
||||
assert!(!destination.exists());
|
||||
|
||||
let result = ensure_export_destination_dir(&state, &destination).await;
|
||||
|
||||
assert!(result.is_err(), "a mount that was recorded at configuration time must not be silently recreated");
|
||||
assert!(!destination.exists(), "the backup directory must not be resurrected on the wrong filesystem");
|
||||
|
||||
let _ = std::fs::remove_dir_all(&scratch);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn record_export_destination_identity_rejects_a_directory_that_disappeared_before_save() {
|
||||
let scratch = std::env::temp_dir().join(format!("dbx-export-dest-eager-new-{}", uuid::Uuid::new_v4()));
|
||||
std::fs::create_dir_all(&scratch).unwrap();
|
||||
let state = test_app_state(&scratch).await;
|
||||
|
||||
// The schedule editor only accepts an existing directory. If that
|
||||
// directory disappears before the save request reaches the backend,
|
||||
// the schedule must not be persisted without a recorded identity.
|
||||
let destination = scratch.join("backups").join("mydb");
|
||||
assert!(!destination.exists());
|
||||
let error = record_export_destination_identity(&state, &destination)
|
||||
.await
|
||||
.expect_err("a missing scheduled destination must be rejected");
|
||||
|
||||
assert!(error.contains("does not exist or is not a directory"));
|
||||
assert!(!destination.exists());
|
||||
|
||||
let _ = std::fs::remove_dir_all(&scratch);
|
||||
}
|
||||
|
||||
// Regression test for review feedback on #6327: the directory identity
|
||||
// check and the later `File::create` are separate operations, so the
|
||||
// mount can disappear and be replaced by something else at the same path
|
||||
// in between. `export_destination_identity_mismatch` is the comparison
|
||||
// `export_database_sql_core` runs against the handle it actually opened;
|
||||
// exercised directly here since reproducing a real cross-filesystem swap
|
||||
// mid-write is not something a portable unit test can simulate.
|
||||
#[test]
|
||||
fn export_destination_identity_mismatch_detects_a_changed_device() {
|
||||
assert!(export_destination_identity_mismatch(Some(1), Some(2)));
|
||||
assert!(!export_destination_identity_mismatch(Some(1), Some(1)));
|
||||
assert!(
|
||||
!export_destination_identity_mismatch(None, Some(2)),
|
||||
"an unknown expected device has nothing to compare against"
|
||||
);
|
||||
assert!(
|
||||
export_destination_identity_mismatch(Some(1), None),
|
||||
"an opened file with unknown identity must not bypass a known expected device"
|
||||
);
|
||||
assert!(!export_destination_identity_mismatch(None, None));
|
||||
}
|
||||
|
||||
// Regression test for review feedback on #6327: the non-Unix path used
|
||||
// to report no device identity at all, so replacing a Windows drive or
|
||||
// mount at the same path went undetected. This only runs on native
|
||||
// Windows (this repo's CI has no Windows job that executes `cargo test`,
|
||||
// only `cargo check`, so it is exercised locally by Windows contributors
|
||||
// and by the compile-check itself).
|
||||
#[cfg(windows)]
|
||||
#[test]
|
||||
fn export_destination_device_id_for_path_and_open_file_agree_on_windows() {
|
||||
let scratch = std::env::temp_dir().join(format!("dbx-export-dest-win-{}", uuid::Uuid::new_v4()));
|
||||
std::fs::create_dir_all(&scratch).unwrap();
|
||||
|
||||
let dir_dev = super::export_destination_device_id_for_path(&scratch);
|
||||
assert!(dir_dev.is_some(), "a real local directory should report a volume serial number");
|
||||
|
||||
let file_path = scratch.join("probe.txt");
|
||||
let file = std::fs::File::create(&file_path).unwrap();
|
||||
let file_dev = super::export_destination_device_id_for_file(&file);
|
||||
drop(file);
|
||||
|
||||
assert_eq!(dir_dev, file_dev, "a file and its parent directory must resolve to the same volume");
|
||||
|
||||
let _ = std::fs::remove_dir_all(&scratch);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user