fix(backup): check referenced profile bodies before restore

This commit is contained in:
Tunglies
2026-12-04 01:48:32 +08:00
parent 44aa5f2430
commit 2cdd0dd701
+38 -11
View File
@@ -160,7 +160,7 @@ pub async fn delete_webdav_backup(filename: String) -> Result<()> {
})
}
async fn restore_config_path(staged: &Path, home: &Path, name: &str, retry: bool) -> Result<PathBuf> {
async fn restore_input_path(staged: &Path, home: &Path, name: &str, retry: bool) -> Result<PathBuf> {
let path = staged.join(name);
if !retry && !fs::try_exists(&path).await? {
return Ok(home.join(name));
@@ -182,19 +182,30 @@ async fn validate_restore(
.await
.map_err(|join| anyhow!("backup staging task failed: {join}"))??;
let validated: Result<()> = async {
let verge = restore_config_path(staged.path(), home_dir, VERGE_CONFIG, retry).await?;
let profiles_path = restore_config_path(staged.path(), home_dir, PROFILE_YAML, retry).await?;
let verge = restore_input_path(staged.path(), home_dir, VERGE_CONFIG, retry).await?;
let profiles_path = restore_input_path(staged.path(), home_dir, PROFILE_YAML, retry).await?;
help::read_yaml::<IVerge>(&verge).await?;
let profiles: IProfiles = help::read_yaml(&profiles_path).await?;
if retry {
let bodies = staged.path().join(PROFILES_DIR);
for file in profiles.items.iter().flatten().filter_map(|item| item.file.as_deref()) {
validate_profile_file(file)?;
let body = fs::symlink_metadata(bodies.join(file))
.await
.with_context(|| format!("failed to inspect recovery backup profile body {file}"))?;
anyhow::ensure!(body.is_file(), "recovery backup profile body {file} is not a file");
let staged_bodies = staged.path().join(PROFILES_DIR);
let live_bodies = home_dir.join(PROFILES_DIR);
for file in profiles.items.iter().flatten().filter_map(|item| item.file.as_deref()) {
let file = if !retry && let Ok(relative) = Path::new(file).strip_prefix(".") {
relative.to_str().context("invalid profile filename")?
} else {
file
};
validate_profile_file(file)?;
let body = restore_input_path(&staged_bodies, &live_bodies, file, retry).await?;
if body.parent() == Some(&live_bodies) {
anyhow::ensure!(
fs::symlink_metadata(&live_bodies).await?.is_dir(),
"live profiles path is not a directory"
);
}
let metadata = fs::symlink_metadata(&body)
.await
.with_context(|| format!("failed to inspect backup profile body {file}"))?;
anyhow::ensure!(metadata.is_file(), "backup profile body {file} is not a file");
}
Ok(())
}
@@ -506,7 +517,23 @@ mod tests {
.await
.is_err()
);
assert!(
validate_restore(open_backup(Some(index), false)?, home.path(), false)
.await
.is_err()
);
assert_eq!(fs::read(&path).await?, live);
fs::create_dir(home.path().join(PROFILES_DIR)).await?;
fs::write(home.path().join(PROFILES_DIR).join("r.yaml"), b"old body").await?;
validate_restore(open_backup(Some(index), false)?, home.path(), false).await?;
assert!(
validate_restore(open_backup(Some(index), false)?, home.path(), true)
.await
.is_err()
);
validate_restore(open_backup(Some(index), true)?, home.path(), true).await?;
let old_index = b"items: [{uid: r, type: remote, file: ./r.yaml}]";
validate_restore(open_backup(Some(old_index), true)?, home.path(), false).await?;
Ok(())
}
}