fix(import): clean up failed modpack imports
This commit is contained in:
1 parent
798c323a82
commit
d123bb5c19
4 files changed
+178
-11
No files matched your search
@@ -118,7 +118,10 @@ pub async fn execute_import(
|
|||||||
.instances_dir
|
.instances_dir
|
||||||
.join(&name)
|
.join(&name)
|
||||||
.join(crate::storage::MINECRAFT_DIR_NAME);
|
.join(crate::storage::MINECRAFT_DIR_NAME);
|
||||||
extract_mmc_archive(&summary.archive_path, &minecraft_dir)?;
|
if let Err(error) = extract_mmc_archive(&summary.archive_path, &minecraft_dir) {
|
||||||
|
super::cleanup_failed_import(manager, &name);
|
||||||
|
return Err(error);
|
||||||
|
}
|
||||||
|
|
||||||
progress::clear();
|
progress::clear();
|
||||||
tracing::info!(
|
tracing::info!(
|
||||||
@@ -158,11 +161,25 @@ fn extract_mmc_archive(
|
|||||||
let mut entry = archive.by_index(i)?;
|
let mut entry = archive.by_index(i)?;
|
||||||
let entry_name = entry.name().to_string();
|
let entry_name = entry.name().to_string();
|
||||||
|
|
||||||
let Some(relative) = entry_name.strip_prefix(&minecraft_prefix) else {
|
if !entry_name.starts_with(&minecraft_prefix) {
|
||||||
continue;
|
continue;
|
||||||
};
|
}
|
||||||
|
let enclosed = entry.enclosed_name().ok_or_else(|| {
|
||||||
|
std::io::Error::new(
|
||||||
|
std::io::ErrorKind::InvalidData,
|
||||||
|
format!("Unsafe archive path: {entry_name}"),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
let relative = enclosed
|
||||||
|
.strip_prefix(minecraft_prefix.trim_end_matches('/'))
|
||||||
|
.map_err(|_| {
|
||||||
|
std::io::Error::new(
|
||||||
|
std::io::ErrorKind::InvalidData,
|
||||||
|
format!("Invalid archive path: {entry_name}"),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
if relative.is_empty() || entry_name.ends_with('/') {
|
if relative.as_os_str().is_empty() || entry_name.ends_with('/') {
|
||||||
std::fs::create_dir_all(minecraft_dir.join(relative))?;
|
std::fs::create_dir_all(minecraft_dir.join(relative))?;
|
||||||
dirs += 1;
|
dirs += 1;
|
||||||
continue;
|
continue;
|
||||||
@@ -173,7 +190,10 @@ fn extract_mmc_archive(
|
|||||||
std::fs::create_dir_all(parent)?;
|
std::fs::create_dir_all(parent)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let filename = relative.rsplit('/').next().unwrap_or(relative);
|
let filename = relative
|
||||||
|
.file_name()
|
||||||
|
.and_then(|name| name.to_str())
|
||||||
|
.unwrap_or_default();
|
||||||
progress::set_sub_action(filename.to_string());
|
progress::set_sub_action(filename.to_string());
|
||||||
|
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
@@ -368,4 +388,27 @@ mod tests {
|
|||||||
"mmc-pack.json should not land in the instance dir"
|
"mmc-pack.json should not land in the instance dir"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn extract_mmc_archive_rejects_path_traversal() {
|
||||||
|
use std::io::Write;
|
||||||
|
|
||||||
|
let tmp = tempfile::tempdir().unwrap();
|
||||||
|
let archive_path = tmp.path().join("pack.zip");
|
||||||
|
let dest = tmp.path().join("instance/minecraft");
|
||||||
|
let file = std::fs::File::create(&archive_path).unwrap();
|
||||||
|
let mut zip = zip::ZipWriter::new(file);
|
||||||
|
let options: zip::write::SimpleFileOptions = Default::default();
|
||||||
|
zip.start_file("Pack/mmc-pack.json", options).unwrap();
|
||||||
|
zip.write_all(b"{}").unwrap();
|
||||||
|
zip.start_file("Pack/.minecraft/../../escaped.txt", options)
|
||||||
|
.unwrap();
|
||||||
|
zip.write_all(b"escaped").unwrap();
|
||||||
|
zip.finish().unwrap();
|
||||||
|
|
||||||
|
let error = extract_mmc_archive(&archive_path, &dest).unwrap_err();
|
||||||
|
|
||||||
|
assert!(error.to_string().contains("archive path"));
|
||||||
|
assert!(!tmp.path().join("escaped.txt").exists());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -170,6 +170,20 @@ pub async fn execute_import(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn cleanup_failed_import(manager: &InstanceManager, name: &str) {
|
||||||
|
crate::tui::progress::clear();
|
||||||
|
let instance_dir = manager.instances_dir.join(name);
|
||||||
|
if let Err(error) = std::fs::remove_dir_all(&instance_dir)
|
||||||
|
&& error.kind() != std::io::ErrorKind::NotFound
|
||||||
|
{
|
||||||
|
tracing::warn!(
|
||||||
|
"Failed to clean up incomplete imported instance {}: {}",
|
||||||
|
instance_dir.display(),
|
||||||
|
error
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
@@ -330,4 +344,17 @@ mod tests {
|
|||||||
ImportInput::ProjectSlug("fabulously-optimized".to_string())
|
ImportInput::ProjectSlug("fabulously-optimized".to_string())
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn failed_import_cleanup_removes_the_partial_instance() {
|
||||||
|
let tmp = tempfile::tempdir().unwrap();
|
||||||
|
let manager = InstanceManager::new(tmp.path().join("instances"), tmp.path().join("meta"));
|
||||||
|
let instance_dir = manager.instances_dir.join("Broken");
|
||||||
|
std::fs::create_dir_all(&instance_dir).unwrap();
|
||||||
|
std::fs::write(instance_dir.join("partial"), b"data").unwrap();
|
||||||
|
|
||||||
|
cleanup_failed_import(&manager, "Broken");
|
||||||
|
|
||||||
|
assert!(!instance_dir.exists());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -92,12 +92,18 @@ pub async fn execute_import(
|
|||||||
.join(&name)
|
.join(&name)
|
||||||
.join(crate::storage::MINECRAFT_DIR_NAME);
|
.join(crate::storage::MINECRAFT_DIR_NAME);
|
||||||
|
|
||||||
let index = crate::net::modrinth::parse_mrpack(&summary.archive_path)
|
let result: Result<(), Box<dyn std::error::Error + Send + Sync>> = async {
|
||||||
.map_err(|e| -> Box<dyn std::error::Error + Send + Sync> { e.into() })?;
|
let index = crate::net::modrinth::parse_mrpack(&summary.archive_path)
|
||||||
|
.map_err(|e| -> Box<dyn std::error::Error + Send + Sync> { e.into() })?;
|
||||||
download_mod_files(&index, &minecraft_dir).await?;
|
download_mod_files(&index, &minecraft_dir).await?;
|
||||||
|
extract_overrides(&summary.archive_path, &minecraft_dir)?;
|
||||||
extract_overrides(&summary.archive_path, &minecraft_dir)?;
|
Ok(())
|
||||||
|
}
|
||||||
|
.await;
|
||||||
|
if let Err(error) = result {
|
||||||
|
super::cleanup_failed_import(manager, &name);
|
||||||
|
return Err(error);
|
||||||
|
}
|
||||||
|
|
||||||
progress::clear();
|
progress::clear();
|
||||||
tracing::info!("Imported Modrinth pack '{}' as '{}'", summary.name, name);
|
tracing::info!("Imported Modrinth pack '{}' as '{}'", summary.name, name);
|
||||||
|
|||||||
@@ -2,6 +2,9 @@ use std::io::Write;
|
|||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use crate::net::modrinth::MrpackFile;
|
||||||
|
use wiremock::matchers::{method, path};
|
||||||
|
use wiremock::{Mock, MockServer, ResponseTemplate};
|
||||||
|
|
||||||
const INDEX: &[u8] = br#"{
|
const INDEX: &[u8] = br#"{
|
||||||
"formatVersion": 1,
|
"formatVersion": 1,
|
||||||
@@ -85,3 +88,91 @@ fn extraction_rejects_path_traversal() {
|
|||||||
assert!(error.to_string().contains("Unsafe override path"));
|
assert!(error.to_string().contains("Unsafe override path"));
|
||||||
assert!(!tmp.path().join("escaped.txt").exists());
|
assert!(!tmp.path().join("escaped.txt").exists());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn mod_files_download_to_their_manifest_paths() {
|
||||||
|
let _guard = crate::tui::tests::UI_TEST_LOCK
|
||||||
|
.lock()
|
||||||
|
.unwrap_or_else(|error| error.into_inner());
|
||||||
|
let runtime = tokio::runtime::Builder::new_current_thread()
|
||||||
|
.enable_all()
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
runtime.block_on(async {
|
||||||
|
let server = MockServer::start().await;
|
||||||
|
Mock::given(method("GET"))
|
||||||
|
.and(path("/first.jar"))
|
||||||
|
.respond_with(ResponseTemplate::new(200).set_body_bytes(b"first".to_vec()))
|
||||||
|
.expect(1)
|
||||||
|
.mount(&server)
|
||||||
|
.await;
|
||||||
|
Mock::given(method("GET"))
|
||||||
|
.and(path("/second.jar"))
|
||||||
|
.respond_with(ResponseTemplate::new(200).set_body_bytes(b"second".to_vec()))
|
||||||
|
.expect(1)
|
||||||
|
.mount(&server)
|
||||||
|
.await;
|
||||||
|
let index = MrpackIndex {
|
||||||
|
format_version: 1,
|
||||||
|
game: "minecraft".to_owned(),
|
||||||
|
version_id: "1".to_owned(),
|
||||||
|
name: "Downloads".to_owned(),
|
||||||
|
dependencies: Default::default(),
|
||||||
|
files: vec![
|
||||||
|
MrpackFile {
|
||||||
|
path: "mods/first.jar".to_owned(),
|
||||||
|
downloads: vec![format!("{}/first.jar", server.uri())],
|
||||||
|
file_size: 5,
|
||||||
|
},
|
||||||
|
MrpackFile {
|
||||||
|
path: "resourcepacks/second.jar".to_owned(),
|
||||||
|
downloads: vec![format!("{}/second.jar", server.uri())],
|
||||||
|
file_size: 6,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
let tmp = tempfile::tempdir().unwrap();
|
||||||
|
|
||||||
|
download_mod_files(&index, tmp.path()).await.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
std::fs::read(tmp.path().join("mods/first.jar")).unwrap(),
|
||||||
|
b"first"
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
std::fs::read(tmp.path().join("resourcepacks/second.jar")).unwrap(),
|
||||||
|
b"second"
|
||||||
|
);
|
||||||
|
crate::tui::progress::clear();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn mod_file_without_a_download_url_fails_without_creating_a_file() {
|
||||||
|
let _guard = crate::tui::tests::UI_TEST_LOCK
|
||||||
|
.lock()
|
||||||
|
.unwrap_or_else(|error| error.into_inner());
|
||||||
|
let runtime = tokio::runtime::Builder::new_current_thread()
|
||||||
|
.enable_all()
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
runtime.block_on(async {
|
||||||
|
let index = MrpackIndex {
|
||||||
|
format_version: 1,
|
||||||
|
game: "minecraft".to_owned(),
|
||||||
|
version_id: "1".to_owned(),
|
||||||
|
name: "Missing URL".to_owned(),
|
||||||
|
dependencies: Default::default(),
|
||||||
|
files: vec![MrpackFile {
|
||||||
|
path: "mods/missing.jar".to_owned(),
|
||||||
|
downloads: Vec::new(),
|
||||||
|
file_size: 0,
|
||||||
|
}],
|
||||||
|
};
|
||||||
|
let tmp = tempfile::tempdir().unwrap();
|
||||||
|
|
||||||
|
assert!(download_mod_files(&index, tmp.path()).await.is_err());
|
||||||
|
assert!(!tmp.path().join("mods/missing.jar").exists());
|
||||||
|
crate::tui::progress::clear();
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in new issue
Block a user