From 7db8d3f9d383aed19415286b6bdf1a48533fa751 Mon Sep 17 00:00:00 2001 From: Yuri Karamian Date: Sat, 10 Oct 2026 20:14:37 +0200 Subject: [PATCH] fix: harden backups against empty archives, foreign vaults and sync rollback A backup folder that contains the vault now produces complete archives and an archive without content is not restored. Archives from another vault are labelled. After a restore the sync baseline is reset. --- src-tauri/src/backup.rs | 115 +++++++++++++++++++++++- src-tauri/src/commands.rs | 4 +- src-tauri/src/sync.rs | 31 +++++++ src-tauri/src/types.rs | 3 + src/lib/components/SettingsPanel.svelte | 6 ++ src/lib/types.ts | 2 + 6 files changed, 158 insertions(+), 3 deletions(-) diff --git a/src-tauri/src/backup.rs b/src-tauri/src/backup.rs index 8433ce4..3877b7f 100644 --- a/src-tauri/src/backup.rs +++ b/src-tauri/src/backup.rs @@ -54,7 +54,9 @@ fn excluded_path(path: &Path, vault: &Path, backup_dir: &Path, attachments: bool if path == vault { return false; } - if path.starts_with(backup_dir) { + // Only a backup folder inside the vault is skipped. One that contains the vault (its parent, + // the home folder) used to match every path, leaving archives with nothing but the manifest. + if backup_dir.starts_with(vault) && path.starts_with(backup_dir) { return true; } // Old configured backup folders may remain inside the vault after relocation. @@ -203,6 +205,7 @@ pub fn create_backup( path: backup_path.to_string_lossy().to_string(), size: meta.len(), created: now.to_rfc3339(), + other_vault: false, }) } @@ -243,12 +246,15 @@ fn list_owned_backups( } let filename = entry.file_name().to_string_lossy().to_string(); if let Some((created, owned)) = archive_name(&filename, &id) { + // Archives from before vault identities carry none, so they cannot be told apart. + let names_a_vault = filename.len() > "helixnotes-backup-0000-00-00T00-00-00.zip".len(); entries.push(( BackupEntry { filename, path: path.to_string_lossy().to_string(), size: meta.len(), created, + other_vault: names_a_vault && !owned, }, owned, )); @@ -262,7 +268,8 @@ fn list_owned_backups( Ok(entries) } -/// List backups from every vault; only automatic pruning uses vault identity. +/// List backups from every vault, each marked when it belongs to another one; a vault that was +/// moved or renamed must still be able to restore its earlier archives. pub fn list_backups(backup_dir: &Path, vault_path: &str) -> Result, String> { Ok(list_owned_backups(backup_dir, vault_path)? .into_iter() @@ -703,6 +710,12 @@ pub fn restore_backup( )); } } + // An archive holding only its manifest would replace the vault with nothing. + if seen.keys().all(|entry| entry == Path::new(MANIFEST)) { + return Err( + "This backup contains no notes, so restoring it would empty the vault".to_string(), + ); + } let attachments = if let Some(meta) = manifest { if meta.version != 1 || meta.vault.len() != 64 @@ -1094,4 +1107,102 @@ mod tests { "source content" ); } + + fn archive_names(path: &str) -> Vec { + let mut archive = ZipArchive::new(File::open(path).unwrap()).unwrap(); + (0..archive.len()) + .map(|index| archive.by_index(index).unwrap().name().to_string()) + .collect() + } + + #[test] + fn a_backup_folder_that_contains_the_vault_still_archives_the_notes() { + let root = tempdir().unwrap(); + let vault = root.path().join("Documents/Notes"); + fs::create_dir_all(vault.join("Sub")).unwrap(); + fs::write(vault.join("note.md"), "content").unwrap(); + fs::write(vault.join("Sub/deep.md"), "deep").unwrap(); + // The parent folder, and the folder above that. + for output in [root.path().join("Documents"), root.path().to_path_buf()] { + let entry = create_backup(&vault_string(&vault), &output, false).unwrap(); + let names = archive_names(&entry.path); + assert!(names.iter().any(|name| name == "note.md"), "{names:?}"); + assert!(names.iter().any(|name| name == "Sub/deep.md"), "{names:?}"); + // A second run does not swallow the first archive, which now lies beside the vault. + let again = create_backup(&vault_string(&vault), &output, false).unwrap(); + assert!(!archive_names(&again.path) + .iter() + .any(|name| name.ends_with(".zip"))); + } + } + + #[test] + fn an_archive_without_content_is_not_restored() { + let root = tempdir().unwrap(); + let vault = root.path().join("vault"); + let output = root.path().join("backups"); + fs::create_dir_all(&vault).unwrap(); + fs::create_dir_all(&output).unwrap(); + fs::write(vault.join("note.md"), "keep me").unwrap(); + // What the old exclusion produced: nothing but the manifest. + let empty = output.join(format!( + "helixnotes-backup-2026-01-01T00-00-00-{}-{}.zip", + "a".repeat(64), + Uuid::new_v4() + )); + let mut writer = ZipWriter::new(File::create(&empty).unwrap()); + writer + .start_file(super::MANIFEST, SimpleFileOptions::default()) + .unwrap(); + writer + .write_all( + format!( + r#"{{"version":1,"vault":"{}","attachments":false}}"#, + "a".repeat(64) + ) + .as_bytes(), + ) + .unwrap(); + writer.finish().unwrap(); + let error = + restore_backup(&vault_string(&vault), &output, &empty.to_string_lossy()).unwrap_err(); + assert!(error.contains("contains no notes"), "{error}"); + assert_eq!( + fs::read_to_string(vault.join("note.md")).unwrap(), + "keep me" + ); + } + + #[test] + fn backups_from_another_vault_are_marked() { + let root = tempdir().unwrap(); + let output = root.path().join("backups"); + let first = root.path().join("first"); + let second = root.path().join("second"); + fs::create_dir_all(&first).unwrap(); + fs::create_dir_all(&second).unwrap(); + fs::write(first.join("note.md"), "one").unwrap(); + fs::write(second.join("note.md"), "two").unwrap(); + let own = create_backup(&vault_string(&first), &output, false).unwrap(); + let foreign = create_backup(&vault_string(&second), &output, false).unwrap(); + // An archive from before vault identities: it cannot be attributed, so it is not flagged. + fs::copy( + &own.path, + output.join("helixnotes-backup-2020-01-01T00-00-00.zip"), + ) + .unwrap(); + assert!(!own.other_vault); + let listed = list_backups(&output, &vault_string(&first)).unwrap(); + let marked = |path: &str| { + listed + .iter() + .find(|entry| entry.path == path) + .unwrap() + .other_vault + }; + assert!(!marked(&own.path)); + assert!(marked(&foreign.path)); + assert!(listed.iter().filter(|entry| entry.other_vault).count() == 1); + assert_eq!(listed.len(), 3); + } } diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index 68a0a1b..e78aad9 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -3727,7 +3727,9 @@ pub fn restore_backup( let result = { let _writes = state.note_save.blocking_lock(); state.importing.store(true, Ordering::Release); - let result = crate::backup::restore_backup(&vault_path, &backup_dir, &backup_path); + let result = crate::backup::restore_backup(&vault_path, &backup_dir, &backup_path) + // Still under the maintenance lock, so no sync can run in between. + .and_then(|()| crate::sync::forget_baseline(&vault_path)); state.importing.store(false, Ordering::Release); result }; diff --git a/src-tauri/src/sync.rs b/src-tauri/src/sync.rs index 06abe7e..abebf94 100644 --- a/src-tauri/src/sync.rs +++ b/src-tauri/src/sync.rs @@ -119,6 +119,19 @@ fn local_path(vault: &Path, relpath: &str) -> Result { Ok(vault.join(relpath)) } +/// Forgets what the last sync saw. After a backup restore the vault no longer matches it: kept, +/// the next sync would read the rollback as local edits, delete everything newer on the server and +/// overwrite newer notes there with the restored ones. Without it the next sync starts over as a +/// first sync, which merges both sides and deletes nothing. +pub(crate) fn forget_baseline(vault: &str) -> Result<(), String> { + let path = manifest_path(vault); + match fs::remove_file(&path) { + Ok(()) => Ok(()), + Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(()), + Err(error) => Err(local_error("reset sync state", &path, error)), + } +} + fn load_manifest(vault: &str) -> Result { match fs::read(manifest_path(vault)) { Ok(bytes) => serde_json::from_slice(&bytes) @@ -1437,6 +1450,24 @@ mod tests { let event = changed_entry(path, "modify", vault.path()); assert_eq!(event.note.unwrap().preview, "remote content"); } + #[test] + fn forgetting_the_baseline_makes_the_next_sync_a_first_sync() { + let vault = tempfile::tempdir().unwrap(); + let root = vault.path().to_str().unwrap(); + let mut recorded = SyncManifest::default(); + recorded.files.insert( + "gone.md".to_string(), + manifest_entry("hash".to_string(), "\"etag\""), + ); + save_manifest(root, &recorded).unwrap(); + assert!(!load_manifest(root).unwrap().files.is_empty()); + forget_baseline(root).unwrap(); + // No record of earlier files means none can be read as deleted on either side. + assert!(load_manifest(root).unwrap().files.is_empty()); + // Nothing to forget is fine too. + forget_baseline(root).unwrap(); + } + #[test] fn remote_paths_cannot_leave_the_vault() { // Literal and percent-encoded traversal, as a hostile server could list it. diff --git a/src-tauri/src/types.rs b/src-tauri/src/types.rs index 9b650ea..f1cf322 100644 --- a/src-tauri/src/types.rs +++ b/src-tauri/src/types.rs @@ -568,6 +568,9 @@ pub struct BackupEntry { pub path: String, pub size: u64, pub created: String, + /// The archive was made from a different vault folder. All vaults share one backup folder. + #[serde(default)] + pub other_vault: bool, } #[derive(Debug, Clone, Serialize, Deserialize)] diff --git a/src/lib/components/SettingsPanel.svelte b/src/lib/components/SettingsPanel.svelte index 0e4f539..52f71b7 100644 --- a/src/lib/components/SettingsPanel.svelte +++ b/src/lib/components/SettingsPanel.svelte @@ -2226,6 +2226,9 @@
{formatBackupDate(entry.created)} {formatBackupSize(entry.size)} + {#if entry.other_vault} + other vault + {/if}
diff --git a/src/lib/types.ts b/src/lib/types.ts index 7df00f3..1a47ada 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -251,6 +251,8 @@ export interface BackupEntry { path: string; size: number; created: string; + /** Made from a different vault folder; all vaults share one backup folder. */ + other_vault?: boolean; } export interface VersionEntry {